stage_audit.js 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const auditType = require('../const/audit').auditType;
  11. const smsTypeConst = require('../const/sms_type');
  12. const SMS = require('../lib/sms');
  13. const SmsAliConst = require('../const/sms_alitemplate');
  14. const wxConst = require('../const/wechat_template');
  15. const shenpiConst = require('../const/shenpi');
  16. const payConst = require('../const/deal_pay');
  17. const pushType = require('../const/audit').pushType;
  18. const syncApiConst = require('../const/sync_api');
  19. const measureType = require('../const/tender').measureType;
  20. const RevisePrice = require('../lib/revise_price');
  21. const pushOperate = require('../const/spec_3f').pushOperate;
  22. module.exports = app => {
  23. class StageAudit extends app.BaseService {
  24. /**
  25. * 构造函数
  26. *
  27. * @param {Object} ctx - egg全局变量
  28. * @return {void}
  29. */
  30. constructor(ctx) {
  31. super(ctx);
  32. this.tableName = 'stage_audit';
  33. }
  34. /**
  35. * 获取 审核人信息
  36. *
  37. * @param {Number} stageId - 期id
  38. * @param {Number} auditorId - 审核人id
  39. * @param {Number} times - 第几次审批
  40. * @return {Promise<*>}
  41. */
  42. async getAuditor(stageId, auditorId, times = 1) {
  43. const sql =
  44. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  45. 'FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  46. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?';
  47. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  48. return await this.db.queryOne(sql, sqlParam);
  49. }
  50. async getAuditorByOrder(stageId, order, times) {
  51. const sql =
  52. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  53. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  54. ' FROM ' + this.tableName + ' AS la' +
  55. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  56. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  57. ' ORDER BY `order` DESC';
  58. const sqlParam = [stageId, order, times ? times: 1];
  59. return await this.db.queryOne(sql, sqlParam);
  60. }
  61. async getAuditorsByOrder(stageId, order, times) {
  62. const sql =
  63. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`,' +
  64. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  65. ' FROM ' + this.tableName + ' AS la' +
  66. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  67. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?' +
  68. ' ORDER BY `order` DESC';
  69. const sqlParam = [stageId, order, times ? times: 1];
  70. return await this.db.query(sql, sqlParam);
  71. }
  72. async getLastestAuditor(stageId, times, status) {
  73. const sql =
  74. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order,' +
  75. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  76. ' FROM ' + this.tableName + ' AS la' +
  77. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  78. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  79. ' ORDER BY `order` DESC';
  80. const sqlParam = [stageId, status, times ? times: 1];
  81. return await this.db.queryOne(sql, sqlParam);
  82. }
  83. async getLastestAuditors(stageId, times, status) {
  84. const sql =
  85. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order,' +
  86. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  87. ' FROM ' + this.tableName + ' AS la' +
  88. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  89. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  90. ' ORDER BY `order` DESC';
  91. const sqlParam = [stageId, status, times ? times: 1];
  92. const result = await this.db.query(sql, sqlParam);
  93. if (result.length === 0) return [];
  94. return result.filter(x => { return x.order === result[0].order });
  95. }
  96. /**
  97. * 获取 审核列表信息
  98. *
  99. * @param {Number} stageId - 期id
  100. * @param {Number} times - 第几次审批
  101. * @param {Number} order_sort - 列表排序方式
  102. * @return {Promise<*>}
  103. */
  104. async getAuditors(stageId, times = 1, order_sort = 'asc') {
  105. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  106. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
  107. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  108. ' WHERE la.sid = ? AND la.times = ?' +
  109. ' ORDER BY la.order ' + order_sort;
  110. const sqlParam = [stageId, times];
  111. const result = await this.db.query(sql, sqlParam);
  112. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  113. for (const i in result) {
  114. result[i].max_sort = max_sort;
  115. }
  116. return result;
  117. }
  118. async getAllAuditors(tenderId) {
  119. const sql =
  120. 'SELECT sa.aid, sa.tid FROM ' + this.tableName + ' sa' +
  121. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On sa.tid = t.id' +
  122. ' WHERE t.id = ?' +
  123. ' GROUP BY sa.aid';
  124. const sqlParam = [tenderId];
  125. return this.db.query(sql, sqlParam);
  126. }
  127. /**
  128. * 获取标段审核人最后一位的名称
  129. *
  130. * @param {Number} tenderId - 标段id
  131. * @param {Number} auditorId - 审核人id
  132. * @param {Number} times - 第几次审批
  133. * @return {Promise<*>}
  134. */
  135. async getStatusName(stageId) {
  136. const sql = 'SELECT pa.`name` FROM ?? AS sa Left Join ?? AS pa On sa.`aid` = pa.`id`' +
  137. ' WHERE sa.`sid` = ? and sa.`status` != ? ORDER BY sa.`times` DESC, sa.`order` DESC';
  138. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.uncheck];
  139. return await this.db.queryOne(sql, sqlParam);
  140. }
  141. /**
  142. * 获取 当前审核人
  143. *
  144. * @param {Number} stageId - 期id
  145. * @param {Number} times - 第几次审批
  146. * @return {Promise<*>}
  147. */
  148. async getCurAuditor(stageId, times = 1) {
  149. const sql =
  150. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  151. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  152. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  153. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  154. return await this.db.queryOne(sql, sqlParam);
  155. }
  156. async getCurAuditors(stageId, times = 1) {
  157. const sql =
  158. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`, la.audit_type, la.audit_order ' +
  159. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  160. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?';
  161. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  162. return await this.db.query(sql, sqlParam);
  163. }
  164. /**
  165. * 获取 最新审核顺序
  166. *
  167. * @param {Number} stageId - 期id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<number>}
  170. */
  171. async getNewOrder(stageId, times = 1) {
  172. const sql = 'SELECT Max(`order`) As max_order, Max(audit_order) As max_audit_order FROM ' + this.tableName + ' Where `sid` = ? and `times` = ?';
  173. const sqlParam = [stageId, times];
  174. const result = await this.db.queryOne(sql, sqlParam);
  175. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  176. }
  177. /**
  178. * 新增审核人
  179. *
  180. * @param {Number} stageId - 期id
  181. * @param {Number} auditorId - 审核人id
  182. * @param {Number} times - 第几次审批
  183. * @return {Promise<number>}
  184. */
  185. async addAuditor(stageId, auditorId, times = 1, is_gdzs = 0) {
  186. const transaction = await this.db.beginTransaction();
  187. try {
  188. let [newOrder, newAuditOrder] = await this.getNewOrder(stageId, times);
  189. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  190. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  191. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  192. if (is_gdzs) await this._syncOrderByDelete(transaction, stageId, newOrder, times, '+');
  193. const data = {
  194. tid: this.ctx.tender.id,
  195. sid: stageId,
  196. aid: auditorId,
  197. times,
  198. order: newOrder,
  199. status: auditConst.status.uncheck,
  200. audit_order: newAuditOrder,
  201. };
  202. const result = await transaction.insert(this.tableName, data);
  203. await transaction.commit();
  204. return result.effectRows = 1;
  205. } catch (err) {
  206. await transaction.rollback();
  207. throw err;
  208. }
  209. return false;
  210. }
  211. /**
  212. * 移除审核人时,同步其后审核人order
  213. * @param transaction - 事务
  214. * @param {Number} stageId - 标段id
  215. * @param {Number} auditorId - 审核人id
  216. * @param {Number} times - 第几次审批
  217. * @return {Promise<*>}
  218. * @private
  219. */
  220. async _syncOrderByDelete(transaction, stageId, order, times, selfOperate = '-') {
  221. this.initSqlBuilder();
  222. this.sqlBuilder.setAndWhere('sid', {
  223. value: stageId,
  224. operate: '=',
  225. });
  226. this.sqlBuilder.setAndWhere('order', {
  227. value: order,
  228. operate: '>=',
  229. });
  230. this.sqlBuilder.setAndWhere('times', {
  231. value: times,
  232. operate: '=',
  233. });
  234. this.sqlBuilder.setUpdateData('order', {
  235. value: 1,
  236. selfOperate: selfOperate,
  237. });
  238. this.sqlBuilder.setUpdateData('audit_order', {
  239. value: 1,
  240. selfOperate: selfOperate,
  241. });
  242. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  243. const data = await transaction.query(sql, sqlParam);
  244. return data;
  245. }
  246. /**
  247. * 移除审核人
  248. *
  249. * @param {Number} stageId - 期id
  250. * @param {Number} auditorId - 审核人id
  251. * @param {Number} times - 第几次审批
  252. * @return {Promise<boolean>}
  253. */
  254. async deleteAuditor(stageId, auditorId, times = 1) {
  255. const transaction = await this.db.beginTransaction();
  256. try {
  257. const condition = { sid: stageId, aid: auditorId, times };
  258. const auditor = await this.getDataByCondition(condition);
  259. if (!auditor) {
  260. throw '该审核人不存在';
  261. }
  262. // 移除整个流程的人
  263. await transaction.delete(this.tableName, { sid: stageId, order: auditor.order, times});
  264. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  265. await transaction.commit();
  266. } catch (err) {
  267. await transaction.rollback();
  268. throw err;
  269. }
  270. return true;
  271. }
  272. async _updateTender(transaction) {
  273. if (!this.ctx.tender) return;
  274. if (this.ctx.tender.data.measure_type !== measureType.gcl.value) return;
  275. const sum = await this.ctx.service.ledger.addUp({ tender_id: this.ctx.tender.id/* , is_leaf: true*/ });
  276. await transaction.update(this.ctx.service.tender.tableName, {
  277. id: this.ctx.tender.id, total_price: sum.total_price, deal_tp: sum.deal_tp
  278. });
  279. return sum;
  280. }
  281. /**
  282. * 开始审批
  283. *
  284. * @param {Number} stageId - 期id
  285. * @param {Number} times - 第几次审批
  286. * @return {Promise<boolean>}
  287. */
  288. async start(stageId, times = 1) {
  289. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: 1 } });
  290. if (audits.length === 0) {
  291. if(this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  292. throw '请联系管理员添加审批人';
  293. } else {
  294. throw '请先选择审批人,再上报数据';
  295. }
  296. }
  297. const transaction = await this.db.beginTransaction();
  298. try {
  299. const ledgerTp = await this._updateTender(transaction);
  300. const begin_time = new Date();
  301. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time } });
  302. await transaction.updateRows(this.tableName, updateData);
  303. // 计算原报最终数据
  304. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  305. // 复制一份下一审核人数据
  306. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  307. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  308. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  309. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  310. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  311. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  312. // 更新期数据
  313. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  314. const stageTp = {
  315. contract_tp: tpData.contract_tp,
  316. qc_tp: tpData.qc_tp,
  317. positive_qc_tp: tpData.positive_qc_tp,
  318. negative_qc_tp: tpData.negative_qc_tp,
  319. yf_tp: yfPay.tp,
  320. sf_tp: sfPay.tp,
  321. };
  322. this.ctx.stage.tp_history.push({ times: this.ctx.stage.curTimes, order: 0, ...stageTp });
  323. const completeStageTp = {
  324. ...stageTp,
  325. pre_contract_tp: this.ctx.stage.pre_contract_tp || 0, pre_qc_tp: this.ctx.stage.pre_qc_tp || 0,
  326. pre_positive_qc_tp: this.ctx.stage.pre_positive_qc_tp || 0, pre_negative_qc_tp: this.ctx.stage.pre_negative_qc_tp || 0,
  327. pre_yf_tp: this.ctx.stage.pre_yf_tp || 0, pre_sf_tp: this.ctx.stage.pre_sf_tp || 0,
  328. };
  329. await this.ctx.service.tenderCache.updateStageCache4Start(transaction, this.ctx.stage, auditConst.status.checking, audits, ledgerTp, completeStageTp);
  330. await transaction.update(this.ctx.service.stage.tableName, {
  331. id: stageId,
  332. status: auditConst.status.checking,
  333. ...stageTp,
  334. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  335. cache_time_r: this.ctx.stage.cache_time_l,
  336. });
  337. // 多人协同,取消下一审批人存在的锁定
  338. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, audits.map(x => { return x.aid; }), transaction);
  339. // 添加短信通知-需要审批提醒功能
  340. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  341. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  342. const auditIds = this._.map(audits, 'aid');
  343. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  344. users.push(...auditIds);
  345. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  346. qi: stageInfo.order,
  347. code: shenpiUrl,
  348. });
  349. // 微信模板通知
  350. const wechatData = {
  351. wap_url: shenpiUrl,
  352. qi: stageInfo.order,
  353. status: wxConst.status.check,
  354. tips: wxConst.tips.check,
  355. code: this.ctx.session.sessionProject.code,
  356. };
  357. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  358. // 重新发送配置
  359. for (const a of audits) {
  360. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  361. pid: this.ctx.session.sessionProject.id,
  362. tid: this.ctx.tender.id,
  363. uid: a.aid,
  364. sp_type: 'stage',
  365. sp_id: a.id,
  366. table_name: this.tableName,
  367. template: wxConst.template.stage,
  368. wx_data: wechatData,
  369. });
  370. }
  371. // 上报/审批 - 检查三方特殊推送
  372. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  373. await transaction.commit();
  374. // 通知发送 - 第三方更新
  375. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  376. const base_data = {
  377. tid: this.ctx.tender.id,
  378. sid: stageId,
  379. op: 'update',
  380. };
  381. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  382. base_data.op = 'update';
  383. base_data.sid = -1;
  384. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  385. }
  386. } catch (err) {
  387. await transaction.rollback();
  388. throw err;
  389. }
  390. return true;
  391. }
  392. async _checked(pid, stageId, checkData, times) {
  393. const accountId = this.ctx.session.sessionUser.accountId;
  394. const time = new Date();
  395. // 整理当前流程审核人状态更新
  396. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  397. if (audits.length === 0) throw '审核数据错误';
  398. const selfAudit = audits.find(x => { return x.aid === accountId; });
  399. if (!selfAudit) throw '当前标段您无权审批';
  400. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order } });
  401. const nextAudits = await this.getAllDataByCondition({ where: { sid: stageId, times, order: selfAudit.order + 1 } });
  402. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  403. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  404. // 计算并合同支付最终数据
  405. const [yfPay, sfPay, stagePays] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage);
  406. const transaction = await this.db.beginTransaction();
  407. try {
  408. const ledgerTp = await this._updateTender(transaction);
  409. // 添加通知
  410. const noticeContent = await this.getNoticeContent(pid, stageInfo.tid, stageId, accountId, checkData.opinion);
  411. const auditors = await this.getAuditGroupByListWithOwner(stageId, times);
  412. const defaultNoticeRecord = {
  413. pid,
  414. type: pushType.stage,
  415. status: auditConst.status.checked,
  416. content: noticeContent,
  417. };
  418. const records = [];
  419. auditors.forEach(audit => {
  420. records.push({
  421. uid: audit.aid,
  422. ...defaultNoticeRecord,
  423. });
  424. });
  425. this.ctx.stage.userAssists.forEach(u => {
  426. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  427. });
  428. this.ctx.stage.auditAssists.forEach(u => {
  429. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  430. });
  431. await transaction.insert('zh_notice', records);
  432. // 更新本人审批状态
  433. await transaction.update(this.tableName, {
  434. id: selfAudit.id,
  435. status: checkData.checkType,
  436. opinion: checkData.opinion,
  437. end_time: time,
  438. });
  439. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  440. const stageTp = {
  441. contract_tp: tpData.contract_tp,
  442. qc_tp: tpData.qc_tp,
  443. positive_qc_tp: tpData.positive_qc_tp,
  444. negative_qc_tp: tpData.negative_qc_tp,
  445. yf_tp: yfPay.tp,
  446. sf_tp: sfPay.tp,
  447. };
  448. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  449. if (audits.length === 1 || selfAudit.audit_type !== auditType.key.and) {
  450. // 或签更新他人审批状态
  451. if (selfAudit.audit_type === auditType.key.or) {
  452. const updateOther = [];
  453. for (const audit of audits) {
  454. if (audit.aid === selfAudit.aid) continue;
  455. updateOther.push({
  456. id: audit.id,
  457. status: auditConst.status.checkSkip,
  458. opinion: '',
  459. end_time: time,
  460. });
  461. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  462. }
  463. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  464. }
  465. // 无下一审核人表示,审核结束
  466. if (nextAudits.length > 0) {
  467. // 复制一份下一审核人数据
  468. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudits[0].order, transaction);
  469. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  470. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  471. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  472. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  473. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  474. // 流程至下一审批人
  475. const updateData = nextAudits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time: time }; });
  476. await transaction.updateRows(this.tableName, updateData);
  477. // 同步 期信息
  478. await transaction.update(this.ctx.service.stage.tableName, {
  479. id: stageId,
  480. status: auditConst.status.checking,
  481. ...stageTp,
  482. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  483. cache_time_r: this.ctx.stage.cache_time_l,
  484. });
  485. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, nextAudits, flowAudits, ledgerTp, stageTp);
  486. // 多人协同,取消下一审批人存在的锁定
  487. await this.ctx.service.stageAuditAss.cancelLock(this.ctx.stage, nextAudits.map(x => { return x.aid; }), transaction);
  488. // 添加短信通知-需要审批提醒功能
  489. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  490. const nextAuditIds = this._.map(nextAudits, 'aid');
  491. const users = this._.map(this._.filter(this.ctx.auditAssists, function (x) { return nextAuditIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  492. users.push(...nextAuditIds);
  493. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  494. qi: stageInfo.order,
  495. code: shenpiUrl,
  496. });
  497. // 微信模板通知
  498. const wechatData = {
  499. wap_url: shenpiUrl,
  500. qi: stageInfo.order,
  501. status: wxConst.status.check,
  502. tips: wxConst.tips.check,
  503. code: this.ctx.session.sessionProject.code,
  504. };
  505. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  506. // 重新发送配置
  507. for (const a of nextAudits) {
  508. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  509. pid: this.ctx.session.sessionProject.id,
  510. tid: this.ctx.tender.id,
  511. uid: a.aid,
  512. sp_type: 'stage',
  513. sp_id: a.id,
  514. table_name: this.tableName,
  515. template: wxConst.template.stage,
  516. wx_data: wechatData,
  517. });
  518. }
  519. } else {
  520. await this.ctx.service.revisePrice.doPriceUsed(this.ctx.stage, transaction);
  521. await this.ctx.service.tenderTag.saveTenderTag(this.ctx.tender.id, {stage_time: new Date()}, transaction);
  522. const his_id = await this.ctx.service.ledgerHistory.checkBackupLedgerHistory(this.ctx.stage.tid, this.ctx.stage.id);
  523. // 本期结束
  524. // 生成截止本期数据 final数据
  525. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  526. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  527. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  528. // 同步 期信息
  529. await transaction.update(this.ctx.service.stage.tableName, {
  530. id: stageId,
  531. status: checkData.checkType,
  532. ...stageTp,
  533. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  534. cache_time_r: this.ctx.stage.cache_time_l,
  535. his_id,
  536. });
  537. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, nextAudits, flowAudits, ledgerTp, stageTp);
  538. await this.ctx.service.stagePay.cacheOrder(this.ctx.stage, transaction);
  539. // 当前期不是最新一起时
  540. if (this.ctx.stage.highOrder !== this.ctx.stage.order) {
  541. const nextStages = await this.ctx.service.stage.getNextStages(this.ctx.stage.tid, this.ctx.stage.order);
  542. for (const ns of nextStages) {
  543. await this.ctx.service.stagePay.reInitialStageData(this.ctx.stage, ns, transaction);
  544. await this.ctx.service.stageOther.addInitialStageData(ns, this.ctx.stage, transaction);
  545. await this.ctx.service.stageSafeProd.addInitialStageData(ns, this.ctx.stage, transaction);
  546. await this.ctx.service.stageJgcl.addInitialStageData(ns, this.ctx.stage, transaction);
  547. await this.ctx.service.stageTempLand.addInitialStageData(ns, this.ctx.stage, transaction);
  548. }
  549. const preStageTp = {
  550. pre_contract_tp: this.ctx.helper.sum([this.ctx.stage.pre_contract_tp, stageTp.contract_tp, this.ctx.stage.contract_pc_tp]),
  551. pre_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_qc_tp, stageTp.qc_tp, this.ctx.stage.qc_pc_tp]),
  552. pre_positive_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_positive_qc_tp, stageTp.positive_qc_tp, this.ctx.stage.positive_qc_pc_tp]),
  553. pre_negative_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_negative_qc_tp, stageTp.negative_qc_tp, this.ctx.stage.negative_qc_pc_tp]),
  554. pre_yf_tp: this.ctx.helper.add(this.ctx.stage.pre_yf_tp, stageTp.yf_tp),
  555. pre_sf_tp: this.ctx.helper.add(this.ctx.stage.pre_sf_tp, stageTp.sf_tp),
  556. };
  557. await transaction.updateRows(this.ctx.service.stage.tableName, nextStages.map(x => { return { id: x.id, ...preStageTp }; }));
  558. }
  559. // 添加短信通知-审批通过提醒功能
  560. const auditList = await this.getAuditors(stageId, stageInfo.times);
  561. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
  562. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  563. qi: stageInfo.order,
  564. status: SmsAliConst.status.success,
  565. });
  566. // 微信模板通知
  567. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  568. const wechatData = {
  569. wap_url: shenpiUrl,
  570. qi: stageInfo.order,
  571. status: wxConst.status.success,
  572. tips: wxConst.tips.success,
  573. code: this.ctx.session.sessionProject.code,
  574. };
  575. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  576. // 期审批完成,检查角色签名数据
  577. await this.ctx.service.roleRptRel.checkSignDate(transaction, this.ctx.stage, selfAudit, time);
  578. // 审批通过 - 检查三方特殊推送
  579. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
  580. }
  581. // 上报/审批 - 检查三方特殊推送
  582. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  583. } else {
  584. // 同步 期信息
  585. await transaction.update(this.ctx.service.stage.tableName, {
  586. id: stageId,
  587. status: auditConst.status.checking,
  588. ...stageTp,
  589. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  590. cache_time_r: this.ctx.stage.cache_time_l,
  591. });
  592. }
  593. await transaction.commit();
  594. } catch (err) {
  595. await transaction.rollback();
  596. throw err;
  597. }
  598. }
  599. async _checkNo(pid, stageId, checkData, times) {
  600. const accountId = this.ctx.session.sessionUser.accountId;
  601. const time = new Date();
  602. // 整理当前流程审核人状态更新
  603. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  604. if (!audits) throw '审核数据错误';
  605. const selfAudit = audits.find(x => { return x.aid === accountId; });
  606. if (!selfAudit) throw '当前标段您无权审批';
  607. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  608. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  609. const auditors = await this.getUniqAuditor(stageId, times); // 全部参与的审批人
  610. const newAuditors = auditors.map(x => {
  611. return {
  612. aid: x.aid, tid: selfAudit.tid, sid: selfAudit.sid,
  613. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  614. audit_type: x.audit_type, audit_order: x.audit_order,
  615. }
  616. });
  617. const transaction = await this.db.beginTransaction();
  618. try {
  619. const ledgerTp = await this._updateTender(transaction);
  620. // 添加推送
  621. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  622. const defaultNoticeRecord = {
  623. pid,
  624. type: pushType.stage,
  625. status: auditConst.status.checkNo,
  626. content: noticeContent,
  627. };
  628. const records = [
  629. {
  630. uid: this.ctx.stage.user_id,
  631. ...defaultNoticeRecord,
  632. },
  633. ];
  634. auditors.forEach(audit => {
  635. records.push({
  636. uid: audit.aid,
  637. ...defaultNoticeRecord,
  638. });
  639. });
  640. this.ctx.stage.userAssists.forEach(u => {
  641. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  642. });
  643. this.ctx.stage.auditAssists.forEach(u => {
  644. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  645. });
  646. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  647. // 计算并合同支付最终数据
  648. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  649. const stageTp = {
  650. contract_tp: tpData.contract_tp,
  651. qc_tp: tpData.qc_tp,
  652. positive_qc_tp: tpData.positive_qc_tp,
  653. negative_qc_tp: tpData.negative_qc_tp,
  654. yf_tp: yfPay.tp,
  655. sf_tp: sfPay.tp,
  656. };
  657. const orgHisIndex = this.ctx.stage.tp_history.findIndex(x => { return x.times === times && x.order === selfAudit.order; });
  658. if (orgHisIndex) this.ctx.stage.tp_history.splice(orgHisIndex, 1);
  659. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  660. const updateData = [];
  661. audits.forEach(x => {
  662. updateData.push({
  663. id: x.id,
  664. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  665. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  666. end_time: x.aid === selfAudit.aid ? time : null,
  667. });
  668. });
  669. await transaction.updateRows(this.tableName, updateData);
  670. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  671. // 同步 期信息
  672. await transaction.update(this.ctx.service.stage.tableName, {
  673. id: stageId,
  674. status: checkData.checkType,
  675. ...stageTp,
  676. times: times + 1,
  677. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  678. cache_time_r: this.ctx.stage.cache_time_l,
  679. });
  680. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType,
  681. [{ aid: this.ctx.stage.user_id, audit_order: 0, audit_type: auditType.key.common, status: auditConst.status.uncheck }], flowAudits, ledgerTp, stageTp);
  682. // 拷贝新一次审核流程列表
  683. await transaction.insert(this.tableName, newAuditors);
  684. // 计算该审批人最终数据
  685. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  686. // 复制一份最新数据给原报
  687. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  688. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  689. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  690. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  691. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  692. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  693. // 锁定本人数据,保留锁定数据相关确认状态
  694. await this.ctx.service.stageAuditAss.lockConfirm4CheckNo(this.ctx.stage, audits.map(x => { return x.aid; }), auditors, transaction);
  695. // 添加短信通知-审批退回提醒功能
  696. // const mobile_array = [];
  697. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  698. const auditList = await this.getAuditors(stageId, stageInfo.times);
  699. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id, this._.map(this.ctx.stage.userAssists, 'ass_user_id'), this._.map(this.ctx.stage.auditAssists, 'ass_user_id')));
  700. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  701. qi: stageInfo.order,
  702. status: SmsAliConst.status.back,
  703. });
  704. // 微信模板通知
  705. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  706. const wechatData = {
  707. wap_url: shenpiUrl,
  708. qi: stageInfo.order,
  709. status: wxConst.status.back,
  710. tips: wxConst.tips.back,
  711. code: this.ctx.session.sessionProject.code,
  712. };
  713. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  714. // 上报/审批 - 检查三方特殊推送
  715. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  716. await transaction.commit();
  717. } catch (err) {
  718. await transaction.rollback();
  719. throw err;
  720. }
  721. }
  722. async _checkNoPre(pid, stageId, checkData, times) {
  723. const accountId = this.ctx.session.sessionUser.accountId;
  724. const time = new Date();
  725. // 整理当前流程审核人状态更新
  726. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  727. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  728. const selfAudit = audits.find(x => { return x.aid === accountId; });
  729. if (!selfAudit) throw '当前标段您无权审批';
  730. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  731. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  732. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  733. const auditors2 = await this.getAuditGroupByList(stageId, times);
  734. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  735. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  736. const transaction = await this.db.beginTransaction();
  737. try {
  738. const ledgerTp = await this._updateTender(transaction);
  739. // 添加通知
  740. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  741. const defaultNoticeRecord = {
  742. pid,
  743. type: pushType.stage,
  744. status: auditConst.status.checkNoPre,
  745. content: noticeContent,
  746. };
  747. const records = [
  748. {
  749. uid: this.ctx.stage.user_id,
  750. ...defaultNoticeRecord
  751. },
  752. ];
  753. auditors2.forEach(audit => {
  754. records.push({
  755. uid: audit.aid,
  756. ...defaultNoticeRecord
  757. });
  758. });
  759. this.ctx.stage.userAssists.forEach(u => {
  760. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  761. });
  762. this.ctx.stage.auditAssists.forEach(u => {
  763. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  764. });
  765. await transaction.insert('zh_notice', records);
  766. // 计算并合同支付最终数据
  767. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  768. const stageTp = {
  769. contract_tp: tpData.contract_tp,
  770. qc_tp: tpData.qc_tp,
  771. positive_qc_tp: tpData.positive_qc_tp,
  772. negative_qc_tp: tpData.negative_qc_tp,
  773. yf_tp: yfPay.tp,
  774. sf_tp: sfPay.tp,
  775. };
  776. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  777. // 同步 期信息
  778. await transaction.update(this.ctx.service.stage.tableName, {
  779. id: stageId,
  780. times,
  781. ...stageTp,
  782. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  783. cache_time_r: this.ctx.stage.cache_time_l,
  784. });
  785. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, preAuditors, flowAudits, ledgerTp, stageTp);
  786. // 更新同一流程所有审批人状态
  787. const updateData = [];
  788. for (const audit of audits) {
  789. if (audit.aid === selfAudit.aid) {
  790. updateData.push({
  791. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
  792. });
  793. } else {
  794. updateData.push({
  795. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
  796. });
  797. }
  798. }
  799. await transaction.updateRows(this.tableName, updateData);
  800. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  801. // 顺移其后审核人流程顺序
  802. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  803. await transaction.query(sql, [stageId, selfAudit.times, selfAudit.order]);
  804. // 上一审批人,当前审批人 再次添加至流程
  805. const newAuditors = [];
  806. preAuditors.forEach(x => {
  807. newAuditors.push({
  808. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  809. times: x.times, order: selfAudit.order + 1,
  810. status: auditConst.status.checking, begin_time: time,
  811. audit_type: x.audit_type, audit_order: x.audit_order,
  812. });
  813. });
  814. // 获取ids值
  815. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  816. // 获取刚批量添加的所有list
  817. for (let j = 0; j < preAuditors.length; j++) {
  818. newAuditors[j].id = newAuditors_result.insertId + j;
  819. }
  820. const newFlowAuditors = [];
  821. flowAudits.forEach(x => {
  822. newFlowAuditors.push({
  823. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  824. times: x.times, order: selfAudit.order + 2,
  825. status: auditConst.status.uncheck,
  826. audit_type: x.audit_type, audit_order: x.audit_order,
  827. });
  828. });
  829. await transaction.insert(this.tableName, newFlowAuditors);
  830. // 计算该审批人最终数据
  831. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  832. // 复制一份最新数据给下一人
  833. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, selfAudit.order + 1, transaction);
  834. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  835. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  836. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  837. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  838. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  839. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  840. // 锁定本人数据,保留锁定数据相关确认状态
  841. await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audits.map(x => { return x.aid; }), preAuditorIds, transaction);
  842. // 同步 期信息
  843. await transaction.update(this.ctx.service.stage.tableName, {
  844. id: stageId,
  845. status: checkData.checkType,
  846. cache_time_r: this.ctx.stage.cache_time_l,
  847. });
  848. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  849. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  850. const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id');
  851. users.push(...preAuditorIds);
  852. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  853. qi: stageInfo.order,
  854. code: shenpiUrl,
  855. });
  856. // 微信模板通知
  857. const wechatData = {
  858. wap_url: shenpiUrl,
  859. qi: stageInfo.order,
  860. status: wxConst.status.check,
  861. tips: wxConst.tips.check,
  862. code: this.ctx.session.sessionProject.code,
  863. };
  864. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  865. // 重新发送配置
  866. for (const a of newAuditors) {
  867. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  868. pid: this.ctx.session.sessionProject.id,
  869. tid: this.ctx.tender.id,
  870. uid: a.aid,
  871. sp_type: 'stage',
  872. sp_id: a.id,
  873. table_name: this.tableName,
  874. template: wxConst.template.stage,
  875. wx_data: wechatData,
  876. });
  877. }
  878. // 上报/审批 - 检查三方特殊推送
  879. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  880. await transaction.commit();
  881. } catch (err) {
  882. await transaction.rollback();
  883. throw err;
  884. }
  885. }
  886. /**
  887. * 审批
  888. * @param {Number} stageId - 标段id
  889. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  890. * @param {Number} times - 第几次审批
  891. * @return {Promise<void>}
  892. */
  893. async check(stageId, checkData, times = 1) {
  894. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  895. throw '提交数据错误';
  896. }
  897. // // 整理当前流程审核人状态更新
  898. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  899. // if (!audit) {
  900. // throw '审核数据错误';
  901. // }
  902. // const time = new Date();
  903. const pid = this.ctx.session.sessionProject.id;
  904. switch (checkData.checkType) {
  905. case auditConst.status.checked:
  906. await this._checked(pid, stageId, checkData, times);
  907. break;
  908. case auditConst.status.checkNo:
  909. await this._checkNo(pid, stageId, checkData, times);
  910. break;
  911. case auditConst.status.checkNoPre:
  912. await this._checkNoPre(pid, stageId, checkData, times);
  913. break;
  914. default:
  915. throw '无效审批操作';
  916. }
  917. // 通知发送 - 第三方更新
  918. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  919. const base_data = {
  920. tid: this.ctx.tender.id,
  921. sid: stageId,
  922. op: 'update',
  923. };
  924. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  925. base_data.op = 'update';
  926. base_data.sid = -1;
  927. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  928. }
  929. }
  930. /**
  931. * 审批
  932. * @param {Number} stageId - 标段id
  933. * @param {Number} times - 第几次审批
  934. * @return {Promise<void>}
  935. */
  936. async checkAgain(stageId, times = 1, force) {
  937. const accountId = this.ctx.session.sessionUser.accountId;
  938. const time = new Date();
  939. // 整理当前流程审核人状态更新
  940. const auditors = await this.getAllDataByCondition({
  941. where: { sid: stageId, times },
  942. orders: [['order', 'asc']],
  943. });
  944. const maxOrder = auditors[auditors.length - 1].order;
  945. const audits = auditors.filter(x => { return x.order === maxOrder});
  946. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  947. const selfAudit = audits.find(x => { return x.aid === accountId; });
  948. if (!selfAudit && !force) throw '当前标段您无权审批';
  949. const finalAudit = selfAudit || audits[0];
  950. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  951. const transaction = await this.db.beginTransaction();
  952. try {
  953. // 当前审批人2次添加至流程中
  954. const checkAgainAuditors = [], checkingAuditors = [];
  955. audits.forEach(x => {
  956. checkAgainAuditors.push({
  957. tid: x.tid, sid: x.sid, aid: x.aid,
  958. times: x.times, order: x.order + 1,
  959. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  960. begin_time: time, end_time: time, opinion: '',
  961. audit_type: x.audit_type, audit_order: x.audit_order,
  962. });
  963. });
  964. audits.forEach(x => {
  965. checkingAuditors.push({
  966. tid: x.tid, sid: x.sid, aid: x.aid,
  967. times: x.times, order: x.order + 2,
  968. status: auditConst.status.checking,
  969. begin_time: time, end_time: time, opinion: '',
  970. audit_type: x.audit_type, audit_order: x.audit_order,
  971. });
  972. });
  973. await transaction.insert(this.tableName, checkAgainAuditors);
  974. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  975. // 获取刚批量添加的所有list
  976. for (let j = 0; j < checkingAuditors.length; j++) {
  977. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  978. }
  979. // 复制一份最新数据给下一人
  980. await this.ctx.service.stagePay.clearCacheOrder(this.ctx.stage, transaction);
  981. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, finalAudit.order + 1, transaction);
  982. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, finalAudit.order + 2, transaction);
  983. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  984. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  985. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  986. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  987. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  988. // 同步 期信息
  989. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === finalAudit.order });
  990. const stageTp = {
  991. contract_tp: his.contract_tp,
  992. qc_tp: his.qc_tp,
  993. positive_qc_tp: his.positive_qc_tp,
  994. negative_qc_tp: his.negative_qc_tp,
  995. yf_tp: his.tp,
  996. sf_tp: his.tp,
  997. };
  998. this.ctx.stage.tp_history.push({ times, order: finalAudit.order + 1, ...stageTp });
  999. await transaction.update(this.ctx.service.stage.tableName, {
  1000. id: stageId,
  1001. status: auditConst.status.checking,
  1002. cache_time_r: this.ctx.stage.cache_time_l,
  1003. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1004. });
  1005. // 已经引用到本期的单价变更,全部取消
  1006. await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
  1007. // 重算所有单价变更
  1008. const priceCalc = new RevisePrice(this.ctx);
  1009. const pcTp = await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, finalAudit.order + 2, transaction);
  1010. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, checkingAuditors, checkAgainAuditors, null, stageTp, pcTp);
  1011. // 添加短信通知-需要审批提醒功能
  1012. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  1013. const auditorIds = audits.map(x => { return x.aid; });
  1014. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  1015. users.push(...auditorIds);
  1016. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  1017. qi: stageInfo.order,
  1018. code: shenpiUrl,
  1019. });
  1020. // 微信模板通知
  1021. const wechatData = {
  1022. wap_url: shenpiUrl,
  1023. qi: stageInfo.order,
  1024. status: wxConst.status.check,
  1025. tips: wxConst.tips.check,
  1026. code: this.ctx.session.sessionProject.code,
  1027. };
  1028. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  1029. // 重新发送配置
  1030. for (const a of checkingAuditors) {
  1031. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  1032. pid: this.ctx.session.sessionProject.id,
  1033. tid: this.ctx.tender.id,
  1034. uid: a.aid,
  1035. sp_type: 'stage',
  1036. sp_id: a.id,
  1037. table_name: this.tableName,
  1038. template: wxConst.template.stage,
  1039. wx_data: wechatData,
  1040. });
  1041. }
  1042. // 上报/审批 - 检查三方特殊推送
  1043. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1044. await transaction.commit();
  1045. // 通知发送 - 第三方更新
  1046. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1047. const base_data = {
  1048. tid: this.ctx.tender.id,
  1049. sid: stageId,
  1050. op: 'update',
  1051. };
  1052. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1053. base_data.op = 'update';
  1054. base_data.sid = -1;
  1055. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1056. }
  1057. } catch (err) {
  1058. await transaction.rollback();
  1059. throw err;
  1060. }
  1061. }
  1062. /**
  1063. * 原报撤回,直接改动审批人状态
  1064. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  1065. *
  1066. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  1067. * ...
  1068. *
  1069. * @param stage
  1070. * @returns {Promise<void>}
  1071. * @private
  1072. */
  1073. async _userCheckCancel(stage) {
  1074. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1075. const transaction = await this.db.beginTransaction();
  1076. try {
  1077. const ledgerTp = await this._updateTender(transaction);
  1078. // 整理当前流程审核人状态更新
  1079. // 审批人变成待审批状态
  1080. const updateData = stage.curAuditors.map(x => {
  1081. return {
  1082. id: x.id,
  1083. status: auditConst.status.uncheck,
  1084. begin_time: null,
  1085. opinion: null,
  1086. }
  1087. });
  1088. await transaction.updateRows(this.tableName, updateData);
  1089. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  1090. // 计算并合同支付最终数据
  1091. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1092. const stageTp = {
  1093. contract_tp: tpData.contract_tp,
  1094. qc_tp: tpData.qc_tp,
  1095. positive_qc_tp: tpData.positive_qc_tp,
  1096. negative_qc_tp: tpData.negative_qc_tp,
  1097. yf_tp: yfPay.tp,
  1098. sf_tp: sfPay.tp,
  1099. };
  1100. await transaction.update(this.ctx.service.stage.tableName, {
  1101. id: stage.id,
  1102. times: stage.times,
  1103. ...stageTp,
  1104. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < stage.times; })),
  1105. cache_time_r: this.ctx.stage.cache_time_l,
  1106. status: stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1107. });
  1108. // 计量明细:重复数据删除原报,新增数据修改为原报
  1109. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1110. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1111. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1112. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1113. // 合同支付明细:保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
  1114. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1115. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, 0, transaction, this.ctx.stage.times, 1);
  1116. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 1, transaction);
  1117. // 其他台账明细:不需更新历史
  1118. // 更新标段计量数据缓存
  1119. const preFlow = stage.times > 1 ? await this.getLastestAuditors(stage.id, stage.times, auditConst.status.checkNo): null;
  1120. const curFlow = [{ aid: this.ctx.stage.user_id, audit_type: auditType.key.common, audit_order: 1, status: auditConst.status.uncheck}];
  1121. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1122. curFlow, preFlow, ledgerTp, stageTp);
  1123. // 上报/审批 - 检查三方特殊推送
  1124. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1125. await transaction.commit();
  1126. } catch(err) {
  1127. await transaction.rollback();
  1128. throw err;
  1129. }
  1130. }
  1131. /**
  1132. * 审批人撤回审批通过,插入两条数据
  1133. *
  1134. * 一审 1 A checked 一审 1 A checked
  1135. * 二审 2 B checked pre -> 二审 2 B checked
  1136. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  1137. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  1138. * 三审 5 C uncheck order、status改
  1139. * 四审 6 D uncheck order改
  1140. *
  1141. * @param stage
  1142. * @returns {Promise<void>}
  1143. * @private
  1144. */
  1145. async _auditCheckCancel(stage) {
  1146. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1147. throw '撤回用户数据错误';
  1148. }
  1149. const accountId = this.ctx.session.sessionUser.accountId;
  1150. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1151. if (!selfAuditor) throw '撤回用户数据错误';
  1152. const time = new Date();
  1153. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1154. const transaction = await this.db.beginTransaction();
  1155. try {
  1156. const ledgerTp = await this._updateTender(transaction);
  1157. // 整理当前流程审核人状态更新
  1158. // 顺移其后审核人流程顺序
  1159. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1160. await transaction.query(sql, [stage.id, selfAuditor.times, stage.curAuditors[0].order]);
  1161. // 当前审批人2次添加至流程中
  1162. const checkCancelAuditors = [], checkingAuditors = [];
  1163. stage.preAuditors.forEach(x => {
  1164. checkCancelAuditors.push({
  1165. tid: stage.tid, sid: stage.id, aid: x.aid,
  1166. times: x.times, order: x.order + 1,
  1167. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1168. begin_time: time, end_time: time, opinion: '',
  1169. audit_type: x.audit_type, audit_order: x.audit_order,
  1170. });
  1171. });
  1172. stage.preAuditors.forEach(x => {
  1173. checkingAuditors.push({
  1174. tid: stage.tid, sid: stage.id, aid: x.aid,
  1175. times: x.times, order: x.order + 2,
  1176. status: auditConst.status.checking,
  1177. begin_time: time, end_time: time, opinion: '',
  1178. audit_type: x.audit_type, audit_order: x.audit_order,
  1179. });
  1180. });
  1181. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1182. // 当前审批人变成待审批
  1183. await transaction.updateRows(this.tableName, stage.curAuditors.map(x => { return {
  1184. id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2
  1185. }}));
  1186. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(stage.curAuditors, 'id'));
  1187. // 计算并合同支付最终数据
  1188. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1189. const stageTp = {
  1190. contract_tp: tpData.contract_tp,
  1191. qc_tp: tpData.qc_tp,
  1192. positive_qc_tp: tpData.positive_qc_tp,
  1193. negative_qc_tp: tpData.negative_qc_tp,
  1194. yf_tp: yfPay.tp,
  1195. sf_tp: sfPay.tp,
  1196. };
  1197. this.ctx.stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1198. // 同步 期信息
  1199. await transaction.update(this.ctx.service.stage.tableName, {
  1200. id: stage.id,
  1201. times: stage.times,
  1202. ...stageTp,
  1203. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1204. cache_time_r: this.ctx.stage.cache_time_l,
  1205. });
  1206. // 计量明细:勿需修改任何数据
  1207. // 合同支付明细:复制一份最新数据给下一人
  1208. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1209. // 其他台账明细:更新两份历史数据
  1210. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1211. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1212. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1213. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1214. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1215. // 更新标段计量数据缓存
  1216. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1217. // 上报/审批 - 检查三方特殊推送
  1218. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1219. await transaction.commit();
  1220. } catch(err) {
  1221. await transaction.rollback();
  1222. throw err;
  1223. }
  1224. }
  1225. /**
  1226. * 审批人撤回审批退回上一人,插入两条数据
  1227. *
  1228. * 一审 1 A checked 一审 1 A checked
  1229. * 二审 2 B checked 二审 2 B checked
  1230. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  1231. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  1232. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  1233. * 四审 6 D uncheck 四审 6 D uncheck
  1234. *
  1235. * @param stage
  1236. * @returns {Promise<void>}
  1237. * @private
  1238. */
  1239. async _auditCheckCancelNoPre(stage) {
  1240. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1241. throw '撤回用户数据错误';
  1242. }
  1243. const accountId = this.ctx.session.sessionUser.accountId;
  1244. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1245. if (!selfAuditor) throw '撤回用户数据错误';
  1246. const time = new Date();
  1247. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1248. const transaction = await this.db.beginTransaction();
  1249. try {
  1250. const ledgerTp = await this._updateTender(transaction);
  1251. // 整理当前流程审核人状态更新
  1252. // 删除当前审批人
  1253. await transaction.delete(this.tableName, { id: stage.curAuditors.map(x => { return x.id; }) });
  1254. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(stage.curAuditors, 'id'));
  1255. // 添加撤回人到审批流程中
  1256. const newAuditors = [];
  1257. stage.preAuditors.forEach(x => {
  1258. newAuditors.push({
  1259. tid: stage.tid, sid: stage.id, aid: x.aid,
  1260. times: x.times, order: x.order + 1,
  1261. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1262. begin_time: time, end_time: time, opinion: '',
  1263. audit_type: x.audit_type, audit_order: x.audit_order,
  1264. });
  1265. });
  1266. await transaction.insert(this.tableName, newAuditors);
  1267. // 更新上一个人,最新审批状态为审批中
  1268. await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
  1269. where: { sid: stage.id, times: selfAuditor.times, order: selfAuditor.order + 2 }
  1270. });
  1271. // 计算并合同支付最终数据
  1272. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1273. const stageTp = {
  1274. contract_tp: tpData.contract_tp,
  1275. qc_tp: tpData.qc_tp,
  1276. positive_qc_tp: tpData.positive_qc_tp,
  1277. negative_qc_tp: tpData.negative_qc_tp,
  1278. yf_tp: yfPay.tp,
  1279. sf_tp: sfPay.tp,
  1280. };
  1281. stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1282. // 同步 期信息
  1283. await transaction.update(this.ctx.service.stage.tableName, {
  1284. id: stage.id,
  1285. times: stage.times,
  1286. ...stageTp,
  1287. status: auditConst.status.checking,
  1288. tp_history: JSON.stringify(stage.tp_history),
  1289. cache_time_r: stage.cache_time_l,
  1290. });
  1291. // 计量明细:勿需修改任何数据
  1292. // 合同支付明细:复制一份最新数据给下一人
  1293. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1294. // 其他台账明细:更新一份历史数据
  1295. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1296. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1297. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1298. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1299. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1300. // 更新标段计量数据缓存
  1301. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, newAuditors, stage.preAuditors, ledgerTp, stageTp);
  1302. // 上报/审批 - 检查三方特殊推送
  1303. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1304. await transaction.commit();
  1305. } catch(err) {
  1306. await transaction.rollback();
  1307. throw err;
  1308. }
  1309. }
  1310. /**
  1311. * 审批人撤回审批退回原报
  1312. *
  1313. * 1# 一审 1 A checked 1# 一审 1 A checked
  1314. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  1315. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  1316. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  1317. * 三审 5 C uncheck order改
  1318. *
  1319. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  1320. * 二审 2 B uncheck
  1321. * 三审 3 C uncheck
  1322. *
  1323. * @param stage
  1324. * @returns {Promise<void>}
  1325. * @private
  1326. */
  1327. async _auditCheckCancelNo(stage) {
  1328. const accountId = this.ctx.session.sessionUser.accountId;
  1329. const selfAuditor = stage.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  1330. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  1331. const time = new Date();
  1332. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1333. const transaction = await this.db.beginTransaction();
  1334. try {
  1335. const ledgerTp = await this._updateTender(transaction);
  1336. // 整理上一个流程审核人状态更新
  1337. // 顺移其后审核人流程顺序
  1338. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1339. await transaction.query(sql, [stage.id, selfAuditor.times, selfAuditor.order]);
  1340. // 当前审批人2次添加至流程中
  1341. const checkCancelAuditors = [], checkingAuditors = [];
  1342. stage.preAuditors.forEach(x => {
  1343. checkCancelAuditors.push({
  1344. tid: stage.tid, sid: stage.id, aid: x.aid,
  1345. times: x.times, order: x.order + 1,
  1346. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1347. begin_time: time, end_time: time, opinion: '',
  1348. audit_type: x.audit_type, audit_order: x.audit_order,
  1349. });
  1350. });
  1351. stage.preAuditors.forEach(x => {
  1352. checkingAuditors.push({
  1353. tid: stage.tid, sid: stage.id, aid: x.aid,
  1354. times: x.times, order: x.order + 2,
  1355. status: auditConst.status.checking,
  1356. begin_time: time, end_time: time, opinion: '',
  1357. audit_type: x.audit_type, audit_order: x.audit_order,
  1358. });
  1359. });
  1360. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1361. // 删除当前次审批流
  1362. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1363. // 计算并合同支付最终数据
  1364. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1365. const stageTp = {
  1366. contract_tp: tpData.contract_tp,
  1367. qc_tp: tpData.qc_tp,
  1368. positive_qc_tp: tpData.positive_qc_tp,
  1369. negative_qc_tp: tpData.negative_qc_tp,
  1370. yf_tp: yfPay.tp,
  1371. sf_tp: sfPay.tp,
  1372. };
  1373. stage.tp_history.push({ times: selfAuditor.times, order: selfAuditor.order + 1, ...stageTp });
  1374. await transaction.update(this.ctx.service.stage.tableName, {
  1375. id: stage.id,
  1376. times: stage.times - 1,
  1377. ...stageTp,
  1378. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1379. cache_time_r: stage.cache_time_l,
  1380. status: auditConst.status.checking,
  1381. });
  1382. // 计量明细:重复数据删除原报,新增数据修改为原报
  1383. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1384. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1385. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1386. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1387. // 合同支付明细:复制一份最新数据新增的2个审批流,删除最新一次审批数据
  1388. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 1, transaction, stage.times, 0);
  1389. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 2, transaction, stage.times, 0);
  1390. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1391. // 其他台账明细:更新一份历史数据
  1392. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1393. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1394. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1395. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1396. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1397. // 更新标段计量数据缓存
  1398. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, auditConst.status.checking, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1399. // 上报/审批 - 检查三方特殊推送
  1400. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1401. await transaction.commit();
  1402. } catch(err) {
  1403. await transaction.rollback();
  1404. throw err;
  1405. }
  1406. }
  1407. /**
  1408. * 会签未全部审批通过时,撤回仅修改本人状态
  1409. *
  1410. * @param stage
  1411. * @returns {Promise<void>}
  1412. * @private
  1413. */
  1414. async _auditCheckCancelAnd(stage) {
  1415. const accountId = this.ctx.session.sessionUser.accountId;
  1416. const selfAuditor = stage.flowAuditors.find(x => { return x.aid === accountId; });
  1417. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  1418. const transaction = await this.db.beginTransaction();
  1419. try {
  1420. await transaction.update(this.tableName, {
  1421. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  1422. });
  1423. await transaction.commit();
  1424. } catch(err) {
  1425. await transaction.rollback();
  1426. throw err;
  1427. }
  1428. }
  1429. /**
  1430. * 审批撤回
  1431. * @param {Number} stageId - 标段id
  1432. * @param {Number} times - 第几次审批
  1433. * @return {Promise<void>}
  1434. */
  1435. async checkCancel(stage) {
  1436. // 分5种情况,根据ctx.cancancel值判断:
  1437. // 1.原报发起撤回,当前流程删除,并回到待上报
  1438. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  1439. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  1440. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  1441. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  1442. if (stage.cancancel === 5) {
  1443. await this._auditCheckCancelAnd(stage);
  1444. } else {
  1445. switch (this.ctx.stage.cancancel) {
  1446. case 1: await this._userCheckCancel(stage); break;
  1447. case 2: await this._auditCheckCancel(stage); break;
  1448. case 3: await this._auditCheckCancelNoPre(stage); break;
  1449. case 4: await this._auditCheckCancelNo(stage); break;
  1450. default: throw '不可撤回,请刷新页面重试';
  1451. }
  1452. // 通知发送 - 第三方更新
  1453. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1454. const base_data = {
  1455. tid: stage.tid,
  1456. sid: stage.id,
  1457. op: 'update',
  1458. };
  1459. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1460. base_data.op = 'update';
  1461. base_data.sid = -1;
  1462. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1463. }
  1464. }
  1465. }
  1466. /**
  1467. * 获取审核人需要审核的期列表
  1468. *
  1469. * @param auditorId
  1470. * @return {Promise<*>}
  1471. */
  1472. async getAuditStage(auditorId) {
  1473. const sql =
  1474. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1475. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  1476. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  1477. ' FROM ?? AS sa ' +
  1478. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  1479. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  1480. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  1481. ' ORDER BY sa.`begin_time` DESC';
  1482. const sqlParam = [
  1483. this.tableName,
  1484. this.ctx.service.stage.tableName,
  1485. this.ctx.service.tender.tableName,
  1486. auditorId,
  1487. auditConst.status.checking,
  1488. auditorId,
  1489. auditConst.status.checkNo,
  1490. auditConst.status.checkNo,
  1491. ];
  1492. const result = await this.db.query(sql, sqlParam);
  1493. // 过滤result中存在重复sid的值, 保留最新的一条
  1494. const filterResult = [];
  1495. const sidArr = [];
  1496. for (const r of result) {
  1497. if (sidArr.indexOf(r.sid) === -1) {
  1498. filterResult.push(r);
  1499. sidArr.push(r.sid);
  1500. }
  1501. }
  1502. return filterResult;
  1503. }
  1504. /**
  1505. * 获取审核人审核的次数
  1506. *
  1507. * @param auditorId
  1508. * @return {Promise<*>}
  1509. */
  1510. async getCountByChecked(auditorId) {
  1511. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  1512. }
  1513. /**
  1514. * 获取最近一次审批结束时间
  1515. *
  1516. * @param auditorId
  1517. * @return {Promise<*>}
  1518. */
  1519. async getLastEndTimeByChecked(auditorId) {
  1520. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1521. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1522. const sqlParam = [this.tableName, auditorId];
  1523. const result = await this.db.queryOne(sql, sqlParam);
  1524. return result ? result.end_time : null;
  1525. }
  1526. /**
  1527. * 获取 某时间后 审批进度 更新的期
  1528. * @param {Number} pid - 查询标段
  1529. * @param {Number} uid - 查询人
  1530. * @param {Date} time - 查询时间
  1531. * @return {Promise<*>}
  1532. */
  1533. async getNoticeStage(pid, uid, time) {
  1534. let notice = await this.db.select('zh_notice', {
  1535. where: { pid, type: pushType.stage, uid },
  1536. orders: [['create_time', 'desc']],
  1537. limit: 10,
  1538. offset: 0,
  1539. });
  1540. notice = notice.map(v => {
  1541. const extra = JSON.parse(v.content);
  1542. delete v.content;
  1543. return { ...v, ...extra };
  1544. });
  1545. return notice;
  1546. }
  1547. /**
  1548. * 用于添加推送所需的content内容
  1549. * @param {Number} pid 项目id
  1550. * @param {Number} tid 台账id
  1551. * @param {Number} sid 期id
  1552. * @param {Number} uid 审核人id
  1553. */
  1554. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1555. const noticeSql =
  1556. 'SELECT * FROM (SELECT ' +
  1557. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1558. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1559. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1560. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1561. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1562. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1563. const content = await this.db.query(noticeSql, noticeSqlParam);
  1564. if (content.length) {
  1565. content[0].opinion = opinion;
  1566. }
  1567. return content.length ? JSON.stringify(content[0]) : '';
  1568. }
  1569. /**
  1570. * 获取审核人流程列表
  1571. *
  1572. * @param auditorId
  1573. * @return {Promise<*>}
  1574. */
  1575. async getAuditGroupByList(stageId, times, transaction = false) {
  1576. // const sql =
  1577. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
  1578. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1579. // ' WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
  1580. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1581. const sql =
  1582. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
  1583. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `sid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  1584. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  1585. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  1586. const sqlParam = [this.tableName, stageId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, stageId, times, 0];
  1587. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  1588. }
  1589. /**
  1590. * 获取审核人流程列表
  1591. *
  1592. * @param auditorId
  1593. * @return {Promise<*>}
  1594. */
  1595. async getAuditGroupByListWithOwner(stageId, times) {
  1596. const result = await this.getAuditGroupByList(stageId, times);
  1597. const sql =
  1598. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1599. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1600. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1601. ' ON s.user_id = pa.id' +
  1602. ' WHERE s.id = ?';
  1603. const sqlParam = [times, stageId, stageId];
  1604. const user = await this.db.queryOne(sql, sqlParam);
  1605. result.unshift(user);
  1606. return result;
  1607. }
  1608. /**
  1609. * 复制上一期的审批人列表给最新一期
  1610. *
  1611. * @param transaction - 新增一期的事务
  1612. * @param {Object} preStage - 上一期
  1613. * @param {Object} newStage - 最新一期
  1614. * @return {Promise<*>}
  1615. */
  1616. async copyPreStageAuditors(transaction, preStage, newStage) {
  1617. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1618. const newAuditors = [];
  1619. for (const a of auditors) {
  1620. const na = {
  1621. tid: preStage.tid,
  1622. sid: newStage.id,
  1623. aid: a.aid,
  1624. times: newStage.times,
  1625. order: a.audit_order,
  1626. status: auditConst.status.uncheck,
  1627. audit_type: a.audit_type,
  1628. audit_order: a.audit_order,
  1629. };
  1630. newAuditors.push(na);
  1631. }
  1632. if (newAuditors.length > 0) {
  1633. const result = await transaction.insert(this.tableName, newAuditors);
  1634. return (result.effectRows = auditors.length);
  1635. } else {
  1636. return true;
  1637. }
  1638. }
  1639. /**
  1640. * 移除审核人
  1641. *
  1642. * @param {Number} stageId - 期id
  1643. * @param {Number} status - 期状态
  1644. * @param {Number} status - 期次数
  1645. * @return {Promise<boolean>}
  1646. */
  1647. async getAuditorByStatus(stageId, status, times = 1) {
  1648. let auditor = null;
  1649. let sql = '';
  1650. let sqlParam = '';
  1651. switch (status) {
  1652. case auditConst.status.checking:
  1653. case auditConst.status.checked:
  1654. case auditConst.status.checkNoPre:
  1655. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1656. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1657. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1658. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1659. auditor = await this.db.queryOne(sql, sqlParam);
  1660. break;
  1661. case auditConst.status.checkNo:
  1662. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1663. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1664. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1665. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1666. auditor = await this.db.queryOne(sql, sqlParam);
  1667. break;
  1668. case auditConst.status.uncheck:
  1669. default:
  1670. break;
  1671. }
  1672. return auditor;
  1673. }
  1674. async getAuditorsByStatus(stageId, status, times = 1) {
  1675. let auditor = [];
  1676. let sql = '';
  1677. let sqlParam = '';
  1678. let cur;
  1679. switch (status) {
  1680. case auditConst.status.checking:
  1681. case auditConst.status.checked:
  1682. case auditConst.status.checkNoPre:
  1683. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, times, status]);
  1684. if (!cur) return [];
  1685. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1686. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1687. ' WHERE la.`sid` = ? and la.`order` = ? and times = ?';
  1688. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, times];
  1689. auditor = await this.db.query(sql, sqlParam);
  1690. break;
  1691. case auditConst.status.checkNo:
  1692. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, parseInt(times) - 1, status]);
  1693. if (!cur) return [];
  1694. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1695. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1696. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?';
  1697. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, parseInt(times) - 1];
  1698. auditor = await this.db.query(sql, sqlParam);
  1699. break;
  1700. case auditConst.status.uncheck:
  1701. default:
  1702. break;
  1703. }
  1704. return auditor;
  1705. }
  1706. /**
  1707. * 取某一期已批准审核信息(报表用)
  1708. *
  1709. * @param {Number} stageId - 期id
  1710. * @param {Number} times - 期次数
  1711. * @return {Promise<boolean>}
  1712. */
  1713. async getStageAudit(stageId, times = 1) {
  1714. const sql = 'SELECT a1.aid, a1.begin_time, a1.end_time, a1.status, a1.opinion ' + 'FROM ?? AS a1 ' + 'WHERE a1.`sid` = ? and a1.`times` = ? ' + 'ORDER BY a1.order';
  1715. const sqlParam = [this.tableName, stageId, times];
  1716. const rst = await this.db.query(sql, sqlParam);
  1717. return rst;
  1718. }
  1719. /**
  1720. * 取待审批期列表(wap用)
  1721. *
  1722. * @param auditorId
  1723. * @return {Promise<*>}
  1724. */
  1725. async getAuditStageByWap(auditorId) {
  1726. const sql =
  1727. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1728. // ' s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
  1729. ' s.*,' +
  1730. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1731. ' ti.`deal_info` ' +
  1732. ' FROM ?? AS sa' +
  1733. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1734. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1735. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1736. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1737. const sqlParam = [
  1738. this.tableName,
  1739. this.ctx.service.stage.tableName,
  1740. this.ctx.service.tender.tableName,
  1741. this.ctx.service.tenderInfo.tableName,
  1742. auditorId,
  1743. auditConst.status.checking,
  1744. ];
  1745. return await this.db.query(sql, sqlParam);
  1746. }
  1747. /**
  1748. * 删除 某期 某次 全审批流程
  1749. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1750. * @param {Number} sid - 标段id
  1751. * @param {Number} times - 第几次审批
  1752. * @param transaction - 删除事务
  1753. * @return {Promise<void>}
  1754. */
  1755. async _timesDelete(sid, times, transaction) {
  1756. // 审批流程
  1757. await transaction.delete(this.tableName, { sid, times });
  1758. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1759. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1760. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1761. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1762. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1763. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1764. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1765. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid, times });
  1766. // 其他台账
  1767. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1768. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1769. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1770. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1771. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1772. }
  1773. /**
  1774. * 删除本次审批流程
  1775. * @param {Number} stageId - 标段id
  1776. * @param {Number} times - 第几次审批
  1777. * @return {Promise<void>}
  1778. */
  1779. async timesDelete() {
  1780. const transaction = await this.db.beginTransaction();
  1781. try {
  1782. // 删除最新一次数据
  1783. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1784. // 审批退回,未重新上报时,需删除最新两次数据
  1785. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1786. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1787. if (isCheckNo) {
  1788. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1789. }
  1790. // 添加上一次审批人
  1791. const sql = 'SELECT `tid`, `sid`, `aid`, `order`, `audit_type`, `audit_order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1792. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1793. const auditors = await this.db.query(sql, sqlParam);
  1794. let order = 1;
  1795. for (const a of auditors) {
  1796. a.times = nowTimes;
  1797. a.order = order;
  1798. a.status = auditConst.status.uncheck;
  1799. order++;
  1800. }
  1801. const ledgerTp = await this._updateTender(transaction);
  1802. // 拷贝新一次审核流程列表
  1803. await transaction.insert(this.tableName, auditors);
  1804. // 计算缓存
  1805. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1806. // 计算并合同支付最终数据
  1807. const lastAudit = await this.getDataByCondition({
  1808. sid: this.ctx.stage.id,
  1809. times: nowTimes - 1,
  1810. status: auditConst.status.checkNo,
  1811. });
  1812. if (!lastAudit) throw '审批数据错误';
  1813. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1814. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1815. const yfPay = stagePay.find(function(x) {
  1816. return x.ptype === payConst.payType.yf;
  1817. });
  1818. const sfPay = stagePay.find(function(x) {
  1819. return x.ptype === payConst.payType.sf;
  1820. });
  1821. // 同步 期信息
  1822. const time = new Date();
  1823. if (this.ctx.stage.isCheckFirst) await this.ctx.service.tenderCache.updateStageCache4DelTimes(transaction, this.ctx.stage, nowTimes);
  1824. await transaction.update(this.ctx.service.stage.tableName, {
  1825. id: this.ctx.stage.id,
  1826. status: auditConst.status.checkNo,
  1827. contract_tp: tpData.contract_tp,
  1828. qc_tp: tpData.qc_tp,
  1829. positive_qc_tp: tpData.positive_qc_tp,
  1830. negative_qc_tp: tpData.negative_qc_tp,
  1831. times: nowTimes,
  1832. yf_tp: yfPay ? yfPay.tp : null,
  1833. sf_tp: sfPay ? sfPay.tp : null,
  1834. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times <= nowTimes; })),
  1835. cache_time_l: time,
  1836. cache_time_r: time,
  1837. });
  1838. await transaction.commit();
  1839. // 通知发送 - 第三方更新
  1840. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1841. const base_data = {
  1842. tid: this.ctx.tender.id,
  1843. sid: this.ctx.stage.id,
  1844. op: 'update',
  1845. };
  1846. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1847. base_data.op = 'update';
  1848. base_data.sid = -1;
  1849. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1850. }
  1851. } catch (err) {
  1852. await transaction.rollback();
  1853. throw err;
  1854. }
  1855. }
  1856. // 固定审批流-更新
  1857. async updateNewAuditList(stage, newList) {
  1858. const transaction = await this.db.beginTransaction();
  1859. try {
  1860. // 先删除旧的审批流,再添加新的
  1861. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1862. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
  1863. const newAuditors = [];
  1864. for (const auditor of newList) {
  1865. newAuditors.push({
  1866. tid: stage.tid, sid: stage.id, aid: auditor.audit_id,
  1867. times: stage.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1868. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1869. });
  1870. }
  1871. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1872. await transaction.commit();
  1873. } catch (err) {
  1874. await transaction.rollback();
  1875. throw err;
  1876. }
  1877. }
  1878. // 固定终审-更新
  1879. async updateLastAudit(stage, auditList, lastId) {
  1880. const transaction = await this.db.beginTransaction();
  1881. try {
  1882. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1883. const existAudit = auditList.find(x => { return x.aid === lastId });
  1884. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1885. if (existAudit) {
  1886. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1887. const sameOrder = auditList.filter(x => { return x.order === existAudit.order });
  1888. if (sameOrder.length === 1) {
  1889. const updateData = [];
  1890. auditList.forEach(x => {
  1891. if (x.order <= existAudit.order) return;
  1892. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1893. });
  1894. if (updateData.length > 0) {
  1895. await transaction.updateRows(updateData);
  1896. }
  1897. order = order - 1;
  1898. }
  1899. }
  1900. // 添加终审
  1901. const newAuditor = {
  1902. tid: stage.tid, sid: stage.id, aid: lastId,
  1903. times: stage.times, order, status: auditConst.status.uncheck,
  1904. audit_type: auditType.key.common, audit_order: order,
  1905. };
  1906. await transaction.insert(this.tableName, newAuditor);
  1907. await transaction.commit();
  1908. } catch (err) {
  1909. await transaction.rollback();
  1910. throw err;
  1911. }
  1912. }
  1913. async getFinalAuditGroup(stageId, times) {
  1914. const sql =
  1915. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`sid`, Max(la.`order`) as max_order ' +
  1916. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1917. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1918. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1919. const result = await this.db.query(sql, sqlParam);
  1920. for (const r of result) {
  1921. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1922. r.status = auditor.status;
  1923. r.opinion = auditor.opinion;
  1924. r.begin_time = auditor.begin_time;
  1925. r.end_time = auditor.end_time;
  1926. }
  1927. return result;
  1928. }
  1929. async getNumByMonth(tid, startMonth, endMonth) {
  1930. // const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tid = ? GROUP BY sid) AND status = ? AND end_time between ? and ?';
  1931. const sql = 'SELECT COUNT(*) as num FROM ?? t1 JOIN (SELECT MAX(id) as max_id FROM ?? WHERE tid = ? GROUP BY sid) t2 ON t1.id = t2.max_id WHERE t1.status = ? AND t1.end_time between ? and ?';
  1932. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1933. const result = await this.db.queryOne(sql, sqlParam);
  1934. return result ? result.num : 0;
  1935. }
  1936. /**
  1937. * 删除本次审批流程
  1938. * @param {Number} stageId - 标段id
  1939. * @param {Number} times - 第几次审批
  1940. * @param {Object} data - 更改参数
  1941. * @return {Promise<void>}
  1942. */
  1943. async saveAudit(stageId, times, data) {
  1944. const transaction = await this.db.beginTransaction();
  1945. try {
  1946. const auditors = await this.getAuditGroupByList(stageId, times);
  1947. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1948. if (data.operate !== 'del') {
  1949. const exist = await this.getDataByCondition({ sid: stageId, times, aid: data.new_aid });
  1950. if (exist) throw '该审核人已存在,请勿重复添加';
  1951. }
  1952. if (data.operate === 'add') {
  1953. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1954. throw '当前人下无法操作新增';
  1955. }
  1956. const newAudit = {
  1957. tid: this.ctx.tender.id,
  1958. sid: stageId,
  1959. aid: data.new_aid,
  1960. order: now_audit.order + 1,
  1961. audit_order: now_audit.audit_order + 1,
  1962. audit_type: auditType.key.common,
  1963. times: times,
  1964. status: 1
  1965. };
  1966. // order+1
  1967. await this._syncOrderByDelete(transaction, stageId, now_audit.order+1, times, '+');
  1968. await transaction.insert(this.tableName, newAudit);
  1969. // 更新审批流程页数据,如果存在
  1970. } else if (data.operate === 'add-sibling') {
  1971. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1972. throw '当前人下无法操作新增';
  1973. }
  1974. const newAudit = {
  1975. tid: this.ctx.tender.id,
  1976. sid: stageId,
  1977. aid: data.new_aid,
  1978. order: now_audit.order,
  1979. audit_order: now_audit.audit_order,
  1980. audit_type: now_audit.audit_type,
  1981. times: times,
  1982. status: 1
  1983. };
  1984. await transaction.insert(this.tableName, newAudit);
  1985. } else if (data.operate === 'del') {
  1986. if (now_audit.status !== auditConst.status.uncheck) {
  1987. throw '当前人无法操作删除';
  1988. }
  1989. const flowAuditors = auditors.filter(x => { return x.order === now_audit.order; });
  1990. await transaction.delete(this.tableName, { sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1991. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, stageId, now_audit.order, times);
  1992. // 旧的更新为is_old为1
  1993. await transaction.update(this.tableName, { is_old: 1 }, {
  1994. where: {
  1995. sid: stageId,
  1996. times,
  1997. aid: data.old_aid,
  1998. }
  1999. });
  2000. } else if (data.operate === 'change') {
  2001. const nowAudit = await this.getDataByCondition({ sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  2002. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  2003. throw '当前人无法操作替换';
  2004. }
  2005. nowAudit.aid = data.new_aid;
  2006. await transaction.update(this.tableName, nowAudit);
  2007. // 旧的更新为is_old为1
  2008. await transaction.update(this.tableName, { is_old: 1 }, {
  2009. where: {
  2010. sid: stageId,
  2011. times,
  2012. aid: data.old_aid,
  2013. }
  2014. });
  2015. }
  2016. if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  2017. const newAuditors = await transaction.select(this.tableName, { where: { sid: stageId, times } });
  2018. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors);
  2019. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  2020. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, uniqNewAuditorGroup);
  2021. } else if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdzs) {
  2022. const newAuditors = await this.getAuditGroupByList(stageId, times, transaction);
  2023. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, this._.map(newAuditors, 'aid'));
  2024. }
  2025. // 更新到审批流程方法
  2026. await transaction.commit();
  2027. } catch (err) {
  2028. await transaction.rollback();
  2029. throw err;
  2030. }
  2031. }
  2032. async getAuditorGroup(stageId, times) {
  2033. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  2034. return this.ctx.helper.groupAuditors(auditors);
  2035. }
  2036. async getUserGroup(stageId, times) {
  2037. const group = await this.getAuditorGroup(stageId, times);
  2038. const sql =
  2039. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  2040. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  2041. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  2042. ' ON s.user_id = pa.id' +
  2043. ' WHERE s.id = ?';
  2044. const sqlParam = [times, stageId, stageId];
  2045. const user = await this.db.queryOne(sql, sqlParam);
  2046. group.unshift([ user ]);
  2047. return group;
  2048. }
  2049. async getUniqUserGroup(stageId, times) {
  2050. const group = await this.getAuditorGroup(stageId, times);
  2051. const sql =
  2052. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  2053. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  2054. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  2055. ' ON s.user_id = pa.id' +
  2056. ' WHERE s.id = ?';
  2057. const sqlParam = [times, stageId, stageId];
  2058. const user = await this.db.queryOne(sql, sqlParam);
  2059. user.audit_order = 0;
  2060. group.unshift([ user ]);
  2061. return this.ctx.helper.groupAuditorsUniq(group);
  2062. }
  2063. async getAuditorHistory(stageId, times, reverse = false) {
  2064. const history = [];
  2065. if (times >= 1) {
  2066. for (let i = 1; i <= times; i++) {
  2067. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, i);
  2068. const group = this.ctx.helper.groupAuditors(auditors);
  2069. const historyGroup = [];
  2070. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  2071. for (const g of group) {
  2072. const his = {
  2073. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  2074. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  2075. auditors: g
  2076. };
  2077. if (his.audit_type === auditType.key.common) {
  2078. his.name = g[0].name;
  2079. } else {
  2080. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  2081. }
  2082. his.is_final = his.audit_order === max_order;
  2083. if (g[0].begin_time) {
  2084. his.begin_time = g[0].begin_time;
  2085. const beginTime = this.ctx.moment(g[0].begin_time);
  2086. his.beginYear = beginTime.format('YYYY');
  2087. his.beginDate = beginTime.format('MM-DD');
  2088. his.beginTime = beginTime.format('HH:mm:ss');
  2089. }
  2090. let end_time;
  2091. g.forEach(x => {
  2092. if (x.status === auditConst.status.checkSkip) return;
  2093. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  2094. if (x.end_time && (!end_time || x.end_time > end_time)) {
  2095. end_time = x.end_time;
  2096. if (his.status !== auditConst.status.checking) his.status = x.status;
  2097. }
  2098. });
  2099. if (end_time) {
  2100. his.end_time = end_time;
  2101. const endTime = this.ctx.moment(end_time);
  2102. his.endYear = endTime.format('YYYY');
  2103. his.endDate = endTime.format('MM-DD');
  2104. his.endTime = endTime.format('HH:mm:ss');
  2105. }
  2106. historyGroup.push(his);
  2107. }
  2108. if (reverse) {
  2109. history.push(historyGroup.reverse());
  2110. } else {
  2111. history.push(historyGroup);
  2112. }
  2113. }
  2114. }
  2115. return history;
  2116. }
  2117. async getUniqAuditor(stageId, times) {
  2118. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  2119. const result = [];
  2120. auditors.forEach(x => {
  2121. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  2122. result.push(x);
  2123. }
  2124. });
  2125. return result;
  2126. }
  2127. }
  2128. return StageAudit;
  2129. };