material_audit.js 28 KB

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