stage_audit.js 120 KB

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