material_audit.js 34 KB

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