stage_audit.js 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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 payConst = require('../const/deal_pay');
  14. const pushType = require('../const/audit').pushType;
  15. module.exports = app => {
  16. class StageAudit extends app.BaseService {
  17. /**
  18. * 构造函数
  19. *
  20. * @param {Object} ctx - egg全局变量
  21. * @return {void}
  22. */
  23. constructor(ctx) {
  24. super(ctx);
  25. this.tableName = 'stage_audit';
  26. }
  27. /**
  28. * 获取 审核人信息
  29. *
  30. * @param {Number} stageId - 期id
  31. * @param {Number} auditorId - 审核人id
  32. * @param {Number} times - 第几次审批
  33. * @return {Promise<*>}
  34. */
  35. async getAuditor(stageId, auditorId, times = 1) {
  36. const sql = '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` ' +
  37. 'FROM ?? AS la, ?? AS pa ' +
  38. 'WHERE la.`sid` = ? and la.`aid` = ? and la.`times` = ?' +
  39. ' and la.`aid` = pa.`id`';
  40. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditorId, times];
  41. return await this.db.queryOne(sql, sqlParam);
  42. }
  43. /**
  44. * 获取 审核列表信息
  45. *
  46. * @param {Number} stageId - 期id
  47. * @param {Number} times - 第几次审批
  48. * @param {Number} order_sort - 列表排序方式
  49. * @return {Promise<*>}
  50. */
  51. async getAuditors(stageId, times = 1, order_sort = 'asc') {
  52. const sql = '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`, g.`sort` ' +
  53. 'FROM ?? AS la, ?? AS pa, (SELECT `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `sid` = ? AND `times` = ? GROUP BY `aid`) as g ' +
  54. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order` ' + order_sort;
  55. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, stageId, times, stageId, times];
  56. const result = await this.db.query(sql, sqlParam);
  57. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `sid` = ? AND `times` = ? GROUP BY `aid`) as a';
  58. const sqlParam2 = [this.tableName, stageId, times];
  59. const count = await this.db.queryOne(sql2, sqlParam2);
  60. for (const i in result) {
  61. result[i].max_sort = count.num;
  62. }
  63. return result;
  64. }
  65. async getAllAuditors(tenderId) {
  66. const sql = 'SELECT sa.aid, sa.tid FROM ' + this.tableName + ' sa' +
  67. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On sa.tid = t.id' +
  68. ' WHERE t.id = ?' +
  69. ' GROUP BY sa.aid';
  70. const sqlParam = [tenderId];
  71. return this.db.query(sql, sqlParam);
  72. }
  73. /**
  74. * 获取标段审核人最后一位的名称
  75. *
  76. * @param {Number} tenderId - 标段id
  77. * @param {Number} auditorId - 审核人id
  78. * @param {Number} times - 第几次审批
  79. * @return {Promise<*>}
  80. */
  81. async getStatusName(stageId) {
  82. const sql = 'SELECT pa.`name` ' +
  83. 'FROM ?? AS sa, ?? AS pa ' +
  84. 'WHERE sa.`sid` = ?' +
  85. ' and sa.`aid` = pa.`id` and sa.`status` != ? ORDER BY sa.`times` DESC, sa.`order` DESC';
  86. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.uncheck];
  87. return await this.db.queryOne(sql, sqlParam);
  88. }
  89. /**
  90. * 获取 当前审核人
  91. *
  92. * @param {Number} stageId - 期id
  93. * @param {Number} times - 第几次审批
  94. * @return {Promise<*>}
  95. */
  96. async getCurAuditor(stageId, times = 1) {
  97. const sql = '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` ' +
  98. 'FROM ?? AS la, ?? AS pa ' +
  99. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ?' +
  100. ' and la.`aid` = pa.`id`';
  101. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checking, times];
  102. return await this.db.queryOne(sql, sqlParam);
  103. }
  104. /**
  105. * 获取 最新审核顺序
  106. *
  107. * @param {Number} stageId - 期id
  108. * @param {Number} times - 第几次审批
  109. * @return {Promise<number>}
  110. */
  111. async getNewOrder(stageId, times = 1) {
  112. const sql = 'SELECT Max(??) As max_order FROM ?? Where `sid` = ? and `times` = ?';
  113. const sqlParam = ['order', this.tableName, stageId, times];
  114. const result = await this.db.queryOne(sql, sqlParam);
  115. return result && result.max_order ? result.max_order + 1 : 1;
  116. }
  117. /**
  118. * 新增审核人
  119. *
  120. * @param {Number} stageId - 期id
  121. * @param {Number} auditorId - 审核人id
  122. * @param {Number} times - 第几次审批
  123. * @return {Promise<number>}
  124. */
  125. async addAuditor(stageId, auditorId, times = 1) {
  126. const newOrder = await this.getNewOrder(stageId, times);
  127. const data = {
  128. tid: this.ctx.tender.id,
  129. sid: stageId,
  130. aid: auditorId,
  131. times,
  132. order: newOrder,
  133. status: auditConst.status.uncheck,
  134. };
  135. const result = await this.db.insert(this.tableName, data);
  136. return result.effectRows = 1;
  137. }
  138. /**
  139. * 移除审核人时,同步其后审核人order
  140. * @param transaction - 事务
  141. * @param {Number} stageId - 标段id
  142. * @param {Number} auditorId - 审核人id
  143. * @param {Number} times - 第几次审批
  144. * @return {Promise<*>}
  145. * @private
  146. */
  147. async _syncOrderByDelete(transaction, stageId, order, times) {
  148. this.initSqlBuilder();
  149. this.sqlBuilder.setAndWhere('sid', {
  150. value: stageId,
  151. operate: '=',
  152. });
  153. this.sqlBuilder.setAndWhere('order', {
  154. value: order,
  155. operate: '>=',
  156. });
  157. this.sqlBuilder.setAndWhere('times', {
  158. value: times,
  159. operate: '=',
  160. });
  161. this.sqlBuilder.setUpdateData('order', {
  162. value: 1,
  163. selfOperate: '-',
  164. });
  165. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  166. const data = await transaction.query(sql, sqlParam);
  167. return data;
  168. }
  169. /**
  170. * 移除审核人
  171. *
  172. * @param {Number} stageId - 期id
  173. * @param {Number} auditorId - 审核人id
  174. * @param {Number} times - 第几次审批
  175. * @return {Promise<boolean>}
  176. */
  177. async deleteAuditor(stageId, auditorId, times = 1) {
  178. const transaction = await this.db.beginTransaction();
  179. try {
  180. const condition = { sid: stageId, aid: auditorId, times };
  181. const auditor = await this.getDataByCondition(condition);
  182. if (!auditor) {
  183. throw '该审核人不存在';
  184. }
  185. await this._syncOrderByDelete(transaction, stageId, auditor.order, times);
  186. await transaction.delete(this.tableName, condition);
  187. await transaction.commit();
  188. } catch (err) {
  189. await transaction.rollback();
  190. throw err;
  191. }
  192. return true;
  193. }
  194. /**
  195. * 开始审批
  196. *
  197. * @param {Number} stageId - 期id
  198. * @param {Number} times - 第几次审批
  199. * @return {Promise<boolean>}
  200. */
  201. async start(stageId, times = 1) {
  202. const audit = await this.getDataByCondition({ sid: stageId, times, order: 1 });
  203. if (!audit) {
  204. throw '请先选择审批人,再上报数据';
  205. }
  206. const transaction = await this.db.beginTransaction();
  207. try {
  208. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  209. // 计算原报最终数据
  210. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  211. // 复制一份下一审核人数据
  212. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  213. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  214. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  215. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  216. // 更新期数据
  217. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  218. this.ctx.stage.tp_history.push({
  219. times: this.ctx.stage.curTimes, order: 0,
  220. contract_tp: tpData.contract_tp,
  221. qc_tp: tpData.qc_tp,
  222. yf_tp: yfPay.tp,
  223. sf_tp: sfPay.tp,
  224. });
  225. await transaction.update(this.ctx.service.stage.tableName, {
  226. id: stageId, status: auditConst.status.checking,
  227. contract_tp: tpData.contract_tp,
  228. qc_tp: tpData.qc_tp,
  229. yf_tp: yfPay.tp,
  230. sf_tp: sfPay.tp,
  231. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  232. cache_time_r: this.ctx.stage.cache_time_l,
  233. });
  234. // 添加短信通知-需要审批提醒功能
  235. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  236. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  237. // const smsType = JSON.parse(smsUser.sms_type);
  238. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  239. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  240. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  241. // const sms = new SMS(this.ctx);
  242. // const tenderName = await sms.contentChange(tenderInfo.name);
  243. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  244. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  245. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  246. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  247. // sms.send(smsUser.auth_mobile, content);
  248. // }
  249. // }
  250. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  251. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  252. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL,
  253. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  254. // todo 更新标段tender状态 ?
  255. await transaction.commit();
  256. } catch (err) {
  257. await transaction.rollback();
  258. throw err;
  259. }
  260. return true;
  261. }
  262. async _checked(pid, stageId, checkData, times) {
  263. const time = new Date();
  264. // 整理当前流程审核人状态更新
  265. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  266. if (!audit) {
  267. throw '审核数据错误';
  268. }
  269. const nextAudit = await this.getDataByCondition({ sid: stageId, times, order: audit.order + 1 });
  270. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  271. const transaction = await this.db.beginTransaction();
  272. try {
  273. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  274. // 计算并合同支付最终数据
  275. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  276. this.ctx.stage.tp_history.push({
  277. times, order: audit.order,
  278. contract_tp: tpData.contract_tp,
  279. qc_tp: tpData.qc_tp,
  280. yf_tp: yfPay.tp,
  281. sf_tp: sfPay.tp,
  282. });
  283. // 无下一审核人表示,审核结束
  284. if (nextAudit) {
  285. // 复制一份下一审核人数据
  286. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  287. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  288. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  289. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  290. // 流程至下一审批人
  291. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  292. // 同步 期信息
  293. await transaction.update(this.ctx.service.stage.tableName, {
  294. id: stageId, status: auditConst.status.checking,
  295. contract_tp: tpData.contract_tp,
  296. qc_tp: tpData.qc_tp,
  297. yf_tp: yfPay.tp,
  298. sf_tp: sfPay.tp,
  299. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  300. cache_time_r: this.ctx.stage.cache_time_l,
  301. });
  302. // 添加短信通知-需要审批提醒功能
  303. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  304. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  305. // const smsType = JSON.parse(smsUser.sms_type);
  306. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  307. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  308. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  309. // const sms = new SMS(this.ctx);
  310. // const tenderName = await sms.contentChange(tenderInfo.name);
  311. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  312. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  313. // // const result = '';
  314. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  315. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  316. // sms.send(smsUser.auth_mobile, content);
  317. // }
  318. // }
  319. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  320. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  321. await this.ctx.helper.sendAliSms(nextAudit.aid, smsTypeConst.const.JL,
  322. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  323. } else {
  324. // 本期结束
  325. // 生成截止本期数据 final数据
  326. console.time('generatePre');
  327. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  328. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  329. console.timeEnd('generatePre');
  330. // 同步 期信息
  331. await transaction.update(this.ctx.service.stage.tableName, {
  332. id: stageId, status: checkData.checkType,
  333. contract_tp: tpData.contract_tp,
  334. qc_tp: tpData.qc_tp,
  335. yf_tp: yfPay.tp,
  336. sf_tp: sfPay.tp,
  337. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  338. cache_time_r: this.ctx.stage.cache_time_l,
  339. });
  340. // 添加短信通知-审批通过提醒功能
  341. // const mobile_array = [];
  342. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  343. const auditList = await this.getAuditors(stageId, stageInfo.times);
  344. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  345. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  346. // const smsType = JSON.parse(smsUser1.sms_type);
  347. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  348. // mobile_array.push(smsUser1.auth_mobile);
  349. // }
  350. // }
  351. // for (const user of auditList) {
  352. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  353. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  354. // const smsType = JSON.parse(smsUser.sms_type);
  355. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  356. // mobile_array.push(smsUser.auth_mobile);
  357. // }
  358. // }
  359. // }
  360. // if (mobile_array.length > 0) {
  361. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  362. // const sms = new SMS(this.ctx);
  363. // const tenderName = await sms.contentChange(tenderInfo.name);
  364. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  365. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  366. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  367. // sms.send(mobile_array, content);
  368. // }
  369. const users = this._.pull(this._.map(auditList, 'aid'), stageInfo.user_id);
  370. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL,
  371. smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, { qi: stageInfo.order, status: SmsAliConst.status.success });
  372. // 添加到消息推送表
  373. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId)
  374. await transaction.insert('zh_notice', { pid, type: pushType.stage, uid: this.ctx.stage.user_id, status: auditConst.status.checked, is_read: 0, content: noticeContent });
  375. }
  376. await transaction.commit();
  377. } catch (err) {
  378. await transaction.rollback();
  379. throw err;
  380. }
  381. }
  382. async _checkNo(pid, stageId, checkData, times) {
  383. const time = new Date();
  384. // 整理当前流程审核人状态更新
  385. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  386. if (!audit) {
  387. throw '审核数据错误';
  388. }
  389. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  390. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  391. const sqlParam = [this.tableName, stageId, times];
  392. const auditors = await this.db.query(sql, sqlParam);
  393. let order = 1;
  394. for (const a of auditors) {
  395. a.times = times + 1;
  396. a.order = order;
  397. a.status = auditConst.status.uncheck;
  398. order++;
  399. }
  400. const transaction = await this.db.beginTransaction();
  401. try {
  402. // 计算并合同支付最终数据
  403. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  404. this.ctx.stage.tp_history.push({
  405. times, order: audit.order,
  406. contract_tp: tpData.contract_tp,
  407. qc_tp: tpData.qc_tp,
  408. yf_tp: yfPay.tp,
  409. sf_tp: sfPay.tp,
  410. });
  411. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  412. // 同步 期信息
  413. await transaction.update(this.ctx.service.stage.tableName, {
  414. id: stageId, status: checkData.checkType,
  415. contract_tp: tpData.contract_tp,
  416. qc_tp: tpData.qc_tp,
  417. times: times + 1,
  418. yf_tp: yfPay.tp,
  419. sf_tp: sfPay.tp,
  420. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  421. cache_time_r: this.ctx.stage.cache_time_l,
  422. });
  423. // 拷贝新一次审核流程列表
  424. await transaction.insert(this.tableName, auditors);
  425. // 计算该审批人最终数据
  426. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  427. // 复制一份最新数据给原报
  428. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  429. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  430. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  431. // 添加短信通知-审批退回提醒功能
  432. // const mobile_array = [];
  433. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  434. const auditList = await this.getAuditors(stageId, stageInfo.times);
  435. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  436. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  437. // const smsType = JSON.parse(smsUser1.sms_type);
  438. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  439. // mobile_array.push(smsUser1.auth_mobile);
  440. // }
  441. // }
  442. // for (const user of auditList) {
  443. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  444. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  445. // const smsType = JSON.parse(smsUser.sms_type);
  446. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  447. // mobile_array.push(smsUser.auth_mobile);
  448. // }
  449. // }
  450. // }
  451. // if (mobile_array.length > 0) {
  452. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  453. // const sms = new SMS(this.ctx);
  454. // const tenderName = await sms.contentChange(tenderInfo.name);
  455. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  456. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  457. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  458. // sms.send(mobile_array, content);
  459. // }
  460. const users = this._.pull(this._.map(auditList, 'aid'), stageInfo.user_id);
  461. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL,
  462. smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, { qi: stageInfo.order, status: SmsAliConst.status.back });
  463. // 添加到消息推送表
  464. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId)
  465. await transaction.insert('zh_notice', { pid, type: pushType.stage, uid: this.ctx.stage.user_id, status: auditConst.status.checkNo, is_read: 0, content: noticeContent });
  466. await transaction.commit();
  467. } catch (err) {
  468. await transaction.rollback();
  469. throw err;
  470. }
  471. }
  472. async _checkNoPre(stageId, checkData, times) {
  473. const time = new Date();
  474. // 整理当前流程审核人状态更新
  475. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  476. if (!audit || audit.order <= 1) {
  477. throw '审核数据错误';
  478. }
  479. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  480. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  481. const auditors2 = await this.getAuditGroupByList(stageId, times);
  482. const auditorIndex = await auditors2.findIndex(function(item) {
  483. return item.aid === audit.aid;
  484. });
  485. const preAuditor = auditors2[auditorIndex - 1];
  486. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  487. const transaction = await this.db.beginTransaction();
  488. try {
  489. // 计算并合同支付最终数据
  490. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  491. this.ctx.stage.tp_history.push({
  492. times, order: audit.order,
  493. contract_tp: tpData.contract_tp,
  494. qc_tp: tpData.qc_tp,
  495. yf_tp: yfPay.tp,
  496. sf_tp: sfPay.tp,
  497. });
  498. // 同步 期信息
  499. await transaction.update(this.ctx.service.stage.tableName, {
  500. id: stageId,
  501. contract_tp: tpData.contract_tp,
  502. qc_tp: tpData.qc_tp,
  503. times,
  504. yf_tp: yfPay.tp,
  505. sf_tp: sfPay.tp,
  506. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  507. cache_time_r: this.ctx.stage.cache_time_l,
  508. });
  509. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  510. // 顺移气候审核人流程顺序
  511. this.initSqlBuilder();
  512. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  513. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  514. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  515. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  516. const data = await transaction.query(sql, sqlParam);
  517. // 上一审批人,当前审批人 再次添加至流程
  518. const newAuditors = [];
  519. newAuditors.push({
  520. tid: audit.tid, sid: audit.sid, aid: preAuditor.aid,
  521. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  522. begin_time: time,
  523. });
  524. newAuditors.push({
  525. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  526. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  527. });
  528. await transaction.insert(this.tableName, newAuditors);
  529. // 计算该审批人最终数据
  530. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  531. // 复制一份最新数据给下一人
  532. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  533. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  534. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  535. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  536. // 同步 期信息
  537. await transaction.update(this.ctx.service.stage.tableName, {
  538. id: stageId, status: checkData.checkType,
  539. cache_time_r: this.ctx.stage.cache_time_l,
  540. });
  541. // 添加短信通知-需要审批提醒功能
  542. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  543. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  544. // const smsType = JSON.parse(smsUser.sms_type);
  545. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  546. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  547. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  548. // const sms = new SMS(this.ctx);
  549. // const tenderName = await sms.contentChange(tenderInfo.name);
  550. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  551. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  552. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  553. // // const result = '';
  554. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  555. // sms.send(smsUser.auth_mobile, content);
  556. // }
  557. // }
  558. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  559. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  560. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL,
  561. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  562. await transaction.commit();
  563. } catch (err) {
  564. await transaction.rollback();
  565. throw err;
  566. }
  567. }
  568. /**
  569. * 审批
  570. * @param {Number} stageId - 标段id
  571. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  572. * @param {Number} times - 第几次审批
  573. * @return {Promise<void>}
  574. */
  575. async check(stageId, checkData, times = 1) {
  576. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  577. throw '提交数据错误';
  578. }
  579. // // 整理当前流程审核人状态更新
  580. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  581. // if (!audit) {
  582. // throw '审核数据错误';
  583. // }
  584. // const time = new Date();
  585. const pid = this.ctx.session.sessionProject.id;
  586. switch (checkData.checkType) {
  587. case auditConst.status.checked:
  588. await this._checked(pid, stageId, checkData, times);
  589. break;
  590. case auditConst.status.checkNo:
  591. await this._checkNo(pid, stageId, checkData, times);
  592. break;
  593. case auditConst.status.checkNoPre:
  594. await this._checkNoPre(stageId, checkData, times);
  595. break;
  596. default:
  597. throw '无效审批操作';
  598. }
  599. // const transaction = await this.db.beginTransaction();
  600. // try {
  601. // // 更新当前审核流程
  602. // await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  603. // if (checkData.checkType === auditConst.status.checked) { // 审批通过
  604. // const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  605. // // 无下一审核人表示,审核结束
  606. // if (nextAudit) {
  607. // // 计算该审批人最终数据
  608. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  609. // // 复制一份下一审核人数据
  610. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  611. // // 流程至下一审批人
  612. // await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  613. // // 同步 期信息
  614. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  615. // await transaction.update(this.ctx.service.stage.tableName, {
  616. // id: stageId, status: auditConst.status.checking,
  617. // contract_tp: tpData.contract_tp,
  618. // qc_tp: tpData.qc_tp,
  619. // });
  620. // } else {
  621. // // 本期结束
  622. // // 生成截止本期数据 final数据
  623. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  624. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  625. // // 计算并合同支付最终数据
  626. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  627. // // 同步 期信息
  628. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  629. // await transaction.update(this.ctx.service.stage.tableName, {
  630. // id: stageId, status: checkData.checkType,
  631. // contract_tp: tpData.contract_tp,
  632. // qc_tp: tpData.qc_tp,
  633. // });
  634. // }
  635. // } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  636. // // 同步 期信息
  637. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  638. // await transaction.update(this.ctx.service.stage.tableName, {
  639. // id: stageId, status: checkData.checkType,
  640. // contract_tp: tpData.contract_tp,
  641. // qc_tp: tpData.qc_tp,
  642. // times: times + 1,
  643. // });
  644. // // 拷贝新一次审核流程列表
  645. // // const auditors = await this.getAllDataByCondition({
  646. // // where: {sid: stageId, times: times},
  647. // // columns: ['tid', 'sid', 'aid', 'order']
  648. // // });
  649. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  650. // const sqlParam = [this.tableName, stageId, times];
  651. // const auditors = await this.db.query(sql, sqlParam);
  652. // let order = 1;
  653. // for (const a of auditors) {
  654. // a.times = times + 1;
  655. // a.order = order;
  656. // a.status = auditConst.status.uncheck;
  657. // order++;
  658. // }
  659. // await transaction.insert(this.tableName, auditors);
  660. // // 计算该审批人最终数据
  661. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  662. // // 复制一份最新数据给原报
  663. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  664. // } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  665. // // 同步 期信息
  666. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  667. // await transaction.update(this.ctx.service.stage.tableName, {
  668. // id: stageId, status: checkData.checkType,
  669. // contract_tp: tpData.contract_tp,
  670. // qc_tp: tpData.qc_tp,
  671. // });
  672. // // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  673. // if (audit.order > 1) {
  674. // // 顺移气候审核人流程顺序
  675. // this.initSqlBuilder();
  676. // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  677. // this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  678. // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  679. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  680. // const data = await transaction.query(sql, sqlParam);
  681. //
  682. // // 上一审批人,当前审批人 再次添加至流程
  683. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  684. // const newAuditors = [];
  685. // newAuditors.push({
  686. // tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  687. // times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  688. // begin_time: time,
  689. // });
  690. // newAuditors.push({
  691. // tid: audit.tid, sid: audit.sid, aid: audit.aid,
  692. // times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  693. // });
  694. // await transaction.insert(this.tableName, newAuditors);
  695. //
  696. // // 计算该审批人最终数据
  697. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  698. // // 复制一份最新数据给上一人
  699. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  700. // } else {
  701. // throw '审核数据错误';
  702. // }
  703. // } else {
  704. // throw '无效审批操作';
  705. // }
  706. //
  707. // await transaction.commit();
  708. // } catch (err) {
  709. // await transaction.rollback();
  710. // throw err;
  711. // }
  712. }
  713. /**
  714. * 审批
  715. * @param {Number} stageId - 标段id
  716. * @param {Number} times - 第几次审批
  717. * @return {Promise<void>}
  718. */
  719. async checkAgain(stageId, times = 1) {
  720. const time = new Date();
  721. // 整理当前流程审核人状态更新
  722. const audit = (await this.getAllDataByCondition({ where: { sid: stageId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  723. if (!audit || audit.order < 1) {
  724. throw '审核数据错误';
  725. }
  726. const transaction = await this.db.beginTransaction();
  727. try {
  728. // 当前审批人2次添加至流程中
  729. const newAuditors = [];
  730. newAuditors.push({
  731. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  732. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  733. begin_time: time, end_time: time, opinion: '',
  734. });
  735. newAuditors.push({
  736. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  737. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  738. begin_time: time,
  739. });
  740. await transaction.insert(this.tableName, newAuditors);
  741. // 复制一份最新数据给下一人
  742. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  743. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  744. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  745. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  746. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  747. // 本期结束
  748. // 生成截止本期数据 final数据
  749. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  750. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  751. // 同步 期信息
  752. await transaction.update(this.ctx.service.stage.tableName, {
  753. id: stageId, status: auditConst.status.checking,
  754. cache_time_r: this.ctx.stage.cache_time_l,
  755. });
  756. // 添加短信通知-需要审批提醒功能
  757. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  758. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  759. // const smsType = JSON.parse(smsUser.sms_type);
  760. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  761. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  762. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  763. // const sms = new SMS(this.ctx);
  764. // const tenderName = await sms.contentChange(tenderInfo.name);
  765. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  766. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  767. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  768. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  769. // sms.send(smsUser.auth_mobile, content);
  770. // }
  771. // }
  772. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  773. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  774. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL,
  775. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  776. await transaction.commit();
  777. } catch (err) {
  778. await transaction.rollback();
  779. throw err;
  780. }
  781. }
  782. /**
  783. * 获取审核人需要审核的期列表
  784. *
  785. * @param auditorId
  786. * @return {Promise<*>}
  787. */
  788. async getAuditStage(auditorId) {
  789. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  790. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  791. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  792. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  793. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  794. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  795. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  796. return await this.db.query(sql, sqlParam);
  797. }
  798. /**
  799. * 获取 某时间后 审批进度 更新的期
  800. * @param {Number} pid - 查询标段
  801. * @param {Number} uid - 查询人
  802. * @param {Date} time - 查询时间
  803. * @return {Promise<*>}
  804. */
  805. async getNoticeStage(pid, uid, time) {
  806. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  807. // ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  808. // ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  809. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  810. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  811. // ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  812. // ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  813. // ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  814. // ' WHERE sa.`end_time` > ? and t.`project_id` = ?' +
  815. // ' ORDER By sa.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  816. // ' ORDER By new_t.`end_time`';
  817. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.stage.tableName, this.tableName,
  818. // this.ctx.service.projectAccount.tableName, time, pid];
  819. // return await this.db.query(sql, sqlParam);
  820. let notice = await this.db.select('zh_notice', {
  821. where: { pid, type: pushType.stage, uid, is_read: 0 },
  822. });
  823. notice = notice.map(v => {
  824. const extra = JSON.parse(v.content)
  825. delete v.content
  826. return { ...v, ...extra }
  827. })
  828. return notice;
  829. }
  830. /**
  831. * 用于添加推送所需的content内容
  832. * @param {Number} pid 项目id
  833. * @param {Number} tid 台账id
  834. * @param {Number} sid 期id
  835. */
  836. async getNoticeContent(pid, tid, sid) {
  837. const noticeSql = 'SELECT * FROM (SELECT ' +
  838. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  839. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  840. ' LEFT JOIN ?? As s On s.`id` = ?' +
  841. ' LEFT JOIN ?? As pa ON t.`user_id` = pa.`id`' +
  842. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  843. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, pid];
  844. const content = await this.db.query(noticeSql, noticeSqlParam);
  845. return content.length ? JSON.stringify(content[0]) : '';
  846. }
  847. /**
  848. * 获取审核人流程列表
  849. *
  850. * @param auditorId
  851. * @return {Promise<*>}
  852. */
  853. async getAuditGroupByList(stageId, times) {
  854. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  855. 'FROM ?? AS la, ?? AS pa ' +
  856. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  857. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  858. return await this.db.query(sql, sqlParam);
  859. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  860. // const sqlParam = [this.tableName, stageId, times];
  861. // return await this.db.query(sql, sqlParam);
  862. }
  863. /**
  864. * 获取审核人流程列表
  865. *
  866. * @param auditorId
  867. * @return {Promise<*>}
  868. */
  869. async getAuditGroupByListWithOwner(stageId, times) {
  870. const result = await this.getAuditGroupByList(stageId, times);
  871. const sql = 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  872. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  873. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  874. ' ON s.user_id = pa.id' +
  875. ' WHERE s.id = ?';
  876. const sqlParam = [times, stageId, stageId];
  877. const user = await this.db.queryOne(sql, sqlParam);
  878. result.unshift(user);
  879. return result;
  880. }
  881. /**
  882. * 复制上一期的审批人列表给最新一期
  883. *
  884. * @param transaction - 新增一期的事务
  885. * @param {Object} preStage - 上一期
  886. * @param {Object} newStage - 最新一期
  887. * @return {Promise<*>}
  888. */
  889. async copyPreStageAuditors(transaction, preStage, newStage) {
  890. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  891. const newAuditors = [];
  892. for (const a of auditors) {
  893. const na = {
  894. tid: preStage.tid,
  895. sid: newStage.id,
  896. aid: a.aid,
  897. times: newStage.times,
  898. order: newAuditors.length + 1,
  899. status: auditConst.status.uncheck,
  900. };
  901. newAuditors.push(na);
  902. }
  903. const result = await transaction.insert(this.tableName, newAuditors);
  904. return result.effectRows = auditors.length;
  905. }
  906. /**
  907. * 移除审核人
  908. *
  909. * @param {Number} stageId - 期id
  910. * @param {Number} status - 期状态
  911. * @param {Number} status - 期次数
  912. * @return {Promise<boolean>}
  913. */
  914. async getAuditorByStatus(stageId, status, times = 1) {
  915. let auditor = null;
  916. let sql = '';
  917. let sqlParam = '';
  918. switch (status) {
  919. case auditConst.status.checking :
  920. case auditConst.status.checked :
  921. case auditConst.status.checkNoPre :
  922. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  923. 'FROM ?? AS la, ?? AS pa ' +
  924. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  925. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  926. auditor = await this.db.queryOne(sql, sqlParam);
  927. break;
  928. case auditConst.status.checkNo :
  929. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  930. 'FROM ?? AS la, ?? AS pa ' +
  931. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  932. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  933. auditor = await this.db.queryOne(sql, sqlParam);
  934. break;
  935. case auditConst.status.uncheck :
  936. default:break;
  937. }
  938. return auditor;
  939. }
  940. /**
  941. * 取某一期已批准审核信息(报表用)
  942. *
  943. * @param {Number} stageId - 期id
  944. * @param {Number} times - 期次数
  945. * @return {Promise<boolean>}
  946. */
  947. async getStageAudit(stageId, times = 1) {
  948. const sql = 'SELECT a1.aid, a1.begin_time, a1.end_time, a1.status, a1.opinion ' +
  949. 'FROM ?? AS a1 ' +
  950. 'WHERE a1.`sid` = ? and a1.`times` = ? ' +
  951. 'ORDER BY a1.order'
  952. ;
  953. const sqlParam = [this.tableName, stageId, times];
  954. const rst = await this.db.query(sql, sqlParam);
  955. return rst;
  956. }
  957. /**
  958. * 取待审批期列表(wap用)
  959. *
  960. * @param auditorId
  961. * @return {Promise<*>}
  962. */
  963. async getAuditStageByWap(auditorId) {
  964. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  965. // ' 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`, ' +
  966. ' s.*,' +
  967. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  968. ' ti.`deal_info` ' +
  969. ' FROM ?? AS sa, ?? AS s, ?? As t, ?? AS ti ' +
  970. ' WHERE sa.`aid` = ? and sa.`status` = ?' +
  971. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id` and ti.`tid` = t.`id`';
  972. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, this.ctx.service.tenderInfo.tableName, auditorId, auditConst.status.checking];
  973. return await this.db.query(sql, sqlParam);
  974. }
  975. /**
  976. * 删除 某期 某次 全审批流程
  977. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  978. * @param {Number} sid - 标段id
  979. * @param {Number} times - 第几次审批
  980. * @param transaction - 删除事务
  981. * @return {Promise<void>}
  982. */
  983. async _timesDelete(sid, times, transaction) {
  984. // 审批流程
  985. await transaction.delete(this.tableName, { sid: sid, times: times });
  986. await transaction.delete(this.ctx.service.pos.tableName, {add_stage: sid, add_times: times});
  987. await transaction.delete(this.ctx.service.stageBills.tableName, { sid: sid, times: times });
  988. await transaction.delete(this.ctx.service.stagePos.tableName, { sid: sid, times: times });
  989. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: sid, times: times });
  990. await transaction.delete(this.ctx.service.stageChange.tableName, { sid: sid, stimes: times });
  991. await transaction.delete(this.ctx.service.stagePay.tableName, { sid: sid, stimes: times });
  992. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  993. // 其他台账
  994. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  995. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  996. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  997. }
  998. /**
  999. * 删除本次审批流程
  1000. * @param {Number} stageId - 标段id
  1001. * @param {Number} times - 第几次审批
  1002. * @return {Promise<void>}
  1003. */
  1004. async timesDelete() {
  1005. const transaction = await this.db.beginTransaction();
  1006. try {
  1007. // 删除最新一次数据
  1008. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1009. // 审批退回,未重新上报时,需删除最新两次数据
  1010. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1011. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1012. if (isCheckNo) {
  1013. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1014. }
  1015. // 添加上一次审批人
  1016. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1017. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1018. const auditors = await this.db.query(sql, sqlParam);
  1019. let order = 1;
  1020. for (const a of auditors) {
  1021. a.times = nowTimes;
  1022. a.order = order;
  1023. a.status = auditConst.status.uncheck;
  1024. order++;
  1025. }
  1026. // 拷贝新一次审核流程列表
  1027. await transaction.insert(this.tableName, auditors);
  1028. // 计算缓存
  1029. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1030. // 计算并合同支付最终数据
  1031. const lastAudit = await this.getDataByCondition({sid: this.ctx.stage.id, times: nowTimes - 1, status: auditConst.status.checkNo});
  1032. if (!lastAudit) throw '审批数据错误';
  1033. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1034. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1035. const yfPay = stagePay.find(function (x) {
  1036. return x.ptype === payConst.payType.yf;
  1037. });
  1038. const sfPay = stagePay.find(function (x) {
  1039. return x.ptype === payConst.payType.sf;
  1040. });
  1041. // 同步 期信息
  1042. const time = new Date();
  1043. await transaction.update(this.ctx.service.stage.tableName, {
  1044. id: this.ctx.stage.id,
  1045. status: auditConst.status.checkNo,
  1046. contract_tp: tpData.contract_tp,
  1047. qc_tp: tpData.qc_tp,
  1048. times: nowTimes,
  1049. yf_tp: yfPay.tp,
  1050. sf_tp: sfPay.tp,
  1051. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1052. cache_time_l: time,
  1053. cache_time_r: time,
  1054. });
  1055. await transaction.commit();
  1056. } catch (err) {
  1057. await transaction.rollback();
  1058. throw err;
  1059. }
  1060. }
  1061. }
  1062. return StageAudit;
  1063. };