stage_audit.js 76 KB

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