stage_audit.js 75 KB

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