material_audit.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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. * @return {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. * @return {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. * @return {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. * @return {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. * @return {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. async _checkNoPre(materialId, checkData, times) {
  384. const time = new Date();
  385. // 整理当前流程审核人状态更新
  386. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  387. console.log('audit', audit);
  388. if (!audit || audit.order <= 1) {
  389. throw '审核数据错误';
  390. }
  391. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  392. const auditors2 = await this.getAuditGroupByList(materialId, times);
  393. const auditorIndex = await auditors2.findIndex(function(item) {
  394. return item.aid === audit.aid;
  395. });
  396. const preAuditor = auditors2[auditorIndex - 1];
  397. const transaction = await this.db.beginTransaction();
  398. try {
  399. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  400. const newAuditors = [];
  401. newAuditors.push({
  402. tid: audit.tid, mid: audit.mid, aid: preAuditor.aid,
  403. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  404. begin_time: time,
  405. });
  406. newAuditors.push({
  407. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  408. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  409. });
  410. await transaction.insert(this.tableName, newAuditors);
  411. await transaction.commit();
  412. } catch (error) {
  413. await transaction.rollback();
  414. throw error;
  415. }
  416. }
  417. /**
  418. * 审批
  419. * @param {Number} materialId - 材料调差期id
  420. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  421. * @param {Number} times - 第几次审批
  422. * @return {Promise<void>}
  423. */
  424. async check(materialId, checkData, times = 1) {
  425. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  426. throw '提交数据错误';
  427. }
  428. console.log('checkData:', checkData);
  429. console.log('times', times);
  430. switch (checkData.checkType) {
  431. case auditConst.status.checked:
  432. await this._checked(materialId, checkData, times);
  433. break;
  434. case auditConst.status.checkNo:
  435. await this._checkNo(materialId, checkData, times);
  436. break;
  437. case auditConst.status.checkNoPre:
  438. await this._checkNoPre(materialId, checkData, times);
  439. break;
  440. default:
  441. throw '无效审批操作';
  442. }
  443. }
  444. /**
  445. * 审批
  446. * @param {Number} materialId - 材料调差期id
  447. * @param {Number} times - 第几次审批
  448. * @return {Promise<void>}
  449. */
  450. async checkAgain(materialId, times = 1) {
  451. const time = new Date();
  452. // 整理当前流程审核人状态更新
  453. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  454. if (!audit || audit.order <= 1) {
  455. throw '审核数据错误';
  456. }
  457. const transaction = await this.db.beginTransaction();
  458. try {
  459. // 当前审批人2次添加至流程中
  460. const newAuditors = [];
  461. newAuditors.push({
  462. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  463. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  464. begin_time: time, end_time: time, opinion: '',
  465. });
  466. newAuditors.push({
  467. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  468. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  469. begin_time: time,
  470. });
  471. await transaction.insert(this.tableName, newAuditors);
  472. // 复制一份最新数据给下一人
  473. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  474. // 本期结束
  475. // 生成截止本期数据 final数据
  476. // await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  477. // await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  478. // 同步 期信息
  479. await transaction.update(this.ctx.service.material.tableName, {
  480. id: materialId, status: auditConst.status.checking,
  481. });
  482. // // 添加短信通知-需要审批提醒功能
  483. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  484. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  485. // const smsType = JSON.parse(smsUser.sms_type);
  486. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  487. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  488. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  489. // const sms = new SMS(this.ctx);
  490. // const tenderName = await sms.contentChange(tenderInfo.name);
  491. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  492. // sms.send(smsUser.auth_mobile, content);
  493. // }
  494. // }
  495. await transaction.commit();
  496. } catch (err) {
  497. await transaction.rollback();
  498. throw err;
  499. }
  500. }
  501. /**
  502. * 获取审核人需要审核的期列表
  503. *
  504. * @param auditorId
  505. * @return {Promise<*>}
  506. */
  507. async getAuditMaterial(auditorId) {
  508. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  509. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  510. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  511. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  512. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  513. ' and ma.`mid` = m.`id` and ma.`tid` = t.`id`';
  514. 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];
  515. return await this.db.query(sql, sqlParam);
  516. }
  517. /**
  518. * 获取 某时间后 审批进度 更新的期
  519. * @param {Number} pid - 查询标段
  520. * @param {Number} uid - 查询人
  521. * @param {Date} time - 查询时间
  522. * @return {Promise<*>}
  523. */
  524. async getNoticeMaterial(pid, uid, time) {
  525. const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  526. ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  527. ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  528. ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  529. ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  530. ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  531. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  532. ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  533. ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  534. ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  535. ' ORDER BY new_t.`end_time`';
  536. const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  537. this.ctx.service.projectAccount.tableName, time, pid];
  538. return await this.db.query(sql, sqlParam);
  539. }
  540. /**
  541. * 获取审核人流程列表
  542. *
  543. * @param auditorId
  544. * @return {Promise<*>}
  545. */
  546. async getAuditGroupByList(materialId, times) {
  547. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  548. 'FROM ?? AS la, ?? AS pa ' +
  549. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  550. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  551. return await this.db.query(sql, sqlParam);
  552. }
  553. /**
  554. * 复制上一期的审批人列表给最新一期
  555. *
  556. * @param transaction - 新增一期的事务
  557. * @param {Object} preMaterial - 上一期
  558. * @param {Object} newaMaterial - 最新一期
  559. * @return {Promise<*>}
  560. */
  561. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  562. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  563. const newAuditors = [];
  564. for (const a of auditors) {
  565. const na = {
  566. tid: preMaterial.tid,
  567. mid: newMaterial.id,
  568. aid: a.aid,
  569. times: newMaterial.times,
  570. order: newAuditors.length + 1,
  571. status: auditConst.status.uncheck,
  572. };
  573. newAuditors.push(na);
  574. }
  575. const result = await transaction.insert(this.tableName, newAuditors);
  576. return result.affectedRows === auditors.length;
  577. }
  578. /**
  579. * 移除审核人
  580. *
  581. * @param {Number} materialId - 材料调差期id
  582. * @param {Number} status - 期状态
  583. * @param {Number} status - 期次数
  584. * @return {Promise<boolean>}
  585. */
  586. async getAuditorByStatus(materialId, status, times = 1) {
  587. let auditor = null;
  588. let sql = '';
  589. let sqlParam = '';
  590. switch (status) {
  591. case auditConst.status.checking :
  592. case auditConst.status.checked :
  593. case auditConst.status.checkNoPre :
  594. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  595. 'FROM ?? AS la, ?? AS pa ' +
  596. 'WHERE la.`mid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  597. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  598. auditor = await this.db.queryOne(sql, sqlParam);
  599. break;
  600. case auditConst.status.checkNo :
  601. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  602. 'FROM ?? AS la, ?? AS pa ' +
  603. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  604. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  605. auditor = await this.db.queryOne(sql, sqlParam);
  606. break;
  607. case auditConst.status.uncheck :
  608. default:break;
  609. }
  610. return auditor;
  611. }
  612. async getAllAuditors(tenderId) {
  613. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  614. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  615. ' WHERE t.id = ?' +
  616. ' GROUP BY ma.aid';
  617. const sqlParam = [tenderId];
  618. return this.db.query(sql, sqlParam);
  619. }
  620. }
  621. return MaterialAudit;
  622. };