material_audit.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. // 计算原报最终数据
  175. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  176. // 复制一份下一审核人数据
  177. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, 1, transaction);
  178. // 更新期数据
  179. // const tpData = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  180. await transaction.update(this.ctx.service.material.tableName, {
  181. id: materialId, status: auditConst.status.checking,
  182. });
  183. // 添加短信通知-需要审批提醒功能
  184. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  185. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  186. // const smsType = JSON.parse(smsUser.sms_type);
  187. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  188. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  189. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  190. // const sms = new SMS(this.ctx);
  191. // const tenderName = await sms.contentChange(tenderInfo.name);
  192. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  193. // sms.send(smsUser.auth_mobile, content);
  194. // }
  195. // }
  196. // todo 更新标段tender状态 ?
  197. await transaction.commit();
  198. } catch (err) {
  199. await transaction.rollback();
  200. throw err;
  201. }
  202. return true;
  203. }
  204. async _checked(materialId, checkData, times) {
  205. const time = new Date();
  206. // 整理当前流程审核人状态更新
  207. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  208. if (!audit) {
  209. throw '审核数据错误';
  210. }
  211. const nextAudit = await this.getDataByCondition({ mid: materialId, times, order: audit.order + 1 });
  212. const transaction = await this.db.beginTransaction();
  213. try {
  214. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  215. // 无下一审核人表示,审核结束
  216. if (nextAudit) {
  217. // 复制一份下一审核人数据
  218. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  219. // 流程至下一审批人
  220. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  221. // 同步 期信息
  222. await transaction.update(this.ctx.service.material.tableName, {
  223. id: materialId, status: auditConst.status.checking,
  224. });
  225. // 添加短信通知-需要审批提醒功能
  226. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  227. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  228. // const smsType = JSON.parse(smsUser.sms_type);
  229. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  230. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  231. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  232. // const sms = new SMS(this.ctx);
  233. // const tenderName = await sms.contentChange(tenderInfo.name);
  234. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  235. // sms.send(smsUser.auth_mobile, content);
  236. // }
  237. // }
  238. } else {
  239. // 本期结束
  240. // 生成截止本期数据 final数据
  241. // console.time('generatePre');
  242. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  243. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  244. // console.timeEnd('generatePre');
  245. // 同步 期信息
  246. await transaction.update(this.ctx.service.material.tableName, {
  247. id: materialId, status: checkData.checkType,
  248. });
  249. // 添加短信通知-审批通过提醒功能
  250. // const mobile_array = [];
  251. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  252. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  253. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  254. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  255. // const smsType = JSON.parse(smsUser1.sms_type);
  256. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  257. // mobile_array.push(smsUser1.auth_mobile);
  258. // }
  259. // }
  260. // for (const user of auditList) {
  261. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  262. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  263. // const smsType = JSON.parse(smsUser.sms_type);
  264. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  265. // mobile_array.push(smsUser.auth_mobile);
  266. // }
  267. // }
  268. // }
  269. // if (mobile_array.length > 0) {
  270. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  271. // const sms = new SMS(this.ctx);
  272. // const tenderName = await sms.contentChange(tenderInfo.name);
  273. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  274. // sms.send(mobile_array, content);
  275. // }
  276. }
  277. await transaction.commit();
  278. } catch (err) {
  279. await transaction.rollback();
  280. throw err;
  281. }
  282. }
  283. async _checkNo(materialId, checkData, times) {
  284. const time = new Date();
  285. // 整理当前流程审核人状态更新
  286. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  287. if (!audit) {
  288. throw '审核数据错误';
  289. }
  290. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid`';
  291. const sqlParam = [this.tableName, materialId, times];
  292. const auditors = await this.db.query(sql, sqlParam);
  293. let order = 1;
  294. for (const a of auditors) {
  295. a.times = times + 1;
  296. a.order = order;
  297. a.status = auditConst.status.uncheck;
  298. order++;
  299. }
  300. const transaction = await this.db.beginTransaction();
  301. try {
  302. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  303. // 同步 期信息
  304. await transaction.update(this.ctx.service.material.tableName, {
  305. id: materialId, status: checkData.checkType,
  306. times: times + 1,
  307. });
  308. // 拷贝新一次审核流程列表
  309. await transaction.insert(this.tableName, auditors);
  310. // 计算该审批人最终数据
  311. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  312. // 复制一份最新数据给原报
  313. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  314. // 添加短信通知-审批退回提醒功能
  315. // const mobile_array = [];
  316. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  317. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  318. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  319. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  320. // const smsType = JSON.parse(smsUser1.sms_type);
  321. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  322. // mobile_array.push(smsUser1.auth_mobile);
  323. // }
  324. // }
  325. // for (const user of auditList) {
  326. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  327. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  328. // const smsType = JSON.parse(smsUser.sms_type);
  329. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  330. // mobile_array.push(smsUser.auth_mobile);
  331. // }
  332. // }
  333. // }
  334. // if (mobile_array.length > 0) {
  335. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  336. // const sms = new SMS(this.ctx);
  337. // const tenderName = await sms.contentChange(tenderInfo.name);
  338. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  339. // sms.send(mobile_array, content);
  340. // }
  341. await transaction.commit();
  342. } catch (err) {
  343. await transaction.rollback();
  344. throw err;
  345. }
  346. }
  347. /**
  348. * 审批
  349. * @param {Number} materialId - 材料调差期id
  350. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  351. * @param {Number} times - 第几次审批
  352. * @returns {Promise<void>}
  353. */
  354. async check(materialId, checkData, times = 1) {
  355. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo) {
  356. throw '提交数据错误';
  357. }
  358. switch (checkData.checkType) {
  359. case auditConst.status.checked:
  360. await this._checked(materialId, checkData, times);
  361. break;
  362. case auditConst.status.checkNo:
  363. await this._checkNo(materialId, checkData, times);
  364. break;
  365. default:
  366. throw '无效审批操作';
  367. }
  368. }
  369. /**
  370. * 审批
  371. * @param {Number} materialId - 材料调差期id
  372. * @param {Number} times - 第几次审批
  373. * @returns {Promise<void>}
  374. */
  375. async checkAgain(materialId, times = 1) {
  376. const time = new Date();
  377. // 整理当前流程审核人状态更新
  378. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  379. if (!audit || audit.order <= 1) {
  380. throw '审核数据错误';
  381. }
  382. const transaction = await this.db.beginTransaction();
  383. try {
  384. // 当前审批人2次添加至流程中
  385. const newAuditors = [];
  386. newAuditors.push({
  387. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  388. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  389. begin_time: time, end_time: time, opinion: '',
  390. });
  391. newAuditors.push({
  392. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  393. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  394. begin_time: time,
  395. });
  396. await transaction.insert(this.tableName, newAuditors);
  397. // 复制一份最新数据给下一人
  398. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  399. // 本期结束
  400. // 生成截止本期数据 final数据
  401. // await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  402. // await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  403. // 同步 期信息
  404. await transaction.update(this.ctx.service.material.tableName, {
  405. id: materialId, status: auditConst.status.checking,
  406. });
  407. // // 添加短信通知-需要审批提醒功能
  408. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  409. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  410. // const smsType = JSON.parse(smsUser.sms_type);
  411. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  412. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  413. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  414. // const sms = new SMS(this.ctx);
  415. // const tenderName = await sms.contentChange(tenderInfo.name);
  416. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  417. // sms.send(smsUser.auth_mobile, content);
  418. // }
  419. // }
  420. await transaction.commit();
  421. } catch (err) {
  422. await transaction.rollback();
  423. throw err;
  424. }
  425. }
  426. /**
  427. * 获取审核人需要审核的期列表
  428. *
  429. * @param auditorId
  430. * @returns {Promise<*>}
  431. */
  432. async getAuditMaterial(auditorId) {
  433. const sql = 'SELECT sa.`aid`, sa.`times`, sa.`order`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`mid`,' +
  434. ' s.`order` As `sorder`, s.`status` As `sstatus`,' +
  435. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  436. ' FROM ?? AS sa, ?? AS s, ?? As t ' +
  437. ' WHERE ((sa.`aid` = ? and sa.`status` = ?) OR (s.`user_id` = ? and sa.`status` = ? and s.`status` = ? and sa.`times` = (s.`times`-1)))' +
  438. ' and sa.`mid` = s.`id` and sa.`tid` = t.`id`';
  439. 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];
  440. return await this.db.query(sql, sqlParam);
  441. }
  442. /**
  443. * 获取审核人流程列表
  444. *
  445. * @param auditorId
  446. * @returns {Promise<*>}
  447. */
  448. async getAuditGroupByList(materialId, times) {
  449. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  450. 'FROM ?? AS la, ?? AS pa ' +
  451. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  452. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  453. return await this.db.query(sql, sqlParam);
  454. }
  455. /**
  456. * 复制上一期的审批人列表给最新一期
  457. *
  458. * @param transaction - 新增一期的事务
  459. * @param {Object} preMaterial - 上一期
  460. * @param {Object} newaMaterial - 最新一期
  461. * @returns {Promise<*>}
  462. */
  463. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  464. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  465. const newAuditors = [];
  466. for (const a of auditors) {
  467. const na = {
  468. tid: preMaterial.tid,
  469. mid: newMaterial.id,
  470. aid: a.aid,
  471. times: newMaterial.times,
  472. order: newAuditors.length + 1,
  473. status: auditConst.status.uncheck,
  474. };
  475. newAuditors.push(na);
  476. }
  477. const result = await transaction.insert(this.tableName, newAuditors);
  478. return result.effectRows = auditors.length;
  479. }
  480. /**
  481. * 移除审核人
  482. *
  483. * @param {Number} materialId - 材料调差期id
  484. * @param {Number} status - 期状态
  485. * @param {Number} status - 期次数
  486. * @return {Promise<boolean>}
  487. */
  488. async getAuditorByStatus(materialId, status, times = 1) {
  489. let auditor = null;
  490. let sql = '';
  491. let sqlParam = '';
  492. switch (status) {
  493. case auditConst.status.checking :
  494. case auditConst.status.checked :
  495. case auditConst.status.checkNoPre :
  496. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  497. 'FROM ?? AS la, ?? AS pa ' +
  498. 'WHERE la.`mid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  499. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  500. auditor = await this.db.queryOne(sql, sqlParam);
  501. break;
  502. case auditConst.status.checkNo :
  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.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`id` desc';
  506. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  507. auditor = await this.db.queryOne(sql, sqlParam);
  508. break;
  509. case auditConst.status.uncheck :
  510. default:break;
  511. }
  512. return auditor;
  513. }
  514. }
  515. return MaterialAudit;
  516. };