stage_audit.js 113 KB

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