stage_audit.js 63 KB

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