material_audit.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. * @returns {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. * @returns {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` ' +
  50. 'FROM ?? AS la, ?? AS pa ' +
  51. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`order`';
  52. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  53. return await this.db.query(sql, sqlParam);
  54. }
  55. /**
  56. * 获取 当前审核人
  57. *
  58. * @param {Number} materialId - 材料调差期id
  59. * @param {Number} times - 第几次审批
  60. * @returns {Promise<*>}
  61. */
  62. async getCurAuditor(materialId, times = 1) {
  63. 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` ' +
  64. 'FROM ?? AS la, ?? AS pa ' +
  65. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  66. ' and la.`aid` = pa.`id`';
  67. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  68. return await this.db.queryOne(sql, sqlParam);
  69. }
  70. /**
  71. * 获取 最新审核顺序
  72. *
  73. * @param {Number} materialId - 材料调差期id
  74. * @param {Number} times - 第几次审批
  75. * @returns {Promise<number>}
  76. */
  77. async getNewOrder(materialId, times = 1) {
  78. const sql = 'SELECT Max(??) As max_order FROM ?? Where `mid` = ? and `times` = ?';
  79. const sqlParam = ['order', this.tableName, materialId, times];
  80. const result = await this.db.queryOne(sql, sqlParam);
  81. return result && result.max_order ? result.max_order + 1 : 1;
  82. }
  83. /**
  84. * 新增审核人
  85. *
  86. * @param {Number} materialId - 材料调差期id
  87. * @param {Number} auditorId - 审核人id
  88. * @param {Number} times - 第几次审批
  89. * @return {Promise<number>}
  90. */
  91. async addAuditor(materialId, auditorId, times = 1) {
  92. const newOrder = await this.getNewOrder(materialId, times);
  93. const data = {
  94. tid: this.ctx.tender.id,
  95. mid: materialId,
  96. aid: auditorId,
  97. times,
  98. order: newOrder,
  99. status: auditConst.status.uncheck,
  100. };
  101. const result = await this.db.insert(this.tableName, data);
  102. return result.effectRows = 1;
  103. }
  104. /**
  105. * 移除审核人时,同步其后审核人order
  106. * @param transaction - 事务
  107. * @param {Number} materialId - 材料调差期id
  108. * @param {Number} auditorId - 审核人id
  109. * @param {Number} times - 第几次审批
  110. * @returns {Promise<*>}
  111. * @private
  112. */
  113. async _syncOrderByDelete(transaction, materialId, order, times) {
  114. this.initSqlBuilder();
  115. this.sqlBuilder.setAndWhere('mid', {
  116. value: materialId,
  117. operate: '=',
  118. });
  119. this.sqlBuilder.setAndWhere('order', {
  120. value: order,
  121. operate: '>=',
  122. });
  123. this.sqlBuilder.setAndWhere('times', {
  124. value: times,
  125. operate: '=',
  126. });
  127. this.sqlBuilder.setUpdateData('order', {
  128. value: 1,
  129. selfOperate: '-',
  130. });
  131. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  132. const data = await transaction.query(sql, sqlParam);
  133. return data;
  134. }
  135. /**
  136. * 移除审核人
  137. *
  138. * @param {Number} materialId - 材料调差期id
  139. * @param {Number} auditorId - 审核人id
  140. * @param {Number} times - 第几次审批
  141. * @return {Promise<boolean>}
  142. */
  143. async deleteAuditor(materialId, auditorId, times = 1) {
  144. const transaction = await this.db.beginTransaction();
  145. try {
  146. const condition = { mid: materialId, aid: auditorId, times };
  147. const auditor = await this.getDataByCondition(condition);
  148. if (!auditor) {
  149. throw '该审核人不存在';
  150. }
  151. await this._syncOrderByDelete(transaction, materialId, auditor.order, times);
  152. await transaction.delete(this.tableName, condition);
  153. await transaction.commit();
  154. } catch (err) {
  155. await transaction.rollback();
  156. throw err;
  157. }
  158. return true;
  159. }
  160. /**
  161. * 开始审批
  162. *
  163. * @param {Number} materialId - 材料调差期id
  164. * @param {Number} times - 第几次审批
  165. * @return {Promise<boolean>}
  166. */
  167. async start(materialId, times = 1) {
  168. const audit = await this.getDataByCondition({ mid: materialId, times, order: 1 });
  169. if (!audit) {
  170. throw '请先选择审批人,再上报数据';
  171. }
  172. const transaction = await this.db.beginTransaction();
  173. try {
  174. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  175. await transaction.update(this.ctx.service.material.tableName, {
  176. id: materialId, status: auditConst.status.checking,
  177. });
  178. // 本期应耗数量和上期调差金额插入到material_bills_history表里
  179. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  180. const mbhList = [];
  181. for (const mb of materialBillsData) {
  182. const newMbh = {
  183. tid: this.ctx.tender.id,
  184. mid: this.ctx.material.id,
  185. order: this.ctx.material.order,
  186. mb_id: mb.id,
  187. quantity: mb.quantity,
  188. pre_tp: mb.pre_tp,
  189. };
  190. mbhList.push(newMbh);
  191. }
  192. await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  193. // 添加短信通知-需要审批提醒功能
  194. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  195. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  196. // const smsType = JSON.parse(smsUser.sms_type);
  197. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  198. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  199. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  200. // const sms = new SMS(this.ctx);
  201. // const tenderName = await sms.contentChange(tenderInfo.name);
  202. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  203. // sms.send(smsUser.auth_mobile, content);
  204. // }
  205. // }
  206. // todo 更新标段tender状态 ?
  207. await transaction.commit();
  208. } catch (err) {
  209. await transaction.rollback();
  210. throw err;
  211. }
  212. return true;
  213. }
  214. async _checked(materialId, checkData, times) {
  215. const time = new Date();
  216. // 整理当前流程审核人状态更新
  217. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  218. if (!audit) {
  219. throw '审核数据错误';
  220. }
  221. const nextAudit = await this.getDataByCondition({ mid: materialId, times, order: audit.order + 1 });
  222. const transaction = await this.db.beginTransaction();
  223. try {
  224. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  225. // 无下一审核人表示,审核结束
  226. if (nextAudit) {
  227. // 复制一份下一审核人数据
  228. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  229. // 流程至下一审批人
  230. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  231. // 同步 期信息
  232. await transaction.update(this.ctx.service.material.tableName, {
  233. id: materialId, status: auditConst.status.checking,
  234. });
  235. // 添加短信通知-需要审批提醒功能
  236. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  237. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  238. // const smsType = JSON.parse(smsUser.sms_type);
  239. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  240. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  241. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  242. // const sms = new SMS(this.ctx);
  243. // const tenderName = await sms.contentChange(tenderInfo.name);
  244. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  245. // sms.send(smsUser.auth_mobile, content);
  246. // }
  247. // }
  248. } else {
  249. // 本期结束
  250. // 生成截止本期数据 final数据
  251. // console.time('generatePre');
  252. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  253. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  254. // console.timeEnd('generatePre');
  255. // 同步 期信息
  256. await transaction.update(this.ctx.service.material.tableName, {
  257. id: materialId, status: checkData.checkType,
  258. });
  259. // 添加短信通知-审批通过提醒功能
  260. // const mobile_array = [];
  261. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  262. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  263. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  264. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  265. // const smsType = JSON.parse(smsUser1.sms_type);
  266. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  267. // mobile_array.push(smsUser1.auth_mobile);
  268. // }
  269. // }
  270. // for (const user of auditList) {
  271. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  272. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  273. // const smsType = JSON.parse(smsUser.sms_type);
  274. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  275. // mobile_array.push(smsUser.auth_mobile);
  276. // }
  277. // }
  278. // }
  279. // if (mobile_array.length > 0) {
  280. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  281. // const sms = new SMS(this.ctx);
  282. // const tenderName = await sms.contentChange(tenderInfo.name);
  283. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  284. // sms.send(mobile_array, content);
  285. // }
  286. }
  287. await transaction.commit();
  288. } catch (err) {
  289. await transaction.rollback();
  290. throw err;
  291. }
  292. }
  293. async _checkNo(materialId, checkData, times) {
  294. const time = new Date();
  295. // 整理当前流程审核人状态更新
  296. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  297. if (!audit) {
  298. throw '审核数据错误';
  299. }
  300. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid`';
  301. const sqlParam = [this.tableName, materialId, times];
  302. const auditors = await this.db.query(sql, sqlParam);
  303. let order = 1;
  304. for (const a of auditors) {
  305. a.times = times + 1;
  306. a.order = order;
  307. a.status = auditConst.status.uncheck;
  308. order++;
  309. }
  310. const transaction = await this.db.beginTransaction();
  311. try {
  312. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  313. // 同步 期信息
  314. await transaction.update(this.ctx.service.material.tableName, {
  315. id: materialId, status: checkData.checkType,
  316. times: times + 1,
  317. });
  318. // 拷贝新一次审核流程列表
  319. await transaction.insert(this.tableName, auditors);
  320. // 删除material_bills_history信息
  321. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  322. tid: this.ctx.tender.id,
  323. order: this.ctx.material.order,
  324. });
  325. // 计算该审批人最终数据
  326. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  327. // 复制一份最新数据给原报
  328. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  329. // 添加短信通知-审批退回提醒功能
  330. // const mobile_array = [];
  331. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  332. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  333. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  334. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  335. // const smsType = JSON.parse(smsUser1.sms_type);
  336. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  337. // mobile_array.push(smsUser1.auth_mobile);
  338. // }
  339. // }
  340. // for (const user of auditList) {
  341. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  342. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  343. // const smsType = JSON.parse(smsUser.sms_type);
  344. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  345. // mobile_array.push(smsUser.auth_mobile);
  346. // }
  347. // }
  348. // }
  349. // if (mobile_array.length > 0) {
  350. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  351. // const sms = new SMS(this.ctx);
  352. // const tenderName = await sms.contentChange(tenderInfo.name);
  353. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  354. // sms.send(mobile_array, content);
  355. // }
  356. await transaction.commit();
  357. } catch (err) {
  358. await transaction.rollback();
  359. throw err;
  360. }
  361. }
  362. /**
  363. * 审批
  364. * @param {Number} materialId - 材料调差期id
  365. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  366. * @param {Number} times - 第几次审批
  367. * @returns {Promise<void>}
  368. */
  369. async check(materialId, checkData, times = 1) {
  370. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo) {
  371. throw '提交数据错误';
  372. }
  373. switch (checkData.checkType) {
  374. case auditConst.status.checked:
  375. await this._checked(materialId, checkData, times);
  376. break;
  377. case auditConst.status.checkNo:
  378. await this._checkNo(materialId, checkData, times);
  379. break;
  380. default:
  381. throw '无效审批操作';
  382. }
  383. }
  384. /**
  385. * 审批
  386. * @param {Number} materialId - 材料调差期id
  387. * @param {Number} times - 第几次审批
  388. * @returns {Promise<void>}
  389. */
  390. async checkAgain(materialId, times = 1) {
  391. const time = new Date();
  392. // 整理当前流程审核人状态更新
  393. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  394. if (!audit || audit.order <= 1) {
  395. throw '审核数据错误';
  396. }
  397. const transaction = await this.db.beginTransaction();
  398. try {
  399. // 当前审批人2次添加至流程中
  400. const newAuditors = [];
  401. newAuditors.push({
  402. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  403. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  404. begin_time: time, end_time: time, opinion: '',
  405. });
  406. newAuditors.push({
  407. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  408. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  409. begin_time: time,
  410. });
  411. await transaction.insert(this.tableName, newAuditors);
  412. // 复制一份最新数据给下一人
  413. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  414. // 本期结束
  415. // 生成截止本期数据 final数据
  416. // await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  417. // await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  418. // 同步 期信息
  419. await transaction.update(this.ctx.service.material.tableName, {
  420. id: materialId, status: auditConst.status.checking,
  421. });
  422. // // 添加短信通知-需要审批提醒功能
  423. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  424. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  425. // const smsType = JSON.parse(smsUser.sms_type);
  426. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  427. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  428. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  429. // const sms = new SMS(this.ctx);
  430. // const tenderName = await sms.contentChange(tenderInfo.name);
  431. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  432. // sms.send(smsUser.auth_mobile, content);
  433. // }
  434. // }
  435. await transaction.commit();
  436. } catch (err) {
  437. await transaction.rollback();
  438. throw err;
  439. }
  440. }
  441. /**
  442. * 获取审核人需要审核的期列表
  443. *
  444. * @param auditorId
  445. * @returns {Promise<*>}
  446. */
  447. async getAuditMaterial(auditorId) {
  448. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`mid`,' +
  449. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  450. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  451. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  452. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  453. ' and sa.`mid` = s.`id` and sa.`tid` = t.`id`';
  454. 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];
  455. return await this.db.query(sql, sqlParam);
  456. }
  457. /**
  458. * 获取审核人流程列表
  459. *
  460. * @param auditorId
  461. * @returns {Promise<*>}
  462. */
  463. async getAuditGroupByList(materialId, times) {
  464. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  465. 'FROM ?? AS la, ?? AS pa ' +
  466. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  467. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  468. return await this.db.query(sql, sqlParam);
  469. }
  470. /**
  471. * 复制上一期的审批人列表给最新一期
  472. *
  473. * @param transaction - 新增一期的事务
  474. * @param {Object} preMaterial - 上一期
  475. * @param {Object} newaMaterial - 最新一期
  476. * @returns {Promise<*>}
  477. */
  478. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  479. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  480. const newAuditors = [];
  481. for (const a of auditors) {
  482. const na = {
  483. tid: preMaterial.tid,
  484. mid: newMaterial.id,
  485. aid: a.aid,
  486. times: newMaterial.times,
  487. order: newAuditors.length + 1,
  488. status: auditConst.status.uncheck,
  489. };
  490. newAuditors.push(na);
  491. }
  492. const result = await transaction.insert(this.tableName, newAuditors);
  493. return result.effectRows = auditors.length;
  494. }
  495. /**
  496. * 移除审核人
  497. *
  498. * @param {Number} materialId - 材料调差期id
  499. * @param {Number} status - 期状态
  500. * @param {Number} status - 期次数
  501. * @return {Promise<boolean>}
  502. */
  503. async getAuditorByStatus(materialId, status, times = 1) {
  504. let auditor = null;
  505. let sql = '';
  506. let sqlParam = '';
  507. switch (status) {
  508. case auditConst.status.checking :
  509. case auditConst.status.checked :
  510. case auditConst.status.checkNoPre :
  511. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  512. 'FROM ?? AS la, ?? AS pa ' +
  513. 'WHERE la.`mid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  514. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  515. auditor = await this.db.queryOne(sql, sqlParam);
  516. break;
  517. case auditConst.status.checkNo :
  518. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  519. 'FROM ?? AS la, ?? AS pa ' +
  520. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  521. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  522. auditor = await this.db.queryOne(sql, sqlParam);
  523. break;
  524. case auditConst.status.uncheck :
  525. default:break;
  526. }
  527. return auditor;
  528. }
  529. }
  530. return MaterialAudit;
  531. };