material_audit.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/2/27
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. const pushType = require('../const/audit').pushType;
  11. module.exports = app => {
  12. class MaterialAudit extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'material_audit';
  22. }
  23. /**
  24. * 获取 审核人信息
  25. *
  26. * @param {Number} materialId - 材料调差期id
  27. * @param {Number} auditorId - 审核人id
  28. * @param {Number} times - 第几次审批
  29. * @return {Promise<*>}
  30. */
  31. async getAuditor(materialId, auditorId, times = 1) {
  32. const sql = '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` ' +
  33. 'FROM ?? AS la, ?? AS pa ' +
  34. 'WHERE la.`mid` = ? and la.`aid` = ? and la.`times` = ?' +
  35. ' and la.`aid` = pa.`id`';
  36. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditorId, times];
  37. return await this.db.queryOne(sql, sqlParam);
  38. }
  39. /**
  40. * 获取 审核列表信息
  41. *
  42. * @param {Number} materialId - 材料调差期id
  43. * @param {Number} times - 第几次审批
  44. * @return {Promise<*>}
  45. */
  46. async getAuditors(materialId, times = 1) {
  47. const sql = '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` ' +
  48. 'FROM ?? AS la, ?? AS pa, (SELECT `aid`,(@i:=@i+1) as `sort` FROM ??, (select @i:=0) as it WHERE `mid` = ? AND `times` = ? GROUP BY `aid`) as g ' +
  49. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  50. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, materialId, times, materialId, times];
  51. const result = await this.db.query(sql, sqlParam);
  52. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `mid` = ? AND `times` = ? GROUP BY `aid`) as a';
  53. const sqlParam2 = [this.tableName, materialId, times];
  54. const count = await this.db.queryOne(sql2, sqlParam2);
  55. for (const i in result) {
  56. result[i].max_sort = count.num;
  57. }
  58. return result;
  59. }
  60. /**
  61. * 获取 当前审核人
  62. *
  63. * @param {Number} materialId - 材料调差期id
  64. * @param {Number} times - 第几次审批
  65. * @return {Promise<*>}
  66. */
  67. async getCurAuditor(materialId, times = 1) {
  68. const sql = '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` ' +
  69. 'FROM ?? AS la, ?? AS pa ' +
  70. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  71. ' and la.`aid` = pa.`id`';
  72. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  73. return await this.db.queryOne(sql, sqlParam);
  74. }
  75. /**
  76. * 获取 最新审核顺序
  77. *
  78. * @param {Number} materialId - 材料调差期id
  79. * @param {Number} times - 第几次审批
  80. * @return {Promise<number>}
  81. */
  82. async getNewOrder(materialId, times = 1) {
  83. const sql = 'SELECT Max(??) As max_order FROM ?? Where `mid` = ? and `times` = ?';
  84. const sqlParam = ['order', this.tableName, materialId, times];
  85. const result = await this.db.queryOne(sql, sqlParam);
  86. return result && result.max_order ? result.max_order + 1 : 1;
  87. }
  88. /**
  89. * 新增审核人
  90. *
  91. * @param {Number} materialId - 材料调差期id
  92. * @param {Number} auditorId - 审核人id
  93. * @param {Number} times - 第几次审批
  94. * @return {Promise<number>}
  95. */
  96. async addAuditor(materialId, auditorId, times = 1) {
  97. const newOrder = await this.getNewOrder(materialId, times);
  98. const data = {
  99. tid: this.ctx.tender.id,
  100. mid: materialId,
  101. aid: auditorId,
  102. times,
  103. order: newOrder,
  104. status: auditConst.status.uncheck,
  105. };
  106. const result = await this.db.insert(this.tableName, data);
  107. return result.effectRows = 1;
  108. }
  109. /**
  110. * 移除审核人时,同步其后审核人order
  111. * @param transaction - 事务
  112. * @param {Number} materialId - 材料调差期id
  113. * @param {Number} auditorId - 审核人id
  114. * @param {Number} times - 第几次审批
  115. * @return {Promise<*>}
  116. * @private
  117. */
  118. async _syncOrderByDelete(transaction, materialId, order, times) {
  119. this.initSqlBuilder();
  120. this.sqlBuilder.setAndWhere('mid', {
  121. value: materialId,
  122. operate: '=',
  123. });
  124. this.sqlBuilder.setAndWhere('order', {
  125. value: order,
  126. operate: '>=',
  127. });
  128. this.sqlBuilder.setAndWhere('times', {
  129. value: times,
  130. operate: '=',
  131. });
  132. this.sqlBuilder.setUpdateData('order', {
  133. value: 1,
  134. selfOperate: '-',
  135. });
  136. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  137. const data = await transaction.query(sql, sqlParam);
  138. return data;
  139. }
  140. /**
  141. * 移除审核人
  142. *
  143. * @param {Number} materialId - 材料调差期id
  144. * @param {Number} auditorId - 审核人id
  145. * @param {Number} times - 第几次审批
  146. * @return {Promise<boolean>}
  147. */
  148. async deleteAuditor(materialId, auditorId, times = 1) {
  149. const transaction = await this.db.beginTransaction();
  150. try {
  151. const condition = { mid: materialId, aid: auditorId, times };
  152. const auditor = await this.getDataByCondition(condition);
  153. if (!auditor) {
  154. throw '该审核人不存在';
  155. }
  156. await this._syncOrderByDelete(transaction, materialId, auditor.order, times);
  157. await transaction.delete(this.tableName, condition);
  158. await transaction.commit();
  159. } catch (err) {
  160. await transaction.rollback();
  161. throw err;
  162. }
  163. return true;
  164. }
  165. /**
  166. * 开始审批
  167. * @param {Number} materialId - 材料调差期id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<boolean>}
  170. */
  171. async start(materialId, times = 1) {
  172. const audit = await this.getDataByCondition({ mid: materialId, times, order: 1 });
  173. if (!audit) {
  174. throw '请先选择审批人,再上报数据';
  175. }
  176. const transaction = await this.db.beginTransaction();
  177. try {
  178. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  179. await transaction.update(this.ctx.service.material.tableName, {
  180. id: materialId, status: auditConst.status.checking,
  181. });
  182. // 本期一些必要数据(如应耗数量和上期调差金额)插入到material_bills_history表里
  183. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  184. if (materialBillsData.length === 0) {
  185. throw '调差工料不能为空';
  186. }
  187. const mbhList = [];
  188. for (const mb of materialBillsData) {
  189. if (mb.code === '') {
  190. throw '调差工料编号不能为空';
  191. }
  192. const newMbh = {
  193. tid: this.ctx.tender.id,
  194. mid: this.ctx.material.id,
  195. order: this.ctx.material.order,
  196. mb_id: mb.id,
  197. quantity: mb.quantity,
  198. expr: mb.expr,
  199. msg_tp: mb.msg_tp,
  200. msg_times: mb.msg_times,
  201. msg_spread: mb.msg_spread,
  202. m_up_risk: mb.m_up_risk,
  203. m_down_risk: mb.m_down_risk,
  204. m_spread: mb.m_spread,
  205. m_tp: mb.m_tp,
  206. pre_tp: mb.pre_tp,
  207. };
  208. mbhList.push(newMbh);
  209. }
  210. await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  211. // 添加短信通知-需要审批提醒功能
  212. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  213. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  214. // const smsType = JSON.parse(smsUser.sms_type);
  215. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  216. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  217. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  218. // const sms = new SMS(this.ctx);
  219. // const tenderName = await sms.contentChange(tenderInfo.name);
  220. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  221. // sms.send(smsUser.auth_mobile, content);
  222. // }
  223. // }
  224. // todo 更新标段tender状态 ?
  225. await transaction.commit();
  226. } catch (err) {
  227. await transaction.rollback();
  228. throw err;
  229. }
  230. return true;
  231. }
  232. async _checked(pid, materialId, checkData, times) {
  233. const time = new Date();
  234. // 整理当前流程审核人状态更新
  235. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  236. if (!audit) {
  237. throw '审核数据错误';
  238. }
  239. const nextAudit = await this.getDataByCondition({ mid: materialId, times, order: audit.order + 1 });
  240. const transaction = await this.db.beginTransaction();
  241. try {
  242. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  243. // 无下一审核人表示,审核结束
  244. if (nextAudit) {
  245. // 复制一份下一审核人数据
  246. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  247. // 流程至下一审批人
  248. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  249. // 添加记录到推送表
  250. // 同步 期信息
  251. await transaction.update(this.ctx.service.material.tableName, {
  252. id: materialId, status: auditConst.status.checking,
  253. });
  254. // 添加短信通知-需要审批提醒功能
  255. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  256. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  257. // const smsType = JSON.parse(smsUser.sms_type);
  258. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  259. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  260. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  261. // const sms = new SMS(this.ctx);
  262. // const tenderName = await sms.contentChange(tenderInfo.name);
  263. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  264. // sms.send(smsUser.auth_mobile, content);
  265. // }
  266. // }
  267. } else {
  268. // 本期结束
  269. // 生成截止本期数据 final数据
  270. // console.time('generatePre');
  271. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  272. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  273. // console.timeEnd('generatePre');
  274. // 同步 期信息
  275. await transaction.update(this.ctx.service.material.tableName, {
  276. id: materialId, status: checkData.checkType,
  277. });
  278. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId)
  279. // 审核结束,将原报添加到推送表
  280. await transaction.insert('zh_notice', { pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, is_read: 0, content: noticeContent });
  281. // 添加短信通知-审批通过提醒功能
  282. // const mobile_array = [];
  283. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  284. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  285. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  286. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  287. // const smsType = JSON.parse(smsUser1.sms_type);
  288. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  289. // mobile_array.push(smsUser1.auth_mobile);
  290. // }
  291. // }
  292. // for (const user of auditList) {
  293. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  294. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  295. // const smsType = JSON.parse(smsUser.sms_type);
  296. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  297. // mobile_array.push(smsUser.auth_mobile);
  298. // }
  299. // }
  300. // }
  301. // if (mobile_array.length > 0) {
  302. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  303. // const sms = new SMS(this.ctx);
  304. // const tenderName = await sms.contentChange(tenderInfo.name);
  305. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  306. // sms.send(mobile_array, content);
  307. // }
  308. }
  309. await transaction.commit();
  310. } catch (err) {
  311. await transaction.rollback();
  312. throw err;
  313. }
  314. }
  315. async _checkNo(pid, materialId, checkData, times) {
  316. const time = new Date();
  317. // 整理当前流程审核人状态更新
  318. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  319. if (!audit) {
  320. throw '审核数据错误';
  321. }
  322. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  323. const sqlParam = [this.tableName, materialId, times];
  324. const auditors = await this.db.query(sql, sqlParam);
  325. let order = 1;
  326. for (const a of auditors) {
  327. a.times = times + 1;
  328. a.order = order;
  329. a.status = auditConst.status.uncheck;
  330. order++;
  331. }
  332. const transaction = await this.db.beginTransaction();
  333. try {
  334. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  335. // 添加到消息推送表
  336. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId)
  337. await transaction.insert('zh_notice', { pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, is_read: 0, content: noticeContent });
  338. // 同步期信息
  339. await transaction.update(this.ctx.service.material.tableName, {
  340. id: materialId, status: checkData.checkType,
  341. times: times + 1,
  342. });
  343. // 拷贝新一次审核流程列表
  344. await transaction.insert(this.tableName, auditors);
  345. // 删除material_bills_history信息
  346. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  347. tid: this.ctx.tender.id,
  348. order: this.ctx.material.order,
  349. });
  350. // 计算该审批人最终数据
  351. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  352. // 复制一份最新数据给原报
  353. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  354. // 添加短信通知-审批退回提醒功能
  355. // const mobile_array = [];
  356. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  357. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  358. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  359. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  360. // const smsType = JSON.parse(smsUser1.sms_type);
  361. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  362. // mobile_array.push(smsUser1.auth_mobile);
  363. // }
  364. // }
  365. // for (const user of auditList) {
  366. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  367. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  368. // const smsType = JSON.parse(smsUser.sms_type);
  369. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  370. // mobile_array.push(smsUser.auth_mobile);
  371. // }
  372. // }
  373. // }
  374. // if (mobile_array.length > 0) {
  375. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  376. // const sms = new SMS(this.ctx);
  377. // const tenderName = await sms.contentChange(tenderInfo.name);
  378. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  379. // sms.send(mobile_array, content);
  380. // }
  381. await transaction.commit();
  382. } catch (err) {
  383. await transaction.rollback();
  384. throw err;
  385. }
  386. }
  387. async _checkNoPre(materialId, checkData, times) {
  388. const time = new Date();
  389. // 整理当前流程审核人状态更新
  390. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  391. if (!audit || audit.order <= 1) {
  392. throw '审核数据错误';
  393. }
  394. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  395. const auditors2 = await this.getAuditGroupByList(materialId, times);
  396. const auditorIndex = await auditors2.findIndex(function(item) {
  397. return item.aid === audit.aid;
  398. });
  399. const preAuditor = auditors2[auditorIndex - 1];
  400. const transaction = await this.db.beginTransaction();
  401. try {
  402. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  403. // 顺移气候审核人流程顺序
  404. this.initSqlBuilder();
  405. this.sqlBuilder.setAndWhere('mid', { value: materialId, operate: '=' });
  406. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  407. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  408. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  409. const data = await transaction.query(sql, sqlParam);
  410. const newAuditors = [];
  411. newAuditors.push({
  412. tid: audit.tid, mid: audit.mid, aid: preAuditor.aid,
  413. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  414. begin_time: time,
  415. });
  416. newAuditors.push({
  417. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  418. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  419. });
  420. await transaction.insert(this.tableName, newAuditors);
  421. await transaction.commit();
  422. } catch (error) {
  423. await transaction.rollback();
  424. throw error;
  425. }
  426. }
  427. /**
  428. * 审批
  429. * @param {Number} materialId - 材料调差期id
  430. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  431. * @param {Number} times - 第几次审批
  432. * @return {Promise<void>}
  433. */
  434. async check(materialId, checkData, times = 1) {
  435. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  436. throw '提交数据错误';
  437. }
  438. const pid = this.ctx.session.sessionProject.id;
  439. switch (checkData.checkType) {
  440. case auditConst.status.checked:
  441. await this._checked(pid, materialId, checkData, times);
  442. break;
  443. case auditConst.status.checkNo:
  444. await this._checkNo(pid, materialId, checkData, times);
  445. break;
  446. case auditConst.status.checkNoPre:
  447. await this._checkNoPre(materialId, checkData, times);
  448. break;
  449. default:
  450. throw '无效审批操作';
  451. }
  452. }
  453. /**
  454. * 用于添加推送所需的content内容
  455. * @param {Number} pid 项目id
  456. * @param {Number} tid 台账id
  457. * @param {Number} mid 期id
  458. */
  459. async getNoticeContent(pid, tid, mid) {
  460. const noticeSql = 'SELECT * FROM (SELECT ' +
  461. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  462. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  463. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  464. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  465. ' LEFT JOIN ?? As pa ON t.`user_id` = pa.`id`' +
  466. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  467. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, pid];
  468. const content = await this.db.query(noticeSql, noticeSqlParam);
  469. return content.length ? JSON.stringify(content[0]) : '';
  470. }
  471. /**
  472. * 审批
  473. * @param {Number} materialId - 材料调差期id
  474. * @param {Number} times - 第几次审批
  475. * @return {Promise<void>}
  476. */
  477. async checkAgain(materialId, times = 1) {
  478. const time = new Date();
  479. // 整理当前流程审核人状态更新
  480. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  481. if (!audit || audit.order <= 1) {
  482. throw '审核数据错误';
  483. }
  484. const transaction = await this.db.beginTransaction();
  485. try {
  486. // 当前审批人2次添加至流程中
  487. const newAuditors = [];
  488. newAuditors.push({
  489. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  490. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  491. begin_time: time, end_time: time, opinion: '',
  492. });
  493. newAuditors.push({
  494. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  495. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  496. begin_time: time,
  497. });
  498. await transaction.insert(this.tableName, newAuditors);
  499. // 复制一份最新数据给下一人
  500. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  501. // 本期结束
  502. // 生成截止本期数据 final数据
  503. // await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  504. // await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  505. // 同步 期信息
  506. await transaction.update(this.ctx.service.material.tableName, {
  507. id: materialId, status: auditConst.status.checking,
  508. });
  509. // // 添加短信通知-需要审批提醒功能
  510. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  511. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  512. // const smsType = JSON.parse(smsUser.sms_type);
  513. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  514. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  515. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  516. // const sms = new SMS(this.ctx);
  517. // const tenderName = await sms.contentChange(tenderInfo.name);
  518. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  519. // sms.send(smsUser.auth_mobile, content);
  520. // }
  521. // }
  522. await transaction.commit();
  523. } catch (err) {
  524. await transaction.rollback();
  525. throw err;
  526. }
  527. }
  528. /**
  529. * 获取审核人需要审核的期列表
  530. *
  531. * @param auditorId
  532. * @return {Promise<*>}
  533. */
  534. async getAuditMaterial(auditorId) {
  535. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  536. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  537. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  538. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  539. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  540. ' and ma.`mid` = m.`id` and ma.`tid` = t.`id`';
  541. const sqlParam = [this.tableName, this.ctx.service.material.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking, auditorId, auditConst.status.checkNo, auditConst.status.checkNo];
  542. return await this.db.query(sql, sqlParam);
  543. }
  544. /**
  545. * 获取 某时间后 审批进度 更新的期
  546. * @param {Number} pid - 查询标段
  547. * @param {Number} uid - 查询人
  548. * @param {Date} time - 查询时间
  549. * @return {Promise<*>}
  550. */
  551. async getNoticeMaterial(pid, uid, time) {
  552. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  553. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  554. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  555. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  556. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  557. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  558. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  559. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  560. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  561. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  562. // ' ORDER BY new_t.`end_time`';
  563. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  564. // this.ctx.service.projectAccount.tableName, time, pid];
  565. // return await this.db.query(sql, sqlParam);
  566. let notice = await this.db.select('zh_notice', {
  567. where: { pid, type: pushType.material, uid, is_read: 0 },
  568. });
  569. notice = notice.map(v => {
  570. const extra = JSON.parse(v.content)
  571. delete v.content
  572. return { ...v, ...extra }
  573. })
  574. return notice;
  575. }
  576. /**
  577. * 获取审核人流程列表
  578. *
  579. * @param auditorId
  580. * @return {Promise<*>}
  581. */
  582. async getAuditGroupByList(materialId, times) {
  583. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  584. 'FROM ?? AS la, ?? AS pa ' +
  585. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  586. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  587. return await this.db.query(sql, sqlParam);
  588. }
  589. /**
  590. * 复制上一期的审批人列表给最新一期
  591. *
  592. * @param transaction - 新增一期的事务
  593. * @param {Object} preMaterial - 上一期
  594. * @param {Object} newaMaterial - 最新一期
  595. * @return {Promise<*>}
  596. */
  597. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  598. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  599. const newAuditors = [];
  600. for (const a of auditors) {
  601. const na = {
  602. tid: preMaterial.tid,
  603. mid: newMaterial.id,
  604. aid: a.aid,
  605. times: newMaterial.times,
  606. order: newAuditors.length + 1,
  607. status: auditConst.status.uncheck,
  608. };
  609. newAuditors.push(na);
  610. }
  611. const result = await transaction.insert(this.tableName, newAuditors);
  612. return result.affectedRows === auditors.length;
  613. }
  614. /**
  615. * 移除审核人
  616. *
  617. * @param {Number} materialId - 材料调差期id
  618. * @param {Number} status - 期状态
  619. * @param {Number} status - 期次数
  620. * @return {Promise<boolean>}
  621. */
  622. async getAuditorByStatus(materialId, status, times = 1) {
  623. let auditor = null;
  624. let sql = '';
  625. let sqlParam = '';
  626. switch (status) {
  627. case auditConst.status.checking :
  628. case auditConst.status.checked :
  629. case auditConst.status.checkNoPre :
  630. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  631. 'FROM ?? AS la, ?? AS pa ' +
  632. 'WHERE la.`mid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  633. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  634. auditor = await this.db.queryOne(sql, sqlParam);
  635. break;
  636. case auditConst.status.checkNo :
  637. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  638. 'FROM ?? AS la, ?? AS pa ' +
  639. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  640. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  641. auditor = await this.db.queryOne(sql, sqlParam);
  642. break;
  643. case auditConst.status.uncheck :
  644. default:break;
  645. }
  646. return auditor;
  647. }
  648. async getAllAuditors(tenderId) {
  649. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  650. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  651. ' WHERE t.id = ?' +
  652. ' GROUP BY ma.aid';
  653. const sqlParam = [tenderId];
  654. return this.db.query(sql, sqlParam);
  655. }
  656. }
  657. return MaterialAudit;
  658. };