material_audit.js 30 KB

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