material_audit.js 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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 pushOperate = require('../const/spec_3f').pushOperate;
  12. const smsTypeConst = require('../const/sms_type');
  13. const wxConst = require('../const/wechat_template');
  14. const shenpiConst = require('../const/shenpi');
  15. const materialConst = require('../const/material');
  16. module.exports = app => {
  17. class MaterialAudit extends app.BaseService {
  18. /**
  19. * 构造函数
  20. *
  21. * @param {Object} ctx - egg全局变量
  22. * @return {void}
  23. */
  24. constructor(ctx) {
  25. super(ctx);
  26. this.tableName = 'material_audit';
  27. }
  28. /**
  29. * 获取 审核人信息
  30. *
  31. * @param {Number} materialId - 材料调差期id
  32. * @param {Number} auditorId - 审核人id
  33. * @param {Number} times - 第几次审批
  34. * @return {Promise<*>}
  35. */
  36. async getAuditor(materialId, auditorId, times = 1) {
  37. 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` ' +
  38. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  39. ' WHERE la.`mid` = ? and la.`aid` = ? and la.`times` = ?';
  40. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditorId, times];
  41. return await this.db.queryOne(sql, sqlParam);
  42. }
  43. /**
  44. * 获取 审核列表信息
  45. *
  46. * @param {Number} materialId - 材料调差期id
  47. * @param {Number} times - 第几次审批
  48. * @return {Promise<*>}
  49. */
  50. async getAuditors(materialId, times = 1) {
  51. 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` ' +
  52. 'FROM ?? AS la, ?? AS pa, (SELECT t1.`aid`,(@i:=@i+1) as `sort` FROM (SELECT t.`aid`, t.`order` FROM (select `aid`, `order` from ?? WHERE `mid` = ? AND `times` = ? ORDER BY `order` LIMIT 200) t GROUP BY t.`aid` ORDER BY t.`order`) t1, (select @i:=0) as it) as g ' +
  53. 'WHERE la.`mid` = ? and la.`times` = ? and la.`aid` = pa.`id` and g.`aid` = la.`aid` order by la.`order`';
  54. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.tableName, materialId, times, materialId, times];
  55. const result = await this.db.query(sql, sqlParam);
  56. const sql2 = 'SELECT COUNT(a.`aid`) as num FROM (SELECT `aid` FROM ?? WHERE `mid` = ? AND `times` = ? GROUP BY `aid`) as a';
  57. const sqlParam2 = [this.tableName, materialId, times];
  58. const count = await this.db.queryOne(sql2, sqlParam2);
  59. for (const i in result) {
  60. result[i].max_sort = count.num;
  61. }
  62. return result;
  63. }
  64. async getFinalAuditGroup(materialId, times = 1) {
  65. const sql =
  66. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`mid`, Max(la.`order`) as max_order, GROUP_CONCAT(la.tp_data) as tp_data ' +
  67. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  68. ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  69. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  70. const result = await this.db.query(sql, sqlParam);
  71. for (const r of result) {
  72. const auditor = await this.getDataByCondition({mid: materialId, times: r.times, order: r.max_order});
  73. r.status = auditor.status;
  74. r.opinion = auditor.opinion;
  75. r.begin_time = auditor.begin_time;
  76. r.end_time = auditor.end_time;
  77. }
  78. return result;
  79. }
  80. /**
  81. * 获取 当前审核人
  82. *
  83. * @param {Number} materialId - 材料调差期id
  84. * @param {Number} times - 第几次审批
  85. * @return {Promise<*>}
  86. */
  87. async getCurAuditor(materialId, times = 1) {
  88. const sql = 'SELECT la.`id`, 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` ' +
  89. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  90. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?';
  91. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  92. return await this.db.queryOne(sql, sqlParam);
  93. }
  94. /**
  95. * 获取 最新审核顺序
  96. *
  97. * @param {Number} materialId - 材料调差期id
  98. * @param {Number} times - 第几次审批
  99. * @return {Promise<number>}
  100. */
  101. async getNewOrder(materialId, times = 1) {
  102. const sql = 'SELECT Max(??) As max_order FROM ?? Where `mid` = ? and `times` = ?';
  103. const sqlParam = ['order', this.tableName, materialId, times];
  104. const result = await this.db.queryOne(sql, sqlParam);
  105. return result && result.max_order ? result.max_order + 1 : 1;
  106. }
  107. /**
  108. * 新增审核人
  109. *
  110. * @param {Number} materialId - 材料调差期id
  111. * @param {Number} auditorId - 审核人id
  112. * @param {Number} times - 第几次审批
  113. * @return {Promise<number>}
  114. */
  115. async addAuditor(materialId, auditorId, times = 1, is_gdzs = 0) {
  116. const transaction = await this.db.beginTransaction();
  117. try {
  118. let newOrder = await this.getNewOrder(materialId, times);
  119. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  120. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  121. if (is_gdzs) await this._syncOrderByDelete(transaction, materialId, newOrder, times, '+');
  122. const data = {
  123. tid: this.ctx.tender.id,
  124. mid: materialId,
  125. aid: auditorId,
  126. times,
  127. order: newOrder,
  128. status: auditConst.status.uncheck,
  129. };
  130. const result = await transaction.insert(this.tableName, data);
  131. await transaction.commit();
  132. return result.effectRows = 1;
  133. } catch (err) {
  134. await transaction.rollback();
  135. throw err;
  136. }
  137. return false;
  138. }
  139. /**
  140. * 移除审核人时,同步其后审核人order
  141. * @param transaction - 事务
  142. * @param {Number} materialId - 材料调差期id
  143. * @param {Number} auditorId - 审核人id
  144. * @param {Number} times - 第几次审批
  145. * @return {Promise<*>}
  146. * @private
  147. */
  148. async _syncOrderByDelete(transaction, materialId, order, times, selfOperate = '-') {
  149. this.initSqlBuilder();
  150. this.sqlBuilder.setAndWhere('mid', {
  151. value: materialId,
  152. operate: '=',
  153. });
  154. this.sqlBuilder.setAndWhere('order', {
  155. value: order,
  156. operate: '>=',
  157. });
  158. this.sqlBuilder.setAndWhere('times', {
  159. value: times,
  160. operate: '=',
  161. });
  162. this.sqlBuilder.setUpdateData('order', {
  163. value: 1,
  164. selfOperate: selfOperate,
  165. });
  166. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  167. const data = await transaction.query(sql, sqlParam);
  168. return data;
  169. }
  170. /**
  171. * 移除审核人
  172. *
  173. * @param {Number} materialId - 材料调差期id
  174. * @param {Number} auditorId - 审核人id
  175. * @param {Number} times - 第几次审批
  176. * @return {Promise<boolean>}
  177. */
  178. async deleteAuditor(materialId, auditorId, times = 1) {
  179. const transaction = await this.db.beginTransaction();
  180. try {
  181. const condition = { mid: materialId, aid: auditorId, times };
  182. const auditor = await this.getDataByCondition(condition);
  183. if (!auditor) {
  184. throw '该审核人不存在';
  185. }
  186. await this._syncOrderByDelete(transaction, materialId, auditor.order, times);
  187. await transaction.delete(this.tableName, condition);
  188. await transaction.commit();
  189. } catch (err) {
  190. await transaction.rollback();
  191. throw err;
  192. }
  193. return true;
  194. }
  195. async getTpData(transaction, materialId, decimal = (this.ctx.material.decimal ? this.ctx.material.decimal : materialConst.decimal)) {
  196. const materialInfo = await transaction.get(this.ctx.service.material.tableName, { id: materialId });
  197. const tp_data = {
  198. m_tp: materialInfo.m_tp !== null ? this.ctx.helper.round(materialInfo.m_tp, decimal.tp) : null,
  199. tax_tp: materialInfo.material_tax ? (materialInfo.m_tax_tp ? materialInfo.m_tax_tp : materialInfo.m_tp) :
  200. (materialInfo.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), decimal.tp) : null),
  201. ex_tp: materialInfo.ex_tp !== null ? this.ctx.helper.round(materialInfo.ex_tp, decimal.tp) : null,
  202. ex_tax_tp: materialInfo.ex_tp !== null ? this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), decimal.tp) : null,
  203. }
  204. tp_data.total_tp = this.ctx.helper.add(tp_data.m_tp, tp_data.ex_tp);
  205. tp_data.total_tax_tp = !materialInfo.material_tax ? this.ctx.helper.add(tp_data.tax_tp, tp_data.ex_tax_tp) : tp_data.ex_tax_tp;
  206. if (materialInfo.is_stage_self) {
  207. const materialStageList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: materialId } });
  208. const stage_tp = [];
  209. for (const ms of materialStageList) {
  210. stage_tp.push({
  211. id: ms.id,
  212. sid: ms.sid,
  213. order: ms.order,
  214. m_tp: this.ctx.helper.round(ms.m_tp, decimal.tp),
  215. m_tax_tp: this.ctx.helper.round(ms.m_tax_tp, decimal.tp),
  216. });
  217. }
  218. tp_data.stage_tp = stage_tp;
  219. }
  220. return tp_data;
  221. }
  222. /**
  223. * 开始审批
  224. * @param {Number} materialId - 材料调差期id
  225. * @param {Number} times - 第几次审批
  226. * @return {Promise<boolean>}
  227. */
  228. async start(materialId, times = 1) {
  229. const audit = await this.getDataByCondition({ mid: materialId, times, order: 1 });
  230. if (!audit) {
  231. if(this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  232. throw '请联系管理员添加审批人';
  233. } else {
  234. throw '请先选择审批人,再上报数据';
  235. }
  236. }
  237. const transaction = await this.db.beginTransaction();
  238. try {
  239. const tp_data = await this.getTpData(transaction, materialId);
  240. await transaction.update(this.tableName, { id: audit.id, status: auditConst.status.checking, begin_time: new Date(), tp_data: JSON.stringify(tp_data) });
  241. await transaction.update(this.ctx.service.material.tableName, {
  242. id: materialId, status: auditConst.status.checking, tp_data: JSON.stringify(tp_data),
  243. });
  244. // 本期一些必要数据(如应耗数量和上期调差金额)插入到material_bills_history表里
  245. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  246. if (materialBillsData.length === 0 && this.ctx.material.ex_expr === null) {
  247. throw '请至少使用一种调差方式';
  248. }
  249. // 微信模板通知
  250. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  251. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  252. const wechatData = {
  253. qi: materialInfo.order,
  254. status: wxConst.status.check,
  255. tips: wxConst.tips.check,
  256. begin_time: Date.parse(new Date()),
  257. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  258. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  259. };
  260. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  261. // 添加短信通知-需要审批提醒功能
  262. // const smsUser = await this.ctx.service.projectAccount.getDataById(audit.aid);
  263. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  264. // const smsType = JSON.parse(smsUser.sms_type);
  265. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  266. // const tenderInfo = await this.ctx.service.tender.getDataById(audit.tid);
  267. // const stageInfo = await this.ctx.service.stage.getDataById(audit.sid);
  268. // const sms = new SMS(this.ctx);
  269. // const tenderName = await sms.contentChange(tenderInfo.name);
  270. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  271. // sms.send(smsUser.auth_mobile, content);
  272. // }
  273. // }
  274. // todo 更新标段tender状态 ?
  275. // 检查三方特殊推送
  276. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  277. await transaction.commit();
  278. } catch (err) {
  279. await transaction.rollback();
  280. throw err;
  281. }
  282. return true;
  283. }
  284. async _checked(pid, materialId, checkData, times) {
  285. const time = new Date();
  286. // 整理当前流程审核人状态更新
  287. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  288. if (!audit) {
  289. throw '审核数据错误';
  290. }
  291. // 获取审核人列表
  292. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  293. const sqlParam = [this.tableName, materialId, times];
  294. const auditors = await this.db.query(sql, sqlParam);
  295. const nextAudit = await this.getDataByCondition({ mid: materialId, times, order: audit.order + 1 });
  296. const transaction = await this.db.beginTransaction();
  297. try {
  298. // 获取当前总金额及独立单价期的金额,添加到tp_data中,报表使用
  299. const tp_data = await this.getTpData(transaction, materialId);
  300. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time, tp_data: JSON.stringify(tp_data) });
  301. // 获取推送必要信息
  302. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid, checkData.opinion);
  303. // 添加推送
  304. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, content: noticeContent }];
  305. auditors.forEach(audit => {
  306. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  307. });
  308. await transaction.insert('zh_notice', records);
  309. const begin_audit = await this.getDataByCondition({
  310. mid: materialId,
  311. order: 1,
  312. });
  313. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  314. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  315. // 无下一审核人表示,审核结束
  316. if (nextAudit) {
  317. // 复制一份下一审核人数据
  318. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  319. // 流程至下一审批人
  320. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time, tp_data: JSON.stringify(tp_data) });
  321. // 同步 期信息
  322. await transaction.update(this.ctx.service.material.tableName, {
  323. id: materialId, status: auditConst.status.checking,
  324. });
  325. // 微信模板通知
  326. const wechatData = {
  327. qi: materialInfo.order,
  328. status: wxConst.status.check,
  329. tips: wxConst.tips.check,
  330. begin_time: Date.parse(begin_audit.begin_time),
  331. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  332. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  333. };
  334. await this.ctx.helper.sendWechat(nextAudit.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  335. // 检查三方特殊推送
  336. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  337. // 添加短信通知-需要审批提醒功能
  338. // const smsUser = await this.ctx.service.projectAccount.getDataById(nextAudit.aid);
  339. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  340. // const smsType = JSON.parse(smsUser.sms_type);
  341. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  342. // const tenderInfo = await this.ctx.service.tender.getDataById(nextAudit.tid);
  343. // const stageInfo = await this.ctx.service.stage.getDataById(nextAudit.sid);
  344. // const sms = new SMS(this.ctx);
  345. // const tenderName = await sms.contentChange(tenderInfo.name);
  346. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,需要您审批。';
  347. // sms.send(smsUser.auth_mobile, content);
  348. // }
  349. // }
  350. } else {
  351. // 本期结束
  352. // 同步 期信息
  353. await transaction.update(this.ctx.service.material.tableName, {
  354. id: materialId, status: checkData.checkType,
  355. });
  356. // 处理旧数据,防止重复插入到历史表
  357. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: this.ctx.tender.id, order: this.ctx.material.order});
  358. const mbhList = [];
  359. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  360. for (const mb of materialBillsData) {
  361. if (mb.code === '') {
  362. throw '调差工料编号不能为空';
  363. }
  364. const newMbh = {
  365. tid: this.ctx.tender.id,
  366. mid: this.ctx.material.id,
  367. order: this.ctx.material.order,
  368. mb_id: mb.id,
  369. quantity: mb.quantity,
  370. expr: mb.expr,
  371. msg_tp: mb.msg_tp,
  372. msg_times: mb.msg_times,
  373. msg_spread: mb.msg_spread,
  374. m_up_risk: mb.m_up_risk,
  375. m_down_risk: mb.m_down_risk,
  376. m_spread: mb.m_spread,
  377. m_tp: mb.m_tp,
  378. pre_tp: mb.pre_tp,
  379. m_tax_tp: mb.m_tax_tp,
  380. tax_pre_tp: mb.tax_pre_tp,
  381. origin: mb.origin,
  382. is_summary: mb.is_summary,
  383. m_tax: mb.m_tax,
  384. };
  385. mbhList.push(newMbh);
  386. }
  387. if(mbhList.length !== 0) await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  388. // 处理旧数据,防止重复插入到历史表
  389. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: this.ctx.tender.id, order: this.ctx.material.order});
  390. const materialExponentData = await this.ctx.service.materialExponent.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  391. const mehList = [];
  392. for (const me of materialExponentData) {
  393. const newMeh = {
  394. tid: this.ctx.tender.id,
  395. mid: this.ctx.material.id,
  396. order: this.ctx.material.order,
  397. me_id: me.id,
  398. type: me.type,
  399. weight_num: me.weight_num,
  400. basic_price: me.basic_price,
  401. basic_times: me.basic_times,
  402. m_price: me.m_price,
  403. calc_num: me.calc_num,
  404. is_summary: me.is_summary,
  405. };
  406. mehList.push(newMeh);
  407. }
  408. if(mehList.length !== 0) await transaction.insert(this.ctx.service.materialExponentHistory.tableName, mehList);
  409. // 微信模板通知
  410. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  411. const wechatData = {
  412. qi: materialInfo.order,
  413. status: wxConst.status.success,
  414. tips: wxConst.tips.success,
  415. begin_time: Date.parse(begin_audit.begin_time),
  416. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  417. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  418. };
  419. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  420. // 检查三方特殊推送
  421. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  422. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.checked);
  423. // 添加短信通知-审批通过提醒功能
  424. // const mobile_array = [];
  425. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  426. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  427. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  428. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  429. // const smsType = JSON.parse(smsUser1.sms_type);
  430. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  431. // mobile_array.push(smsUser1.auth_mobile);
  432. // }
  433. // }
  434. // for (const user of auditList) {
  435. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  436. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  437. // const smsType = JSON.parse(smsUser.sms_type);
  438. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  439. // mobile_array.push(smsUser.auth_mobile);
  440. // }
  441. // }
  442. // }
  443. // if (mobile_array.length > 0) {
  444. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  445. // const sms = new SMS(this.ctx);
  446. // const tenderName = await sms.contentChange(tenderInfo.name);
  447. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  448. // sms.send(mobile_array, content);
  449. // }
  450. }
  451. await transaction.commit();
  452. } catch (err) {
  453. await transaction.rollback();
  454. throw err;
  455. }
  456. }
  457. async _checkNo(pid, materialId, checkData, times) {
  458. const time = new Date();
  459. // 整理当前流程审核人状态更新
  460. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  461. if (!audit) {
  462. throw '审核数据错误';
  463. }
  464. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  465. const sqlParam = [this.tableName, materialId, times];
  466. const auditors = await this.db.query(sql, sqlParam);
  467. let order = 1;
  468. for (const a of auditors) {
  469. a.times = times + 1;
  470. a.order = order;
  471. a.status = auditConst.status.uncheck;
  472. order++;
  473. }
  474. const transaction = await this.db.beginTransaction();
  475. try {
  476. const tp_data = await this.getTpData(transaction, materialId);
  477. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time, tp_data: JSON.stringify(tp_data), });
  478. // 添加到消息推送表
  479. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid, checkData.opinion);
  480. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  481. auditors.forEach(audit => {
  482. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  483. });
  484. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  485. // 同步期信息
  486. await transaction.update(this.ctx.service.material.tableName, {
  487. id: materialId, status: checkData.checkType,
  488. times: times + 1,
  489. tp_data: JSON.stringify(tp_data),
  490. });
  491. // 拷贝新一次审核流程列表
  492. await transaction.insert(this.tableName, auditors);
  493. // // 删除material_bills_history信息
  494. // await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  495. // tid: this.ctx.tender.id,
  496. // order: this.ctx.material.order,
  497. // });
  498. // // 删除material_exponent_history信息
  499. // await transaction.delete(this.ctx.service.materialExponentHistory.tableName, {
  500. // tid: this.ctx.tender.id,
  501. // order: this.ctx.material.order,
  502. // });
  503. // 微信模板通知
  504. const begin_audit = await this.getDataByCondition({
  505. mid: materialId,
  506. order: 1,
  507. });
  508. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  509. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  510. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  511. const wechatData = {
  512. qi: materialInfo.order,
  513. status: wxConst.status.back,
  514. tips: wxConst.tips.back,
  515. begin_time: Date.parse(begin_audit.begin_time),
  516. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  517. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  518. };
  519. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  520. // 检查三方特殊推送
  521. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  522. // 添加短信通知-审批退回提醒功能
  523. // const mobile_array = [];
  524. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  525. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  526. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  527. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  528. // const smsType = JSON.parse(smsUser1.sms_type);
  529. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  530. // mobile_array.push(smsUser1.auth_mobile);
  531. // }
  532. // }
  533. // for (const user of auditList) {
  534. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  535. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  536. // const smsType = JSON.parse(smsUser.sms_type);
  537. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  538. // mobile_array.push(smsUser.auth_mobile);
  539. // }
  540. // }
  541. // }
  542. // if (mobile_array.length > 0) {
  543. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  544. // const sms = new SMS(this.ctx);
  545. // const tenderName = await sms.contentChange(tenderInfo.name);
  546. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  547. // sms.send(mobile_array, content);
  548. // }
  549. await transaction.commit();
  550. } catch (err) {
  551. await transaction.rollback();
  552. throw err;
  553. }
  554. }
  555. async _checkNoPre(pid, materialId, checkData, times) {
  556. const time = new Date();
  557. // 整理当前流程审核人状态更新
  558. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  559. if (!audit || audit.order <= 1) {
  560. throw '审核数据错误';
  561. }
  562. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  563. const auditors2 = await this.getAuditGroupByList(materialId, times);
  564. const auditorIndex = await auditors2.findIndex(function(item) {
  565. return item.aid === audit.aid;
  566. });
  567. const preAuditor = auditors2[auditorIndex - 1];
  568. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid, checkData.opinion);
  569. const transaction = await this.db.beginTransaction();
  570. try {
  571. const tp_data = await this.getTpData(transaction, materialId);
  572. // 添加到消息推送表
  573. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  574. auditors2.forEach(audit => {
  575. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  576. });
  577. await transaction.insert('zh_notice', records);
  578. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time, tp_data: JSON.stringify(tp_data) });
  579. // 顺移气候审核人流程顺序
  580. this.initSqlBuilder();
  581. this.sqlBuilder.setAndWhere('mid', { value: materialId, operate: '=' });
  582. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  583. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  584. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  585. const data = await transaction.query(sql, sqlParam);
  586. const newAuditors = [];
  587. newAuditors.push({
  588. tid: audit.tid, mid: audit.mid, aid: preAuditor.aid,
  589. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  590. begin_time: time,
  591. tp_data: JSON.stringify(tp_data),
  592. });
  593. newAuditors.push({
  594. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  595. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  596. });
  597. // 微信模板通知
  598. const begin_audit = await this.getDataByCondition({
  599. mid: materialId,
  600. order: 1,
  601. });
  602. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  603. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  604. const wechatData = {
  605. qi: materialInfo.order,
  606. status: wxConst.status.check,
  607. tips: wxConst.tips.check,
  608. begin_time: Date.parse(begin_audit.begin_time),
  609. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  610. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  611. };
  612. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  613. // 检查三方特殊推送
  614. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  615. await transaction.insert(this.tableName, newAuditors);
  616. await transaction.commit();
  617. } catch (error) {
  618. await transaction.rollback();
  619. throw error;
  620. }
  621. }
  622. /**
  623. * 审批
  624. * @param {Number} materialId - 材料调差期id
  625. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  626. * @param {Number} times - 第几次审批
  627. * @return {Promise<void>}
  628. */
  629. async check(materialId, checkData, times = 1) {
  630. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  631. throw '提交数据错误';
  632. }
  633. const pid = this.ctx.session.sessionProject.id;
  634. switch (checkData.checkType) {
  635. case auditConst.status.checked:
  636. await this._checked(pid, materialId, checkData, times);
  637. break;
  638. case auditConst.status.checkNo:
  639. await this._checkNo(pid, materialId, checkData, times);
  640. break;
  641. case auditConst.status.checkNoPre:
  642. await this._checkNoPre(pid, materialId, checkData, times);
  643. break;
  644. default:
  645. throw '无效审批操作';
  646. }
  647. }
  648. /**
  649. * 用于添加推送所需的content内容
  650. * @param {Number} pid 项目id
  651. * @param {Number} tid 台账id
  652. * @param {Number} mid 期id
  653. * @param {Number} uid 审批人id
  654. */
  655. async getNoticeContent(pid, tid, mid, uid, opinion = '') {
  656. const noticeSql = 'SELECT * FROM (SELECT ' +
  657. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  658. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  659. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  660. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  661. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  662. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  663. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  664. const content = await this.db.query(noticeSql, noticeSqlParam);
  665. if (content.length) {
  666. content[0].opinion = opinion;
  667. }
  668. return content.length ? JSON.stringify(content[0]) : '';
  669. }
  670. /**
  671. * 审批
  672. * @param {Number} materialId - 材料调差期id
  673. * @param {Number} times - 第几次审批
  674. * @return {Promise<void>}
  675. */
  676. async checkAgain(materialId, times = 1) {
  677. const time = new Date();
  678. // 整理当前流程审核人状态更新
  679. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  680. if (!audit || audit.order < 1) {
  681. throw '审核数据错误';
  682. }
  683. const transaction = await this.db.beginTransaction();
  684. try {
  685. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  686. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  687. const tp_data = await this.getTpData(transaction, materialId, material_decimal);
  688. // 当前审批人2次添加至流程中
  689. const newAuditors = [];
  690. newAuditors.push({
  691. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  692. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  693. begin_time: time, end_time: time, opinion: '',
  694. });
  695. newAuditors.push({
  696. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  697. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  698. begin_time: time,
  699. tp_data: JSON.stringify(tp_data),
  700. });
  701. await transaction.insert(this.tableName, newAuditors);
  702. // 本期结束
  703. // 同步 期信息
  704. await transaction.update(this.ctx.service.material.tableName, {
  705. id: materialId, status: auditConst.status.checking,
  706. });
  707. // 微信模板通知
  708. const wechatData = {
  709. qi: materialInfo.order,
  710. status: wxConst.status.check,
  711. tips: wxConst.tips.check,
  712. begin_time: Date.parse(new Date()),
  713. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), this.ctx.helper.round(materialInfo.ex_tp, material_decimal.tp)),
  714. hs_m_tp: this.ctx.helper.add(this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), material_decimal.tp), this.ctx.helper.round(this.ctx.helper.mul(materialInfo.ex_tp, 1+materialInfo.exponent_rate/100), material_decimal.tp)),
  715. };
  716. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  717. // 检查三方特殊推送
  718. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  719. await transaction.commit();
  720. } catch (err) {
  721. await transaction.rollback();
  722. throw err;
  723. }
  724. }
  725. /**
  726. * 获取审核人需要审核的期列表
  727. *
  728. * @param auditorId
  729. * @return {Promise<*>}
  730. */
  731. async getAuditMaterial(auditorId) {
  732. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  733. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  734. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  735. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  736. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  737. ' and ma.`mid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  738. 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];
  739. return await this.db.query(sql, sqlParam);
  740. }
  741. /**
  742. * 获取审核人审核的次数
  743. *
  744. * @param auditorId
  745. * @return {Promise<*>}
  746. */
  747. async getCountByChecked(auditorId) {
  748. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  749. }
  750. /**
  751. * 获取最近一次审批结束时间
  752. *
  753. * @param auditorId
  754. * @return {Promise<*>}
  755. */
  756. async getLastEndTimeByChecked(auditorId) {
  757. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  758. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  759. const sqlParam = [this.tableName, auditorId];
  760. const result = await this.db.queryOne(sql, sqlParam);
  761. return result ? result.end_time : null;
  762. }
  763. /**
  764. * 获取 某时间后 审批进度 更新的期
  765. * @param {Number} pid - 查询标段
  766. * @param {Number} uid - 查询人
  767. * @param {Date} time - 查询时间
  768. * @return {Promise<*>}
  769. */
  770. async getNoticeMaterial(pid, uid, time) {
  771. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  772. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  773. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  774. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  775. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  776. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  777. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  778. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  779. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  780. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  781. // ' ORDER BY new_t.`end_time`';
  782. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  783. // this.ctx.service.projectAccount.tableName, time, pid];
  784. // return await this.db.query(sql, sqlParam);
  785. let notice = await this.db.select('zh_notice', {
  786. where: { pid, type: pushType.material, uid },
  787. orders: [['create_time', 'desc']],
  788. limit: 10, offset: 0,
  789. });
  790. notice = notice.map(v => {
  791. const extra = JSON.parse(v.content);
  792. delete v.content;
  793. return { ...v, ...extra };
  794. });
  795. return notice;
  796. }
  797. /**
  798. * 获取审核人流程列表
  799. *
  800. * @param auditorId
  801. * @return {Promise<*>}
  802. */
  803. async getAuditGroupByList(materialId, times) {
  804. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  805. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  806. ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  807. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  808. return await this.db.query(sql, sqlParam);
  809. }
  810. /**
  811. * 获取审核人流程列表(包括原报)
  812. * @param {Number} materialId 调差id
  813. * @param {Number} times 审核次数
  814. * @return {Promise<Array>} 查询结果集(包括原报)
  815. */
  816. async getAuditorsWithOwner(materialId, times = 1) {
  817. const result = await this.getAuditGroupByList(materialId, times);
  818. const sql =
  819. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  820. ' FROM ' +
  821. this.ctx.service.material.tableName +
  822. ' As s' +
  823. ' LEFT JOIN ' +
  824. this.ctx.service.projectAccount.tableName +
  825. ' As pa' +
  826. ' ON s.user_id = pa.id' +
  827. ' WHERE s.id = ?';
  828. const sqlParam = [times, materialId, materialId];
  829. const user = await this.db.queryOne(sql, sqlParam);
  830. result.unshift(user);
  831. return result;
  832. }
  833. /**
  834. * 复制上一期的审批人列表给最新一期
  835. *
  836. * @param transaction - 新增一期的事务
  837. * @param {Object} preMaterial - 上一期
  838. * @param {Object} newaMaterial - 最新一期
  839. * @return {Promise<*>}
  840. */
  841. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  842. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  843. const newAuditors = [];
  844. for (const a of auditors) {
  845. const na = {
  846. tid: preMaterial.tid,
  847. mid: newMaterial.id,
  848. aid: a.aid,
  849. times: newMaterial.times,
  850. order: newAuditors.length + 1,
  851. status: auditConst.status.uncheck,
  852. };
  853. newAuditors.push(na);
  854. }
  855. const result = await transaction.insert(this.tableName, newAuditors);
  856. return result.affectedRows === auditors.length;
  857. }
  858. /**
  859. * 移除审核人
  860. *
  861. * @param {Number} materialId - 材料调差期id
  862. * @param {Number} status - 期状态
  863. * @param {Number} status - 期次数
  864. * @return {Promise<boolean>}
  865. */
  866. async getAuditorByStatus(materialId, status, times = 1) {
  867. let auditor = null;
  868. let sql = '';
  869. let sqlParam = '';
  870. switch (status) {
  871. case auditConst.status.checking :
  872. case auditConst.status.checked :
  873. case auditConst.status.checkNoPre :
  874. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  875. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  876. ' WHERE la.`mid` = ? and la.`status` = ? ' +
  877. ' ORDER BY la.`times` desc, la.`order` desc';
  878. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  879. auditor = await this.db.queryOne(sql, sqlParam);
  880. break;
  881. case auditConst.status.checkNo :
  882. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  883. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  884. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  885. ' ORDER BY la.`times` desc, la.`order` desc';
  886. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  887. auditor = await this.db.queryOne(sql, sqlParam);
  888. break;
  889. case auditConst.status.uncheck :
  890. default:break;
  891. }
  892. return auditor;
  893. }
  894. async getAllAuditors(tenderId) {
  895. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  896. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  897. ' WHERE t.id = ?' +
  898. ' GROUP BY ma.aid';
  899. const sqlParam = [tenderId];
  900. return this.db.query(sql, sqlParam);
  901. }
  902. async updateNewAuditList(material, newIdList) {
  903. const transaction = await this.db.beginTransaction();
  904. try {
  905. // 先删除旧的审批流,再添加新的
  906. await transaction.delete(this.tableName, { mid: material.id, times: material.times });
  907. const newAuditors = [];
  908. let order = 1;
  909. for (const aid of newIdList) {
  910. newAuditors.push({
  911. tid: material.tid, mid: material.id, aid,
  912. times: material.times, order, status: auditConst.status.uncheck,
  913. });
  914. order++;
  915. }
  916. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  917. await transaction.commit();
  918. } catch (err) {
  919. await transaction.rollback();
  920. throw err;
  921. }
  922. }
  923. async updateLastAudit(material, auditList, lastId) {
  924. const transaction = await this.db.beginTransaction();
  925. try {
  926. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  927. const idList = this._.map(auditList, 'aid');
  928. let order = idList.length + 1;
  929. if (idList.indexOf(lastId) !== -1) {
  930. await transaction.delete(this.tableName, { mid: material.id, times: material.times, aid: lastId });
  931. const audit = this._.find(auditList, { 'aid': lastId });
  932. // 顺移之后审核人流程顺序
  933. await this._syncOrderByDelete(transaction, material.id, audit.order, material.times);
  934. order = order - 1;
  935. }
  936. // 添加终审
  937. const newAuditor = {
  938. tid: material.tid, mid: material.id, aid: lastId,
  939. times: material.times, order, status: auditConst.status.uncheck,
  940. };
  941. await transaction.insert(this.tableName, newAuditor);
  942. await transaction.commit();
  943. } catch (err) {
  944. await transaction.rollback();
  945. throw err;
  946. }
  947. }
  948. async getNumByMonth(tid, startMonth, endMonth) {
  949. const sql = 'SELECT COUNT(*) as num FROM ?? WHERE id in (SELECT MAX(id) FROM ?? WHERE tid = ? GROUP BY mid) AND status = ? AND end_time between ? and ?';
  950. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  951. const result = await this.db.queryOne(sql, sqlParam);
  952. return result ? result.num : 0;
  953. }
  954. }
  955. return MaterialAudit;
  956. };