stage_audit.js 66 KB

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