stage_audit.js 72 KB

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