stage_audit.js 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  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`, 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. // 本期结束
  423. // 生成截止本期数据 final数据
  424. await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  425. await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  426. // 同步 期信息
  427. await transaction.update(this.ctx.service.stage.tableName, {
  428. id: stageId,
  429. status: checkData.checkType,
  430. contract_tp: tpData.contract_tp,
  431. qc_tp: tpData.qc_tp,
  432. yf_tp: yfPay.tp,
  433. sf_tp: sfPay.tp,
  434. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  435. cache_time_r: this.ctx.stage.cache_time_l,
  436. });
  437. // 添加短信通知-审批通过提醒功能
  438. // const mobile_array = [];
  439. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  440. const auditList = await this.getAuditors(stageId, stageInfo.times);
  441. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  442. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  443. // const smsType = JSON.parse(smsUser1.sms_type);
  444. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  445. // mobile_array.push(smsUser1.auth_mobile);
  446. // }
  447. // }
  448. // for (const user of auditList) {
  449. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  450. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  451. // const smsType = JSON.parse(smsUser.sms_type);
  452. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  453. // mobile_array.push(smsUser.auth_mobile);
  454. // }
  455. // }
  456. // }
  457. // if (mobile_array.length > 0) {
  458. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  459. // const sms = new SMS(this.ctx);
  460. // const tenderName = await sms.contentChange(tenderInfo.name);
  461. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  462. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  463. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批通过。';
  464. // sms.send(mobile_array, content);
  465. // }
  466. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  467. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  468. qi: stageInfo.order,
  469. status: SmsAliConst.status.success,
  470. });
  471. // 微信模板通知
  472. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  473. const wechatData = {
  474. wap_url: shenpiUrl,
  475. qi: stageInfo.order,
  476. status: wxConst.status.success,
  477. tips: wxConst.tips.success,
  478. code: this.ctx.session.sessionProject.code,
  479. };
  480. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  481. }
  482. await transaction.commit();
  483. } catch (err) {
  484. await transaction.rollback();
  485. throw err;
  486. }
  487. }
  488. async _checkNo(pid, stageId, checkData, times) {
  489. const time = new Date();
  490. // 整理当前流程审核人状态更新
  491. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  492. if (!audit) {
  493. throw '审核数据错误';
  494. }
  495. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  496. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  497. const sqlParam = [this.tableName, stageId, times];
  498. const auditors = await this.db.query(sql, sqlParam);
  499. let order = 1;
  500. for (const a of auditors) {
  501. a.times = times + 1;
  502. a.order = order;
  503. a.status = auditConst.status.uncheck;
  504. order++;
  505. }
  506. const transaction = await this.db.beginTransaction();
  507. try {
  508. // 添加推送
  509. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid);
  510. const records = [
  511. {
  512. pid,
  513. type: pushType.stage,
  514. uid: this.ctx.stage.user_id,
  515. status: auditConst.status.checkNo,
  516. content: noticeContent,
  517. },
  518. ];
  519. auditors.forEach(audit => {
  520. records.push({
  521. pid,
  522. type: pushType.stage,
  523. uid: audit.aid,
  524. status: auditConst.status.checkNo,
  525. content: noticeContent,
  526. });
  527. });
  528. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  529. // 计算并合同支付最终数据
  530. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  531. this.ctx.stage.tp_history.push({
  532. times,
  533. order: audit.order,
  534. contract_tp: tpData.contract_tp,
  535. qc_tp: tpData.qc_tp,
  536. yf_tp: yfPay.tp,
  537. sf_tp: sfPay.tp,
  538. });
  539. await transaction.update(this.tableName, {
  540. id: audit.id,
  541. status: checkData.checkType,
  542. opinion: checkData.opinion,
  543. end_time: time,
  544. });
  545. // 同步 期信息
  546. await transaction.update(this.ctx.service.stage.tableName, {
  547. id: stageId,
  548. status: checkData.checkType,
  549. contract_tp: tpData.contract_tp,
  550. qc_tp: tpData.qc_tp,
  551. times: times + 1,
  552. yf_tp: yfPay.tp,
  553. sf_tp: sfPay.tp,
  554. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  555. cache_time_r: this.ctx.stage.cache_time_l,
  556. });
  557. // 拷贝新一次审核流程列表
  558. await transaction.insert(this.tableName, auditors);
  559. // 计算该审批人最终数据
  560. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  561. // 复制一份最新数据给原报
  562. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  563. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  564. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  565. // 添加短信通知-审批退回提醒功能
  566. // const mobile_array = [];
  567. const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  568. const auditList = await this.getAuditors(stageId, stageInfo.times);
  569. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  570. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '' && smsUser1.sms_type !== null) {
  571. // const smsType = JSON.parse(smsUser1.sms_type);
  572. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  573. // mobile_array.push(smsUser1.auth_mobile);
  574. // }
  575. // }
  576. // for (const user of auditList) {
  577. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  578. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  579. // const smsType = JSON.parse(smsUser.sms_type);
  580. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  581. // mobile_array.push(smsUser.auth_mobile);
  582. // }
  583. // }
  584. // }
  585. // if (mobile_array.length > 0) {
  586. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  587. // const sms = new SMS(this.ctx);
  588. // const tenderName = await sms.contentChange(tenderInfo.name);
  589. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  590. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  591. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,审批退回。';
  592. // sms.send(mobile_array, content);
  593. // }
  594. const users = this._.uniq(this._.concat(this._.map(auditList, 'aid'), stageInfo.user_id));
  595. await this.ctx.helper.sendAliSms(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), SmsAliConst.template.stage_result, {
  596. qi: stageInfo.order,
  597. status: SmsAliConst.status.back,
  598. });
  599. // 微信模板通知
  600. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  601. const wechatData = {
  602. wap_url: shenpiUrl,
  603. qi: stageInfo.order,
  604. status: wxConst.status.back,
  605. tips: wxConst.tips.back,
  606. code: this.ctx.session.sessionProject.code,
  607. };
  608. await this.ctx.helper.sendWechat(users, smsTypeConst.const.JL, smsTypeConst.judge.result.toString(), wxConst.template.stage, wechatData);
  609. await transaction.commit();
  610. } catch (err) {
  611. await transaction.rollback();
  612. throw err;
  613. }
  614. }
  615. async _checkNoPre(pid, stageId, checkData, times) {
  616. const time = new Date();
  617. // 整理当前流程审核人状态更新
  618. const audit = await this.getDataByCondition({ sid: stageId, times, status: auditConst.status.checking });
  619. if (!audit || audit.order <= 1) {
  620. throw '审核数据错误';
  621. }
  622. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  623. // const preAuditor = await this.getDataByCondition({sid: stageId, times: times, order: audit.order - 1});
  624. const auditors2 = await this.getAuditGroupByList(stageId, times);
  625. const auditorIndex = await auditors2.findIndex(function(item) {
  626. return item.aid === audit.aid;
  627. });
  628. const preAuditor = auditors2[auditorIndex - 1];
  629. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  630. const transaction = await this.db.beginTransaction();
  631. try {
  632. // 添加推送
  633. const noticeContent = await this.getNoticeContent(pid, audit.tid, stageId, audit.aid);
  634. const records = [
  635. {
  636. pid,
  637. type: pushType.stage,
  638. uid: this.ctx.stage.user_id,
  639. status: auditConst.status.checkNoPre,
  640. content: noticeContent,
  641. },
  642. ];
  643. auditors2.forEach(audit => {
  644. records.push({
  645. pid,
  646. type: pushType.stage,
  647. uid: audit.aid,
  648. status: auditConst.status.checkNoPre,
  649. content: noticeContent,
  650. });
  651. });
  652. await transaction.insert('zh_notice', records);
  653. // 计算并合同支付最终数据
  654. const [yfPay, sfPay] = await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  655. this.ctx.stage.tp_history.push({
  656. times,
  657. order: audit.order,
  658. contract_tp: tpData.contract_tp,
  659. qc_tp: tpData.qc_tp,
  660. yf_tp: yfPay.tp,
  661. sf_tp: sfPay.tp,
  662. });
  663. // 同步 期信息
  664. await transaction.update(this.ctx.service.stage.tableName, {
  665. id: stageId,
  666. contract_tp: tpData.contract_tp,
  667. qc_tp: tpData.qc_tp,
  668. times,
  669. yf_tp: yfPay.tp,
  670. sf_tp: sfPay.tp,
  671. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  672. cache_time_r: this.ctx.stage.cache_time_l,
  673. });
  674. await transaction.update(this.tableName, {
  675. id: audit.id,
  676. status: checkData.checkType,
  677. opinion: checkData.opinion,
  678. end_time: time,
  679. });
  680. // 顺移气候审核人流程顺序
  681. this.initSqlBuilder();
  682. this.sqlBuilder.setAndWhere('sid', { value: this.ctx.stage.id, operate: '=' });
  683. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  684. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  685. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  686. const data = await transaction.query(sql, sqlParam);
  687. // 上一审批人,当前审批人 再次添加至流程
  688. const newAuditors = [];
  689. newAuditors.push({
  690. tid: audit.tid,
  691. sid: audit.sid,
  692. aid: preAuditor.aid,
  693. times: audit.times,
  694. order: audit.order + 1,
  695. status: auditConst.status.checking,
  696. begin_time: time,
  697. });
  698. newAuditors.push({
  699. tid: audit.tid,
  700. sid: audit.sid,
  701. aid: audit.aid,
  702. times: audit.times,
  703. order: audit.order + 2,
  704. status: auditConst.status.uncheck,
  705. });
  706. await transaction.insert(this.tableName, newAuditors);
  707. // 计算该审批人最终数据
  708. await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  709. // 复制一份最新数据给下一人
  710. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  711. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  712. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  713. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  714. // 同步 期信息
  715. await transaction.update(this.ctx.service.stage.tableName, {
  716. id: stageId,
  717. status: checkData.checkType,
  718. cache_time_r: this.ctx.stage.cache_time_l,
  719. });
  720. // 添加短信通知-需要审批提醒功能
  721. // const smsUser = await this.ctx.service.projectAccount.getDataById(preAuditor.aid);
  722. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  723. // const smsType = JSON.parse(smsUser.sms_type);
  724. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  725. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  726. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  727. // const sms = new SMS(this.ctx);
  728. // const tenderName = await sms.contentChange(tenderInfo.name);
  729. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  730. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  731. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  732. // // const result = '';
  733. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  734. // sms.send(smsUser.auth_mobile, content);
  735. // }
  736. // }
  737. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  738. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  739. await this.ctx.helper.sendAliSms(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  740. qi: stageInfo.order,
  741. code: shenpiUrl,
  742. });
  743. // 微信模板通知
  744. const wechatData = {
  745. wap_url: shenpiUrl,
  746. qi: stageInfo.order,
  747. status: wxConst.status.check,
  748. tips: wxConst.tips.check,
  749. code: this.ctx.session.sessionProject.code,
  750. };
  751. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  752. await transaction.commit();
  753. } catch (err) {
  754. await transaction.rollback();
  755. throw err;
  756. }
  757. }
  758. /**
  759. * 审批
  760. * @param {Number} stageId - 标段id
  761. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  762. * @param {Number} times - 第几次审批
  763. * @return {Promise<void>}
  764. */
  765. async check(stageId, checkData, times = 1) {
  766. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  767. throw '提交数据错误';
  768. }
  769. // // 整理当前流程审核人状态更新
  770. // const audit = await this.getDataByCondition({sid: stageId, times: times, status: auditConst.status.checking});
  771. // if (!audit) {
  772. // throw '审核数据错误';
  773. // }
  774. // const time = new Date();
  775. const pid = this.ctx.session.sessionProject.id;
  776. switch (checkData.checkType) {
  777. case auditConst.status.checked:
  778. await this._checked(pid, stageId, checkData, times);
  779. break;
  780. case auditConst.status.checkNo:
  781. await this._checkNo(pid, stageId, checkData, times);
  782. break;
  783. case auditConst.status.checkNoPre:
  784. await this._checkNoPre(pid, stageId, checkData, times);
  785. break;
  786. default:
  787. throw '无效审批操作';
  788. }
  789. }
  790. /**
  791. * 审批
  792. * @param {Number} stageId - 标段id
  793. * @param {Number} times - 第几次审批
  794. * @return {Promise<void>}
  795. */
  796. async checkAgain(stageId, times = 1) {
  797. const time = new Date();
  798. // 整理当前流程审核人状态更新
  799. const audit = (
  800. await this.getAllDataByCondition({
  801. where: { sid: stageId, times },
  802. orders: [['order', 'desc']],
  803. limit: 1,
  804. offset: 0,
  805. })
  806. )[0];
  807. if (!audit || audit.order < 1) {
  808. throw '审核数据错误';
  809. }
  810. const transaction = await this.db.beginTransaction();
  811. try {
  812. // 当前审批人2次添加至流程中
  813. const newAuditors = [];
  814. newAuditors.push({
  815. tid: audit.tid,
  816. sid: audit.sid,
  817. aid: audit.aid,
  818. times: audit.times,
  819. order: audit.order + 1,
  820. status: auditConst.status.checkAgain,
  821. begin_time: time,
  822. end_time: time,
  823. opinion: '',
  824. });
  825. newAuditors.push({
  826. tid: audit.tid,
  827. sid: audit.sid,
  828. aid: audit.aid,
  829. times: audit.times,
  830. order: audit.order + 2,
  831. status: auditConst.status.checking,
  832. begin_time: time,
  833. });
  834. await transaction.insert(this.tableName, newAuditors);
  835. // 复制一份最新数据给下一人
  836. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 1, transaction);
  837. await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  838. await this.ctx.service.stageJgcl.updateHistory(this.ctx.stage, transaction);
  839. await this.ctx.service.stageBonus.updateHistory(this.ctx.stage, transaction);
  840. await this.ctx.service.stageOther.updateHistory(this.ctx.stage, transaction);
  841. // 本期结束
  842. // 生成截止本期数据 final数据
  843. await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  844. await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  845. // 同步 期信息
  846. await transaction.update(this.ctx.service.stage.tableName, {
  847. id: stageId,
  848. status: auditConst.status.checking,
  849. cache_time_r: this.ctx.stage.cache_time_l,
  850. });
  851. // 添加短信通知-需要审批提醒功能
  852. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  853. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '' && smsUser.sms_type !== null) {
  854. // const smsType = JSON.parse(smsUser.sms_type);
  855. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  856. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  857. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  858. // const sms = new SMS(this.ctx);
  859. // const tenderName = await sms.contentChange(tenderInfo.name);
  860. // const projectName = await sms.contentChange(this.ctx.tender.info.deal_info.buildName);
  861. // const ptmsg = projectName !== '' ? '项目「' + projectName + '」标段「' + tenderName + '」' : tenderName;
  862. // const result = await this.ctx.helper.urlToShort('http://' + this.ctx.request.header.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  863. // const content = '【纵横计量支付】' + ptmsg + '第' + stageInfo.order + '期,需要您审批。' + result;
  864. // sms.send(smsUser.auth_mobile, content);
  865. // }
  866. // }
  867. const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  868. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/stage/' + stageInfo.order);
  869. await this.ctx.helper.sendAliSms(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), SmsAliConst.template.stage_check, {
  870. qi: stageInfo.order,
  871. code: shenpiUrl,
  872. });
  873. // 微信模板通知
  874. const wechatData = {
  875. wap_url: shenpiUrl,
  876. qi: stageInfo.order,
  877. status: wxConst.status.check,
  878. tips: wxConst.tips.check,
  879. code: this.ctx.session.sessionProject.code,
  880. };
  881. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.JL, smsTypeConst.judge.approval.toString(), wxConst.template.stage, wechatData);
  882. await transaction.commit();
  883. } catch (err) {
  884. await transaction.rollback();
  885. throw err;
  886. }
  887. }
  888. /**
  889. * 获取审核人需要审核的期列表
  890. *
  891. * @param auditorId
  892. * @return {Promise<*>}
  893. */
  894. async getAuditStage(auditorId) {
  895. const sql =
  896. 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  897. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  898. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  899. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  900. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  901. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id` ORDER BY sa.`begin_time` DESC';
  902. const sqlParam = [
  903. this.tableName,
  904. this.ctx.service.stage.tableName,
  905. this.ctx.service.tender.tableName,
  906. auditorId,
  907. auditConst.status.checking,
  908. auditorId,
  909. auditConst.status.checkNo,
  910. auditConst.status.checkNo,
  911. ];
  912. return await this.db.query(sql, sqlParam);
  913. }
  914. /**
  915. * 获取 某时间后 审批进度 更新的期
  916. * @param {Number} pid - 查询标段
  917. * @param {Number} uid - 查询人
  918. * @param {Date} time - 查询时间
  919. * @return {Promise<*>}
  920. */
  921. async getNoticeStage(pid, uid, time) {
  922. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  923. // ' s.`order` As `s_order`, s.`status` As `s_status`, ' +
  924. // ' sa.`aid`, sa.`times`, sa.`order`, sa.`end_time`, sa.`tid`, sa.`sid`, sa.`status`, ' +
  925. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  926. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  927. // ' LEFT JOIN ?? As s On t.`id` = s.`tid`' +
  928. // ' LEFT JOIN ?? As sa ON s.`id` = sa.`sid`' +
  929. // ' LEFT JOIN ?? As pa ON sa.`aid` = pa.`id`' +
  930. // ' WHERE sa.`end_time` > ? and t.`project_id` = ?' +
  931. // ' ORDER By sa.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  932. // ' ORDER By new_t.`end_time`';
  933. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.stage.tableName, this.tableName,
  934. // this.ctx.service.projectAccount.tableName, time, pid];
  935. // return await this.db.query(sql, sqlParam);
  936. let notice = await this.db.select('zh_notice', {
  937. where: { pid, type: pushType.stage, uid },
  938. orders: [['create_time', 'desc']],
  939. limit: 10,
  940. offset: 0,
  941. });
  942. notice = notice.map(v => {
  943. const extra = JSON.parse(v.content);
  944. delete v.content;
  945. return { ...v, ...extra };
  946. });
  947. return notice;
  948. }
  949. /**
  950. * 用于添加推送所需的content内容
  951. * @param {Number} pid 项目id
  952. * @param {Number} tid 台账id
  953. * @param {Number} sid 期id
  954. * @param {Number} uid 审核人id
  955. */
  956. async getNoticeContent(pid, tid, sid, uid) {
  957. const noticeSql =
  958. 'SELECT * FROM (SELECT ' +
  959. ' t.`id` As `tid`, t.`name`, s.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  960. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  961. ' LEFT JOIN ?? As s On s.`id` = ?' +
  962. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  963. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  964. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.stage.tableName, sid, this.ctx.service.projectAccount.tableName, uid, pid];
  965. const content = await this.db.query(noticeSql, noticeSqlParam);
  966. return content.length ? JSON.stringify(content[0]) : '';
  967. }
  968. /**
  969. * 获取审核人流程列表
  970. *
  971. * @param auditorId
  972. * @return {Promise<*>}
  973. */
  974. async getAuditGroupByList(stageId, times) {
  975. const sql =
  976. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`aid`, la.`order` ' +
  977. 'FROM ?? AS la, ?? AS pa ' +
  978. 'WHERE la.`sid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  979. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, times];
  980. return await this.db.query(sql, sqlParam);
  981. // const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid`';
  982. // const sqlParam = [this.tableName, stageId, times];
  983. // return await this.db.query(sql, sqlParam);
  984. }
  985. /**
  986. * 获取审核人流程列表
  987. *
  988. * @param auditorId
  989. * @return {Promise<*>}
  990. */
  991. async getAuditGroupByListWithOwner(stageId, times) {
  992. const result = await this.getAuditGroupByList(stageId, times);
  993. const sql =
  994. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As sid, 0 As `order`' +
  995. ' FROM ' +
  996. this.ctx.service.stage.tableName +
  997. ' As s' +
  998. ' LEFT JOIN ' +
  999. this.ctx.service.projectAccount.tableName +
  1000. ' As pa' +
  1001. ' ON s.user_id = pa.id' +
  1002. ' WHERE s.id = ?';
  1003. const sqlParam = [times, stageId, stageId];
  1004. const user = await this.db.queryOne(sql, sqlParam);
  1005. result.unshift(user);
  1006. return result;
  1007. }
  1008. /**
  1009. * 复制上一期的审批人列表给最新一期
  1010. *
  1011. * @param transaction - 新增一期的事务
  1012. * @param {Object} preStage - 上一期
  1013. * @param {Object} newStage - 最新一期
  1014. * @return {Promise<*>}
  1015. */
  1016. async copyPreStageAuditors(transaction, preStage, newStage) {
  1017. const auditors = await this.getAuditGroupByList(preStage.id, preStage.times);
  1018. const newAuditors = [];
  1019. for (const a of auditors) {
  1020. const na = {
  1021. tid: preStage.tid,
  1022. sid: newStage.id,
  1023. aid: a.aid,
  1024. times: newStage.times,
  1025. order: newAuditors.length + 1,
  1026. status: auditConst.status.uncheck,
  1027. };
  1028. newAuditors.push(na);
  1029. }
  1030. const result = await transaction.insert(this.tableName, newAuditors);
  1031. return (result.effectRows = auditors.length);
  1032. }
  1033. /**
  1034. * 移除审核人
  1035. *
  1036. * @param {Number} stageId - 期id
  1037. * @param {Number} status - 期状态
  1038. * @param {Number} status - 期次数
  1039. * @return {Promise<boolean>}
  1040. */
  1041. async getAuditorByStatus(stageId, status, times = 1) {
  1042. let auditor = null;
  1043. let sql = '';
  1044. let sqlParam = '';
  1045. switch (status) {
  1046. case auditConst.status.checking:
  1047. case auditConst.status.checked:
  1048. case auditConst.status.checkNoPre:
  1049. sql =
  1050. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1051. 'FROM ?? AS la, ?? AS pa ' +
  1052. 'WHERE la.`sid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  1053. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, status];
  1054. auditor = await this.db.queryOne(sql, sqlParam);
  1055. break;
  1056. case auditConst.status.checkNo:
  1057. sql =
  1058. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`sid`, la.`order` ' +
  1059. 'FROM ?? AS la, ?? AS pa ' +
  1060. 'WHERE la.`sid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  1061. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, stageId, auditConst.status.checkNo, parseInt(times) - 1];
  1062. auditor = await this.db.queryOne(sql, sqlParam);
  1063. break;
  1064. case auditConst.status.uncheck:
  1065. default:
  1066. break;
  1067. }
  1068. return auditor;
  1069. }
  1070. /**
  1071. * 取某一期已批准审核信息(报表用)
  1072. *
  1073. * @param {Number} stageId - 期id
  1074. * @param {Number} times - 期次数
  1075. * @return {Promise<boolean>}
  1076. */
  1077. async getStageAudit(stageId, times = 1) {
  1078. 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';
  1079. const sqlParam = [this.tableName, stageId, times];
  1080. const rst = await this.db.query(sql, sqlParam);
  1081. return rst;
  1082. }
  1083. /**
  1084. * 取待审批期列表(wap用)
  1085. *
  1086. * @param auditorId
  1087. * @return {Promise<*>}
  1088. */
  1089. async getAuditStageByWap(auditorId) {
  1090. const sql =
  1091. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`sid`,' +
  1092. // ' 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`, ' +
  1093. ' s.*,' +
  1094. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`,' +
  1095. ' ti.`deal_info` ' +
  1096. ' FROM ?? AS sa, ?? AS s, ?? As t, ?? AS ti ' +
  1097. ' WHERE sa.`aid` = ? and sa.`status` = ?' +
  1098. ' and sa.`sid` = s.`id` and sa.`tid` = t.`id` and ti.`tid` = t.`id`';
  1099. const sqlParam = [
  1100. this.tableName,
  1101. this.ctx.service.stage.tableName,
  1102. this.ctx.service.tender.tableName,
  1103. this.ctx.service.tenderInfo.tableName,
  1104. auditorId,
  1105. auditConst.status.checking,
  1106. ];
  1107. return await this.db.query(sql, sqlParam);
  1108. }
  1109. /**
  1110. * 删除 某期 某次 全审批流程
  1111. * 私有,不做判断,不补全最新一轮审批人数据,不计算缓存
  1112. * @param {Number} sid - 标段id
  1113. * @param {Number} times - 第几次审批
  1114. * @param transaction - 删除事务
  1115. * @return {Promise<void>}
  1116. */
  1117. async _timesDelete(sid, times, transaction) {
  1118. // 审批流程
  1119. await transaction.delete(this.tableName, { sid, times });
  1120. await transaction.delete(this.ctx.service.pos.tableName, { add_stage: sid, add_times: times });
  1121. await transaction.delete(this.ctx.service.stageBills.tableName, { sid, times });
  1122. await transaction.delete(this.ctx.service.stagePos.tableName, { sid, times });
  1123. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid, times });
  1124. await transaction.delete(this.ctx.service.stageChange.tableName, { sid, stimes: times });
  1125. await transaction.delete(this.ctx.service.stagePay.tableName, { sid, stimes: times });
  1126. await transaction.delete(this.ctx.service.pay.tableName, { csid: sid, cstimes: times });
  1127. // 其他台账
  1128. await this.ctx.service.stageJgcl.deleteStageTimesData(sid, times, transaction);
  1129. await this.ctx.service.stageOther.deleteStageTimesData(sid, times, transaction);
  1130. await this.ctx.service.stageBonus.deleteStageTimesData(sid, times, transaction);
  1131. }
  1132. /**
  1133. * 删除本次审批流程
  1134. * @param {Number} stageId - 标段id
  1135. * @param {Number} times - 第几次审批
  1136. * @return {Promise<void>}
  1137. */
  1138. async timesDelete() {
  1139. const transaction = await this.db.beginTransaction();
  1140. try {
  1141. // 删除最新一次数据
  1142. await this._timesDelete(this.ctx.stage.id, this.ctx.stage.times, transaction);
  1143. // 审批退回,未重新上报时,需删除最新两次数据
  1144. const isCheckNo = this.ctx.stage.status === auditConst.status.checkNo;
  1145. const nowTimes = isCheckNo ? this.ctx.stage.times - 1 : this.ctx.stage.times;
  1146. if (isCheckNo) {
  1147. await this._timesDelete(this.ctx.stage.id, nowTimes, transaction);
  1148. }
  1149. // 添加上一次审批人
  1150. const sql = 'SELECT `tid`, `sid`, `aid`, `order` FROM ?? WHERE `sid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  1151. const sqlParam = [this.tableName, this.ctx.stage.id, nowTimes - 1];
  1152. const auditors = await this.db.query(sql, sqlParam);
  1153. let order = 1;
  1154. for (const a of auditors) {
  1155. a.times = nowTimes;
  1156. a.order = order;
  1157. a.status = auditConst.status.uncheck;
  1158. order++;
  1159. }
  1160. // 拷贝新一次审核流程列表
  1161. await transaction.insert(this.tableName, auditors);
  1162. // 计算缓存
  1163. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  1164. // 计算并合同支付最终数据
  1165. const lastAudit = await this.getDataByCondition({
  1166. sid: this.ctx.stage.id,
  1167. times: nowTimes - 1,
  1168. status: auditConst.status.checkNo,
  1169. });
  1170. if (!lastAudit) throw '审批数据错误';
  1171. await this.ctx.service.stagePay.copyStagePays4DeleteTimes(this.ctx.stage, nowTimes, 0, lastAudit.times, lastAudit.order, transaction);
  1172. const stagePay = await this.ctx.service.stagePay.getAuditorStageData(this.ctx.stage.id, lastAudit.times, lastAudit.order);
  1173. const yfPay = stagePay.find(function(x) {
  1174. return x.ptype === payConst.payType.yf;
  1175. });
  1176. const sfPay = stagePay.find(function(x) {
  1177. return x.ptype === payConst.payType.sf;
  1178. });
  1179. // 同步 期信息
  1180. const time = new Date();
  1181. await transaction.update(this.ctx.service.stage.tableName, {
  1182. id: this.ctx.stage.id,
  1183. status: auditConst.status.checkNo,
  1184. contract_tp: tpData.contract_tp,
  1185. qc_tp: tpData.qc_tp,
  1186. times: nowTimes,
  1187. yf_tp: yfPay.tp,
  1188. sf_tp: sfPay.tp,
  1189. tp_history: JSON.stringify(this.ctx.stage.tp_history),
  1190. cache_time_l: time,
  1191. cache_time_r: time,
  1192. });
  1193. await transaction.commit();
  1194. } catch (err) {
  1195. await transaction.rollback();
  1196. throw err;
  1197. }
  1198. }
  1199. // 固定审批流-更新
  1200. async updateNewAuditList(stage, newIdList) {
  1201. const transaction = await this.db.beginTransaction();
  1202. try {
  1203. // 先删除旧的审批流,再添加新的
  1204. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times });
  1205. const newAuditors = [];
  1206. let order = 1;
  1207. for (const aid of newIdList) {
  1208. newAuditors.push({
  1209. tid: stage.tid, sid: stage.id, aid,
  1210. times: stage.times, order, status: auditConst.status.uncheck,
  1211. });
  1212. order++;
  1213. }
  1214. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1215. await transaction.commit();
  1216. } catch (err) {
  1217. await transaction.rollback();
  1218. throw err;
  1219. }
  1220. }
  1221. // 固定终审-更新
  1222. async updateLastAudit(stage, auditList, lastId) {
  1223. const transaction = await this.db.beginTransaction();
  1224. try {
  1225. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1226. const idList = this._.map(auditList, 'aid');
  1227. let order = idList.length + 1;
  1228. if (idList.indexOf(lastId) !== -1) {
  1229. await transaction.delete(this.tableName, { sid: stage.id, times: stage.times, aid: lastId });
  1230. const audit = this._.find(auditList, { 'aid': lastId });
  1231. // 顺移之后审核人流程顺序
  1232. await this._syncOrderByDelete(transaction, stage.id, audit.order, stage.times);
  1233. order = order - 1;
  1234. }
  1235. // 添加终审
  1236. const newAuditor = {
  1237. tid: stage.tid, sid: stage.id, aid: lastId,
  1238. times: stage.times, order, status: auditConst.status.uncheck,
  1239. };
  1240. await transaction.insert(this.tableName, newAuditor);
  1241. await transaction.commit();
  1242. } catch (err) {
  1243. await transaction.rollback();
  1244. throw err;
  1245. }
  1246. }
  1247. }
  1248. return StageAudit;
  1249. };