stage_audit.js 60 KB

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