stage_audit.js 74 KB

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