material_audit.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 pushType = require('../const/audit').pushType;
  11. const smsTypeConst = require('../const/sms_type');
  12. const wxConst = require('../const/wechat_template');
  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. * @param {Number} materialId - 材料调差期id
  170. * @param {Number} times - 第几次审批
  171. * @return {Promise<boolean>}
  172. */
  173. async start(materialId, times = 1) {
  174. const audit = await this.getDataByCondition({ mid: materialId, times, order: 1 });
  175. if (!audit) {
  176. throw '请先选择审批人,再上报数据';
  177. }
  178. const transaction = await this.db.beginTransaction();
  179. try {
  180. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date() });
  181. await transaction.update(this.ctx.service.material.tableName, {
  182. id: materialId, status: auditConst.status.checking,
  183. });
  184. // 本期一些必要数据(如应耗数量和上期调差金额)插入到material_bills_history表里
  185. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  186. if (materialBillsData.length === 0) {
  187. throw '调差工料不能为空';
  188. }
  189. const mbhList = [];
  190. for (const mb of materialBillsData) {
  191. if (mb.code === '') {
  192. throw '调差工料编号不能为空';
  193. }
  194. const newMbh = {
  195. tid: this.ctx.tender.id,
  196. mid: this.ctx.material.id,
  197. order: this.ctx.material.order,
  198. mb_id: mb.id,
  199. quantity: mb.quantity,
  200. expr: mb.expr,
  201. msg_tp: mb.msg_tp,
  202. msg_times: mb.msg_times,
  203. msg_spread: mb.msg_spread,
  204. m_up_risk: mb.m_up_risk,
  205. m_down_risk: mb.m_down_risk,
  206. m_spread: mb.m_spread,
  207. m_tp: mb.m_tp,
  208. pre_tp: mb.pre_tp,
  209. is_summary: mb.is_summary,
  210. };
  211. mbhList.push(newMbh);
  212. }
  213. await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  214. const materialExponentData = await this.ctx.service.materialExponent.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  215. const mehList = [];
  216. for (const me of materialExponentData) {
  217. const newMeh = {
  218. tid: this.ctx.tender.id,
  219. mid: this.ctx.material.id,
  220. order: this.ctx.material.order,
  221. me_id: me.id,
  222. type: me.type,
  223. weight_num: me.weight_num,
  224. basic_price: me.basic_price,
  225. basic_times: me.basic_times,
  226. m_price: me.m_price,
  227. calc_num: me.calc_num,
  228. is_summary: me.is_summary,
  229. };
  230. mehList.push(newMeh);
  231. }
  232. console.log(mehList);
  233. await transaction.insert(this.ctx.service.materialExponentHistory.tableName, mehList);
  234. // 微信模板通知
  235. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  236. const wechatData = {
  237. qi: materialInfo.order,
  238. status: wxConst.status.check,
  239. tips: wxConst.tips.check,
  240. begin_time: Date.parse(new Date()),
  241. m_tp: this.ctx.helper.round(materialInfo.m_tp, 2),
  242. hs_m_tp: this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), 2),
  243. };
  244. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  245. // 添加短信通知-需要审批提醒功能
  246. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  247. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  248. // const smsType = JSON.parse(smsUser.sms_type);
  249. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  250. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  251. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  252. // const sms = new SMS(this.ctx);
  253. // const tenderName = await sms.contentChange(tenderInfo.name);
  254. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  255. // sms.send(smsUser.auth_mobile, content);
  256. // }
  257. // }
  258. // todo 更新标段tender状态 ?
  259. await transaction.commit();
  260. } catch (err) {
  261. await transaction.rollback();
  262. throw err;
  263. }
  264. return true;
  265. }
  266. async _checked(pid, materialId, checkData, times) {
  267. const time = new Date();
  268. // 整理当前流程审核人状态更新
  269. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  270. if (!audit) {
  271. throw '审核数据错误';
  272. }
  273. // 获取审核人列表
  274. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  275. const sqlParam = [this.tableName, materialId, times];
  276. const auditors = await this.db.query(sql, sqlParam);
  277. const nextAudit = await this.getDataByCondition({ mid: materialId, times, order: audit.order + 1 });
  278. const transaction = await this.db.beginTransaction();
  279. try {
  280. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  281. // 获取推送必要信息
  282. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid);
  283. // 添加推送
  284. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, content: noticeContent }];
  285. auditors.forEach(audit => {
  286. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  287. });
  288. await transaction.insert('zh_notice', records);
  289. const begin_audit = await this.getDataByCondition({
  290. mid: materialId,
  291. order: 1,
  292. });
  293. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  294. // 无下一审核人表示,审核结束
  295. if (nextAudit) {
  296. // 复制一份下一审核人数据
  297. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  298. // 流程至下一审批人
  299. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  300. // 同步 期信息
  301. await transaction.update(this.ctx.service.material.tableName, {
  302. id: materialId, status: auditConst.status.checking,
  303. });
  304. // 微信模板通知
  305. const wechatData = {
  306. qi: materialInfo.order,
  307. status: wxConst.status.check,
  308. tips: wxConst.tips.check,
  309. begin_time: Date.parse(begin_audit.begin_time),
  310. m_tp: this.ctx.helper.round(materialInfo.m_tp, 2),
  311. hs_m_tp: this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), 2),
  312. };
  313. await this.ctx.helper.sendWechat(nextAudit.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  314. // 添加短信通知-需要审批提醒功能
  315. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  316. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  317. // const smsType = JSON.parse(smsUser.sms_type);
  318. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  319. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  320. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  321. // const sms = new SMS(this.ctx);
  322. // const tenderName = await sms.contentChange(tenderInfo.name);
  323. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  324. // sms.send(smsUser.auth_mobile, content);
  325. // }
  326. // }
  327. } else {
  328. // 本期结束
  329. // 生成截止本期数据 final数据
  330. // console.time('generatePre');
  331. // await this.ctx.service.stageBillsFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  332. // await this.ctx.service.stagePosFinal.generateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  333. // console.timeEnd('generatePre');
  334. // 同步 期信息
  335. await transaction.update(this.ctx.service.material.tableName, {
  336. id: materialId, status: checkData.checkType,
  337. });
  338. // 微信模板通知
  339. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  340. const wechatData = {
  341. qi: materialInfo.order,
  342. status: wxConst.status.success,
  343. tips: wxConst.tips.success,
  344. begin_time: Date.parse(begin_audit.begin_time),
  345. m_tp: this.ctx.helper.round(materialInfo.m_tp, 2),
  346. hs_m_tp: this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), 2),
  347. };
  348. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  349. // 添加短信通知-审批通过提醒功能
  350. // const mobile_array = [];
  351. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  352. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  353. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  354. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  355. // const smsType = JSON.parse(smsUser1.sms_type);
  356. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  357. // mobile_array.push(smsUser1.auth_mobile);
  358. // }
  359. // }
  360. // for (const user of auditList) {
  361. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  362. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  363. // const smsType = JSON.parse(smsUser.sms_type);
  364. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  365. // mobile_array.push(smsUser.auth_mobile);
  366. // }
  367. // }
  368. // }
  369. // if (mobile_array.length > 0) {
  370. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  371. // const sms = new SMS(this.ctx);
  372. // const tenderName = await sms.contentChange(tenderInfo.name);
  373. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  374. // sms.send(mobile_array, content);
  375. // }
  376. }
  377. await transaction.commit();
  378. } catch (err) {
  379. await transaction.rollback();
  380. throw err;
  381. }
  382. }
  383. async _checkNo(pid, materialId, checkData, times) {
  384. const time = new Date();
  385. // 整理当前流程审核人状态更新
  386. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  387. if (!audit) {
  388. throw '审核数据错误';
  389. }
  390. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  391. const sqlParam = [this.tableName, materialId, times];
  392. const auditors = await this.db.query(sql, sqlParam);
  393. let order = 1;
  394. for (const a of auditors) {
  395. a.times = times + 1;
  396. a.order = order;
  397. a.status = auditConst.status.uncheck;
  398. order++;
  399. }
  400. const transaction = await this.db.beginTransaction();
  401. try {
  402. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  403. // 添加到消息推送表
  404. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid);
  405. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  406. auditors.forEach(audit => {
  407. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  408. });
  409. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  410. // 同步期信息
  411. await transaction.update(this.ctx.service.material.tableName, {
  412. id: materialId, status: checkData.checkType,
  413. times: times + 1,
  414. });
  415. // 拷贝新一次审核流程列表
  416. await transaction.insert(this.tableName, auditors);
  417. // 删除material_bills_history信息
  418. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  419. tid: this.ctx.tender.id,
  420. order: this.ctx.material.order,
  421. });
  422. // 删除material_exponent_history信息
  423. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, {
  424. tid: this.ctx.tender.id,
  425. order: this.ctx.material.order,
  426. });
  427. // 微信模板通知
  428. const begin_audit = await this.getDataByCondition({
  429. mid: materialId,
  430. order: 1,
  431. });
  432. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  433. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  434. const wechatData = {
  435. qi: materialInfo.order,
  436. status: wxConst.status.back,
  437. tips: wxConst.tips.back,
  438. begin_time: Date.parse(begin_audit.begin_time),
  439. m_tp: this.ctx.helper.round(materialInfo.m_tp, 2),
  440. hs_m_tp: this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), 2),
  441. };
  442. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  443. // 计算该审批人最终数据
  444. // await this.ctx.service.stagePay.calcAllStagePays(this.ctx.stage, transaction);
  445. // 复制一份最新数据给原报
  446. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times + 1, 0, transaction);
  447. // 添加短信通知-审批退回提醒功能
  448. // const mobile_array = [];
  449. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  450. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  451. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  452. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  453. // const smsType = JSON.parse(smsUser1.sms_type);
  454. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  455. // mobile_array.push(smsUser1.auth_mobile);
  456. // }
  457. // }
  458. // for (const user of auditList) {
  459. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  460. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  461. // const smsType = JSON.parse(smsUser.sms_type);
  462. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  463. // mobile_array.push(smsUser.auth_mobile);
  464. // }
  465. // }
  466. // }
  467. // if (mobile_array.length > 0) {
  468. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  469. // const sms = new SMS(this.ctx);
  470. // const tenderName = await sms.contentChange(tenderInfo.name);
  471. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  472. // sms.send(mobile_array, content);
  473. // }
  474. await transaction.commit();
  475. } catch (err) {
  476. await transaction.rollback();
  477. throw err;
  478. }
  479. }
  480. async _checkNoPre(pid, materialId, checkData, times) {
  481. const time = new Date();
  482. // 整理当前流程审核人状态更新
  483. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  484. if (!audit || audit.order <= 1) {
  485. throw '审核数据错误';
  486. }
  487. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  488. const auditors2 = await this.getAuditGroupByList(materialId, times);
  489. const auditorIndex = await auditors2.findIndex(function(item) {
  490. return item.aid === audit.aid;
  491. });
  492. const preAuditor = auditors2[auditorIndex - 1];
  493. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid);
  494. const transaction = await this.db.beginTransaction();
  495. try {
  496. // 添加到消息推送表
  497. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  498. auditors2.forEach(audit => {
  499. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  500. });
  501. await transaction.insert('zh_notice', records);
  502. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  503. // 顺移气候审核人流程顺序
  504. this.initSqlBuilder();
  505. this.sqlBuilder.setAndWhere('mid', { value: materialId, operate: '=' });
  506. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  507. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  508. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  509. const data = await transaction.query(sql, sqlParam);
  510. const newAuditors = [];
  511. newAuditors.push({
  512. tid: audit.tid, mid: audit.mid, aid: preAuditor.aid,
  513. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  514. begin_time: time,
  515. });
  516. newAuditors.push({
  517. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  518. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  519. });
  520. // 微信模板通知
  521. const begin_audit = await this.getDataByCondition({
  522. mid: materialId,
  523. order: 1,
  524. });
  525. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  526. const wechatData = {
  527. qi: materialInfo.order,
  528. status: wxConst.status.check,
  529. tips: wxConst.tips.check,
  530. begin_time: Date.parse(begin_audit.begin_time),
  531. m_tp: this.ctx.helper.round(materialInfo.m_tp, 2),
  532. hs_m_tp: this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), 2),
  533. };
  534. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  535. await transaction.insert(this.tableName, newAuditors);
  536. await transaction.commit();
  537. } catch (error) {
  538. await transaction.rollback();
  539. throw error;
  540. }
  541. }
  542. /**
  543. * 审批
  544. * @param {Number} materialId - 材料调差期id
  545. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  546. * @param {Number} times - 第几次审批
  547. * @return {Promise<void>}
  548. */
  549. async check(materialId, checkData, times = 1) {
  550. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  551. throw '提交数据错误';
  552. }
  553. const pid = this.ctx.session.sessionProject.id;
  554. switch (checkData.checkType) {
  555. case auditConst.status.checked:
  556. await this._checked(pid, materialId, checkData, times);
  557. break;
  558. case auditConst.status.checkNo:
  559. await this._checkNo(pid, materialId, checkData, times);
  560. break;
  561. case auditConst.status.checkNoPre:
  562. await this._checkNoPre(pid, materialId, checkData, times);
  563. break;
  564. default:
  565. throw '无效审批操作';
  566. }
  567. }
  568. /**
  569. * 用于添加推送所需的content内容
  570. * @param {Number} pid 项目id
  571. * @param {Number} tid 台账id
  572. * @param {Number} mid 期id
  573. * @param {Number} uid 审批人id
  574. */
  575. async getNoticeContent(pid, tid, mid, uid) {
  576. const noticeSql = 'SELECT * FROM (SELECT ' +
  577. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  578. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  579. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  580. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  581. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  582. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  583. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  584. const content = await this.db.query(noticeSql, noticeSqlParam);
  585. return content.length ? JSON.stringify(content[0]) : '';
  586. }
  587. /**
  588. * 审批
  589. * @param {Number} materialId - 材料调差期id
  590. * @param {Number} times - 第几次审批
  591. * @return {Promise<void>}
  592. */
  593. async checkAgain(materialId, times = 1) {
  594. const time = new Date();
  595. // 整理当前流程审核人状态更新
  596. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  597. if (!audit || audit.order <= 1) {
  598. throw '审核数据错误';
  599. }
  600. const transaction = await this.db.beginTransaction();
  601. try {
  602. // 当前审批人2次添加至流程中
  603. const newAuditors = [];
  604. newAuditors.push({
  605. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  606. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  607. begin_time: time, end_time: time, opinion: '',
  608. });
  609. newAuditors.push({
  610. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  611. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  612. begin_time: time,
  613. });
  614. await transaction.insert(this.tableName, newAuditors);
  615. // 复制一份最新数据给下一人
  616. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, audit.order + 2, transaction);
  617. // 本期结束
  618. // 生成截止本期数据 final数据
  619. // await this.ctx.service.stageBillsFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  620. // await this.ctx.service.stagePosFinal.delGenerateFinalData(transaction, this.ctx.tender, this.ctx.stage);
  621. // 同步 期信息
  622. await transaction.update(this.ctx.service.material.tableName, {
  623. id: materialId, status: auditConst.status.checking,
  624. });
  625. // // 添加短信通知-需要审批提醒功能
  626. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  627. // if (smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  628. // const smsType = JSON.parse(smsUser.sms_type);
  629. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  630. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  631. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  632. // const sms = new SMS(this.ctx);
  633. // const tenderName = await sms.contentChange(tenderInfo.name);
  634. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  635. // sms.send(smsUser.auth_mobile, content);
  636. // }
  637. // }
  638. await transaction.commit();
  639. } catch (err) {
  640. await transaction.rollback();
  641. throw err;
  642. }
  643. }
  644. /**
  645. * 获取审核人需要审核的期列表
  646. *
  647. * @param auditorId
  648. * @return {Promise<*>}
  649. */
  650. async getAuditMaterial(auditorId) {
  651. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  652. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  653. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  654. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  655. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  656. ' and ma.`mid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  657. 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];
  658. return await this.db.query(sql, sqlParam);
  659. }
  660. /**
  661. * 获取 某时间后 审批进度 更新的期
  662. * @param {Number} pid - 查询标段
  663. * @param {Number} uid - 查询人
  664. * @param {Date} time - 查询时间
  665. * @return {Promise<*>}
  666. */
  667. async getNoticeMaterial(pid, uid, time) {
  668. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  669. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  670. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  671. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  672. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  673. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  674. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  675. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  676. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  677. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  678. // ' ORDER BY new_t.`end_time`';
  679. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  680. // this.ctx.service.projectAccount.tableName, time, pid];
  681. // return await this.db.query(sql, sqlParam);
  682. let notice = await this.db.select('zh_notice', {
  683. where: { pid, type: pushType.material, uid },
  684. orders: [['create_time', 'desc']],
  685. limit: 10, offset: 0,
  686. });
  687. notice = notice.map(v => {
  688. const extra = JSON.parse(v.content);
  689. delete v.content;
  690. return { ...v, ...extra };
  691. });
  692. return notice;
  693. }
  694. /**
  695. * 获取审核人流程列表
  696. *
  697. * @param auditorId
  698. * @return {Promise<*>}
  699. */
  700. async getAuditGroupByList(materialId, times) {
  701. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  702. 'FROM ?? AS la, ?? AS pa ' +
  703. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` GROUP BY la.`aid` ORDER BY la.`order`';
  704. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  705. return await this.db.query(sql, sqlParam);
  706. }
  707. /**
  708. * 获取审核人流程列表(包括原报)
  709. * @param {Number} materialId 调差id
  710. * @param {Number} times 审核次数
  711. * @return {Promise<Array>} 查询结果集(包括原报)
  712. */
  713. async getAuditorsWithOwner(materialId, times = 1) {
  714. const result = await this.getAuditGroupByList(materialId, times);
  715. const sql =
  716. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  717. ' FROM ' +
  718. this.ctx.service.material.tableName +
  719. ' As s' +
  720. ' LEFT JOIN ' +
  721. this.ctx.service.projectAccount.tableName +
  722. ' As pa' +
  723. ' ON s.user_id = pa.id' +
  724. ' WHERE s.id = ?';
  725. const sqlParam = [times, materialId, materialId];
  726. const user = await this.db.queryOne(sql, sqlParam);
  727. result.unshift(user);
  728. return result;
  729. }
  730. /**
  731. * 复制上一期的审批人列表给最新一期
  732. *
  733. * @param transaction - 新增一期的事务
  734. * @param {Object} preMaterial - 上一期
  735. * @param {Object} newaMaterial - 最新一期
  736. * @return {Promise<*>}
  737. */
  738. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  739. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  740. const newAuditors = [];
  741. for (const a of auditors) {
  742. const na = {
  743. tid: preMaterial.tid,
  744. mid: newMaterial.id,
  745. aid: a.aid,
  746. times: newMaterial.times,
  747. order: newAuditors.length + 1,
  748. status: auditConst.status.uncheck,
  749. };
  750. newAuditors.push(na);
  751. }
  752. const result = await transaction.insert(this.tableName, newAuditors);
  753. return result.affectedRows === auditors.length;
  754. }
  755. /**
  756. * 移除审核人
  757. *
  758. * @param {Number} materialId - 材料调差期id
  759. * @param {Number} status - 期状态
  760. * @param {Number} status - 期次数
  761. * @return {Promise<boolean>}
  762. */
  763. async getAuditorByStatus(materialId, status, times = 1) {
  764. let auditor = null;
  765. let sql = '';
  766. let sqlParam = '';
  767. switch (status) {
  768. case auditConst.status.checking :
  769. case auditConst.status.checked :
  770. case auditConst.status.checkNoPre :
  771. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  772. 'FROM ?? AS la, ?? AS pa ' +
  773. 'WHERE la.`mid` = ? and la.`status` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  774. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  775. auditor = await this.db.queryOne(sql, sqlParam);
  776. break;
  777. case auditConst.status.checkNo :
  778. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  779. 'FROM ?? AS la, ?? AS pa ' +
  780. 'WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ? and la.`aid` = pa.`id` order by la.`times` desc, la.`order` desc';
  781. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  782. auditor = await this.db.queryOne(sql, sqlParam);
  783. break;
  784. case auditConst.status.uncheck :
  785. default:break;
  786. }
  787. return auditor;
  788. }
  789. async getAllAuditors(tenderId) {
  790. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  791. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  792. ' WHERE t.id = ?' +
  793. ' GROUP BY ma.aid';
  794. const sqlParam = [tenderId];
  795. return this.db.query(sql, sqlParam);
  796. }
  797. }
  798. return MaterialAudit;
  799. };