stage_audit.js 71 KB

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