stage_audit.js 76 KB

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