stage_audit.js 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240
  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.stagePay.cacheOrder(this.ctx.stage, transaction);
  538. // 当前期不是最新一起时
  539. if (this.ctx.stage.highOrder !== this.ctx.stage.order) {
  540. const nextStages = await this.ctx.service.stage.getNextStages(this.ctx.stage.tid, this.ctx.stage.order);
  541. for (const ns of nextStages) {
  542. await this.ctx.service.stagePay.reInitialStageData(this.ctx.stage, ns, transaction);
  543. await this.ctx.service.stageOther.addInitialStageData(ns, this.ctx.stage, transaction);
  544. await this.ctx.service.stageSafeProd.addInitialStageData(ns, this.ctx.stage, transaction);
  545. await this.ctx.service.stageJgcl.addInitialStageData(ns, this.ctx.stage, transaction);
  546. await this.ctx.service.stageTempLand.addInitialStageData(ns, this.ctx.stage, transaction);
  547. }
  548. const preStageTp = {
  549. pre_contract_tp: this.ctx.helper.sum([this.ctx.stage.pre_contract_tp, stageTp.contract_tp, this.ctx.stage.contract_pc_tp]),
  550. pre_qc_tp: this.ctx.helper.sum([this.ctx.stage.pre_qc_tp, stageTp.qc_tp, this.ctx.stage.qc_pc_tp]),
  551. 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]),
  552. 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]),
  553. pre_yf_tp: this.ctx.helper.add(this.ctx.stage.pre_yf_tp, stageTp.yf_tp),
  554. pre_sf_tp: this.ctx.helper.add(this.ctx.stage.pre_sf_tp, stageTp.sf_tp),
  555. };
  556. await transaction.updateRows(this.ctx.service.stage.tableName, nextStages.map(x => { return { id: x.id, ...preStageTp }; }));
  557. await this.ctx.service.tenderCache.updateStageCache4MultiChecked(transaction, this.ctx.stage, flowAudits, ledgerTp, stageTp, nextStages[0], preStageTp);
  558. } else {
  559. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, nextAudits, flowAudits, ledgerTp, stageTp);
  560. }
  561. // 添加短信通知-审批通过提醒功能
  562. const auditList = await this.getAuditors(stageId, stageInfo.times);
  563. 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')));
  564. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  565. qi: stageInfo.order,
  566. status: SmsAliConst.status.success,
  567. });
  568. // 微信模板通知
  569. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  570. const wechatData = {
  571. wap_url: shenpiUrl,
  572. qi: stageInfo.order,
  573. status: wxConst.status.success,
  574. tips: wxConst.tips.success,
  575. code: this.ctx.session.sessionProject.code,
  576. };
  577. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  578. // 期审批完成,检查角色签名数据
  579. await this.ctx.service.roleRptRel.checkSignDate(transaction, this.ctx.stage, selfAudit, time);
  580. // 审批通过 - 检查三方特殊推送
  581. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.checked);
  582. }
  583. // 上报/审批 - 检查三方特殊推送
  584. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  585. } else {
  586. // 同步 期信息
  587. await transaction.update(this.ctx.service.stage.tableName, {
  588. id: stageId,
  589. status: auditConst.status.checking,
  590. ...stageTp,
  591. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  592. cache_time_r: this.ctx.stage.cache_time_l,
  593. });
  594. }
  595. await transaction.commit();
  596. } catch (err) {
  597. await transaction.rollback();
  598. throw err;
  599. }
  600. }
  601. async _checkNo(pid, stageId, checkData, times) {
  602. const accountId = this.ctx.session.sessionUser.accountId;
  603. const time = new Date();
  604. // 整理当前流程审核人状态更新
  605. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  606. if (!audits) throw '审核数据错误';
  607. const selfAudit = audits.find(x => { return x.aid === accountId; });
  608. if (!selfAudit) throw '当前标段您无权审批';
  609. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  610. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  611. const auditors = await this.getUniqAuditor(stageId, times); // 全部参与的审批人
  612. const newAuditors = auditors.map(x => {
  613. return {
  614. aid: x.aid, tid: selfAudit.tid, sid: selfAudit.sid,
  615. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  616. audit_type: x.audit_type, audit_order: x.audit_order,
  617. }
  618. });
  619. const transaction = await this.db.beginTransaction();
  620. try {
  621. const ledgerTp = await this._updateTender(transaction);
  622. // 添加推送
  623. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  624. const defaultNoticeRecord = {
  625. pid,
  626. type: pushType.stage,
  627. status: auditConst.status.checkNo,
  628. content: noticeContent,
  629. };
  630. const records = [
  631. {
  632. uid: this.ctx.stage.user_id,
  633. ...defaultNoticeRecord,
  634. },
  635. ];
  636. auditors.forEach(audit => {
  637. records.push({
  638. uid: audit.aid,
  639. ...defaultNoticeRecord,
  640. });
  641. });
  642. this.ctx.stage.userAssists.forEach(u => {
  643. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  644. });
  645. this.ctx.stage.auditAssists.forEach(u => {
  646. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  647. });
  648. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  649. // 计算并合同支付最终数据
  650. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  651. const stageTp = {
  652. contract_tp: tpData.contract_tp,
  653. qc_tp: tpData.qc_tp,
  654. positive_qc_tp: tpData.positive_qc_tp,
  655. negative_qc_tp: tpData.negative_qc_tp,
  656. yf_tp: yfPay.tp,
  657. sf_tp: sfPay.tp,
  658. };
  659. const orgHisIndex = this.ctx.stage.tp_history.findIndex(x => { return x.times === times && x.order === selfAudit.order; });
  660. if (orgHisIndex) this.ctx.stage.tp_history.splice(orgHisIndex, 1);
  661. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  662. const updateData = [];
  663. audits.forEach(x => {
  664. updateData.push({
  665. id: x.id,
  666. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  667. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  668. end_time: x.aid === selfAudit.aid ? time : null,
  669. });
  670. });
  671. await transaction.updateRows(this.tableName, updateData);
  672. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  673. // 同步 期信息
  674. await transaction.update(this.ctx.service.stage.tableName, {
  675. id: stageId,
  676. status: checkData.checkType,
  677. ...stageTp,
  678. times: times + 1,
  679. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  680. cache_time_r: this.ctx.stage.cache_time_l,
  681. });
  682. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType,
  683. [{ aid: this.ctx.stage.user_id, audit_order: 0, audit_type: auditType.key.common, status: auditConst.status.uncheck }], flowAudits, ledgerTp, stageTp);
  684. // 拷贝新一次审核流程列表
  685. await transaction.insert(this.tableName, newAuditors);
  686. // 计算该审批人最终数据
  687. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  688. // 复制一份最新数据给原报
  689. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  690. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  691. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  692. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  693. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  694. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  695. // 锁定本人数据,保留锁定数据相关确认状态
  696. await this.ctx.service.stageAuditAss.lockConfirm4CheckNo(this.ctx.stage, audits.map(x => { return x.aid; }), auditors, transaction);
  697. // 添加短信通知-审批退回提醒功能
  698. // const mobile_array = [];
  699. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  700. const auditList = await this.getAuditors(stageId, stageInfo.times);
  701. 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')));
  702. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  703. qi: stageInfo.order,
  704. status: SmsAliConst.status.back,
  705. });
  706. // 微信模板通知
  707. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  708. const wechatData = {
  709. wap_url: shenpiUrl,
  710. qi: stageInfo.order,
  711. status: wxConst.status.back,
  712. tips: wxConst.tips.back,
  713. code: this.ctx.session.sessionProject.code,
  714. };
  715. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  716. // 上报/审批 - 检查三方特殊推送
  717. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  718. await transaction.commit();
  719. } catch (err) {
  720. await transaction.rollback();
  721. throw err;
  722. }
  723. }
  724. async _checkNoPre(pid, stageId, checkData, times) {
  725. const accountId = this.ctx.session.sessionUser.accountId;
  726. const time = new Date();
  727. // 整理当前流程审核人状态更新
  728. const audits = await this.getAllDataByCondition({ where: { sid: stageId, times, status: auditConst.status.checking } });
  729. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  730. const selfAudit = audits.find(x => { return x.aid === accountId; });
  731. if (!selfAudit) throw '当前标段您无权审批';
  732. const flowAudits = await this.getAllDataByCondition({ where: { sid: stageId, times: selfAudit.times, order: selfAudit.order }});
  733. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  734. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  735. const auditors2 = await this.getAuditGroupByList(stageId, times);
  736. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  737. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  738. const transaction = await this.db.beginTransaction();
  739. try {
  740. const ledgerTp = await this._updateTender(transaction);
  741. // 添加通知
  742. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, stageId, selfAudit.aid, checkData.opinion);
  743. const defaultNoticeRecord = {
  744. pid,
  745. type: pushType.stage,
  746. status: auditConst.status.checkNoPre,
  747. content: noticeContent,
  748. };
  749. const records = [
  750. {
  751. uid: this.ctx.stage.user_id,
  752. ...defaultNoticeRecord
  753. },
  754. ];
  755. auditors2.forEach(audit => {
  756. records.push({
  757. uid: audit.aid,
  758. ...defaultNoticeRecord
  759. });
  760. });
  761. this.ctx.stage.userAssists.forEach(u => {
  762. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  763. });
  764. this.ctx.stage.auditAssists.forEach(u => {
  765. records.push({ uid: u.ass_user_id, ...defaultNoticeRecord});
  766. });
  767. await transaction.insert('zh_notice', records);
  768. // 计算并合同支付最终数据
  769. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  770. const stageTp = {
  771. contract_tp: tpData.contract_tp,
  772. qc_tp: tpData.qc_tp,
  773. positive_qc_tp: tpData.positive_qc_tp,
  774. negative_qc_tp: tpData.negative_qc_tp,
  775. yf_tp: yfPay.tp,
  776. sf_tp: sfPay.tp,
  777. };
  778. this.ctx.stage.tp_history.push({ times, order: selfAudit.order, ...stageTp });
  779. // 同步 期信息
  780. await transaction.update(this.ctx.service.stage.tableName, {
  781. id: stageId,
  782. times,
  783. ...stageTp,
  784. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  785. cache_time_r: this.ctx.stage.cache_time_l,
  786. });
  787. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, checkData.checkType, preAuditors, flowAudits, ledgerTp, stageTp);
  788. // 更新同一流程所有审批人状态
  789. const updateData = [];
  790. for (const audit of audits) {
  791. if (audit.aid === selfAudit.aid) {
  792. updateData.push({
  793. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time,
  794. });
  795. } else {
  796. updateData.push({
  797. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null,
  798. });
  799. }
  800. }
  801. await transaction.updateRows(this.tableName, updateData);
  802. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  803. // 顺移其后审核人流程顺序
  804. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  805. await transaction.query(sql, [stageId, selfAudit.times, selfAudit.order]);
  806. // 上一审批人,当前审批人 再次添加至流程
  807. const newAuditors = [];
  808. preAuditors.forEach(x => {
  809. newAuditors.push({
  810. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  811. times: x.times, order: selfAudit.order + 1,
  812. status: auditConst.status.checking, begin_time: time,
  813. audit_type: x.audit_type, audit_order: x.audit_order,
  814. });
  815. });
  816. // 获取ids值
  817. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  818. // 获取刚批量添加的所有list
  819. for (let j = 0; j < preAuditors.length; j++) {
  820. newAuditors[j].id = newAuditors_result.insertId + j;
  821. }
  822. const newFlowAuditors = [];
  823. flowAudits.forEach(x => {
  824. newFlowAuditors.push({
  825. tid: this.ctx.stage.tid, sid: this.ctx.stage.id, aid: x.aid,
  826. times: x.times, order: selfAudit.order + 2,
  827. status: auditConst.status.uncheck,
  828. audit_type: x.audit_type, audit_order: x.audit_order,
  829. });
  830. });
  831. await transaction.insert(this.tableName, newFlowAuditors);
  832. // 计算该审批人最终数据
  833. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  834. // 复制一份最新数据给下一人
  835. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, selfAudit.order + 1, transaction);
  836. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  837. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  838. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  839. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  840. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  841. const preAuditorIds = preAuditors.map(x => { return x.aid; });
  842. // 锁定本人数据,保留锁定数据相关确认状态
  843. await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, audits.map(x => { return x.aid; }), preAuditorIds, transaction);
  844. // 同步 期信息
  845. await transaction.update(this.ctx.service.stage.tableName, {
  846. id: stageId,
  847. status: checkData.checkType,
  848. cache_time_r: this.ctx.stage.cache_time_l,
  849. });
  850. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  851. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  852. const users = this._.map(this.ctx.stage.auditAssists.filter(x => {return preAuditorIds.indexOf(x.user_id) >= 0 }), 'ass_user_id');
  853. users.push(...preAuditorIds);
  854. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  855. qi: stageInfo.order,
  856. code: shenpiUrl,
  857. });
  858. // 微信模板通知
  859. const wechatData = {
  860. wap_url: shenpiUrl,
  861. qi: stageInfo.order,
  862. status: wxConst.status.check,
  863. tips: wxConst.tips.check,
  864. code: this.ctx.session.sessionProject.code,
  865. };
  866. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  867. // 重新发送配置
  868. for (const a of newAuditors) {
  869. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  870. pid: this.ctx.session.sessionProject.id,
  871. tid: this.ctx.tender.id,
  872. uid: a.aid,
  873. sp_type: 'stage',
  874. sp_id: a.id,
  875. table_name: this.tableName,
  876. template: wxConst.template.stage,
  877. wx_data: wechatData,
  878. });
  879. }
  880. // 上报/审批 - 检查三方特殊推送
  881. await this.ctx.service.specMsg.addStageMsg(transaction, pid, this.ctx.stage, pushOperate.stage.flow);
  882. await transaction.commit();
  883. } catch (err) {
  884. await transaction.rollback();
  885. throw err;
  886. }
  887. }
  888. /**
  889. * 审批
  890. * @param {Number} stageId - 标段id
  891. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  892. * @param {Number} times - 第几次审批
  893. * @return {Promise<void>}
  894. */
  895. async check(stageId, checkData, times = 1) {
  896. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  897. throw '提交数据错误';
  898. }
  899. // // 整理当前流程审核人状态更新
  900. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  901. // if (!audit) {
  902. // throw '审核数据错误';
  903. // }
  904. // const time = new Date();
  905. const pid = this.ctx.session.sessionProject.id;
  906. switch (checkData.checkType) {
  907. case auditConst.status.checked:
  908. await this._checked(pid, stageId, checkData, times);
  909. break;
  910. case auditConst.status.checkNo:
  911. await this._checkNo(pid, stageId, checkData, times);
  912. break;
  913. case auditConst.status.checkNoPre:
  914. await this._checkNoPre(pid, stageId, checkData, times);
  915. break;
  916. default:
  917. throw '无效审批操作';
  918. }
  919. // 通知发送 - 第三方更新
  920. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  921. const base_data = {
  922. tid: this.ctx.tender.id,
  923. sid: stageId,
  924. op: 'update',
  925. };
  926. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  927. base_data.op = 'update';
  928. base_data.sid = -1;
  929. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  930. }
  931. }
  932. /**
  933. * 审批
  934. * @param {Number} stageId - 标段id
  935. * @param {Number} times - 第几次审批
  936. * @return {Promise<void>}
  937. */
  938. async checkAgain(stageId, times = 1, force) {
  939. const accountId = this.ctx.session.sessionUser.accountId;
  940. const time = new Date();
  941. // 整理当前流程审核人状态更新
  942. const auditors = await this.getAllDataByCondition({
  943. where: { sid: stageId, times },
  944. orders: [['order', 'asc']],
  945. });
  946. const maxOrder = auditors[auditors.length - 1].order;
  947. const audits = auditors.filter(x => { return x.order === maxOrder});
  948. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  949. const selfAudit = audits.find(x => { return x.aid === accountId; });
  950. if (!selfAudit && !force) throw '当前标段您无权审批';
  951. const finalAudit = selfAudit || audits[0];
  952. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  953. const transaction = await this.db.beginTransaction();
  954. try {
  955. // 当前审批人2次添加至流程中
  956. const checkAgainAuditors = [], checkingAuditors = [];
  957. audits.forEach(x => {
  958. checkAgainAuditors.push({
  959. tid: x.tid, sid: x.sid, aid: x.aid,
  960. times: x.times, order: x.order + 1,
  961. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  962. begin_time: time, end_time: time, opinion: '',
  963. audit_type: x.audit_type, audit_order: x.audit_order,
  964. });
  965. });
  966. audits.forEach(x => {
  967. checkingAuditors.push({
  968. tid: x.tid, sid: x.sid, aid: x.aid,
  969. times: x.times, order: x.order + 2,
  970. status: auditConst.status.checking,
  971. begin_time: time, end_time: time, opinion: '',
  972. audit_type: x.audit_type, audit_order: x.audit_order,
  973. });
  974. });
  975. await transaction.insert(this.tableName, checkAgainAuditors);
  976. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  977. // 获取刚批量添加的所有list
  978. for (let j = 0; j < checkingAuditors.length; j++) {
  979. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  980. }
  981. // 复制一份最新数据给下一人
  982. await this.ctx.service.stagePay.clearCacheOrder(this.ctx.stage, transaction);
  983. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, finalAudit.order + 1, transaction);
  984. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, finalAudit.order + 2, transaction);
  985. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  986. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  987. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  988. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  989. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  990. // 同步 期信息
  991. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === finalAudit.order });
  992. const stageTp = {
  993. contract_tp: his.contract_tp,
  994. qc_tp: his.qc_tp,
  995. positive_qc_tp: his.positive_qc_tp,
  996. negative_qc_tp: his.negative_qc_tp,
  997. yf_tp: his.tp,
  998. sf_tp: his.tp,
  999. };
  1000. this.ctx.stage.tp_history.push({ times, order: finalAudit.order + 1, ...stageTp });
  1001. await transaction.update(this.ctx.service.stage.tableName, {
  1002. id: stageId,
  1003. status: auditConst.status.checking,
  1004. cache_time_r: this.ctx.stage.cache_time_l,
  1005. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1006. final_auditor_str: '',
  1007. });
  1008. // 已经引用到本期的单价变更,全部取消
  1009. await this.ctx.service.revisePrice.cancelPriceUsed(this.ctx.stage, transaction);
  1010. // 重算所有单价变更
  1011. const priceCalc = new RevisePrice(this.ctx);
  1012. const pcTp = await priceCalc.stageCheckAgainPriceChange(this.ctx.stage, finalAudit.order + 2, transaction);
  1013. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, checkingAuditors, checkAgainAuditors, null, stageTp, pcTp);
  1014. // 添加短信通知-需要审批提醒功能
  1015. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/stage/' + stageInfo.order);
  1016. const auditorIds = audits.map(x => { return x.aid; });
  1017. const users = this._.map(this.ctx.stage.auditAssists.filter(x => { return auditorIds.indexOf(x.user_id) >= 0; }), 'ass_user_id');
  1018. users.push(...auditorIds);
  1019. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  1020. qi: stageInfo.order,
  1021. code: shenpiUrl,
  1022. });
  1023. // 微信模板通知
  1024. const wechatData = {
  1025. wap_url: shenpiUrl,
  1026. qi: stageInfo.order,
  1027. status: wxConst.status.check,
  1028. tips: wxConst.tips.check,
  1029. code: this.ctx.session.sessionProject.code,
  1030. };
  1031. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  1032. // 重新发送配置
  1033. for (const a of checkingAuditors) {
  1034. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.JL, {
  1035. pid: this.ctx.session.sessionProject.id,
  1036. tid: this.ctx.tender.id,
  1037. uid: a.aid,
  1038. sp_type: 'stage',
  1039. sp_id: a.id,
  1040. table_name: this.tableName,
  1041. template: wxConst.template.stage,
  1042. wx_data: wechatData,
  1043. });
  1044. }
  1045. // 上报/审批 - 检查三方特殊推送
  1046. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1047. await transaction.commit();
  1048. // 通知发送 - 第三方更新
  1049. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1050. const base_data = {
  1051. tid: this.ctx.tender.id,
  1052. sid: stageId,
  1053. op: 'update',
  1054. };
  1055. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1056. base_data.op = 'update';
  1057. base_data.sid = -1;
  1058. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1059. }
  1060. } catch (err) {
  1061. await transaction.rollback();
  1062. throw err;
  1063. }
  1064. }
  1065. /**
  1066. * 原报撤回,直接改动审批人状态
  1067. * 如果存在审批人数据,将其改为原报流程数据,但保留原提交人
  1068. *
  1069. * 一审 1 A checking -> A uncheck status改 pay/jl:删0(jl为增量数据,只删重复部分) 1->0 删1
  1070. * ...
  1071. *
  1072. * @param stage
  1073. * @returns {Promise<void>}
  1074. * @private
  1075. */
  1076. async _userCheckCancel(stage) {
  1077. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1078. const transaction = await this.db.beginTransaction();
  1079. try {
  1080. const ledgerTp = await this._updateTender(transaction);
  1081. // 整理当前流程审核人状态更新
  1082. // 审批人变成待审批状态
  1083. const updateData = stage.curAuditors.map(x => {
  1084. return {
  1085. id: x.id,
  1086. status: auditConst.status.uncheck,
  1087. begin_time: null,
  1088. opinion: null,
  1089. }
  1090. });
  1091. await transaction.updateRows(this.tableName, updateData);
  1092. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  1093. // 计算并合同支付最终数据
  1094. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1095. const stageTp = {
  1096. contract_tp: tpData.contract_tp,
  1097. qc_tp: tpData.qc_tp,
  1098. positive_qc_tp: tpData.positive_qc_tp,
  1099. negative_qc_tp: tpData.negative_qc_tp,
  1100. yf_tp: yfPay.tp,
  1101. sf_tp: sfPay.tp,
  1102. };
  1103. await transaction.update(this.ctx.service.stage.tableName, {
  1104. id: stage.id,
  1105. times: stage.times,
  1106. ...stageTp,
  1107. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < stage.times; })),
  1108. cache_time_r: this.ctx.stage.cache_time_l,
  1109. status: stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1110. });
  1111. // 计量明细:重复数据删除原报,新增数据修改为原报
  1112. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1113. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1114. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1115. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, stage.times, 0, stage.times, 1, transaction);
  1116. // 合同支付明细:保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
  1117. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1118. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, 0, transaction, this.ctx.stage.times, 1);
  1119. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 1, transaction);
  1120. // 其他台账明细:不需更新历史
  1121. // 更新标段计量数据缓存
  1122. const preFlow = stage.times > 1 ? await this.getLastestAuditors(stage.id, stage.times, auditConst.status.checkNo): null;
  1123. const curFlow = [{ aid: this.ctx.stage.user_id, audit_type: auditType.key.common, audit_order: 1, status: auditConst.status.uncheck}];
  1124. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, stage.times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  1125. curFlow, preFlow, ledgerTp, stageTp);
  1126. // 上报/审批 - 检查三方特殊推送
  1127. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1128. await transaction.commit();
  1129. } catch(err) {
  1130. await transaction.rollback();
  1131. throw err;
  1132. }
  1133. }
  1134. /**
  1135. * 审批人撤回审批通过,插入两条数据
  1136. *
  1137. * 一审 1 A checked 一审 1 A checked
  1138. * 二审 2 B checked pre -> 二审 2 B checked
  1139. * 三审 3 C checking cur 二审 3 B checkCancel 增 增extra_his 增tp_his
  1140. * 四审 4 D uncheck 二审 4 B checking 增 增pay_cur
  1141. * 三审 5 C uncheck order、status改
  1142. * 四审 6 D uncheck order改
  1143. *
  1144. * @param stage
  1145. * @returns {Promise<void>}
  1146. * @private
  1147. */
  1148. async _auditCheckCancel(stage) {
  1149. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1150. throw '撤回用户数据错误';
  1151. }
  1152. const accountId = this.ctx.session.sessionUser.accountId;
  1153. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1154. if (!selfAuditor) throw '撤回用户数据错误';
  1155. const time = new Date();
  1156. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1157. const transaction = await this.db.beginTransaction();
  1158. try {
  1159. const ledgerTp = await this._updateTender(transaction);
  1160. // 整理当前流程审核人状态更新
  1161. // 顺移其后审核人流程顺序
  1162. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1163. await transaction.query(sql, [stage.id, selfAuditor.times, stage.curAuditors[0].order]);
  1164. // 当前审批人2次添加至流程中
  1165. const checkCancelAuditors = [], checkingAuditors = [];
  1166. stage.preAuditors.forEach(x => {
  1167. checkCancelAuditors.push({
  1168. tid: stage.tid, sid: stage.id, aid: x.aid,
  1169. times: x.times, order: x.order + 1,
  1170. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1171. begin_time: time, end_time: time, opinion: '',
  1172. audit_type: x.audit_type, audit_order: x.audit_order,
  1173. });
  1174. });
  1175. stage.preAuditors.forEach(x => {
  1176. checkingAuditors.push({
  1177. tid: stage.tid, sid: stage.id, aid: x.aid,
  1178. times: x.times, order: x.order + 2,
  1179. status: auditConst.status.checking,
  1180. begin_time: time, end_time: time, opinion: '',
  1181. audit_type: x.audit_type, audit_order: x.audit_order,
  1182. });
  1183. });
  1184. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1185. // 当前审批人变成待审批
  1186. await transaction.updateRows(this.tableName, stage.curAuditors.map(x => { return {
  1187. id: x.id, begin_time: null, status: auditConst.status.uncheck, order: x.order + 2
  1188. }}));
  1189. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(stage.curAuditors, 'id'));
  1190. // 计算并合同支付最终数据
  1191. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1192. const stageTp = {
  1193. contract_tp: tpData.contract_tp,
  1194. qc_tp: tpData.qc_tp,
  1195. positive_qc_tp: tpData.positive_qc_tp,
  1196. negative_qc_tp: tpData.negative_qc_tp,
  1197. yf_tp: yfPay.tp,
  1198. sf_tp: sfPay.tp,
  1199. };
  1200. this.ctx.stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1201. // 同步 期信息
  1202. await transaction.update(this.ctx.service.stage.tableName, {
  1203. id: stage.id,
  1204. times: stage.times,
  1205. ...stageTp,
  1206. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1207. cache_time_r: this.ctx.stage.cache_time_l,
  1208. });
  1209. // 计量明细:勿需修改任何数据
  1210. // 合同支付明细:复制一份最新数据给下一人
  1211. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1212. // 其他台账明细:更新两份历史数据
  1213. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1214. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1215. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1216. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1217. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1218. // 更新标段计量数据缓存
  1219. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1220. // 上报/审批 - 检查三方特殊推送
  1221. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1222. await transaction.commit();
  1223. } catch(err) {
  1224. await transaction.rollback();
  1225. throw err;
  1226. }
  1227. }
  1228. /**
  1229. * 审批人撤回审批退回上一人,插入两条数据
  1230. *
  1231. * 一审 1 A checked 一审 1 A checked
  1232. * 二审 2 B checked 二审 2 B checked
  1233. * 三审 3 C checkNoPre pre -> 三审 3 C checkNoPre
  1234. * 二审 4 B checking cur 三审 4 C checkCancel 删4B 增4C 增extra_his 增tp_his
  1235. * 三审 5 C uncheck 三审 5 C checking status改 增pay_cur
  1236. * 四审 6 D uncheck 四审 6 D uncheck
  1237. *
  1238. * @param stage
  1239. * @returns {Promise<void>}
  1240. * @private
  1241. */
  1242. async _auditCheckCancelNoPre(stage) {
  1243. if (stage.curAuditors.length === 0 || stage.curAuditors[0].order <= 1) {
  1244. throw '撤回用户数据错误';
  1245. }
  1246. const accountId = this.ctx.session.sessionUser.accountId;
  1247. const selfAuditor = this.ctx.stage.preAuditors.find(x => { return x.aid === accountId; });
  1248. if (!selfAuditor) throw '撤回用户数据错误';
  1249. const time = new Date();
  1250. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1251. const transaction = await this.db.beginTransaction();
  1252. try {
  1253. const ledgerTp = await this._updateTender(transaction);
  1254. // 整理当前流程审核人状态更新
  1255. // 删除当前审批人
  1256. await transaction.delete(this.tableName, { id: stage.curAuditors.map(x => { return x.id; }) });
  1257. await this.ctx.service.noticeAgain.deleteNoticeAgain(transaction, this.tableName, this._.map(stage.curAuditors, 'id'));
  1258. // 添加撤回人到审批流程中
  1259. const newAuditors = [];
  1260. stage.preAuditors.forEach(x => {
  1261. newAuditors.push({
  1262. tid: stage.tid, sid: stage.id, aid: x.aid,
  1263. times: x.times, order: x.order + 1,
  1264. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1265. begin_time: time, end_time: time, opinion: '',
  1266. audit_type: x.audit_type, audit_order: x.audit_order,
  1267. });
  1268. });
  1269. await transaction.insert(this.tableName, newAuditors);
  1270. // 更新上一个人,最新审批状态为审批中
  1271. await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
  1272. where: { sid: stage.id, times: selfAuditor.times, order: selfAuditor.order + 2 }
  1273. });
  1274. // 计算并合同支付最终数据
  1275. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1276. const stageTp = {
  1277. contract_tp: tpData.contract_tp,
  1278. qc_tp: tpData.qc_tp,
  1279. positive_qc_tp: tpData.positive_qc_tp,
  1280. negative_qc_tp: tpData.negative_qc_tp,
  1281. yf_tp: yfPay.tp,
  1282. sf_tp: sfPay.tp,
  1283. };
  1284. stage.tp_history.push({ times: stage.times, order: selfAuditor.order + 1, ...stageTp });
  1285. // 同步 期信息
  1286. await transaction.update(this.ctx.service.stage.tableName, {
  1287. id: stage.id,
  1288. times: stage.times,
  1289. ...stageTp,
  1290. status: auditConst.status.checking,
  1291. tp_history: JSON.stringify(stage.tp_history),
  1292. cache_time_r: stage.cache_time_l,
  1293. });
  1294. // 计量明细:勿需修改任何数据
  1295. // 合同支付明细:复制一份最新数据给下一人
  1296. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times, selfAuditor.order + 2, transaction, stage.times, selfAuditor.order + 1);
  1297. // 其他台账明细:更新一份历史数据
  1298. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1299. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1300. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1301. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1302. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1303. // 更新标段计量数据缓存
  1304. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, stage.status, newAuditors, stage.preAuditors, ledgerTp, stageTp);
  1305. // 上报/审批 - 检查三方特殊推送
  1306. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1307. await transaction.commit();
  1308. } catch(err) {
  1309. await transaction.rollback();
  1310. throw err;
  1311. }
  1312. }
  1313. /**
  1314. * 审批人撤回审批退回原报
  1315. *
  1316. * 1# 一审 1 A checked 1# 一审 1 A checked
  1317. * 二审 2 B checkNo pre -> 二审 2 B checkNo
  1318. * 三审 3 C uncheck 二审 3 B checkCancel 增 pay: 2#0 -> 1#3 jl: 2#0 -> 1#3 增tp_his 增extra_his
  1319. * 二审 4 B checking 增 pay: 2#0 -> 1#4
  1320. * 三审 5 C uncheck order改
  1321. *
  1322. * 2# 一审 1 A uncheck 2# 删 pay: 2#0删 jl: 2#0删
  1323. * 二审 2 B uncheck
  1324. * 三审 3 C uncheck
  1325. *
  1326. * @param stage
  1327. * @returns {Promise<void>}
  1328. * @private
  1329. */
  1330. async _auditCheckCancelNo(stage) {
  1331. const accountId = this.ctx.session.sessionUser.accountId;
  1332. const selfAuditor = stage.preAuditors.find(x => { return x.aid === accountId && x.status === auditConst.status.checkNo; });
  1333. if (!selfAuditor) throw '该标段由他人审批退回,您不可撤回';
  1334. const time = new Date();
  1335. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  1336. const transaction = await this.db.beginTransaction();
  1337. try {
  1338. const ledgerTp = await this._updateTender(transaction);
  1339. // 整理上一个流程审核人状态更新
  1340. // 顺移其后审核人流程顺序
  1341. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE sid = ? AND times = ? AND `order` > ?';
  1342. await transaction.query(sql, [stage.id, selfAuditor.times, selfAuditor.order]);
  1343. // 当前审批人2次添加至流程中
  1344. const checkCancelAuditors = [], checkingAuditors = [];
  1345. stage.preAuditors.forEach(x => {
  1346. checkCancelAuditors.push({
  1347. tid: stage.tid, sid: stage.id, aid: x.aid,
  1348. times: x.times, order: x.order + 1,
  1349. status: x.aid === selfAuditor.aid ? auditConst.status.checkCancel : auditConst.status.checkSkip,
  1350. begin_time: time, end_time: time, opinion: '',
  1351. audit_type: x.audit_type, audit_order: x.audit_order,
  1352. });
  1353. });
  1354. stage.preAuditors.forEach(x => {
  1355. checkingAuditors.push({
  1356. tid: stage.tid, sid: stage.id, aid: x.aid,
  1357. times: x.times, order: x.order + 2,
  1358. status: auditConst.status.checking,
  1359. begin_time: time, end_time: time, opinion: '',
  1360. audit_type: x.audit_type, audit_order: x.audit_order,
  1361. });
  1362. });
  1363. await transaction.insert(this.tableName, [...checkCancelAuditors, ...checkingAuditors]);
  1364. // 删除当前次审批流
  1365. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1366. // 计算并合同支付最终数据
  1367. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(stage, transaction);
  1368. const stageTp = {
  1369. contract_tp: tpData.contract_tp,
  1370. qc_tp: tpData.qc_tp,
  1371. positive_qc_tp: tpData.positive_qc_tp,
  1372. negative_qc_tp: tpData.negative_qc_tp,
  1373. yf_tp: yfPay.tp,
  1374. sf_tp: sfPay.tp,
  1375. };
  1376. stage.tp_history.push({ times: selfAuditor.times, order: selfAuditor.order + 1, ...stageTp });
  1377. await transaction.update(this.ctx.service.stage.tableName, {
  1378. id: stage.id,
  1379. times: stage.times - 1,
  1380. ...stageTp,
  1381. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1382. cache_time_r: stage.cache_time_l,
  1383. status: auditConst.status.checking,
  1384. });
  1385. // 计量明细:重复数据删除原报,新增数据修改为原报
  1386. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1387. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1388. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1389. await this.ctx.service.stageChange.updateStageChange4CheckCancel(stage.id, selfAuditor.times, selfAuditor.order + 1, stage.times, 0, transaction);
  1390. // 合同支付明细:复制一份最新数据新增的2个审批流,删除最新一次审批数据
  1391. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 1, transaction, stage.times, 0);
  1392. await this.ctx.service.stagePay.copyAuditStagePays(stage, stage.times - 1, selfAuditor.order + 2, transaction, stage.times, 0);
  1393. await this.ctx.service.stagePay.deleteAuditStagePays(stage, stage.times, 0, transaction);
  1394. // 其他台账明细:更新一份历史数据
  1395. await this.ctx.service.stageJgcl.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1396. await this.ctx.service.stageBonus.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1397. await this.ctx.service.stageOther.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1398. await this.ctx.service.stageSafeProd.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1399. await this.ctx.service.stageTempLand.updateHistory4TimesOrder(stage, stage.times, [selfAuditor.order + 1], transaction);
  1400. // 更新标段计量数据缓存
  1401. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, stage, auditConst.status.checking, checkingAuditors, checkCancelAuditors, ledgerTp, stageTp);
  1402. // 上报/审批 - 检查三方特殊推送
  1403. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, stage, pushOperate.stage.flow);
  1404. await transaction.commit();
  1405. } catch(err) {
  1406. await transaction.rollback();
  1407. throw err;
  1408. }
  1409. }
  1410. /**
  1411. * 会签未全部审批通过时,撤回仅修改本人状态
  1412. *
  1413. * @param stage
  1414. * @returns {Promise<void>}
  1415. * @private
  1416. */
  1417. async _auditCheckCancelAnd(stage) {
  1418. const accountId = this.ctx.session.sessionUser.accountId;
  1419. const selfAuditor = stage.flowAuditors.find(x => { return x.aid === accountId; });
  1420. if (!selfAuditor || selfAuditor.status !== auditConst.status.checked) throw '不可撤回';
  1421. const transaction = await this.db.beginTransaction();
  1422. try {
  1423. await transaction.update(this.tableName, {
  1424. id: selfAuditor.id, status: auditConst.status.checking, opinion: '', end_time: null,
  1425. });
  1426. await transaction.commit();
  1427. } catch(err) {
  1428. await transaction.rollback();
  1429. throw err;
  1430. }
  1431. }
  1432. /**
  1433. * 审批撤回
  1434. * @param {Number} stageId - 标段id
  1435. * @param {Number} times - 第几次审批
  1436. * @return {Promise<void>}
  1437. */
  1438. async checkCancel(stage) {
  1439. // 分5种情况,根据ctx.cancancel值判断:
  1440. // 1.原报发起撤回,当前流程删除,并回到待上报
  1441. // 2.审批人撤回审批通过,增加流程,并回到它审批中
  1442. // 3.审批人撤回审批退回上一人,并删除退回人,增加流程,并回到它审批中,并更新计量期状态为审批中
  1443. // 4.审批人撤回退回原报操作,删除新增的审批流,增加流程,回滚到它审批中
  1444. // 5.会签审批人撤回审批通过(还有其他审批人未审批通过),仅修改本人流程状态
  1445. if (stage.cancancel === 5) {
  1446. await this._auditCheckCancelAnd(stage);
  1447. } else {
  1448. switch (this.ctx.stage.cancancel) {
  1449. case 1: await this._userCheckCancel(stage); break;
  1450. case 2: await this._auditCheckCancel(stage); break;
  1451. case 3: await this._auditCheckCancelNoPre(stage); break;
  1452. case 4: await this._auditCheckCancelNo(stage); break;
  1453. default: throw '不可撤回,请刷新页面重试';
  1454. }
  1455. // 通知发送 - 第三方更新
  1456. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1457. const base_data = {
  1458. tid: stage.tid,
  1459. sid: stage.id,
  1460. op: 'update',
  1461. };
  1462. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1463. base_data.op = 'update';
  1464. base_data.sid = -1;
  1465. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1466. }
  1467. }
  1468. }
  1469. /**
  1470. * 获取审核人需要审核的期列表
  1471. *
  1472. * @param auditorId
  1473. * @return {Promise<*>}
  1474. */
  1475. async getAuditStage(auditorId) {
  1476. const sql =
  1477. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1478. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  1479. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  1480. ' FROM ?? AS sa ' +
  1481. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  1482. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  1483. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  1484. ' ORDER BY sa.`begin_time` DESC';
  1485. const sqlParam = [
  1486. this.tableName,
  1487. this.ctx.service.stage.tableName,
  1488. this.ctx.service.tender.tableName,
  1489. auditorId,
  1490. auditConst.status.checking,
  1491. auditorId,
  1492. auditConst.status.checkNo,
  1493. auditConst.status.checkNo,
  1494. ];
  1495. const result = await this.db.query(sql, sqlParam);
  1496. // 过滤result中存在重复sid的值, 保留最新的一条
  1497. const filterResult = [];
  1498. const sidArr = [];
  1499. for (const r of result) {
  1500. if (sidArr.indexOf(r.sid) === -1) {
  1501. filterResult.push(r);
  1502. sidArr.push(r.sid);
  1503. }
  1504. }
  1505. return filterResult;
  1506. }
  1507. /**
  1508. * 获取审核人审核的次数
  1509. *
  1510. * @param auditorId
  1511. * @return {Promise<*>}
  1512. */
  1513. async getCountByChecked(auditorId) {
  1514. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  1515. }
  1516. /**
  1517. * 获取最近一次审批结束时间
  1518. *
  1519. * @param auditorId
  1520. * @return {Promise<*>}
  1521. */
  1522. async getLastEndTimeByChecked(auditorId) {
  1523. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1524. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1525. const sqlParam = [this.tableName, auditorId];
  1526. const result = await this.db.queryOne(sql, sqlParam);
  1527. return result ? result.end_time : null;
  1528. }
  1529. /**
  1530. * 获取 某时间后 审批进度 更新的期
  1531. * @param {Number} pid - 查询标段
  1532. * @param {Number} uid - 查询人
  1533. * @param {Date} time - 查询时间
  1534. * @return {Promise<*>}
  1535. */
  1536. async getNoticeStage(pid, uid, time) {
  1537. let notice = await this.db.select('zh_notice', {
  1538. where: { pid, type: pushType.stage, uid },
  1539. orders: [['create_time', 'desc']],
  1540. limit: 10,
  1541. offset: 0,
  1542. });
  1543. notice = notice.map(v => {
  1544. const extra = JSON.parse(v.content);
  1545. delete v.content;
  1546. return { ...v, ...extra };
  1547. });
  1548. return notice;
  1549. }
  1550. /**
  1551. * 用于添加推送所需的content内容
  1552. * @param {Number} pid 项目id
  1553. * @param {Number} tid 台账id
  1554. * @param {Number} sid 期id
  1555. * @param {Number} uid 审核人id
  1556. */
  1557. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1558. const noticeSql =
  1559. 'SELECT * FROM (SELECT ' +
  1560. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1561. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1562. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1563. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1564. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1565. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1566. const content = await this.db.query(noticeSql, noticeSqlParam);
  1567. if (content.length) {
  1568. content[0].opinion = opinion;
  1569. }
  1570. return content.length ? JSON.stringify(content[0]) : '';
  1571. }
  1572. /**
  1573. * 获取审核人流程列表
  1574. *
  1575. * @param auditorId
  1576. * @return {Promise<*>}
  1577. */
  1578. async getAuditGroupByList(stageId, times, transaction = false) {
  1579. // const sql =
  1580. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
  1581. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1582. // ' WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
  1583. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1584. const sql =
  1585. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.`status`, la.audit_type, la.audit_order' +
  1586. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `sid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  1587. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  1588. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  1589. const sqlParam = [this.tableName, stageId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, stageId, times, 0];
  1590. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  1591. }
  1592. /**
  1593. * 获取审核人流程列表
  1594. *
  1595. * @param auditorId
  1596. * @return {Promise<*>}
  1597. */
  1598. async getAuditGroupByListWithOwner(stageId, times) {
  1599. const result = await this.getAuditGroupByList(stageId, times);
  1600. const sql =
  1601. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1602. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1603. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1604. ' ON s.user_id = pa.id' +
  1605. ' WHERE s.id = ?';
  1606. const sqlParam = [times, stageId, stageId];
  1607. const user = await this.db.queryOne(sql, sqlParam);
  1608. result.unshift(user);
  1609. return result;
  1610. }
  1611. /**
  1612. * 复制上一期的审批人列表给最新一期
  1613. *
  1614. * @param transaction - 新增一期的事务
  1615. * @param {Object} preStage - 上一期
  1616. * @param {Object} newStage - 最新一期
  1617. * @return {Promise<*>}
  1618. */
  1619. async copyPreStageAuditors(transaction, preStage, newStage) {
  1620. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1621. const newAuditors = [];
  1622. for (const a of auditors) {
  1623. const na = {
  1624. tid: preStage.tid,
  1625. sid: newStage.id,
  1626. aid: a.aid,
  1627. times: newStage.times,
  1628. order: a.audit_order,
  1629. status: auditConst.status.uncheck,
  1630. audit_type: a.audit_type,
  1631. audit_order: a.audit_order,
  1632. };
  1633. newAuditors.push(na);
  1634. }
  1635. if (newAuditors.length > 0) {
  1636. const result = await transaction.insert(this.tableName, newAuditors);
  1637. return (result.effectRows = auditors.length);
  1638. } else {
  1639. return true;
  1640. }
  1641. }
  1642. /**
  1643. * 移除审核人
  1644. *
  1645. * @param {Number} stageId - 期id
  1646. * @param {Number} status - 期状态
  1647. * @param {Number} status - 期次数
  1648. * @return {Promise<boolean>}
  1649. */
  1650. async getAuditorByStatus(stageId, status, times = 1) {
  1651. let auditor = null;
  1652. let sql = '';
  1653. let sqlParam = '';
  1654. switch (status) {
  1655. case auditConst.status.checking:
  1656. case auditConst.status.checked:
  1657. case auditConst.status.checkNoPre:
  1658. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1659. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1660. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1661. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1662. auditor = await this.db.queryOne(sql, sqlParam);
  1663. break;
  1664. case auditConst.status.checkNo:
  1665. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1666. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1667. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1668. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1669. auditor = await this.db.queryOne(sql, sqlParam);
  1670. break;
  1671. case auditConst.status.uncheck:
  1672. default:
  1673. break;
  1674. }
  1675. return auditor;
  1676. }
  1677. async getAuditorsByStatus(stageId, status, times = 1) {
  1678. let auditor = [];
  1679. let sql = '';
  1680. let sqlParam = '';
  1681. let cur;
  1682. switch (status) {
  1683. case auditConst.status.checking:
  1684. case auditConst.status.checked:
  1685. case auditConst.status.checkNoPre:
  1686. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where sid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [stageId, times, status]);
  1687. if (!cur) return [];
  1688. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1689. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1690. ' WHERE la.`sid` = ? and la.`order` = ? and times = ?';
  1691. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, times];
  1692. auditor = await this.db.query(sql, sqlParam);
  1693. break;
  1694. case auditConst.status.checkNo:
  1695. 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]);
  1696. if (!cur) return [];
  1697. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.audit_order, la.audit_type ' +
  1698. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1699. ' WHERE la.`sid` = ? and la.`order` = ? and la.`times` = ?';
  1700. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, cur.order, parseInt(times) - 1];
  1701. auditor = await this.db.query(sql, sqlParam);
  1702. break;
  1703. case auditConst.status.uncheck:
  1704. default:
  1705. break;
  1706. }
  1707. return auditor;
  1708. }
  1709. /**
  1710. * 取某一期已批准审核信息(报表用)
  1711. *
  1712. * @param {Number} stageId - 期id
  1713. * @param {Number} times - 期次数
  1714. * @return {Promise<boolean>}
  1715. */
  1716. async getStageAudit(stageId, times = 1) {
  1717. 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';
  1718. const sqlParam = [this.tableName, stageId, times];
  1719. const rst = await this.db.query(sql, sqlParam);
  1720. return rst;
  1721. }
  1722. /**
  1723. * 取待审批期列表(wap用)
  1724. *
  1725. * @param auditorId
  1726. * @return {Promise<*>}
  1727. */
  1728. async getAuditStageByWap(auditorId) {
  1729. const sql =
  1730. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1731. // ' 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`, ' +
  1732. ' s.*,' +
  1733. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1734. ' ti.`deal_info` ' +
  1735. ' FROM ?? AS sa' +
  1736. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1737. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1738. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1739. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1740. const sqlParam = [
  1741. this.tableName,
  1742. this.ctx.service.stage.tableName,
  1743. this.ctx.service.tender.tableName,
  1744. this.ctx.service.tenderInfo.tableName,
  1745. auditorId,
  1746. auditConst.status.checking,
  1747. ];
  1748. return await this.db.query(sql, sqlParam);
  1749. }
  1750. /**
  1751. * 删除 某期 某次 全审批流程
  1752. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1753. * @param {Number} sid - 标段id
  1754. * @param {Number} times - 第几次审批
  1755. * @param transaction - 删除事务
  1756. * @return {Promise<void>}
  1757. */
  1758. async _timesDelete(sid, times, transaction) {
  1759. // 审批流程
  1760. await transaction.delete(this.tableName, { sid, times });
  1761. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1762. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1763. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1764. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1765. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1766. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1767. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1768. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid, times });
  1769. // 其他台账
  1770. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1771. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1772. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1773. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1774. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1775. }
  1776. /**
  1777. * 删除本次审批流程
  1778. * @param {Number} stageId - 标段id
  1779. * @param {Number} times - 第几次审批
  1780. * @return {Promise<void>}
  1781. */
  1782. async timesDelete() {
  1783. const transaction = await this.db.beginTransaction();
  1784. try {
  1785. // 删除最新一次数据
  1786. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1787. // 审批退回,未重新上报时,需删除最新两次数据
  1788. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1789. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1790. if (isCheckNo) {
  1791. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1792. }
  1793. // 添加上一次审批人
  1794. const sql = 'SELECT `tid`, `sid`, `aid`, `order`, `audit_type`, `audit_order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1795. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1796. const auditors = await this.db.query(sql, sqlParam);
  1797. let order = 1;
  1798. for (const a of auditors) {
  1799. a.times = nowTimes;
  1800. a.order = order;
  1801. a.status = auditConst.status.uncheck;
  1802. order++;
  1803. }
  1804. const ledgerTp = await this._updateTender(transaction);
  1805. // 拷贝新一次审核流程列表
  1806. await transaction.insert(this.tableName, auditors);
  1807. // 计算缓存
  1808. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1809. // 计算并合同支付最终数据
  1810. const lastAudit = await this.getDataByCondition({
  1811. sid: this.ctx.stage.id,
  1812. times: nowTimes - 1,
  1813. status: auditConst.status.checkNo,
  1814. });
  1815. if (!lastAudit) throw '审批数据错误';
  1816. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1817. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1818. const yfPay = stagePay.find(function(x) {
  1819. return x.ptype === payConst.payType.yf;
  1820. });
  1821. const sfPay = stagePay.find(function(x) {
  1822. return x.ptype === payConst.payType.sf;
  1823. });
  1824. // 同步 期信息
  1825. const time = new Date();
  1826. if (this.ctx.stage.isCheckFirst) await this.ctx.service.tenderCache.updateStageCache4DelTimes(transaction, this.ctx.stage, nowTimes);
  1827. await transaction.update(this.ctx.service.stage.tableName, {
  1828. id: this.ctx.stage.id,
  1829. status: auditConst.status.checkNo,
  1830. contract_tp: tpData.contract_tp,
  1831. qc_tp: tpData.qc_tp,
  1832. positive_qc_tp: tpData.positive_qc_tp,
  1833. negative_qc_tp: tpData.negative_qc_tp,
  1834. times: nowTimes,
  1835. yf_tp: yfPay ? yfPay.tp : null,
  1836. sf_tp: sfPay ? sfPay.tp : null,
  1837. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times <= nowTimes; })),
  1838. cache_time_l: time,
  1839. cache_time_r: time,
  1840. });
  1841. await transaction.commit();
  1842. // 通知发送 - 第三方更新
  1843. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1844. const base_data = {
  1845. tid: this.ctx.tender.id,
  1846. sid: this.ctx.stage.id,
  1847. op: 'update',
  1848. };
  1849. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1850. base_data.op = 'update';
  1851. base_data.sid = -1;
  1852. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1853. }
  1854. } catch (err) {
  1855. await transaction.rollback();
  1856. throw err;
  1857. }
  1858. }
  1859. // 固定审批流-更新
  1860. async updateNewAuditList(stage, newList) {
  1861. const transaction = await this.db.beginTransaction();
  1862. try {
  1863. // 先删除旧的审批流,再添加新的
  1864. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1865. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
  1866. const newAuditors = [];
  1867. for (const auditor of newList) {
  1868. newAuditors.push({
  1869. tid: stage.tid, sid: stage.id, aid: auditor.audit_id,
  1870. times: stage.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1871. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1872. });
  1873. }
  1874. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1875. await transaction.commit();
  1876. } catch (err) {
  1877. await transaction.rollback();
  1878. throw err;
  1879. }
  1880. }
  1881. // 固定终审-更新
  1882. async updateLastAudit(stage, auditList, lastId) {
  1883. const transaction = await this.db.beginTransaction();
  1884. try {
  1885. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1886. const existAudit = auditList.find(x => { return x.aid === lastId });
  1887. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1888. if (existAudit) {
  1889. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1890. const sameOrder = auditList.filter(x => { return x.order === existAudit.order });
  1891. if (sameOrder.length === 1) {
  1892. const updateData = [];
  1893. auditList.forEach(x => {
  1894. if (x.order <= existAudit.order) return;
  1895. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1896. });
  1897. if (updateData.length > 0) {
  1898. await transaction.updateRows(updateData);
  1899. }
  1900. order = order - 1;
  1901. }
  1902. }
  1903. // 添加终审
  1904. const newAuditor = {
  1905. tid: stage.tid, sid: stage.id, aid: lastId,
  1906. times: stage.times, order, status: auditConst.status.uncheck,
  1907. audit_type: auditType.key.common, audit_order: order,
  1908. };
  1909. await transaction.insert(this.tableName, newAuditor);
  1910. await transaction.commit();
  1911. } catch (err) {
  1912. await transaction.rollback();
  1913. throw err;
  1914. }
  1915. }
  1916. async getFinalAuditGroup(stageId, times) {
  1917. const sql =
  1918. '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 ' +
  1919. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1920. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1921. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1922. const result = await this.db.query(sql, sqlParam);
  1923. for (const r of result) {
  1924. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1925. r.status = auditor.status;
  1926. r.opinion = auditor.opinion;
  1927. r.begin_time = auditor.begin_time;
  1928. r.end_time = auditor.end_time;
  1929. }
  1930. return result;
  1931. }
  1932. async getNumByMonth(tid, startMonth, endMonth) {
  1933. // 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 ?';
  1934. 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 ?';
  1935. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1936. const result = await this.db.queryOne(sql, sqlParam);
  1937. return result ? result.num : 0;
  1938. }
  1939. /**
  1940. * 删除本次审批流程
  1941. * @param {Number} stageId - 标段id
  1942. * @param {Number} times - 第几次审批
  1943. * @param {Object} data - 更改参数
  1944. * @return {Promise<void>}
  1945. */
  1946. async saveAudit(stageId, times, sp_group, data) {
  1947. const transaction = await this.db.beginTransaction();
  1948. try {
  1949. const auditors = await this.getAuditGroupByList(stageId, times);
  1950. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1951. if (data.operate !== 'del') {
  1952. const exist = await this.getDataByCondition({ sid: stageId, times, aid: data.new_aid });
  1953. if (exist) throw '该审核人已存在,请勿重复添加';
  1954. }
  1955. if (data.operate === 'add') {
  1956. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1957. throw '当前人下无法操作新增';
  1958. }
  1959. const newAudit = {
  1960. tid: this.ctx.tender.id,
  1961. sid: stageId,
  1962. aid: data.new_aid,
  1963. order: now_audit.order + 1,
  1964. audit_order: now_audit.audit_order + 1,
  1965. audit_type: auditType.key.common,
  1966. times: times,
  1967. status: 1
  1968. };
  1969. // order+1
  1970. await this._syncOrderByDelete(transaction, stageId, now_audit.order+1, times, '+');
  1971. await transaction.insert(this.tableName, newAudit);
  1972. // 更新审批流程页数据,如果存在
  1973. } else if (data.operate === 'add-sibling') {
  1974. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1975. throw '当前人下无法操作新增';
  1976. }
  1977. const newAudit = {
  1978. tid: this.ctx.tender.id,
  1979. sid: stageId,
  1980. aid: data.new_aid,
  1981. order: now_audit.order,
  1982. audit_order: now_audit.audit_order,
  1983. audit_type: now_audit.audit_type,
  1984. times: times,
  1985. status: 1
  1986. };
  1987. await transaction.insert(this.tableName, newAudit);
  1988. } else if (data.operate === 'del') {
  1989. if (now_audit.status !== auditConst.status.uncheck) {
  1990. throw '当前人无法操作删除';
  1991. }
  1992. const flowAuditors = auditors.filter(x => { return x.order === now_audit.order; });
  1993. await transaction.delete(this.tableName, { sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1994. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, stageId, now_audit.order, times);
  1995. // 旧的更新为is_old为1
  1996. await transaction.update(this.tableName, { is_old: 1 }, {
  1997. where: {
  1998. sid: stageId,
  1999. times,
  2000. aid: data.old_aid,
  2001. }
  2002. });
  2003. } else if (data.operate === 'change') {
  2004. const nowAudit = await this.getDataByCondition({ sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  2005. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  2006. throw '当前人无法操作替换';
  2007. }
  2008. nowAudit.aid = data.new_aid;
  2009. await transaction.update(this.tableName, nowAudit);
  2010. // 旧的更新为is_old为1
  2011. await transaction.update(this.tableName, { is_old: 1 }, {
  2012. where: {
  2013. sid: stageId,
  2014. times,
  2015. aid: data.old_aid,
  2016. }
  2017. });
  2018. }
  2019. if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl) {
  2020. const newAuditors = await transaction.select(this.tableName, { where: { sid: stageId, times } });
  2021. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors);
  2022. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  2023. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.stage, shenpiConst.sp_type.stage, uniqNewAuditorGroup, sp_group);
  2024. } else if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdzs) {
  2025. const newAuditors = await this.getAuditGroupByList(stageId, times, transaction);
  2026. 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'));
  2027. }
  2028. // 更新到审批流程方法
  2029. await transaction.commit();
  2030. } catch (err) {
  2031. await transaction.rollback();
  2032. throw err;
  2033. }
  2034. }
  2035. async getAuditorGroup(stageId, times) {
  2036. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  2037. return this.ctx.helper.groupAuditors(auditors);
  2038. }
  2039. async getUserGroup(stageId, times) {
  2040. const group = await this.getAuditorGroup(stageId, times);
  2041. const sql =
  2042. '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' +
  2043. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  2044. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  2045. ' ON s.user_id = pa.id' +
  2046. ' WHERE s.id = ?';
  2047. const sqlParam = [times, stageId, stageId];
  2048. const user = await this.db.queryOne(sql, sqlParam);
  2049. group.unshift([ user ]);
  2050. return group;
  2051. }
  2052. async getUniqUserGroup(stageId, times) {
  2053. const group = await this.getAuditorGroup(stageId, times);
  2054. const sql =
  2055. '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' +
  2056. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  2057. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  2058. ' ON s.user_id = pa.id' +
  2059. ' WHERE s.id = ?';
  2060. const sqlParam = [times, stageId, stageId];
  2061. const user = await this.db.queryOne(sql, sqlParam);
  2062. user.audit_order = 0;
  2063. group.unshift([ user ]);
  2064. return this.ctx.helper.groupAuditorsUniq(group);
  2065. }
  2066. async getAuditorHistory(stageId, times, reverse = false) {
  2067. const history = [];
  2068. if (times >= 1) {
  2069. for (let i = 1; i <= times; i++) {
  2070. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, i);
  2071. const group = this.ctx.helper.groupAuditors(auditors);
  2072. const historyGroup = [];
  2073. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  2074. for (const g of group) {
  2075. const his = {
  2076. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  2077. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  2078. auditors: g
  2079. };
  2080. if (his.audit_type === auditType.key.common) {
  2081. his.name = g[0].name;
  2082. } else {
  2083. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  2084. }
  2085. his.is_final = his.audit_order === max_order;
  2086. if (g[0].begin_time) {
  2087. his.begin_time = g[0].begin_time;
  2088. const beginTime = this.ctx.moment(g[0].begin_time);
  2089. his.beginYear = beginTime.format('YYYY');
  2090. his.beginDate = beginTime.format('MM-DD');
  2091. his.beginTime = beginTime.format('HH:mm:ss');
  2092. }
  2093. let end_time;
  2094. g.forEach(x => {
  2095. if (x.status === auditConst.status.checkSkip) return;
  2096. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  2097. if (x.end_time && (!end_time || x.end_time > end_time)) {
  2098. end_time = x.end_time;
  2099. if (his.status !== auditConst.status.checking) his.status = x.status;
  2100. }
  2101. });
  2102. if (end_time) {
  2103. his.end_time = end_time;
  2104. const endTime = this.ctx.moment(end_time);
  2105. his.endYear = endTime.format('YYYY');
  2106. his.endDate = endTime.format('MM-DD');
  2107. his.endTime = endTime.format('HH:mm:ss');
  2108. }
  2109. historyGroup.push(his);
  2110. }
  2111. if (reverse) {
  2112. history.push(historyGroup.reverse());
  2113. } else {
  2114. history.push(historyGroup);
  2115. }
  2116. }
  2117. }
  2118. return history;
  2119. }
  2120. async getUniqAuditor(stageId, times) {
  2121. const auditors = await this.ctx.service.stageAudit.getAuditors(stageId, times); // 全部参与的审批人
  2122. const result = [];
  2123. auditors.forEach(x => {
  2124. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  2125. result.push(x);
  2126. }
  2127. });
  2128. return result;
  2129. }
  2130. }
  2131. return StageAudit;
  2132. };