stage_audit.js 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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. await transaction.update(this.ctx.service.stage.tableName, {
  911. id: stageId,
  912. status: auditConst.status.checking,
  913. cache_time_r: this.ctx.stage.cache_time_l,
  914. });
  915. // 添加短信通知-需要审批提醒功能
  916. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  917. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  918. // const smsType = JSON.parse(smsUser.sms_type);
  919. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  920. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  921. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  922. // const sms = new SMS(this.ctx);
  923. // const tenderName = await sms.contentChange(tenderInfo.name);
  924. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  925. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  926. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  927. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  928. // sms.send(smsUser.auth_mobile, content);
  929. // }
  930. // }
  931. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  932. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  933. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  934. qi: stageInfo.order,
  935. code: shenpiUrl,
  936. });
  937. // 微信模板通知
  938. const wechatData = {
  939. wap_url: shenpiUrl,
  940. qi: stageInfo.order,
  941. status: wxConst.status.check,
  942. tips: wxConst.tips.check,
  943. code: this.ctx.session.sessionProject.code,
  944. };
  945. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  946. await transaction.commit();
  947. // 通知发送 - 第三方更新
  948. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  949. const base_data = {
  950. tid: this.ctx.tender.id,
  951. sid: stageId,
  952. op: 'update',
  953. };
  954. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  955. base_data.op = 'update';
  956. base_data.sid = -1;
  957. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  958. }
  959. } catch (err) {
  960. await transaction.rollback();
  961. throw err;
  962. }
  963. }
  964. /**
  965. * 获取审核人需要审核的期列表
  966. *
  967. * @param auditorId
  968. * @return {Promise<*>}
  969. */
  970. async getAuditStage(auditorId) {
  971. const sql =
  972. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  973. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  974. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  975. ' FROM ?? AS sa ' +
  976. ' Left Join ?? AS s On sa.`sid` = s.`id` ' +
  977. ' Left Join ?? As t ON sa.`tid` = t.`id`' +
  978. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  979. ' ORDER BY sa.`begin_time` DESC';
  980. const sqlParam = [
  981. this.tableName,
  982. this.ctx.service.stage.tableName,
  983. this.ctx.service.tender.tableName,
  984. auditorId,
  985. auditConst.status.checking,
  986. auditorId,
  987. auditConst.status.checkNo,
  988. auditConst.status.checkNo,
  989. ];
  990. return await this.db.query(sql, sqlParam);
  991. }
  992. /**
  993. * 获取 某时间后 审批进度 更新的期
  994. * @param {Number} pid - 查询标段
  995. * @param {Number} uid - 查询人
  996. * @param {Date} time - 查询时间
  997. * @return {Promise<*>}
  998. */
  999. async getNoticeStage(pid, uid, time) {
  1000. let notice = await this.db.select('zh_notice', {
  1001. where: { pid, type: pushType.stage, uid },
  1002. orders: [['create_time', 'desc']],
  1003. limit: 10,
  1004. offset: 0,
  1005. });
  1006. notice = notice.map(v => {
  1007. const extra = JSON.parse(v.content);
  1008. delete v.content;
  1009. return { ...v, ...extra };
  1010. });
  1011. return notice;
  1012. }
  1013. /**
  1014. * 用于添加推送所需的content内容
  1015. * @param {Number} pid 项目id
  1016. * @param {Number} tid 台账id
  1017. * @param {Number} sid 期id
  1018. * @param {Number} uid 审核人id
  1019. */
  1020. async getNoticeContent(pid, tid, sid, uid) {
  1021. const noticeSql =
  1022. 'SELECT * FROM (SELECT ' +
  1023. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  1024. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  1025. ' LEFT JOIN ?? As s On s.`id` = ?' +
  1026. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  1027. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  1028. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  1029. const content = await this.db.query(noticeSql, noticeSqlParam);
  1030. return content.length ? JSON.stringify(content[0]) : '';
  1031. }
  1032. /**
  1033. * 获取审核人流程列表
  1034. *
  1035. * @param auditorId
  1036. * @return {Promise<*>}
  1037. */
  1038. async getAuditGroupByList(stageId, times) {
  1039. const sql =
  1040. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  1041. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1042. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1043. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1044. return await this.db.query(sql, sqlParam);
  1045. }
  1046. /**
  1047. * 获取审核人流程列表
  1048. *
  1049. * @param auditorId
  1050. * @return {Promise<*>}
  1051. */
  1052. async getAuditGroupByListWithOwner(stageId, times) {
  1053. const result = await this.getAuditGroupByList(stageId, times);
  1054. const sql =
  1055. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  1056. ' FROM ' + this.ctx.service.stage.tableName + ' As s' +
  1057. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1058. ' ON s.user_id = pa.id' +
  1059. ' WHERE s.id = ?';
  1060. const sqlParam = [times, stageId, stageId];
  1061. const user = await this.db.queryOne(sql, sqlParam);
  1062. result.unshift(user);
  1063. return result;
  1064. }
  1065. /**
  1066. * 复制上一期的审批人列表给最新一期
  1067. *
  1068. * @param transaction - 新增一期的事务
  1069. * @param {Object} preStage - 上一期
  1070. * @param {Object} newStage - 最新一期
  1071. * @return {Promise<*>}
  1072. */
  1073. async copyPreStageAuditors(transaction, preStage, newStage) {
  1074. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1075. const newAuditors = [];
  1076. for (const a of auditors) {
  1077. const na = {
  1078. tid: preStage.tid,
  1079. sid: newStage.id,
  1080. aid: a.aid,
  1081. times: newStage.times,
  1082. order: newAuditors.length + 1,
  1083. status: auditConst.status.uncheck,
  1084. };
  1085. newAuditors.push(na);
  1086. }
  1087. const result = await transaction.insert(this.tableName, newAuditors);
  1088. return (result.effectRows = auditors.length);
  1089. }
  1090. /**
  1091. * 移除审核人
  1092. *
  1093. * @param {Number} stageId - 期id
  1094. * @param {Number} status - 期状态
  1095. * @param {Number} status - 期次数
  1096. * @return {Promise<boolean>}
  1097. */
  1098. async getAuditorByStatus(stageId, status, times = 1) {
  1099. let auditor = null;
  1100. let sql = '';
  1101. let sqlParam = '';
  1102. switch (status) {
  1103. case auditConst.status.checking:
  1104. case auditConst.status.checked:
  1105. case auditConst.status.checkNoPre:
  1106. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1107. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1108. ' WHERE la.`sid` = ? and la.`status` = ? order by la.`times` desc, la.`order` desc';
  1109. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1110. auditor = await this.db.queryOne(sql, sqlParam);
  1111. break;
  1112. case auditConst.status.checkNo:
  1113. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1114. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1115. ' WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? order by la.`times` desc, la.`order` desc';
  1116. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1117. auditor = await this.db.queryOne(sql, sqlParam);
  1118. break;
  1119. case auditConst.status.uncheck:
  1120. default:
  1121. break;
  1122. }
  1123. return auditor;
  1124. }
  1125. /**
  1126. * 取某一期已批准审核信息(报表用)
  1127. *
  1128. * @param {Number} stageId - 期id
  1129. * @param {Number} times - 期次数
  1130. * @return {Promise<boolean>}
  1131. */
  1132. async getStageAudit(stageId, times = 1) {
  1133. 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';
  1134. const sqlParam = [this.tableName, stageId, times];
  1135. const rst = await this.db.query(sql, sqlParam);
  1136. return rst;
  1137. }
  1138. /**
  1139. * 取待审批期列表(wap用)
  1140. *
  1141. * @param auditorId
  1142. * @return {Promise<*>}
  1143. */
  1144. async getAuditStageByWap(auditorId) {
  1145. const sql =
  1146. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1147. // ' 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`, ' +
  1148. ' s.*,' +
  1149. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1150. ' ti.`deal_info` ' +
  1151. ' FROM ?? AS sa' +
  1152. ' Left Join ?? AS s On sa.`sid` = s.`id`' +
  1153. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1154. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1155. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1156. const sqlParam = [
  1157. this.tableName,
  1158. this.ctx.service.stage.tableName,
  1159. this.ctx.service.tender.tableName,
  1160. this.ctx.service.tenderInfo.tableName,
  1161. auditorId,
  1162. auditConst.status.checking,
  1163. ];
  1164. return await this.db.query(sql, sqlParam);
  1165. }
  1166. /**
  1167. * 删除 某期 某次 全审批流程
  1168. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1169. * @param {Number} sid - 标段id
  1170. * @param {Number} times - 第几次审批
  1171. * @param transaction - 删除事务
  1172. * @return {Promise<void>}
  1173. */
  1174. async _timesDelete(sid, times, transaction) {
  1175. // 审批流程
  1176. await transaction.delete(this.tableName, { sid, times });
  1177. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1178. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1179. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1180. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1181. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1182. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1183. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1184. // 其他台账
  1185. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1186. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1187. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1188. await this.ctx.service.stageSafeProd.deleteStageTimesData(sid, times, transaction);
  1189. await this.ctx.service.stageTempLand.deleteStageTimesData(sid, times, transaction);
  1190. }
  1191. /**
  1192. * 删除本次审批流程
  1193. * @param {Number} stageId - 标段id
  1194. * @param {Number} times - 第几次审批
  1195. * @return {Promise<void>}
  1196. */
  1197. async timesDelete() {
  1198. const transaction = await this.db.beginTransaction();
  1199. try {
  1200. // 删除最新一次数据
  1201. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1202. // 审批退回,未重新上报时,需删除最新两次数据
  1203. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1204. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1205. if (isCheckNo) {
  1206. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1207. }
  1208. // 添加上一次审批人
  1209. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1210. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1211. const auditors = await this.db.query(sql, sqlParam);
  1212. let order = 1;
  1213. for (const a of auditors) {
  1214. a.times = nowTimes;
  1215. a.order = order;
  1216. a.status = auditConst.status.uncheck;
  1217. order++;
  1218. }
  1219. await this._updateTender(transaction);
  1220. // 拷贝新一次审核流程列表
  1221. await transaction.insert(this.tableName, auditors);
  1222. // 计算缓存
  1223. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1224. // 计算并合同支付最终数据
  1225. const lastAudit = await this.getDataByCondition({
  1226. sid: this.ctx.stage.id,
  1227. times: nowTimes - 1,
  1228. status: auditConst.status.checkNo,
  1229. });
  1230. if (!lastAudit) throw '审批数据错误';
  1231. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1232. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1233. const yfPay = stagePay.find(function(x) {
  1234. return x.ptype === payConst.payType.yf;
  1235. });
  1236. const sfPay = stagePay.find(function(x) {
  1237. return x.ptype === payConst.payType.sf;
  1238. });
  1239. // 同步 期信息
  1240. const time = new Date();
  1241. await transaction.update(this.ctx.service.stage.tableName, {
  1242. id: this.ctx.stage.id,
  1243. status: auditConst.status.checkNo,
  1244. contract_tp: tpData.contract_tp,
  1245. qc_tp: tpData.qc_tp,
  1246. times: nowTimes,
  1247. yf_tp: yfPay ? yfPay.tp : null,
  1248. sf_tp: sfPay ? sfPay.tp : null,
  1249. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1250. cache_time_l: time,
  1251. cache_time_r: time,
  1252. });
  1253. await transaction.commit();
  1254. // 通知发送 - 第三方更新
  1255. if (this.ctx.session.sessionProject.custom && syncApiConst.notice_type.indexOf(this.ctx.session.sessionProject.customType) !== -1) {
  1256. const base_data = {
  1257. tid: this.ctx.tender.id,
  1258. sid: this.ctx.stage.id,
  1259. op: 'update',
  1260. };
  1261. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1262. base_data.op = 'update';
  1263. base_data.sid = -1;
  1264. this.ctx.helper.syncNoticeSend(this.ctx.session.sessionProject.customType, JSON.stringify(base_data));
  1265. }
  1266. } catch (err) {
  1267. await transaction.rollback();
  1268. throw err;
  1269. }
  1270. }
  1271. // 固定审批流-更新
  1272. async updateNewAuditList(stage, newIdList) {
  1273. const transaction = await this.db.beginTransaction();
  1274. try {
  1275. // 先删除旧的审批流,再添加新的
  1276. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1277. const newAuditors = [];
  1278. let order = 1;
  1279. for (const aid of newIdList) {
  1280. newAuditors.push({
  1281. tid: stage.tid, sid: stage.id, aid,
  1282. times: stage.times, order, status: auditConst.status.uncheck,
  1283. });
  1284. order++;
  1285. }
  1286. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1287. await transaction.commit();
  1288. } catch (err) {
  1289. await transaction.rollback();
  1290. throw err;
  1291. }
  1292. }
  1293. // 固定终审-更新
  1294. async updateLastAudit(stage, auditList, lastId) {
  1295. const transaction = await this.db.beginTransaction();
  1296. try {
  1297. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1298. const idList = this._.map(auditList, 'aid');
  1299. let order = idList.length + 1;
  1300. if (idList.indexOf(lastId) !== -1) {
  1301. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1302. const audit = this._.find(auditList, { 'aid': lastId });
  1303. // 顺移之后审核人流程顺序
  1304. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1305. order = order - 1;
  1306. }
  1307. // 添加终审
  1308. const newAuditor = {
  1309. tid: stage.tid, sid: stage.id, aid: lastId,
  1310. times: stage.times, order, status: auditConst.status.uncheck,
  1311. };
  1312. await transaction.insert(this.tableName, newAuditor);
  1313. await transaction.commit();
  1314. } catch (err) {
  1315. await transaction.rollback();
  1316. throw err;
  1317. }
  1318. }
  1319. async getFinalAuditGroup(stageId, times) {
  1320. const sql =
  1321. '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 ' +
  1322. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1323. ' WHERE la.`sid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  1324. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  1325. const result = await this.db.query(sql, sqlParam);
  1326. for (const r of result) {
  1327. const auditor = await this.getDataByCondition({sid: stageId, times: r.times, order: r.max_order});
  1328. r.status = auditor.status;
  1329. r.opinion = auditor.opinion;
  1330. r.begin_time = auditor.begin_time;
  1331. r.end_time = auditor.end_time;
  1332. }
  1333. return result;
  1334. }
  1335. async getNumByMonth(tid, startMonth, endMonth) {
  1336. 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 ?';
  1337. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1338. const result = await this.db.queryOne(sql, sqlParam);
  1339. return result ? result.num : 0;
  1340. }
  1341. }
  1342. return StageAudit;
  1343. };