stage_audit.js 68 KB

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