stage_audit.js 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  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. let his_id;
  447. if (this.ctx.tender.measureType === measureType.tz.value) {
  448. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
  449. his_id = ledgerHis.id;
  450. } else {
  451. const sapCount = await this.ctx.service.pos.count({ add_stage: this.ctx.stage.id });
  452. if (sapCount > 0) {
  453. his_id = await this.ctx.service.ledgerHistory.backupStageLedgerHistory(this.ctx.stage);
  454. } else {
  455. const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
  456. his_id = ledgerHis.id;
  457. }
  458. }
  459. // 本期结束
  460. // 生成截止本期数据 final数据
  461. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  462. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  463. await this.ctx.service.stageChangeFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  464. // 同步 期信息
  465. await transaction.update(this.ctx.service.stage.tableName, {
  466. id: stageId,
  467. status: checkData.checkType,
  468. contract_tp: tpData.contract_tp,
  469. qc_tp: tpData.qc_tp,
  470. yf_tp: yfPay.tp,
  471. sf_tp: sfPay.tp,
  472. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  473. cache_time_r: this.ctx.stage.cache_time_l,
  474. his_id,
  475. });
  476. // 添加短信通知-审批通过提醒功能
  477. // const mobile_array = [];
  478. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  479. const auditList = await this.getAuditors(stageId, stageInfo.times);
  480. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  481. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  482. // const smsType = JSON.parse(smsUser1.sms_type);
  483. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  484. // mobile_array.push(smsUser1.auth_mobile);
  485. // }
  486. // }
  487. // for (const user of auditList) {
  488. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  489. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  490. // const smsType = JSON.parse(smsUser.sms_type);
  491. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  492. // mobile_array.push(smsUser.auth_mobile);
  493. // }
  494. // }
  495. // }
  496. // if (mobile_array.length > 0) {
  497. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  498. // const sms = new SMS(this.ctx);
  499. // const tenderName = await sms.contentChange(tenderInfo.name);
  500. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  501. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  502. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  503. // sms.send(mobile_array, content);
  504. // }
  505. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  506. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  507. qi: stageInfo.order,
  508. status: SmsAliConst.status.success,
  509. });
  510. // 微信模板通知
  511. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  512. const wechatData = {
  513. wap_url: shenpiUrl,
  514. qi: stageInfo.order,
  515. status: wxConst.status.success,
  516. tips: wxConst.tips.success,
  517. code: this.ctx.session.sessionProject.code,
  518. };
  519. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  520. }
  521. await transaction.commit();
  522. } catch (err) {
  523. await transaction.rollback();
  524. throw err;
  525. }
  526. }
  527. async _checkNo(pid, stageId, checkData, times) {
  528. const time = new Date();
  529. // 整理当前流程审核人状态更新
  530. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  531. if (!audit) {
  532. throw '审核数据错误';
  533. }
  534. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  535. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  536. const sqlParam = [this.tableName, stageId, times];
  537. const auditors = await this.db.query(sql, sqlParam);
  538. let order = 1;
  539. for (const a of auditors) {
  540. a.times = times + 1;
  541. a.order = order;
  542. a.status = auditConst.status.uncheck;
  543. order++;
  544. }
  545. const transaction = await this.db.beginTransaction();
  546. try {
  547. await this._updateTender(transaction);
  548. // 添加推送
  549. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid);
  550. const records = [
  551. {
  552. pid,
  553. type: pushType.stage,
  554. uid: this.ctx.stage.user_id,
  555. status: auditConst.status.checkNo,
  556. content: noticeContent,
  557. },
  558. ];
  559. auditors.forEach(audit => {
  560. records.push({
  561. pid,
  562. type: pushType.stage,
  563. uid: audit.aid,
  564. status: auditConst.status.checkNo,
  565. content: noticeContent,
  566. });
  567. });
  568. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  569. // 计算并合同支付最终数据
  570. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  571. this.ctx.stage.tp_history.push({
  572. times,
  573. order: audit.order,
  574. contract_tp: tpData.contract_tp,
  575. qc_tp: tpData.qc_tp,
  576. yf_tp: yfPay.tp,
  577. sf_tp: sfPay.tp,
  578. });
  579. await transaction.update(this.tableName, {
  580. id: audit.id,
  581. status: checkData.checkType,
  582. opinion: checkData.opinion,
  583. end_time: time,
  584. });
  585. // 同步 期信息
  586. await transaction.update(this.ctx.service.stage.tableName, {
  587. id: stageId,
  588. status: checkData.checkType,
  589. contract_tp: tpData.contract_tp,
  590. qc_tp: tpData.qc_tp,
  591. times: times + 1,
  592. yf_tp: yfPay.tp,
  593. sf_tp: sfPay.tp,
  594. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  595. cache_time_r: this.ctx.stage.cache_time_l,
  596. });
  597. // 拷贝新一次审核流程列表
  598. await transaction.insert(this.tableName, auditors);
  599. // 计算该审批人最终数据
  600. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  601. // 复制一份最新数据给原报
  602. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  603. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  604. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  605. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  606. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  607. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  608. // 添加短信通知-审批退回提醒功能
  609. // const mobile_array = [];
  610. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  611. const auditList = await this.getAuditors(stageId, stageInfo.times);
  612. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  613. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  614. // const smsType = JSON.parse(smsUser1.sms_type);
  615. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  616. // mobile_array.push(smsUser1.auth_mobile);
  617. // }
  618. // }
  619. // for (const user of auditList) {
  620. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  621. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  622. // const smsType = JSON.parse(smsUser.sms_type);
  623. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  624. // mobile_array.push(smsUser.auth_mobile);
  625. // }
  626. // }
  627. // }
  628. // if (mobile_array.length > 0) {
  629. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  630. // const sms = new SMS(this.ctx);
  631. // const tenderName = await sms.contentChange(tenderInfo.name);
  632. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  633. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  634. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  635. // sms.send(mobile_array, content);
  636. // }
  637. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  638. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  639. qi: stageInfo.order,
  640. status: SmsAliConst.status.back,
  641. });
  642. // 微信模板通知
  643. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  644. const wechatData = {
  645. wap_url: shenpiUrl,
  646. qi: stageInfo.order,
  647. status: wxConst.status.back,
  648. tips: wxConst.tips.back,
  649. code: this.ctx.session.sessionProject.code,
  650. };
  651. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  652. await transaction.commit();
  653. } catch (err) {
  654. await transaction.rollback();
  655. throw err;
  656. }
  657. }
  658. async _checkNoPre(pid, stageId, checkData, times) {
  659. const time = new Date();
  660. // 整理当前流程审核人状态更新
  661. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  662. if (!audit || audit.order <= 1) {
  663. throw '审核数据错误';
  664. }
  665. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  666. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  667. const auditors2 = await this.getAuditGroupByList(stageId, times);
  668. const auditorIndex = await auditors2.findIndex(function(item) {
  669. return item.aid === audit.aid;
  670. });
  671. const preAuditor = auditors2[auditorIndex - 1];
  672. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  673. const transaction = await this.db.beginTransaction();
  674. try {
  675. await this._updateTender(transaction);
  676. // 添加推送
  677. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid);
  678. const records = [
  679. {
  680. pid,
  681. type: pushType.stage,
  682. uid: this.ctx.stage.user_id,
  683. status: auditConst.status.checkNoPre,
  684. content: noticeContent,
  685. },
  686. ];
  687. auditors2.forEach(audit => {
  688. records.push({
  689. pid,
  690. type: pushType.stage,
  691. uid: audit.aid,
  692. status: auditConst.status.checkNoPre,
  693. content: noticeContent,
  694. });
  695. });
  696. await transaction.insert('zh_notice', records);
  697. // 计算并合同支付最终数据
  698. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  699. this.ctx.stage.tp_history.push({
  700. times,
  701. order: audit.order,
  702. contract_tp: tpData.contract_tp,
  703. qc_tp: tpData.qc_tp,
  704. yf_tp: yfPay.tp,
  705. sf_tp: sfPay.tp,
  706. });
  707. // 同步 期信息
  708. await transaction.update(this.ctx.service.stage.tableName, {
  709. id: stageId,
  710. contract_tp: tpData.contract_tp,
  711. qc_tp: tpData.qc_tp,
  712. times,
  713. yf_tp: yfPay.tp,
  714. sf_tp: sfPay.tp,
  715. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  716. cache_time_r: this.ctx.stage.cache_time_l,
  717. });
  718. await transaction.update(this.tableName, {
  719. id: audit.id,
  720. status: checkData.checkType,
  721. opinion: checkData.opinion,
  722. end_time: time,
  723. });
  724. // 顺移气候审核人流程顺序
  725. this.initSqlBuilder();
  726. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  727. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  728. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  729. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  730. const data = await transaction.query(sql, sqlParam);
  731. // 上一审批人,当前审批人 再次添加至流程
  732. const newAuditors = [];
  733. newAuditors.push({
  734. tid: audit.tid,
  735. sid: audit.sid,
  736. aid: preAuditor.aid,
  737. times: audit.times,
  738. order: audit.order + 1,
  739. status: auditConst.status.checking,
  740. begin_time: time,
  741. });
  742. newAuditors.push({
  743. tid: audit.tid,
  744. sid: audit.sid,
  745. aid: audit.aid,
  746. times: audit.times,
  747. order: audit.order + 2,
  748. status: auditConst.status.uncheck,
  749. });
  750. await transaction.insert(this.tableName, newAuditors);
  751. // 计算该审批人最终数据
  752. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  753. // 复制一份最新数据给下一人
  754. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  755. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  756. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  757. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  758. await this.ctx.service.stageSafeProd.updateHistory(this.ctx.stage, transaction);
  759. await this.ctx.service.stageTempLand.updateHistory(this.ctx.stage, transaction);
  760. // 多人协同数据确认删除本人和上一个审批人确认状态
  761. await this.ctx.service.cooperationConfirm.delBycheckNoPre(audit.aid, this.ctx.stage, transaction);
  762. await this.ctx.service.cooperationConfirm.delBycheckNoPre(preAuditor.aid, this.ctx.stage, transaction);
  763. // 同步 期信息
  764. await transaction.update(this.ctx.service.stage.tableName, {
  765. id: stageId,
  766. status: checkData.checkType,
  767. cache_time_r: this.ctx.stage.cache_time_l,
  768. });
  769. // 添加短信通知-需要审批提醒功能
  770. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  771. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  772. // const smsType = JSON.parse(smsUser.sms_type);
  773. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  774. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  775. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  776. // const sms = new SMS(this.ctx);
  777. // const tenderName = await sms.contentChange(tenderInfo.name);
  778. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  779. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  780. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  781. // // const result = '';
  782. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  783. // sms.send(smsUser.auth_mobile, content);
  784. // }
  785. // }
  786. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  787. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  788. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  789. qi: stageInfo.order,
  790. code: shenpiUrl,
  791. });
  792. // 微信模板通知
  793. const wechatData = {
  794. wap_url: shenpiUrl,
  795. qi: stageInfo.order,
  796. status: wxConst.status.check,
  797. tips: wxConst.tips.check,
  798. code: this.ctx.session.sessionProject.code,
  799. };
  800. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  801. await transaction.commit();
  802. } catch (err) {
  803. await transaction.rollback();
  804. throw err;
  805. }
  806. }
  807. /**
  808. * 审批
  809. * @param {Number} stageId - 标段id
  810. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  811. * @param {Number} times - 第几次审批
  812. * @return {Promise<void>}
  813. */
  814. async check(stageId, checkData, times = 1) {
  815. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  816. throw '提交数据错误';
  817. }
  818. // // 整理当前流程审核人状态更新
  819. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  820. // if (!audit) {
  821. // throw '审核数据错误';
  822. // }
  823. // const time = new Date();
  824. const pid = this.ctx.session.sessionProject.id;
  825. switch (checkData.checkType) {
  826. case auditConst.status.checked:
  827. await this._checked(pid, stageId, checkData, times);
  828. break;
  829. case auditConst.status.checkNo:
  830. await this._checkNo(pid, stageId, checkData, times);
  831. break;
  832. case auditConst.status.checkNoPre:
  833. await this._checkNoPre(pid, stageId, checkData, times);
  834. break;
  835. default:
  836. throw '无效审批操作';
  837. }
  838. // 通知发送 - 第三方更新
  839. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  840. const base_data = {
  841. tid: this.ctx.tender.id,
  842. sid: stageId,
  843. op: 'update',
  844. };
  845. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  846. base_data.op = 'update';
  847. base_data.sid = -1;
  848. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  849. }
  850. }
  851. /**
  852. * 审批
  853. * @param {Number} stageId - 标段id
  854. * @param {Number} times - 第几次审批
  855. * @return {Promise<void>}
  856. */
  857. async checkAgain(stageId, times = 1) {
  858. const time = new Date();
  859. // 整理当前流程审核人状态更新
  860. const audit = (
  861. await this.getAllDataByCondition({
  862. where: { sid: stageId, times },
  863. orders: [['order', 'desc']],
  864. limit: 1,
  865. offset: 0,
  866. })
  867. )[0];
  868. if (!audit || audit.order < 1) {
  869. throw '审核数据错误';
  870. }
  871. const transaction = await this.db.beginTransaction();
  872. try {
  873. // 当前审批人2次添加至流程中
  874. const newAuditors = [];
  875. newAuditors.push({
  876. tid: audit.tid,
  877. sid: audit.sid,
  878. aid: audit.aid,
  879. times: audit.times,
  880. order: audit.order + 1,
  881. status: auditConst.status.checkAgain,
  882. begin_time: time,
  883. end_time: time,
  884. opinion: '',
  885. });
  886. newAuditors.push({
  887. tid: audit.tid,
  888. sid: audit.sid,
  889. aid: audit.aid,
  890. times: audit.times,
  891. order: audit.order + 2,
  892. status: auditConst.status.checking,
  893. begin_time: time,
  894. });
  895. await transaction.insert(this.tableName, newAuditors);
  896. // 复制一份最新数据给下一人
  897. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  898. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  899. await this.ctx.service.stageJgcl.updateHistory4CheckAgain(this.ctx.stage, transaction);
  900. await this.ctx.service.stageBonus.updateHistory4CheckAgain(this.ctx.stage, transaction);
  901. await this.ctx.service.stageOther.updateHistory4CheckAgain(this.ctx.stage, transaction);
  902. await this.ctx.service.stageSafeProd.updateHistory4CheckAgain(this.ctx.stage, transaction);
  903. await this.ctx.service.stageTempLand.updateHistory4CheckAgain(this.ctx.stage, transaction);
  904. // 本期结束
  905. // 生成截止本期数据 final数据
  906. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  907. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  908. await this.ctx.service.stageChangeFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  909. // 同步 期信息
  910. const his = this.ctx.stage.tp_history.find(x => { return x.times === times && x.order === audit.order });
  911. this.ctx.stage.tp_history.push({
  912. times,
  913. order: audit.order + 1,
  914. contract_tp: his.contract_tp,
  915. qc_tp: his.qc_tp,
  916. yf_tp: his.tp,
  917. sf_tp: his.tp,
  918. });
  919. await transaction.update(this.ctx.service.stage.tableName, {
  920. id: stageId,
  921. status: auditConst.status.checking,
  922. cache_time_r: this.ctx.stage.cache_time_l,
  923. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  924. });
  925. // 添加短信通知-需要审批提醒功能
  926. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  927. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  928. // const smsType = JSON.parse(smsUser.sms_type);
  929. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  930. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  931. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  932. // const sms = new SMS(this.ctx);
  933. // const tenderName = await sms.contentChange(tenderInfo.name);
  934. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  935. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  936. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  937. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  938. // sms.send(smsUser.auth_mobile, content);
  939. // }
  940. // }
  941. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  942. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  943. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  944. qi: stageInfo.order,
  945. code: shenpiUrl,
  946. });
  947. // 微信模板通知
  948. const wechatData = {
  949. wap_url: shenpiUrl,
  950. qi: stageInfo.order,
  951. status: wxConst.status.check,
  952. tips: wxConst.tips.check,
  953. code: this.ctx.session.sessionProject.code,
  954. };
  955. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  956. await transaction.commit();
  957. // 通知发送 - 第三方更新
  958. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  959. const base_data = {
  960. tid: this.ctx.tender.id,
  961. sid: stageId,
  962. op: 'update',
  963. };
  964. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  965. base_data.op = 'update';
  966. base_data.sid = -1;
  967. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  968. }
  969. } catch (err) {
  970. await transaction.rollback();
  971. throw err;
  972. }
  973. }
  974. /**
  975. * 获取审核人需要审核的期列表
  976. *
  977. * @param auditorId
  978. * @return {Promise<*>}
  979. */
  980. async getAuditStage(auditorId) {
  981. const sql =
  982. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  983. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  984. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  985. ' FROM ?? AS sa ' +
  986. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  987. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  988. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  989. ' ORDER BY sa.`begin_time` DESC';
  990. const sqlParam = [
  991. this.tableName,
  992. this.ctx.service.stage.tableName,
  993. this.ctx.service.tender.tableName,
  994. auditorId,
  995. auditConst.status.checking,
  996. auditorId,
  997. auditConst.status.checkNo,
  998. auditConst.status.checkNo,
  999. ];
  1000. return await this.db.query(sql, sqlParam);
  1001. }
  1002. /**
  1003. * 获取 某时间后 审批进度 更新的期
  1004. * @param {Number} pid - 查询标段
  1005. * @param {Number} uid - 查询人
  1006. * @param {Date} time - 查询时间
  1007. * @return {Promise<*>}
  1008. */
  1009. async getNoticeStage(pid, uid, time) {
  1010. let notice = await this.db.select('zh_notice', {
  1011. where: { pid, type: pushType.stage, uid },
  1012. orders: [['create_time', 'desc']],
  1013. limit: 10,
  1014. offset: 0,
  1015. });
  1016. notice = notice.map(v => {
  1017. const extra = JSON.parse(v.content);
  1018. delete v.content;
  1019. return { ...v, ...extra };
  1020. });
  1021. return notice;
  1022. }
  1023. /**
  1024. * 用于添加推送所需的content内容
  1025. * @param {Number} pid 项目id
  1026. * @param {Number} tid 台账id
  1027. * @param {Number} sid 期id
  1028. * @param {Number} uid 审核人id
  1029. */
  1030. async getNoticeContent(pid, tid, sid, uid) {
  1031. const noticeSql =
  1032. 'SELECT * FROM (SELECT ' +
  1033. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1034. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1035. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1036. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1037. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1038. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1039. const content = await this.db.query(noticeSql, noticeSqlParam);
  1040. return content.length ? JSON.stringify(content[0]) : '';
  1041. }
  1042. /**
  1043. * 获取审核人流程列表
  1044. *
  1045. * @param auditorId
  1046. * @return {Promise<*>}
  1047. */
  1048. async getAuditGroupByList(stageId, times) {
  1049. const sql =
  1050. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  1051. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1052. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1053. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1054. return await this.db.query(sql, sqlParam);
  1055. }
  1056. /**
  1057. * 获取审核人流程列表
  1058. *
  1059. * @param auditorId
  1060. * @return {Promise<*>}
  1061. */
  1062. async getAuditGroupByListWithOwner(stageId, times) {
  1063. const result = await this.getAuditGroupByList(stageId, times);
  1064. const sql =
  1065. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1066. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1067. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1068. ' ON s.user_id = pa.id' +
  1069. ' WHERE s.id = ?';
  1070. const sqlParam = [times, stageId, stageId];
  1071. const user = await this.db.queryOne(sql, sqlParam);
  1072. result.unshift(user);
  1073. return result;
  1074. }
  1075. /**
  1076. * 复制上一期的审批人列表给最新一期
  1077. *
  1078. * @param transaction - 新增一期的事务
  1079. * @param {Object} preStage - 上一期
  1080. * @param {Object} newStage - 最新一期
  1081. * @return {Promise<*>}
  1082. */
  1083. async copyPreStageAuditors(transaction, preStage, newStage) {
  1084. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1085. const newAuditors = [];
  1086. for (const a of auditors) {
  1087. const na = {
  1088. tid: preStage.tid,
  1089. sid: newStage.id,
  1090. aid: a.aid,
  1091. times: newStage.times,
  1092. order: newAuditors.length + 1,
  1093. status: auditConst.status.uncheck,
  1094. };
  1095. newAuditors.push(na);
  1096. }
  1097. const result = await transaction.insert(this.tableName, newAuditors);
  1098. return (result.effectRows = auditors.length);
  1099. }
  1100. /**
  1101. * 移除审核人
  1102. *
  1103. * @param {Number} stageId - 期id
  1104. * @param {Number} status - 期状态
  1105. * @param {Number} status - 期次数
  1106. * @return {Promise<boolean>}
  1107. */
  1108. async getAuditorByStatus(stageId, status, times = 1) {
  1109. let auditor = null;
  1110. let sql = '';
  1111. let sqlParam = '';
  1112. switch (status) {
  1113. case auditConst.status.checking:
  1114. case auditConst.status.checked:
  1115. case auditConst.status.checkNoPre:
  1116. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1117. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1118. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1119. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1120. auditor = await this.db.queryOne(sql, sqlParam);
  1121. break;
  1122. case auditConst.status.checkNo:
  1123. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1124. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1125. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1126. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1127. auditor = await this.db.queryOne(sql, sqlParam);
  1128. break;
  1129. case auditConst.status.uncheck:
  1130. default:
  1131. break;
  1132. }
  1133. return auditor;
  1134. }
  1135. /**
  1136. * 取某一期已批准审核信息(报表用)
  1137. *
  1138. * @param {Number} stageId - 期id
  1139. * @param {Number} times - 期次数
  1140. * @return {Promise<boolean>}
  1141. */
  1142. async getStageAudit(stageId, times = 1) {
  1143. 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';
  1144. const sqlParam = [this.tableName, stageId, times];
  1145. const rst = await this.db.query(sql, sqlParam);
  1146. return rst;
  1147. }
  1148. /**
  1149. * 取待审批期列表(wap用)
  1150. *
  1151. * @param auditorId
  1152. * @return {Promise<*>}
  1153. */
  1154. async getAuditStageByWap(auditorId) {
  1155. const sql =
  1156. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1157. // ' 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`, ' +
  1158. ' s.*,' +
  1159. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1160. ' ti.`deal_info` ' +
  1161. ' FROM ?? AS sa' +
  1162. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1163. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1164. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1165. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1166. const sqlParam = [
  1167. this.tableName,
  1168. this.ctx.service.stage.tableName,
  1169. this.ctx.service.tender.tableName,
  1170. this.ctx.service.tenderInfo.tableName,
  1171. auditorId,
  1172. auditConst.status.checking,
  1173. ];
  1174. return await this.db.query(sql, sqlParam);
  1175. }
  1176. /**
  1177. * 删除 某期 某次 全审批流程
  1178. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1179. * @param {Number} sid - 标段id
  1180. * @param {Number} times - 第几次审批
  1181. * @param transaction - 删除事务
  1182. * @return {Promise<void>}
  1183. */
  1184. async _timesDelete(sid, times, transaction) {
  1185. // 审批流程
  1186. await transaction.delete(this.tableName, { sid, times });
  1187. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1188. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1189. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1190. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1191. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1192. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1193. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1194. // 其他台账
  1195. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1196. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1197. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1198. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1199. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1200. }
  1201. /**
  1202. * 删除本次审批流程
  1203. * @param {Number} stageId - 标段id
  1204. * @param {Number} times - 第几次审批
  1205. * @return {Promise<void>}
  1206. */
  1207. async timesDelete() {
  1208. const transaction = await this.db.beginTransaction();
  1209. try {
  1210. // 删除最新一次数据
  1211. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1212. // 审批退回,未重新上报时,需删除最新两次数据
  1213. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1214. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1215. if (isCheckNo) {
  1216. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1217. }
  1218. // 添加上一次审批人
  1219. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1220. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1221. const auditors = await this.db.query(sql, sqlParam);
  1222. let order = 1;
  1223. for (const a of auditors) {
  1224. a.times = nowTimes;
  1225. a.order = order;
  1226. a.status = auditConst.status.uncheck;
  1227. order++;
  1228. }
  1229. await this._updateTender(transaction);
  1230. // 拷贝新一次审核流程列表
  1231. await transaction.insert(this.tableName, auditors);
  1232. // 计算缓存
  1233. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1234. // 计算并合同支付最终数据
  1235. const lastAudit = await this.getDataByCondition({
  1236. sid: this.ctx.stage.id,
  1237. times: nowTimes - 1,
  1238. status: auditConst.status.checkNo,
  1239. });
  1240. if (!lastAudit) throw '审批数据错误';
  1241. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1242. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1243. const yfPay = stagePay.find(function(x) {
  1244. return x.ptype === payConst.payType.yf;
  1245. });
  1246. const sfPay = stagePay.find(function(x) {
  1247. return x.ptype === payConst.payType.sf;
  1248. });
  1249. // 同步 期信息
  1250. const time = new Date();
  1251. await transaction.update(this.ctx.service.stage.tableName, {
  1252. id: this.ctx.stage.id,
  1253. status: auditConst.status.checkNo,
  1254. contract_tp: tpData.contract_tp,
  1255. qc_tp: tpData.qc_tp,
  1256. times: nowTimes,
  1257. yf_tp: yfPay ? yfPay.tp : null,
  1258. sf_tp: sfPay ? sfPay.tp : null,
  1259. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1260. cache_time_l: time,
  1261. cache_time_r: time,
  1262. });
  1263. await transaction.commit();
  1264. // 通知发送 - 第三方更新
  1265. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1266. const base_data = {
  1267. tid: this.ctx.tender.id,
  1268. sid: this.ctx.stage.id,
  1269. op: 'update',
  1270. };
  1271. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1272. base_data.op = 'update';
  1273. base_data.sid = -1;
  1274. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1275. }
  1276. } catch (err) {
  1277. await transaction.rollback();
  1278. throw err;
  1279. }
  1280. }
  1281. // 固定审批流-更新
  1282. async updateNewAuditList(stage, newIdList) {
  1283. const transaction = await this.db.beginTransaction();
  1284. try {
  1285. // 先删除旧的审批流,再添加新的
  1286. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1287. const newAuditors = [];
  1288. let order = 1;
  1289. for (const aid of newIdList) {
  1290. newAuditors.push({
  1291. tid: stage.tid, sid: stage.id, aid,
  1292. times: stage.times, order, status: auditConst.status.uncheck,
  1293. });
  1294. order++;
  1295. }
  1296. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1297. await transaction.commit();
  1298. } catch (err) {
  1299. await transaction.rollback();
  1300. throw err;
  1301. }
  1302. }
  1303. // 固定终审-更新
  1304. async updateLastAudit(stage, auditList, lastId) {
  1305. const transaction = await this.db.beginTransaction();
  1306. try {
  1307. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1308. const idList = this._.map(auditList, 'aid');
  1309. let order = idList.length + 1;
  1310. if (idList.indexOf(lastId) !== -1) {
  1311. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1312. const audit = this._.find(auditList, { 'aid': lastId });
  1313. // 顺移之后审核人流程顺序
  1314. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1315. order = order - 1;
  1316. }
  1317. // 添加终审
  1318. const newAuditor = {
  1319. tid: stage.tid, sid: stage.id, aid: lastId,
  1320. times: stage.times, order, status: auditConst.status.uncheck,
  1321. };
  1322. await transaction.insert(this.tableName, newAuditor);
  1323. await transaction.commit();
  1324. } catch (err) {
  1325. await transaction.rollback();
  1326. throw err;
  1327. }
  1328. }
  1329. async getFinalAuditGroup(stageId, times) {
  1330. const sql =
  1331. '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 ' +
  1332. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1333. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1334. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1335. const result = await this.db.query(sql, sqlParam);
  1336. for (const r of result) {
  1337. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1338. r.status = auditor.status;
  1339. r.opinion = auditor.opinion;
  1340. r.begin_time = auditor.begin_time;
  1341. r.end_time = auditor.end_time;
  1342. }
  1343. return result;
  1344. }
  1345. async getNumByMonth(tid, startMonth, endMonth) {
  1346. 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 ?';
  1347. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1348. const result = await this.db.queryOne(sql, sqlParam);
  1349. return result ? result.num : 0;
  1350. }
  1351. }
  1352. return StageAudit;
  1353. };