material_audit.js 51 KB

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