material_audit.js 31 KB

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