material_audit.js 31 KB

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