stage_audit.js 122 KB

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