stage_audit.js 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  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. await transaction.update(this.ctx.service.stage.tableName, {
  949. id: stageId,
  950. times,
  951. ...stageTp,
  952. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times < times; })),
  953. cache_time_r: this.ctx.stage.cache_time_l,
  954. status: times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  955. });
  956. const preFlow = times > 1 ? await this.getLastestAuditor(stageId, times, auditConst.status.checkNo): null;
  957. const curFlow = { aid: this.ctx.stage.user_id, order: 1, status: auditConst.status.uncheck};
  958. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, times === 1 ? auditConst.status.uncheck : auditConst.status.checkNo,
  959. curFlow, preFlow, ledgerTp, stageTp);
  960. // 计算该审批人最终数据
  961. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  962. // 复制一份最新数据给下一人
  963. // 保留审批人数据,覆盖到原报中并删除该审批人合同支付数据
  964. await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction);
  965. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction, this.ctx.stage.times, 1);
  966. await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  967. newTimes = this.ctx.stage.times;
  968. newOrder = 0;
  969. newSaid = this.ctx.stage.user_id;
  970. oldTimes = this.ctx.stage.times;
  971. oldOrder = 1;
  972. oldSaid = curAudit.aid;
  973. } else if (this.ctx.stage.cancancel === 2) {
  974. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  975. // 整理当前流程审核人状态更新
  976. const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  977. const preAudit = this.ctx.stage.preAudit;
  978. if (!curAudit || curAudit.order <= 1) {
  979. throw '撤回用户数据错误';
  980. }
  981. // 顺移气候审核人流程顺序
  982. this.initSqlBuilder();
  983. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  984. this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
  985. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  986. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  987. const data = await transaction.query(sql, sqlParam);
  988. // 当前审批人2次添加至流程中
  989. const newAuditors = [];
  990. newAuditors.push({
  991. tid: curAudit.tid,
  992. sid: curAudit.sid,
  993. aid: preAudit.aid,
  994. times: curAudit.times,
  995. order: curAudit.order,
  996. status: auditConst.status.checkCancel,
  997. begin_time: time,
  998. end_time: time,
  999. opinion: '',
  1000. });
  1001. newAuditors.push({
  1002. tid: curAudit.tid,
  1003. sid: curAudit.sid,
  1004. aid: preAudit.aid,
  1005. times: curAudit.times,
  1006. order: curAudit.order + 1,
  1007. status: auditConst.status.checking,
  1008. begin_time: time,
  1009. });
  1010. await transaction.insert(this.tableName, newAuditors);
  1011. // 当前审批人变成待审批
  1012. await transaction.update(this.tableName, { id: curAudit.id, order: curAudit.order + 2, begin_time: null, status: auditConst.status.uncheck });
  1013. // 计算并合同支付最终数据
  1014. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1015. const stageTp = {
  1016. contract_tp: tpData.contract_tp,
  1017. qc_tp: tpData.qc_tp,
  1018. positive_qc_tp: tpData.positive_qc_tp,
  1019. negative_qc_tp: tpData.negative_qc_tp,
  1020. yf_tp: yfPay.tp,
  1021. sf_tp: sfPay.tp,
  1022. };
  1023. this.ctx.stage.tp_history.push({ times, order: curAudit.order, ...stageTp });
  1024. // 同步 期信息
  1025. await transaction.update(this.ctx.service.stage.tableName, {
  1026. id: stageId,
  1027. times,
  1028. ...stageTp,
  1029. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1030. cache_time_r: this.ctx.stage.cache_time_l,
  1031. });
  1032. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, this.ctx.stage.status, newAuditors[1], newAuditors[0], ledgerTp, stageTp);
  1033. // 计算该审批人最终数据
  1034. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1035. // 复制一份最新数据给下一人
  1036. 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);
  1037. newTimes = this.ctx.stage.times;
  1038. newOrder = curAudit.order + 1;
  1039. newSaid = preAudit.aid;
  1040. oldTimes = this.ctx.stage.times;
  1041. oldOrder = curAudit.order;
  1042. oldSaid = curAudit.aid;
  1043. // 锁定本人数据,保留锁定数据相关确认状态
  1044. // await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, curAudit.aid, preAudit.aid, transaction);
  1045. } else if (this.ctx.stage.cancancel === 3) {
  1046. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1047. // 整理当前流程审核人状态更新
  1048. const curAudit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  1049. const preAudit = this.ctx.stage.preAudit;
  1050. if (!curAudit || curAudit.order <= 1) {
  1051. throw '撤回用户数据错误';
  1052. }
  1053. // 添加撤回人到审批流程中
  1054. const newAuditors = [];
  1055. newAuditors.push({
  1056. tid: curAudit.tid,
  1057. sid: curAudit.sid,
  1058. aid: preAudit.aid,
  1059. times: curAudit.times,
  1060. order: curAudit.order,
  1061. status: auditConst.status.checkCancel,
  1062. begin_time: time,
  1063. end_time: time,
  1064. opinion: '',
  1065. });
  1066. await transaction.insert(this.tableName, newAuditors);
  1067. // 删除当前审批人
  1068. await transaction.delete(this.tableName, { id: curAudit.id });
  1069. // 更新上一个人为审批中
  1070. await transaction.update(this.tableName, { begin_time: time, status: auditConst.status.checking }, {
  1071. where: {
  1072. sid: curAudit.sid,
  1073. times: curAudit.times,
  1074. order: curAudit.order + 1,
  1075. }
  1076. });
  1077. // 计算并合同支付最终数据
  1078. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1079. const stageTp = {
  1080. contract_tp: tpData.contract_tp,
  1081. qc_tp: tpData.qc_tp,
  1082. positive_qc_tp: tpData.positive_qc_tp,
  1083. negative_qc_tp: tpData.negative_qc_tp,
  1084. yf_tp: yfPay.tp,
  1085. sf_tp: sfPay.tp,
  1086. };
  1087. this.ctx.stage.tp_history.push({ times, order: curAudit.order, ...stageTp });
  1088. // 同步 期信息
  1089. await transaction.update(this.ctx.service.stage.tableName, {
  1090. id: stageId,
  1091. times,
  1092. ...stageTp,
  1093. status: auditConst.status.checking,
  1094. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1095. cache_time_r: this.ctx.stage.cache_time_l,
  1096. });
  1097. const cur = {tid: curAudit.tid, sid: curAudit.sid, aid: preAudit.aid, times: curAudit.times, order: curAudit.order + 1, status: auditConst.status.checking};
  1098. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, this.ctx.stage.status, cur, newAuditors[0], ledgerTp, stageTp);
  1099. // 计算该审批人最终数据
  1100. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1101. // 复制一份最新数据给下一人
  1102. 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);
  1103. newTimes = this.ctx.stage.times;
  1104. newOrder = curAudit.order + 1;
  1105. newSaid = preAudit.aid;
  1106. oldTimes = this.ctx.stage.times;
  1107. oldOrder = curAudit.order;
  1108. oldSaid = curAudit.aid;
  1109. // 锁定本人数据,保留锁定数据相关确认状态
  1110. // await this.ctx.service.stageAuditAss.lockConfirm4CheckNoPre(this.ctx.stage, curAudit.aid, preAudit.aid, transaction);
  1111. } else if (this.ctx.stage.cancancel === 4) {
  1112. // 原报撤回,判断是否为多次,多次则为退回状态
  1113. // 整理上一个流程审核人状态更新
  1114. const curAudit = await this.getDataByCondition({ sid: stageId, times: times - 1, status: auditConst.status.checkNo });
  1115. // 顺移气候审核人流程顺序
  1116. this.initSqlBuilder();
  1117. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  1118. this.sqlBuilder.setAndWhere('order', { value: curAudit.order, operate: '>' });
  1119. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  1120. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  1121. const data = await transaction.query(sql, sqlParam);
  1122. // 当前审批人2次添加至流程中
  1123. const newAuditors = [];
  1124. newAuditors.push({
  1125. tid: curAudit.tid,
  1126. sid: curAudit.sid,
  1127. aid: curAudit.aid,
  1128. times: curAudit.times,
  1129. order: curAudit.order + 1,
  1130. status: auditConst.status.checkCancel,
  1131. begin_time: time,
  1132. end_time: time,
  1133. opinion: '',
  1134. });
  1135. newAuditors.push({
  1136. tid: curAudit.tid,
  1137. sid: curAudit.sid,
  1138. aid: curAudit.aid,
  1139. times: curAudit.times,
  1140. order: curAudit.order + 2,
  1141. status: auditConst.status.checking,
  1142. begin_time: time,
  1143. });
  1144. await transaction.insert(this.tableName, newAuditors);
  1145. // 删除当前次审批流
  1146. await transaction.delete(this.tableName, { sid: stageId, times });
  1147. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1148. // 计算并合同支付最终数据
  1149. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1150. const stageTp = {
  1151. contract_tp: tpData.contract_tp,
  1152. qc_tp: tpData.qc_tp,
  1153. positive_qc_tp: tpData.positive_qc_tp,
  1154. negative_qc_tp: tpData.negative_qc_tp,
  1155. yf_tp: yfPay.tp,
  1156. sf_tp: sfPay.tp,
  1157. };
  1158. this.ctx.stage.tp_history.push({ times: times - 1, order: curAudit.order, ...stageTp });
  1159. await transaction.update(this.ctx.service.stage.tableName, {
  1160. id: stageId,
  1161. times: times - 1,
  1162. ...stageTp,
  1163. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1164. cache_time_r: this.ctx.stage.cache_time_l,
  1165. status: auditConst.status.checking,
  1166. });
  1167. await this.ctx.service.tenderCache.updateStageCache4Flow(transaction, this.ctx.stage, auditConst.status.checking, newAuditors[1], newAuditors[0], ledgerTp, stageTp);
  1168. // 计算该审批人最终数据
  1169. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  1170. // 复制一份最新数据给下一人
  1171. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times - 1, curAudit.order + 1, transaction, this.ctx.stage.times, 0);
  1172. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times - 1, curAudit.order + 2, transaction, this.ctx.stage.times, 0);
  1173. await this.ctx.service.stagePay.deleteAuditStagePays(this.ctx.stage, this.ctx.stage.times, 0, transaction);
  1174. newTimes = this.ctx.stage.times - 1;
  1175. newOrder = curAudit.order + 2;
  1176. newSaid = curAudit.aid;
  1177. oldTimes = this.ctx.stage.times;
  1178. oldOrder = 0;
  1179. oldSaid = this.ctx.stage.user_id;
  1180. }
  1181. // 先覆盖再删除
  1182. await this.ctx.service.stageBills.updateStageBills4CheckCancel(stageId, newSaid, newTimes, newOrder, oldSaid, oldTimes, oldOrder, transaction);
  1183. await this.ctx.service.stagePos.updateStagePos4CheckCancel(stageId, newSaid, newTimes, newOrder, oldSaid, oldTimes, oldOrder, transaction);
  1184. await this.ctx.service.stageDetail.updateStageDetail4CheckCancel(stageId, newTimes, newOrder, oldTimes, oldOrder, transaction);
  1185. await this.ctx.service.stageJgcl.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
  1186. await this.ctx.service.stageBonus.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
  1187. await this.ctx.service.stageOther.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
  1188. await this.ctx.service.stageSafeProd.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
  1189. await this.ctx.service.stageTempLand.updateHistory4CheckCancel(this.ctx.stage, newTimes, newOrder, transaction);
  1190. // 上报/审批 - 检查三方特殊推送
  1191. await this.ctx.service.specMsg.addStageMsg(transaction, this.ctx.session.sessionProject.id, this.ctx.stage, pushOperate.stage.flow);
  1192. await transaction.commit();
  1193. // 通知发送 - 第三方更新
  1194. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1195. const base_data = {
  1196. tid: this.ctx.tender.id,
  1197. sid: stageId,
  1198. op: 'update',
  1199. };
  1200. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1201. base_data.op = 'update';
  1202. base_data.sid = -1;
  1203. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1204. }
  1205. } catch (err) {
  1206. await transaction.rollback();
  1207. throw err;
  1208. }
  1209. }
  1210. /**
  1211. * 获取审核人需要审核的期列表
  1212. *
  1213. * @param auditorId
  1214. * @return {Promise<*>}
  1215. */
  1216. async getAuditStage(auditorId) {
  1217. const sql =
  1218. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1219. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  1220. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  1221. ' FROM ?? AS sa ' +
  1222. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  1223. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  1224. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  1225. ' ORDER BY sa.`begin_time` DESC';
  1226. const sqlParam = [
  1227. this.tableName,
  1228. this.ctx.service.stage.tableName,
  1229. this.ctx.service.tender.tableName,
  1230. auditorId,
  1231. auditConst.status.checking,
  1232. auditorId,
  1233. auditConst.status.checkNo,
  1234. auditConst.status.checkNo,
  1235. ];
  1236. return await this.db.query(sql, sqlParam);
  1237. }
  1238. /**
  1239. * 获取审核人审核的次数
  1240. *
  1241. * @param auditorId
  1242. * @return {Promise<*>}
  1243. */
  1244. async getCountByChecked(auditorId) {
  1245. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  1246. }
  1247. /**
  1248. * 获取最近一次审批结束时间
  1249. *
  1250. * @param auditorId
  1251. * @return {Promise<*>}
  1252. */
  1253. async getLastEndTimeByChecked(auditorId) {
  1254. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  1255. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') ORDER BY `end_time` DESC';
  1256. const sqlParam = [this.tableName, auditorId];
  1257. const result = await this.db.queryOne(sql, sqlParam);
  1258. return result ? result.end_time : null;
  1259. }
  1260. /**
  1261. * 获取 某时间后 审批进度 更新的期
  1262. * @param {Number} pid - 查询标段
  1263. * @param {Number} uid - 查询人
  1264. * @param {Date} time - 查询时间
  1265. * @return {Promise<*>}
  1266. */
  1267. async getNoticeStage(pid, uid, time) {
  1268. let notice = await this.db.select('zh_notice', {
  1269. where: { pid, type: pushType.stage, uid },
  1270. orders: [['create_time', 'desc']],
  1271. limit: 10,
  1272. offset: 0,
  1273. });
  1274. notice = notice.map(v => {
  1275. const extra = JSON.parse(v.content);
  1276. delete v.content;
  1277. return { ...v, ...extra };
  1278. });
  1279. return notice;
  1280. }
  1281. /**
  1282. * 用于添加推送所需的content内容
  1283. * @param {Number} pid 项目id
  1284. * @param {Number} tid 台账id
  1285. * @param {Number} sid 期id
  1286. * @param {Number} uid 审核人id
  1287. */
  1288. async getNoticeContent(pid, tid, sid, uid, opinion = '') {
  1289. const noticeSql =
  1290. 'SELECT * FROM (SELECT ' +
  1291. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1292. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1293. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1294. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1295. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1296. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1297. const content = await this.db.query(noticeSql, noticeSqlParam);
  1298. if (content.length) {
  1299. content[0].opinion = opinion;
  1300. }
  1301. return content.length ? JSON.stringify(content[0]) : '';
  1302. }
  1303. /**
  1304. * 获取审核人流程列表
  1305. *
  1306. * @param auditorId
  1307. * @return {Promise<*>}
  1308. */
  1309. async getAuditGroupByList(stageId, times, transaction = false) {
  1310. // const sql =
  1311. // 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order`, la.`status`' +
  1312. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1313. // ' WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = 0 GROUP BY la.`aid` ORDER BY la.`order`';
  1314. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1315. const sql =
  1316. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order`, la.`status`' +
  1317. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `sid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  1318. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  1319. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`sid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  1320. const sqlParam = [this.tableName, stageId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, stageId, times, 0];
  1321. return transaction !== false ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam);
  1322. }
  1323. /**
  1324. * 获取审核人流程列表
  1325. *
  1326. * @param auditorId
  1327. * @return {Promise<*>}
  1328. */
  1329. async getAuditGroupByListWithOwner(stageId, times) {
  1330. const result = await this.getAuditGroupByList(stageId, times);
  1331. const sql =
  1332. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1333. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1334. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1335. ' ON s.user_id = pa.id' +
  1336. ' WHERE s.id = ?';
  1337. const sqlParam = [times, stageId, stageId];
  1338. const user = await this.db.queryOne(sql, sqlParam);
  1339. result.unshift(user);
  1340. return result;
  1341. }
  1342. /**
  1343. * 复制上一期的审批人列表给最新一期
  1344. *
  1345. * @param transaction - 新增一期的事务
  1346. * @param {Object} preStage - 上一期
  1347. * @param {Object} newStage - 最新一期
  1348. * @return {Promise<*>}
  1349. */
  1350. async copyPreStageAuditors(transaction, preStage, newStage) {
  1351. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1352. const newAuditors = [];
  1353. for (const a of auditors) {
  1354. const na = {
  1355. tid: preStage.tid,
  1356. sid: newStage.id,
  1357. aid: a.aid,
  1358. times: newStage.times,
  1359. order: newAuditors.length + 1,
  1360. status: auditConst.status.uncheck,
  1361. };
  1362. newAuditors.push(na);
  1363. }
  1364. const result = await transaction.insert(this.tableName, newAuditors);
  1365. return (result.effectRows = auditors.length);
  1366. }
  1367. /**
  1368. * 移除审核人
  1369. *
  1370. * @param {Number} stageId - 期id
  1371. * @param {Number} status - 期状态
  1372. * @param {Number} status - 期次数
  1373. * @return {Promise<boolean>}
  1374. */
  1375. async getAuditorByStatus(stageId, status, times = 1) {
  1376. let auditor = null;
  1377. let sql = '';
  1378. let sqlParam = '';
  1379. switch (status) {
  1380. case auditConst.status.checking:
  1381. case auditConst.status.checked:
  1382. case auditConst.status.checkNoPre:
  1383. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1384. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1385. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1386. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1387. auditor = await this.db.queryOne(sql, sqlParam);
  1388. break;
  1389. case auditConst.status.checkNo:
  1390. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1391. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1392. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1393. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1394. auditor = await this.db.queryOne(sql, sqlParam);
  1395. break;
  1396. case auditConst.status.uncheck:
  1397. default:
  1398. break;
  1399. }
  1400. return auditor;
  1401. }
  1402. /**
  1403. * 取某一期已批准审核信息(报表用)
  1404. *
  1405. * @param {Number} stageId - 期id
  1406. * @param {Number} times - 期次数
  1407. * @return {Promise<boolean>}
  1408. */
  1409. async getStageAudit(stageId, times = 1) {
  1410. 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';
  1411. const sqlParam = [this.tableName, stageId, times];
  1412. const rst = await this.db.query(sql, sqlParam);
  1413. return rst;
  1414. }
  1415. /**
  1416. * 取待审批期列表(wap用)
  1417. *
  1418. * @param auditorId
  1419. * @return {Promise<*>}
  1420. */
  1421. async getAuditStageByWap(auditorId) {
  1422. const sql =
  1423. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1424. // ' 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`, ' +
  1425. ' s.*,' +
  1426. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1427. ' ti.`deal_info` ' +
  1428. ' FROM ?? AS sa' +
  1429. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1430. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1431. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1432. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1433. const sqlParam = [
  1434. this.tableName,
  1435. this.ctx.service.stage.tableName,
  1436. this.ctx.service.tender.tableName,
  1437. this.ctx.service.tenderInfo.tableName,
  1438. auditorId,
  1439. auditConst.status.checking,
  1440. ];
  1441. return await this.db.query(sql, sqlParam);
  1442. }
  1443. /**
  1444. * 删除 某期 某次 全审批流程
  1445. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1446. * @param {Number} sid - 标段id
  1447. * @param {Number} times - 第几次审批
  1448. * @param transaction - 删除事务
  1449. * @return {Promise<void>}
  1450. */
  1451. async _timesDelete(sid, times, transaction) {
  1452. // 审批流程
  1453. await transaction.delete(this.tableName, { sid, times });
  1454. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1455. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1456. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1457. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1458. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1459. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1460. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1461. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid, times });
  1462. // 其他台账
  1463. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1464. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1465. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1466. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1467. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1468. }
  1469. /**
  1470. * 删除本次审批流程
  1471. * @param {Number} stageId - 标段id
  1472. * @param {Number} times - 第几次审批
  1473. * @return {Promise<void>}
  1474. */
  1475. async timesDelete() {
  1476. const transaction = await this.db.beginTransaction();
  1477. try {
  1478. // 删除最新一次数据
  1479. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1480. // 审批退回,未重新上报时,需删除最新两次数据
  1481. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1482. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1483. if (isCheckNo) {
  1484. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1485. }
  1486. // 添加上一次审批人
  1487. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1488. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1489. const auditors = await this.db.query(sql, sqlParam);
  1490. let order = 1;
  1491. for (const a of auditors) {
  1492. a.times = nowTimes;
  1493. a.order = order;
  1494. a.status = auditConst.status.uncheck;
  1495. order++;
  1496. }
  1497. const ledgerTp = await this._updateTender(transaction);
  1498. // 拷贝新一次审核流程列表
  1499. await transaction.insert(this.tableName, auditors);
  1500. // 计算缓存
  1501. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1502. // 计算并合同支付最终数据
  1503. const lastAudit = await this.getDataByCondition({
  1504. sid: this.ctx.stage.id,
  1505. times: nowTimes - 1,
  1506. status: auditConst.status.checkNo,
  1507. });
  1508. if (!lastAudit) throw '审批数据错误';
  1509. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1510. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1511. const yfPay = stagePay.find(function(x) {
  1512. return x.ptype === payConst.payType.yf;
  1513. });
  1514. const sfPay = stagePay.find(function(x) {
  1515. return x.ptype === payConst.payType.sf;
  1516. });
  1517. // 同步 期信息
  1518. const time = new Date();
  1519. await transaction.update(this.ctx.service.stage.tableName, {
  1520. id: this.ctx.stage.id,
  1521. status: auditConst.status.checkNo,
  1522. contract_tp: tpData.contract_tp,
  1523. qc_tp: tpData.qc_tp,
  1524. positive_qc_tp: tpData.positive_qc_tp,
  1525. negative_qc_tp: tpData.negative_qc_tp,
  1526. times: nowTimes,
  1527. yf_tp: yfPay ? yfPay.tp : null,
  1528. sf_tp: sfPay ? sfPay.tp : null,
  1529. tp_history: JSON.stringify(this.ctx.stage.tp_history.filter(x => { return x.times <= nowTimes; })),
  1530. cache_time_l: time,
  1531. cache_time_r: time,
  1532. });
  1533. await transaction.commit();
  1534. // 通知发送 - 第三方更新
  1535. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1536. const base_data = {
  1537. tid: this.ctx.tender.id,
  1538. sid: this.ctx.stage.id,
  1539. op: 'update',
  1540. };
  1541. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1542. base_data.op = 'update';
  1543. base_data.sid = -1;
  1544. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1545. }
  1546. } catch (err) {
  1547. await transaction.rollback();
  1548. throw err;
  1549. }
  1550. }
  1551. // 固定审批流-更新
  1552. async updateNewAuditList(stage, newIdList) {
  1553. const transaction = await this.db.beginTransaction();
  1554. try {
  1555. // 先删除旧的审批流,再添加新的
  1556. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1557. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times });
  1558. const newAuditors = [];
  1559. let order = 1;
  1560. for (const aid of newIdList) {
  1561. newAuditors.push({
  1562. tid: stage.tid, sid: stage.id, aid,
  1563. times: stage.times, order, status: auditConst.status.uncheck,
  1564. });
  1565. order++;
  1566. }
  1567. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1568. await transaction.commit();
  1569. } catch (err) {
  1570. await transaction.rollback();
  1571. throw err;
  1572. }
  1573. }
  1574. // 固定终审-更新
  1575. async updateLastAudit(stage, auditList, lastId) {
  1576. const transaction = await this.db.beginTransaction();
  1577. try {
  1578. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1579. const idList = this._.map(auditList, 'aid');
  1580. let order = idList.length + 1;
  1581. if (idList.indexOf(lastId) !== -1) {
  1582. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1583. await transaction.delete(this.ctx.service.stageAuditAss.tableName, { sid: stage.id, times: stage.times, user_id: lastId });
  1584. const audit = this._.find(auditList, { 'aid': lastId });
  1585. // 顺移之后审核人流程顺序
  1586. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1587. order = order - 1;
  1588. }
  1589. // 添加终审
  1590. const newAuditor = {
  1591. tid: stage.tid, sid: stage.id, aid: lastId,
  1592. times: stage.times, order, status: auditConst.status.uncheck,
  1593. };
  1594. await transaction.insert(this.tableName, newAuditor);
  1595. await transaction.commit();
  1596. } catch (err) {
  1597. await transaction.rollback();
  1598. throw err;
  1599. }
  1600. }
  1601. async getFinalAuditGroup(stageId, times) {
  1602. const sql =
  1603. '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 ' +
  1604. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1605. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1606. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1607. const result = await this.db.query(sql, sqlParam);
  1608. for (const r of result) {
  1609. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1610. r.status = auditor.status;
  1611. r.opinion = auditor.opinion;
  1612. r.begin_time = auditor.begin_time;
  1613. r.end_time = auditor.end_time;
  1614. }
  1615. return result;
  1616. }
  1617. async getNumByMonth(tid, startMonth, endMonth) {
  1618. 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 ?';
  1619. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1620. const result = await this.db.queryOne(sql, sqlParam);
  1621. return result ? result.num : 0;
  1622. }
  1623. /**
  1624. * 删除本次审批流程
  1625. * @param {Number} stageId - 标段id
  1626. * @param {Number} times - 第几次审批
  1627. * @param {Object} data - 更改参数
  1628. * @return {Promise<void>}
  1629. */
  1630. async saveAudit(stageId, times, data) {
  1631. const transaction = await this.db.beginTransaction();
  1632. try {
  1633. const auditors = await this.getAuditGroupByList(stageId, times);
  1634. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1635. if (data.operate === 'add') {
  1636. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1637. throw '当前人下无法操作新增';
  1638. }
  1639. const newAudit = {
  1640. tid: this.ctx.tender.id,
  1641. sid: stageId,
  1642. aid: data.new_aid,
  1643. order: now_audit.order+1,
  1644. times: times,
  1645. status: 1
  1646. };
  1647. // order+1
  1648. await this._syncOrderByDelete(transaction, stageId, now_audit.order+1, times, '+');
  1649. await transaction.insert(this.tableName, newAudit);
  1650. // 更新审批流程页数据,如果存在
  1651. } else if (data.operate === 'del') {
  1652. if (now_audit.status !== auditConst.status.uncheck) {
  1653. throw '当前人无法操作删除';
  1654. }
  1655. await transaction.delete(this.tableName, { sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1656. await this._syncOrderByDelete(transaction, stageId, now_audit.order, times);
  1657. // 旧的更新为is_old为1
  1658. await transaction.update(this.tableName, { is_old: 1 }, {
  1659. where: {
  1660. sid: stageId,
  1661. times,
  1662. aid: data.old_aid,
  1663. }
  1664. });
  1665. } else if (data.operate === 'change') {
  1666. const nowAudit = await this.getDataByCondition({ sid: stageId, times, aid: now_audit.aid, order: now_audit.order });
  1667. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1668. throw '当前人无法操作替换';
  1669. }
  1670. nowAudit.aid = data.new_aid;
  1671. await transaction.update(this.tableName, nowAudit);
  1672. // 旧的更新为is_old为1
  1673. await transaction.update(this.tableName, { is_old: 1 }, {
  1674. where: {
  1675. sid: stageId,
  1676. times,
  1677. aid: data.old_aid,
  1678. }
  1679. });
  1680. }
  1681. if (this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdspl || this.ctx.tender.info.shenpi.stage === shenpiConst.sp_status.gdzs) {
  1682. const newAuditors = await this.getAuditGroupByList(stageId, times, transaction);
  1683. 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'));
  1684. }
  1685. // 更新到审批流程方法
  1686. await transaction.commit();
  1687. } catch (err) {
  1688. await transaction.rollback();
  1689. throw err;
  1690. }
  1691. }
  1692. }
  1693. return StageAudit;
  1694. };