stage_audit.js 93 KB

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