material_audit.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. // 获取审核人列表
  240. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  241. const sqlParam = [this.tableName, materialId, times];
  242. const auditors = await this.db.query(sql, sqlParam);
  243. const nextAudit = await this.getDataByCondition({ mid: materialId, times, order: audit.order + 1 });
  244. const transaction = await this.db.beginTransaction();
  245. try {
  246. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  247. // 获取推送必要信息
  248. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid);
  249. // 添加推送
  250. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, content: noticeContent }];
  251. auditors.forEach(audit => {
  252. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  253. });
  254. await transaction.insert('zh_notice', records);
  255. // 无下一审核人表示,审核结束
  256. if (nextAudit) {
  257. // 复制一份下一审核人数据
  258. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  259. // 流程至下一审批人
  260. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  261. // 同步 期信息
  262. await transaction.update(this.ctx.service.material.tableName, {
  263. id: materialId, status: auditConst.status.checking,
  264. });
  265. // 添加短信通知-需要审批提醒功能
  266. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  267. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  268. // const smsType = JSON.parse(smsUser.sms_type);
  269. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  270. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  271. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  272. // const sms = new SMS(this.ctx);
  273. // const tenderName = await sms.contentChange(tenderInfo.name);
  274. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  275. // sms.send(smsUser.auth_mobile, content);
  276. // }
  277. // }
  278. } else {
  279. // 本期结束
  280. // 生成截止本期数据 final数据
  281. // console.time('generatePre');
  282. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  283. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  284. // console.timeEnd('generatePre');
  285. // 同步 期信息
  286. await transaction.update(this.ctx.service.material.tableName, {
  287. id: materialId, status: checkData.checkType,
  288. });
  289. // 添加短信通知-审批通过提醒功能
  290. // const mobile_array = [];
  291. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  292. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  293. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  294. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  295. // const smsType = JSON.parse(smsUser1.sms_type);
  296. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  297. // mobile_array.push(smsUser1.auth_mobile);
  298. // }
  299. // }
  300. // for (const user of auditList) {
  301. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  302. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  303. // const smsType = JSON.parse(smsUser.sms_type);
  304. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  305. // mobile_array.push(smsUser.auth_mobile);
  306. // }
  307. // }
  308. // }
  309. // if (mobile_array.length > 0) {
  310. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  311. // const sms = new SMS(this.ctx);
  312. // const tenderName = await sms.contentChange(tenderInfo.name);
  313. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  314. // sms.send(mobile_array, content);
  315. // }
  316. }
  317. await transaction.commit();
  318. } catch (err) {
  319. await transaction.rollback();
  320. throw err;
  321. }
  322. }
  323. async _checkNo(pid, materialId, checkData, times) {
  324. const time = new Date();
  325. // 整理当前流程审核人状态更新
  326. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  327. if (!audit) {
  328. throw '审核数据错误';
  329. }
  330. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  331. const sqlParam = [this.tableName, materialId, times];
  332. const auditors = await this.db.query(sql, sqlParam);
  333. let order = 1;
  334. for (const a of auditors) {
  335. a.times = times + 1;
  336. a.order = order;
  337. a.status = auditConst.status.uncheck;
  338. order++;
  339. }
  340. const transaction = await this.db.beginTransaction();
  341. try {
  342. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  343. // 添加到消息推送表
  344. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid);
  345. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  346. auditors.forEach(audit => {
  347. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  348. });
  349. await transaction.insert('zh_notice', records);
  350. // 同步期信息
  351. await transaction.update(this.ctx.service.material.tableName, {
  352. id: materialId, status: checkData.checkType,
  353. times: times + 1,
  354. });
  355. // 拷贝新一次审核流程列表
  356. await transaction.insert(this.tableName, auditors);
  357. // 删除material_bills_history信息
  358. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  359. tid: this.ctx.tender.id,
  360. order: this.ctx.material.order,
  361. });
  362. // 计算该审批人最终数据
  363. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  364. // 复制一份最新数据给原报
  365. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  366. // 添加短信通知-审批退回提醒功能
  367. // const mobile_array = [];
  368. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  369. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  370. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  371. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  372. // const smsType = JSON.parse(smsUser1.sms_type);
  373. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  374. // mobile_array.push(smsUser1.auth_mobile);
  375. // }
  376. // }
  377. // for (const user of auditList) {
  378. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  379. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  380. // const smsType = JSON.parse(smsUser.sms_type);
  381. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  382. // mobile_array.push(smsUser.auth_mobile);
  383. // }
  384. // }
  385. // }
  386. // if (mobile_array.length > 0) {
  387. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  388. // const sms = new SMS(this.ctx);
  389. // const tenderName = await sms.contentChange(tenderInfo.name);
  390. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  391. // sms.send(mobile_array, content);
  392. // }
  393. await transaction.commit();
  394. } catch (err) {
  395. await transaction.rollback();
  396. throw err;
  397. }
  398. }
  399. async _checkNoPre(pid, materialId, checkData, times) {
  400. const time = new Date();
  401. // 整理当前流程审核人状态更新
  402. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  403. if (!audit || audit.order <= 1) {
  404. throw '审核数据错误';
  405. }
  406. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  407. const auditors2 = await this.getAuditGroupByList(materialId, times);
  408. const auditorIndex = await auditors2.findIndex(function(item) {
  409. return item.aid === audit.aid;
  410. });
  411. const preAuditor = auditors2[auditorIndex - 1];
  412. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid);
  413. const transaction = await this.db.beginTransaction();
  414. try {
  415. // 添加到消息推送表
  416. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  417. auditors2.forEach(audit => {
  418. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  419. });
  420. await transaction.insert('zh_notice', records);
  421. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  422. // 顺移气候审核人流程顺序
  423. this.initSqlBuilder();
  424. this.sqlBuilder.setAndWhere('mid', { value: materialId, operate: '=' });
  425. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  426. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  427. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  428. const data = await transaction.query(sql, sqlParam);
  429. const newAuditors = [];
  430. newAuditors.push({
  431. tid: audit.tid, mid: audit.mid, aid: preAuditor.aid,
  432. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  433. begin_time: time,
  434. });
  435. newAuditors.push({
  436. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  437. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  438. });
  439. await transaction.insert(this.tableName, newAuditors);
  440. await transaction.commit();
  441. } catch (error) {
  442. await transaction.rollback();
  443. throw error;
  444. }
  445. }
  446. /**
  447. * 审批
  448. * @param {Number} materialId - 材料调差期id
  449. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  450. * @param {Number} times - 第几次审批
  451. * @return {Promise<void>}
  452. */
  453. async check(materialId, checkData, times = 1) {
  454. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  455. throw '提交数据错误';
  456. }
  457. const pid = this.ctx.session.sessionProject.id;
  458. switch (checkData.checkType) {
  459. case auditConst.status.checked:
  460. await this._checked(pid, materialId, checkData, times);
  461. break;
  462. case auditConst.status.checkNo:
  463. await this._checkNo(pid, materialId, checkData, times);
  464. break;
  465. case auditConst.status.checkNoPre:
  466. await this._checkNoPre(pid, materialId, checkData, times);
  467. break;
  468. default:
  469. throw '无效审批操作';
  470. }
  471. }
  472. /**
  473. * 用于添加推送所需的content内容
  474. * @param {Number} pid 项目id
  475. * @param {Number} tid 台账id
  476. * @param {Number} mid 期id
  477. * @param {Number} uid 审批人id
  478. */
  479. async getNoticeContent(pid, tid, mid, uid) {
  480. const noticeSql = 'SELECT * FROM (SELECT ' +
  481. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  482. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  483. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  484. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  485. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  486. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  487. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  488. const content = await this.db.query(noticeSql, noticeSqlParam);
  489. return content.length ? JSON.stringify(content[0]) : '';
  490. }
  491. /**
  492. * 审批
  493. * @param {Number} materialId - 材料调差期id
  494. * @param {Number} times - 第几次审批
  495. * @return {Promise<void>}
  496. */
  497. async checkAgain(materialId, times = 1) {
  498. const time = new Date();
  499. // 整理当前流程审核人状态更新
  500. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  501. if (!audit || audit.order <= 1) {
  502. throw '审核数据错误';
  503. }
  504. const transaction = await this.db.beginTransaction();
  505. try {
  506. // 当前审批人2次添加至流程中
  507. const newAuditors = [];
  508. newAuditors.push({
  509. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  510. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  511. begin_time: time, end_time: time, opinion: '',
  512. });
  513. newAuditors.push({
  514. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  515. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  516. begin_time: time,
  517. });
  518. await transaction.insert(this.tableName, newAuditors);
  519. // 复制一份最新数据给下一人
  520. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  521. // 本期结束
  522. // 生成截止本期数据 final数据
  523. // await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  524. // await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  525. // 同步 期信息
  526. await transaction.update(this.ctx.service.material.tableName, {
  527. id: materialId, status: auditConst.status.checking,
  528. });
  529. // // 添加短信通知-需要审批提醒功能
  530. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  531. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  532. // const smsType = JSON.parse(smsUser.sms_type);
  533. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  534. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  535. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  536. // const sms = new SMS(this.ctx);
  537. // const tenderName = await sms.contentChange(tenderInfo.name);
  538. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  539. // sms.send(smsUser.auth_mobile, content);
  540. // }
  541. // }
  542. await transaction.commit();
  543. } catch (err) {
  544. await transaction.rollback();
  545. throw err;
  546. }
  547. }
  548. /**
  549. * 获取审核人需要审核的期列表
  550. *
  551. * @param auditorId
  552. * @return {Promise<*>}
  553. */
  554. async getAuditMaterial(auditorId) {
  555. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  556. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  557. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  558. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  559. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  560. ' and ma.`mid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  561. 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];
  562. return await this.db.query(sql, sqlParam);
  563. }
  564. /**
  565. * 获取 某时间后 审批进度 更新的期
  566. * @param {Number} pid - 查询标段
  567. * @param {Number} uid - 查询人
  568. * @param {Date} time - 查询时间
  569. * @return {Promise<*>}
  570. */
  571. async getNoticeMaterial(pid, uid, time) {
  572. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  573. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  574. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  575. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  576. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  577. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  578. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  579. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  580. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  581. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  582. // ' ORDER BY new_t.`end_time`';
  583. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  584. // this.ctx.service.projectAccount.tableName, time, pid];
  585. // return await this.db.query(sql, sqlParam);
  586. let notice = await this.db.select('zh_notice', {
  587. where: { pid, type: pushType.material, uid },
  588. orders: [['create_time', 'desc']],
  589. limit: 10, offset: 0,
  590. });
  591. notice = notice.map(v => {
  592. const extra = JSON.parse(v.content);
  593. delete v.content;
  594. return { ...v, ...extra };
  595. });
  596. return notice;
  597. }
  598. /**
  599. * 获取审核人流程列表
  600. *
  601. * @param auditorId
  602. * @return {Promise<*>}
  603. */
  604. async getAuditGroupByList(materialId, times) {
  605. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  606. 'FROM ?? AS la, ?? AS pa ' +
  607. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  608. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  609. return await this.db.query(sql, sqlParam);
  610. }
  611. /**
  612. * 复制上一期的审批人列表给最新一期
  613. *
  614. * @param transaction - 新增一期的事务
  615. * @param {Object} preMaterial - 上一期
  616. * @param {Object} newaMaterial - 最新一期
  617. * @return {Promise<*>}
  618. */
  619. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  620. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  621. const newAuditors = [];
  622. for (const a of auditors) {
  623. const na = {
  624. tid: preMaterial.tid,
  625. mid: newMaterial.id,
  626. aid: a.aid,
  627. times: newMaterial.times,
  628. order: newAuditors.length + 1,
  629. status: auditConst.status.uncheck,
  630. };
  631. newAuditors.push(na);
  632. }
  633. const result = await transaction.insert(this.tableName, newAuditors);
  634. return result.affectedRows === auditors.length;
  635. }
  636. /**
  637. * 移除审核人
  638. *
  639. * @param {Number} materialId - 材料调差期id
  640. * @param {Number} status - 期状态
  641. * @param {Number} status - 期次数
  642. * @return {Promise<boolean>}
  643. */
  644. async getAuditorByStatus(materialId, status, times = 1) {
  645. let auditor = null;
  646. let sql = '';
  647. let sqlParam = '';
  648. switch (status) {
  649. case auditConst.status.checking :
  650. case auditConst.status.checked :
  651. case auditConst.status.checkNoPre :
  652. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  653. 'FROM ?? AS la, ?? AS pa ' +
  654. 'WHERE la.`mid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  655. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  656. auditor = await this.db.queryOne(sql, sqlParam);
  657. break;
  658. case auditConst.status.checkNo :
  659. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  660. 'FROM ?? AS la, ?? AS pa ' +
  661. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  662. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  663. auditor = await this.db.queryOne(sql, sqlParam);
  664. break;
  665. case auditConst.status.uncheck :
  666. default:break;
  667. }
  668. return auditor;
  669. }
  670. async getAllAuditors(tenderId) {
  671. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  672. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  673. ' WHERE t.id = ?' +
  674. ' GROUP BY ma.aid';
  675. const sqlParam = [tenderId];
  676. return this.db.query(sql, sqlParam);
  677. }
  678. }
  679. return MaterialAudit;
  680. };