stage_audit.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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. // 添加推送
  274. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId)
  275. const records = [{ pid, type: pushType.stage, uid: this.ctx.stage.user_id, status: auditConst.status.checkNo, content: noticeContent }]
  276. ctx.stage.auditors.forEach( audit => {
  277. records.push({ pid, type: pushType.stage, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent })
  278. })
  279. await transaction.insert('zh_notice', records);
  280. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  281. // 计算并合同支付最终数据
  282. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  283. this.ctx.stage.tp_history.push({
  284. times, order: audit.order,
  285. contract_tp: tpData.contract_tp,
  286. qc_tp: tpData.qc_tp,
  287. yf_tp: yfPay.tp,
  288. sf_tp: sfPay.tp,
  289. });
  290. // 无下一审核人表示,审核结束
  291. if (nextAudit) {
  292. // 复制一份下一审核人数据
  293. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  294. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  295. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  296. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  297. // 流程至下一审批人
  298. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  299. // 同步 期信息
  300. await transaction.update(this.ctx.service.stage.tableName, {
  301. id: stageId, status: auditConst.status.checking,
  302. contract_tp: tpData.contract_tp,
  303. qc_tp: tpData.qc_tp,
  304. yf_tp: yfPay.tp,
  305. sf_tp: sfPay.tp,
  306. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  307. cache_time_r: this.ctx.stage.cache_time_l,
  308. });
  309. // 添加短信通知-需要审批提醒功能
  310. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  311. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  312. // const smsType = JSON.parse(smsUser.sms_type);
  313. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  314. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  315. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  316. // const sms = new SMS(this.ctx);
  317. // const tenderName = await sms.contentChange(tenderInfo.name);
  318. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  319. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  320. // // const result = '';
  321. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  322. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  323. // sms.send(smsUser.auth_mobile, content);
  324. // }
  325. // }
  326. const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  327. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  328. await this.ctx.helper.sendAliSms(nextAudit.aid, smsTypeConst.const.JL,
  329. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  330. } else {
  331. // 本期结束
  332. // 生成截止本期数据 final数据
  333. console.time('generatePre');
  334. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  335. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  336. console.timeEnd('generatePre');
  337. // 同步 期信息
  338. await transaction.update(this.ctx.service.stage.tableName, {
  339. id: stageId, status: checkData.checkType,
  340. contract_tp: tpData.contract_tp,
  341. qc_tp: tpData.qc_tp,
  342. yf_tp: yfPay.tp,
  343. sf_tp: sfPay.tp,
  344. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  345. cache_time_r: this.ctx.stage.cache_time_l,
  346. });
  347. // 添加短信通知-审批通过提醒功能
  348. // const mobile_array = [];
  349. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  350. const auditList = await this.getAuditors(stageId, stageInfo.times);
  351. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  352. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  353. // const smsType = JSON.parse(smsUser1.sms_type);
  354. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  355. // mobile_array.push(smsUser1.auth_mobile);
  356. // }
  357. // }
  358. // for (const user of auditList) {
  359. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  360. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  361. // const smsType = JSON.parse(smsUser.sms_type);
  362. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  363. // mobile_array.push(smsUser.auth_mobile);
  364. // }
  365. // }
  366. // }
  367. // if (mobile_array.length > 0) {
  368. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  369. // const sms = new SMS(this.ctx);
  370. // const tenderName = await sms.contentChange(tenderInfo.name);
  371. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  372. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  373. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  374. // sms.send(mobile_array, content);
  375. // }
  376. const users = this._.pull(this._.map(auditList, 'aid'), stageInfo.user_id);
  377. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL,
  378. smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, { qi: stageInfo.order, status: SmsAliConst.status.success });
  379. }
  380. await transaction.commit();
  381. } catch (err) {
  382. await transaction.rollback();
  383. throw err;
  384. }
  385. }
  386. async _checkNo(pid, stageId, checkData, times) {
  387. const time = new Date();
  388. // 整理当前流程审核人状态更新
  389. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  390. if (!audit) {
  391. throw '审核数据错误';
  392. }
  393. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  394. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  395. const sqlParam = [this.tableName, stageId, times];
  396. const auditors = await this.db.query(sql, sqlParam);
  397. let order = 1;
  398. for (const a of auditors) {
  399. a.times = times + 1;
  400. a.order = order;
  401. a.status = auditConst.status.uncheck;
  402. order++;
  403. }
  404. const transaction = await this.db.beginTransaction();
  405. try {
  406. // 添加推送
  407. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId)
  408. const records = [{ pid, type: pushType.stage, uid: this.ctx.stage.user_id, status: auditConst.status.checkNo, content: noticeContent }]
  409. auditors.forEach( audit => {
  410. records.push({ pid, type: pushType.stage, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent })
  411. })
  412. await transaction.insert('zh_notice', records);
  413. // 计算并合同支付最终数据
  414. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  415. this.ctx.stage.tp_history.push({
  416. times, order: audit.order,
  417. contract_tp: tpData.contract_tp,
  418. qc_tp: tpData.qc_tp,
  419. yf_tp: yfPay.tp,
  420. sf_tp: sfPay.tp,
  421. });
  422. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  423. // 同步 期信息
  424. await transaction.update(this.ctx.service.stage.tableName, {
  425. id: stageId, status: checkData.checkType,
  426. contract_tp: tpData.contract_tp,
  427. qc_tp: tpData.qc_tp,
  428. times: times + 1,
  429. yf_tp: yfPay.tp,
  430. sf_tp: sfPay.tp,
  431. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  432. cache_time_r: this.ctx.stage.cache_time_l,
  433. });
  434. // 拷贝新一次审核流程列表
  435. await transaction.insert(this.tableName, auditors);
  436. // 计算该审批人最终数据
  437. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  438. // 复制一份最新数据给原报
  439. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  440. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  441. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  442. // 添加短信通知-审批退回提醒功能
  443. // const mobile_array = [];
  444. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  445. const auditList = await this.getAuditors(stageId, stageInfo.times);
  446. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  447. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  448. // const smsType = JSON.parse(smsUser1.sms_type);
  449. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  450. // mobile_array.push(smsUser1.auth_mobile);
  451. // }
  452. // }
  453. // for (const user of auditList) {
  454. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  455. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  456. // const smsType = JSON.parse(smsUser.sms_type);
  457. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  458. // mobile_array.push(smsUser.auth_mobile);
  459. // }
  460. // }
  461. // }
  462. // if (mobile_array.length > 0) {
  463. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  464. // const sms = new SMS(this.ctx);
  465. // const tenderName = await sms.contentChange(tenderInfo.name);
  466. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  467. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  468. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  469. // sms.send(mobile_array, content);
  470. // }
  471. const users = this._.pull(this._.map(auditList, 'aid'), stageInfo.user_id);
  472. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL,
  473. smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, { qi: stageInfo.order, status: SmsAliConst.status.back });
  474. await transaction.commit();
  475. } catch (err) {
  476. await transaction.rollback();
  477. throw err;
  478. }
  479. }
  480. async _checkNoPre(stageId, checkData, times) {
  481. const time = new Date();
  482. // 整理当前流程审核人状态更新
  483. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  484. if (!audit || audit.order <= 1) {
  485. throw '审核数据错误';
  486. }
  487. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  488. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  489. const auditors2 = await this.getAuditGroupByList(stageId, times);
  490. const auditorIndex = await auditors2.findIndex(function(item) {
  491. return item.aid === audit.aid;
  492. });
  493. const preAuditor = auditors2[auditorIndex - 1];
  494. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  495. const transaction = await this.db.beginTransaction();
  496. try {
  497. // 添加推送
  498. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId)
  499. const records = [{ pid, type: pushType.stage, uid: this.ctx.stage.user_id, status: auditConst.status.checkNoPre, content: noticeContent }]
  500. auditors2.forEach( audit => {
  501. records.push({ pid, type: pushType.stage, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent })
  502. })
  503. await transaction.insert('zh_notice', records);
  504. // 计算并合同支付最终数据
  505. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  506. this.ctx.stage.tp_history.push({
  507. times, order: audit.order,
  508. contract_tp: tpData.contract_tp,
  509. qc_tp: tpData.qc_tp,
  510. yf_tp: yfPay.tp,
  511. sf_tp: sfPay.tp,
  512. });
  513. // 同步 期信息
  514. await transaction.update(this.ctx.service.stage.tableName, {
  515. id: stageId,
  516. contract_tp: tpData.contract_tp,
  517. qc_tp: tpData.qc_tp,
  518. times,
  519. yf_tp: yfPay.tp,
  520. sf_tp: sfPay.tp,
  521. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  522. cache_time_r: this.ctx.stage.cache_time_l,
  523. });
  524. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  525. // 顺移气候审核人流程顺序
  526. this.initSqlBuilder();
  527. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  528. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  529. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  530. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  531. const data = await transaction.query(sql, sqlParam);
  532. // 上一审批人,当前审批人 再次添加至流程
  533. const newAuditors = [];
  534. newAuditors.push({
  535. tid: audit.tid, sid: audit.sid, aid: preAuditor.aid,
  536. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  537. begin_time: time,
  538. });
  539. newAuditors.push({
  540. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  541. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  542. });
  543. await transaction.insert(this.tableName, newAuditors);
  544. // 计算该审批人最终数据
  545. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  546. // 复制一份最新数据给下一人
  547. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  548. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  549. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  550. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  551. // 同步 期信息
  552. await transaction.update(this.ctx.service.stage.tableName, {
  553. id: stageId, status: checkData.checkType,
  554. cache_time_r: this.ctx.stage.cache_time_l,
  555. });
  556. // 添加短信通知-需要审批提醒功能
  557. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  558. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  559. // const smsType = JSON.parse(smsUser.sms_type);
  560. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  561. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  562. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  563. // const sms = new SMS(this.ctx);
  564. // const tenderName = await sms.contentChange(tenderInfo.name);
  565. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  566. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  567. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  568. // // const result = '';
  569. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  570. // sms.send(smsUser.auth_mobile, content);
  571. // }
  572. // }
  573. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  574. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  575. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL,
  576. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  577. await transaction.commit();
  578. } catch (err) {
  579. await transaction.rollback();
  580. throw err;
  581. }
  582. }
  583. /**
  584. * 审批
  585. * @param {Number} stageId - 标段id
  586. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  587. * @param {Number} times - 第几次审批
  588. * @return {Promise<void>}
  589. */
  590. async check(stageId, checkData, times = 1) {
  591. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  592. throw '提交数据错误';
  593. }
  594. // // 整理当前流程审核人状态更新
  595. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  596. // if (!audit) {
  597. // throw '审核数据错误';
  598. // }
  599. // const time = new Date();
  600. const pid = this.ctx.session.sessionProject.id;
  601. switch (checkData.checkType) {
  602. case auditConst.status.checked:
  603. await this._checked(pid, stageId, checkData, times);
  604. break;
  605. case auditConst.status.checkNo:
  606. await this._checkNo(pid, stageId, checkData, times);
  607. break;
  608. case auditConst.status.checkNoPre:
  609. await this._checkNoPre(stageId, checkData, times);
  610. break;
  611. default:
  612. throw '无效审批操作';
  613. }
  614. // const transaction = await this.db.beginTransaction();
  615. // try {
  616. // // 更新当前审核流程
  617. // await transaction.update(this.tableName, {id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time});
  618. // if (checkData.checkType === auditConst.status.checked) { // 审批通过
  619. // const nextAudit = await this.getDataByCondition({sid: stageId, times: times, order: audit.order + 1});
  620. // // 无下一审核人表示,审核结束
  621. // if (nextAudit) {
  622. // // 计算该审批人最终数据
  623. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  624. // // 复制一份下一审核人数据
  625. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  626. // // 流程至下一审批人
  627. // await transaction.update(this.tableName, {id: nextAudit.id, status: auditConst.status.checking, begin_time: time});
  628. // // 同步 期信息
  629. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  630. // await transaction.update(this.ctx.service.stage.tableName, {
  631. // id: stageId, status: auditConst.status.checking,
  632. // contract_tp: tpData.contract_tp,
  633. // qc_tp: tpData.qc_tp,
  634. // });
  635. // } else {
  636. // // 本期结束
  637. // // 生成截止本期数据 final数据
  638. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  639. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  640. // // 计算并合同支付最终数据
  641. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  642. // // 同步 期信息
  643. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  644. // await transaction.update(this.ctx.service.stage.tableName, {
  645. // id: stageId, status: checkData.checkType,
  646. // contract_tp: tpData.contract_tp,
  647. // qc_tp: tpData.qc_tp,
  648. // });
  649. // }
  650. // } else if (checkData.checkType === auditConst.status.checkNo) { // 审批退回 原报, times+1
  651. // // 同步 期信息
  652. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  653. // await transaction.update(this.ctx.service.stage.tableName, {
  654. // id: stageId, status: checkData.checkType,
  655. // contract_tp: tpData.contract_tp,
  656. // qc_tp: tpData.qc_tp,
  657. // times: times + 1,
  658. // });
  659. // // 拷贝新一次审核流程列表
  660. // // const auditors = await this.getAllDataByCondition({
  661. // // where: {sid: stageId, times: times},
  662. // // columns: ['tid', 'sid', 'aid', 'order']
  663. // // });
  664. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  665. // const sqlParam = [this.tableName, stageId, times];
  666. // const auditors = await this.db.query(sql, sqlParam);
  667. // let order = 1;
  668. // for (const a of auditors) {
  669. // a.times = times + 1;
  670. // a.order = order;
  671. // a.status = auditConst.status.uncheck;
  672. // order++;
  673. // }
  674. // await transaction.insert(this.tableName, auditors);
  675. // // 计算该审批人最终数据
  676. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  677. // // 复制一份最新数据给原报
  678. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  679. // } else if (checkData.checkType === auditConst.status.checkNoPre) { // 审批退回 上一审批人
  680. // // 同步 期信息
  681. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  682. // await transaction.update(this.ctx.service.stage.tableName, {
  683. // id: stageId, status: checkData.checkType,
  684. // contract_tp: tpData.contract_tp,
  685. // qc_tp: tpData.qc_tp,
  686. // });
  687. // // 将当前审批人 与 上一审批人再次添加至流程,顺移其后审批人流程顺序
  688. // if (audit.order > 1) {
  689. // // 顺移气候审核人流程顺序
  690. // this.initSqlBuilder();
  691. // this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=', });
  692. // this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>', });
  693. // this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+', });
  694. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  695. // const data = await transaction.query(sql, sqlParam);
  696. //
  697. // // 上一审批人,当前审批人 再次添加至流程
  698. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  699. // const newAuditors = [];
  700. // newAuditors.push({
  701. // tid: preAuditor.tid, sid: preAuditor.sid, aid: preAuditor.aid,
  702. // times: preAuditor.times, order: preAuditor.order + 2, status: auditConst.status.checking,
  703. // begin_time: time,
  704. // });
  705. // newAuditors.push({
  706. // tid: audit.tid, sid: audit.sid, aid: audit.aid,
  707. // times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck
  708. // });
  709. // await transaction.insert(this.tableName, newAuditors);
  710. //
  711. // // 计算该审批人最终数据
  712. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  713. // // 复制一份最新数据给上一人
  714. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  715. // } else {
  716. // throw '审核数据错误';
  717. // }
  718. // } else {
  719. // throw '无效审批操作';
  720. // }
  721. //
  722. // await transaction.commit();
  723. // } catch (err) {
  724. // await transaction.rollback();
  725. // throw err;
  726. // }
  727. }
  728. /**
  729. * 审批
  730. * @param {Number} stageId - 标段id
  731. * @param {Number} times - 第几次审批
  732. * @return {Promise<void>}
  733. */
  734. async checkAgain(stageId, times = 1) {
  735. const time = new Date();
  736. // 整理当前流程审核人状态更新
  737. const audit = (await this.getAllDataByCondition({ where: { sid: stageId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  738. if (!audit || audit.order < 1) {
  739. throw '审核数据错误';
  740. }
  741. const transaction = await this.db.beginTransaction();
  742. try {
  743. // 当前审批人2次添加至流程中
  744. const newAuditors = [];
  745. newAuditors.push({
  746. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  747. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  748. begin_time: time, end_time: time, opinion: '',
  749. });
  750. newAuditors.push({
  751. tid: audit.tid, sid: audit.sid, aid: audit.aid,
  752. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  753. begin_time: time,
  754. });
  755. await transaction.insert(this.tableName, newAuditors);
  756. // 复制一份最新数据给下一人
  757. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  758. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  759. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  760. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  761. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  762. // 本期结束
  763. // 生成截止本期数据 final数据
  764. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  765. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  766. // 同步 期信息
  767. await transaction.update(this.ctx.service.stage.tableName, {
  768. id: stageId, status: auditConst.status.checking,
  769. cache_time_r: this.ctx.stage.cache_time_l,
  770. });
  771. // 添加短信通知-需要审批提醒功能
  772. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  773. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  774. // const smsType = JSON.parse(smsUser.sms_type);
  775. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  776. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  777. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  778. // const sms = new SMS(this.ctx);
  779. // const tenderName = await sms.contentChange(tenderInfo.name);
  780. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  781. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  782. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  783. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  784. // sms.send(smsUser.auth_mobile, content);
  785. // }
  786. // }
  787. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  788. const shenpiUrl = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  789. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL,
  790. smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, { qi: stageInfo.order, code: shenpiUrl });
  791. await transaction.commit();
  792. } catch (err) {
  793. await transaction.rollback();
  794. throw err;
  795. }
  796. }
  797. /**
  798. * 获取审核人需要审核的期列表
  799. *
  800. * @param auditorId
  801. * @return {Promise<*>}
  802. */
  803. async getAuditStage(auditorId) {
  804. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  805. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  806. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  807. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  808. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  809. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id`';
  810. 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];
  811. return await this.db.query(sql, sqlParam);
  812. }
  813. /**
  814. * 获取 某时间后 审批进度 更新的期
  815. * @param {Number} pid - 查询标段
  816. * @param {Number} uid - 查询人
  817. * @param {Date} time - 查询时间
  818. * @return {Promise<*>}
  819. */
  820. async getNoticeStage(pid, uid, time) {
  821. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  822. // ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  823. // ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  824. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  825. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  826. // ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  827. // ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  828. // ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  829. // ' WHERE sa.`end_time` > ? and t.`project_id` = ?' +
  830. // ' ORDER By sa.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  831. // ' ORDER By new_t.`end_time`';
  832. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.stage.tableName, this.tableName,
  833. // this.ctx.service.projectAccount.tableName, time, pid];
  834. // return await this.db.query(sql, sqlParam);
  835. let notice = await this.db.select('zh_notice', {
  836. where: { pid, type: pushType.stage, uid },
  837. orders: [['create_time', 'desc']],
  838. limit: 10, offset: 0
  839. });
  840. notice = notice.map(v => {
  841. const extra = JSON.parse(v.content)
  842. delete v.content
  843. return { ...v, ...extra }
  844. })
  845. return notice;
  846. }
  847. /**
  848. * 用于添加推送所需的content内容
  849. * @param {Number} pid 项目id
  850. * @param {Number} tid 台账id
  851. * @param {Number} sid 期id
  852. * @param {Number} uid 审核人id
  853. */
  854. async getNoticeContent(pid, tid, sid, uid) {
  855. const noticeSql = 'SELECT * FROM (SELECT ' +
  856. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  857. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  858. ' LEFT JOIN ?? As s On s.`id` = ?' +
  859. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  860. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  861. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  862. const content = await this.db.query(noticeSql, noticeSqlParam);
  863. return content.length ? JSON.stringify(content[0]) : '';
  864. }
  865. /**
  866. * 获取审核人流程列表
  867. *
  868. * @param auditorId
  869. * @return {Promise<*>}
  870. */
  871. async getAuditGroupByList(stageId, times) {
  872. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  873. 'FROM ?? AS la, ?? AS pa ' +
  874. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  875. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  876. return await this.db.query(sql, sqlParam);
  877. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  878. // const sqlParam = [this.tableName, stageId, times];
  879. // return await this.db.query(sql, sqlParam);
  880. }
  881. /**
  882. * 获取审核人流程列表
  883. *
  884. * @param auditorId
  885. * @return {Promise<*>}
  886. */
  887. async getAuditGroupByListWithOwner(stageId, times) {
  888. const result = await this.getAuditGroupByList(stageId, times);
  889. const sql = 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  890. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  891. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  892. ' ON s.user_id = pa.id' +
  893. ' WHERE s.id = ?';
  894. const sqlParam = [times, stageId, stageId];
  895. const user = await this.db.queryOne(sql, sqlParam);
  896. result.unshift(user);
  897. return result;
  898. }
  899. /**
  900. * 复制上一期的审批人列表给最新一期
  901. *
  902. * @param transaction - 新增一期的事务
  903. * @param {Object} preStage - 上一期
  904. * @param {Object} newStage - 最新一期
  905. * @return {Promise<*>}
  906. */
  907. async copyPreStageAuditors(transaction, preStage, newStage) {
  908. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  909. const newAuditors = [];
  910. for (const a of auditors) {
  911. const na = {
  912. tid: preStage.tid,
  913. sid: newStage.id,
  914. aid: a.aid,
  915. times: newStage.times,
  916. order: newAuditors.length + 1,
  917. status: auditConst.status.uncheck,
  918. };
  919. newAuditors.push(na);
  920. }
  921. const result = await transaction.insert(this.tableName, newAuditors);
  922. return result.effectRows = auditors.length;
  923. }
  924. /**
  925. * 移除审核人
  926. *
  927. * @param {Number} stageId - 期id
  928. * @param {Number} status - 期状态
  929. * @param {Number} status - 期次数
  930. * @return {Promise<boolean>}
  931. */
  932. async getAuditorByStatus(stageId, status, times = 1) {
  933. let auditor = null;
  934. let sql = '';
  935. let sqlParam = '';
  936. switch (status) {
  937. case auditConst.status.checking :
  938. case auditConst.status.checked :
  939. case auditConst.status.checkNoPre :
  940. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  941. 'FROM ?? AS la, ?? AS pa ' +
  942. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  943. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  944. auditor = await this.db.queryOne(sql, sqlParam);
  945. break;
  946. case auditConst.status.checkNo :
  947. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  948. 'FROM ?? AS la, ?? AS pa ' +
  949. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  950. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  951. auditor = await this.db.queryOne(sql, sqlParam);
  952. break;
  953. case auditConst.status.uncheck :
  954. default:break;
  955. }
  956. return auditor;
  957. }
  958. /**
  959. * 取某一期已批准审核信息(报表用)
  960. *
  961. * @param {Number} stageId - 期id
  962. * @param {Number} times - 期次数
  963. * @return {Promise<boolean>}
  964. */
  965. async getStageAudit(stageId, times = 1) {
  966. const sql = 'SELECT a1.aid, a1.begin_time, a1.end_time, a1.status, a1.opinion ' +
  967. 'FROM ?? AS a1 ' +
  968. 'WHERE a1.`sid` = ? and a1.`times` = ? ' +
  969. 'ORDER BY a1.order'
  970. ;
  971. const sqlParam = [this.tableName, stageId, times];
  972. const rst = await this.db.query(sql, sqlParam);
  973. return rst;
  974. }
  975. /**
  976. * 取待审批期列表(wap用)
  977. *
  978. * @param auditorId
  979. * @return {Promise<*>}
  980. */
  981. async getAuditStageByWap(auditorId) {
  982. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  983. // ' 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`, ' +
  984. ' s.*,' +
  985. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  986. ' ti.`deal_info` ' +
  987. ' FROM ?? AS sa, ?? AS s, ?? As t, ?? AS ti ' +
  988. ' WHERE sa.`aid` = ? and sa.`status` = ?' +
  989. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id` and ti.`tid` = t.`id`';
  990. const sqlParam = [this.tableName, this.ctx.service.stage.tableName, this.ctx.service.tender.tableName, this.ctx.service.tenderInfo.tableName, auditorId, auditConst.status.checking];
  991. return await this.db.query(sql, sqlParam);
  992. }
  993. /**
  994. * 删除 某期 某次 全审批流程
  995. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  996. * @param {Number} sid - 标段id
  997. * @param {Number} times - 第几次审批
  998. * @param transaction - 删除事务
  999. * @return {Promise<void>}
  1000. */
  1001. async _timesDelete(sid, times, transaction) {
  1002. // 审批流程
  1003. await transaction.delete(this.tableName, { sid: sid, times: times });
  1004. await transaction.delete(this.ctx.service.pos.tableName, {add_stage: sid, add_times: times});
  1005. await transaction.delete(this.ctx.service.stageBills.tableName, { sid: sid, times: times });
  1006. await transaction.delete(this.ctx.service.stagePos.tableName, { sid: sid, times: times });
  1007. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: sid, times: times });
  1008. await transaction.delete(this.ctx.service.stageChange.tableName, { sid: sid, stimes: times });
  1009. await transaction.delete(this.ctx.service.stagePay.tableName, { sid: sid, stimes: times });
  1010. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1011. // 其他台账
  1012. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1013. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1014. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1015. }
  1016. /**
  1017. * 删除本次审批流程
  1018. * @param {Number} stageId - 标段id
  1019. * @param {Number} times - 第几次审批
  1020. * @return {Promise<void>}
  1021. */
  1022. async timesDelete() {
  1023. const transaction = await this.db.beginTransaction();
  1024. try {
  1025. // 删除最新一次数据
  1026. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1027. // 审批退回,未重新上报时,需删除最新两次数据
  1028. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1029. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1030. if (isCheckNo) {
  1031. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1032. }
  1033. // 添加上一次审批人
  1034. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1035. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1036. const auditors = await this.db.query(sql, sqlParam);
  1037. let order = 1;
  1038. for (const a of auditors) {
  1039. a.times = nowTimes;
  1040. a.order = order;
  1041. a.status = auditConst.status.uncheck;
  1042. order++;
  1043. }
  1044. // 拷贝新一次审核流程列表
  1045. await transaction.insert(this.tableName, auditors);
  1046. // 计算缓存
  1047. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1048. // 计算并合同支付最终数据
  1049. const lastAudit = await this.getDataByCondition({sid: this.ctx.stage.id, times: nowTimes - 1, status: auditConst.status.checkNo});
  1050. if (!lastAudit) throw '审批数据错误';
  1051. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1052. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1053. const yfPay = stagePay.find(function (x) {
  1054. return x.ptype === payConst.payType.yf;
  1055. });
  1056. const sfPay = stagePay.find(function (x) {
  1057. return x.ptype === payConst.payType.sf;
  1058. });
  1059. // 同步 期信息
  1060. const time = new Date();
  1061. await transaction.update(this.ctx.service.stage.tableName, {
  1062. id: this.ctx.stage.id,
  1063. status: auditConst.status.checkNo,
  1064. contract_tp: tpData.contract_tp,
  1065. qc_tp: tpData.qc_tp,
  1066. times: nowTimes,
  1067. yf_tp: yfPay.tp,
  1068. sf_tp: sfPay.tp,
  1069. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1070. cache_time_l: time,
  1071. cache_time_r: time,
  1072. });
  1073. await transaction.commit();
  1074. } catch (err) {
  1075. await transaction.rollback();
  1076. throw err;
  1077. }
  1078. }
  1079. }
  1080. return StageAudit;
  1081. };