material_audit.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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.exponent_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, checkData.opinion);
  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.exponent_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. // 同步 期信息
  318. await transaction.update(this.ctx.service.material.tableName, {
  319. id: materialId, status: checkData.checkType,
  320. });
  321. // 处理旧数据,防止重复插入到历史表
  322. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, { tid: this.ctx.tender.id, order: this.ctx.material.order});
  323. const mbhList = [];
  324. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  325. for (const mb of materialBillsData) {
  326. if (mb.code === '') {
  327. throw '调差工料编号不能为空';
  328. }
  329. const newMbh = {
  330. tid: this.ctx.tender.id,
  331. mid: this.ctx.material.id,
  332. order: this.ctx.material.order,
  333. mb_id: mb.id,
  334. quantity: mb.quantity,
  335. expr: mb.expr,
  336. msg_tp: mb.msg_tp,
  337. msg_times: mb.msg_times,
  338. msg_spread: mb.msg_spread,
  339. m_up_risk: mb.m_up_risk,
  340. m_down_risk: mb.m_down_risk,
  341. m_spread: mb.m_spread,
  342. m_tp: mb.m_tp,
  343. pre_tp: mb.pre_tp,
  344. m_tax_tp: mb.m_tax_tp,
  345. tax_pre_tp: mb.tax_pre_tp,
  346. origin: mb.origin,
  347. is_summary: mb.is_summary,
  348. m_tax: mb.m_tax,
  349. };
  350. mbhList.push(newMbh);
  351. }
  352. if(mbhList.length !== 0) await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  353. // 处理旧数据,防止重复插入到历史表
  354. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, { tid: this.ctx.tender.id, order: this.ctx.material.order});
  355. const materialExponentData = await this.ctx.service.materialExponent.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  356. const mehList = [];
  357. for (const me of materialExponentData) {
  358. const newMeh = {
  359. tid: this.ctx.tender.id,
  360. mid: this.ctx.material.id,
  361. order: this.ctx.material.order,
  362. me_id: me.id,
  363. type: me.type,
  364. weight_num: me.weight_num,
  365. basic_price: me.basic_price,
  366. basic_times: me.basic_times,
  367. m_price: me.m_price,
  368. calc_num: me.calc_num,
  369. is_summary: me.is_summary,
  370. };
  371. mehList.push(newMeh);
  372. }
  373. if(mehList.length !== 0) await transaction.insert(this.ctx.service.materialExponentHistory.tableName, mehList);
  374. // 微信模板通知
  375. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  376. const wechatData = {
  377. qi: materialInfo.order,
  378. status: wxConst.status.success,
  379. tips: wxConst.tips.success,
  380. begin_time: Date.parse(begin_audit.begin_time),
  381. 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)),
  382. 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)),
  383. };
  384. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  385. // 添加短信通知-审批通过提醒功能
  386. // const mobile_array = [];
  387. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  388. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  389. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  390. // if (smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  391. // const smsType = JSON.parse(smsUser1.sms_type);
  392. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  393. // mobile_array.push(smsUser1.auth_mobile);
  394. // }
  395. // }
  396. // for (const user of auditList) {
  397. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  398. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  399. // const smsType = JSON.parse(smsUser.sms_type);
  400. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  401. // mobile_array.push(smsUser.auth_mobile);
  402. // }
  403. // }
  404. // }
  405. // if (mobile_array.length > 0) {
  406. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  407. // const sms = new SMS(this.ctx);
  408. // const tenderName = await sms.contentChange(tenderInfo.name);
  409. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批通过。';
  410. // sms.send(mobile_array, content);
  411. // }
  412. }
  413. await transaction.commit();
  414. } catch (err) {
  415. await transaction.rollback();
  416. throw err;
  417. }
  418. }
  419. async _checkNo(pid, materialId, checkData, times) {
  420. const time = new Date();
  421. // 整理当前流程审核人状态更新
  422. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  423. if (!audit) {
  424. throw '审核数据错误';
  425. }
  426. const sql = 'SELECT `tid`, `mid`, `aid`, `order` FROM ?? WHERE `mid` = ? and `times` = ? GROUP BY `aid` ORDER BY `id` ASC';
  427. const sqlParam = [this.tableName, materialId, times];
  428. const auditors = await this.db.query(sql, sqlParam);
  429. let order = 1;
  430. for (const a of auditors) {
  431. a.times = times + 1;
  432. a.order = order;
  433. a.status = auditConst.status.uncheck;
  434. order++;
  435. }
  436. const transaction = await this.db.beginTransaction();
  437. try {
  438. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  439. // 添加到消息推送表
  440. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid, checkData.opinion);
  441. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  442. auditors.forEach(audit => {
  443. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  444. });
  445. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  446. // 同步期信息
  447. await transaction.update(this.ctx.service.material.tableName, {
  448. id: materialId, status: checkData.checkType,
  449. times: times + 1,
  450. });
  451. // 拷贝新一次审核流程列表
  452. await transaction.insert(this.tableName, auditors);
  453. // // 删除material_bills_history信息
  454. // await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  455. // tid: this.ctx.tender.id,
  456. // order: this.ctx.material.order,
  457. // });
  458. // // 删除material_exponent_history信息
  459. // await transaction.delete(this.ctx.service.materialExponentHistory.tableName, {
  460. // tid: this.ctx.tender.id,
  461. // order: this.ctx.material.order,
  462. // });
  463. // 微信模板通知
  464. const begin_audit = await this.getDataByCondition({
  465. mid: materialId,
  466. order: 1,
  467. });
  468. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  469. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  470. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  471. const wechatData = {
  472. qi: materialInfo.order,
  473. status: wxConst.status.back,
  474. tips: wxConst.tips.back,
  475. begin_time: Date.parse(begin_audit.begin_time),
  476. 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)),
  477. 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)),
  478. };
  479. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  480. // 添加短信通知-审批退回提醒功能
  481. // const mobile_array = [];
  482. // const stageInfo = await this.ctx.service.stage.getDataById(stageId);
  483. // const auditList = await this.getAuditors(stageId, stageInfo.times);
  484. // const smsUser1 = await this.ctx.service.projectAccount.getDataById(stageInfo.user_id);
  485. // if (smsUser1.auth_mobile !== '' && smsUser1.auth_mobile !== undefined && smsUser1.sms_type !== '') {
  486. // const smsType = JSON.parse(smsUser1.sms_type);
  487. // if (smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  488. // mobile_array.push(smsUser1.auth_mobile);
  489. // }
  490. // }
  491. // for (const user of auditList) {
  492. // const smsUser = await this.ctx.service.projectAccount.getDataById(user.aid);
  493. // if (smsUser.auth_mobile !== '' && smsUser.auth_mobile !== undefined && smsUser.sms_type !== '') {
  494. // const smsType = JSON.parse(smsUser.sms_type);
  495. // if (mobile_array.indexOf(smsUser.auth_mobile) === -1 && smsType[smsTypeConst.const.JL] !== undefined && smsType[smsTypeConst.const.JL].indexOf(smsTypeConst.judge.result.toString()) !== -1) {
  496. // mobile_array.push(smsUser.auth_mobile);
  497. // }
  498. // }
  499. // }
  500. // if (mobile_array.length > 0) {
  501. // const tenderInfo = await this.ctx.service.tender.getDataById(stageInfo.tid);
  502. // const sms = new SMS(this.ctx);
  503. // const tenderName = await sms.contentChange(tenderInfo.name);
  504. // const content = '【纵横计量支付】' + tenderName + '第' + stageInfo.order + '期,审批退回。';
  505. // sms.send(mobile_array, content);
  506. // }
  507. await transaction.commit();
  508. } catch (err) {
  509. await transaction.rollback();
  510. throw err;
  511. }
  512. }
  513. async _checkNoPre(pid, materialId, checkData, times) {
  514. const time = new Date();
  515. // 整理当前流程审核人状态更新
  516. const audit = await this.getDataByCondition({ mid: materialId, times, status: auditConst.status.checking });
  517. if (!audit || audit.order <= 1) {
  518. throw '审核数据错误';
  519. }
  520. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  521. const auditors2 = await this.getAuditGroupByList(materialId, times);
  522. const auditorIndex = await auditors2.findIndex(function(item) {
  523. return item.aid === audit.aid;
  524. });
  525. const preAuditor = auditors2[auditorIndex - 1];
  526. const noticeContent = await this.getNoticeContent(pid, audit.tid, materialId, audit.aid, checkData.opinion);
  527. const transaction = await this.db.beginTransaction();
  528. try {
  529. // 添加到消息推送表
  530. const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  531. auditors2.forEach(audit => {
  532. records.push({ pid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  533. });
  534. await transaction.insert('zh_notice', records);
  535. await transaction.update(this.tableName, { id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time });
  536. // 顺移气候审核人流程顺序
  537. this.initSqlBuilder();
  538. this.sqlBuilder.setAndWhere('mid', { value: materialId, operate: '=' });
  539. this.sqlBuilder.setAndWhere('order', { value: audit.order, operate: '>' });
  540. this.sqlBuilder.setUpdateData('order', { value: 2, selfOperate: '+' });
  541. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  542. const data = await transaction.query(sql, sqlParam);
  543. const newAuditors = [];
  544. newAuditors.push({
  545. tid: audit.tid, mid: audit.mid, aid: preAuditor.aid,
  546. times: audit.times, order: audit.order + 1, status: auditConst.status.checking,
  547. begin_time: time,
  548. });
  549. newAuditors.push({
  550. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  551. times: audit.times, order: audit.order + 2, status: auditConst.status.uncheck,
  552. });
  553. // 微信模板通知
  554. const begin_audit = await this.getDataByCondition({
  555. mid: materialId,
  556. order: 1,
  557. });
  558. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  559. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  560. const wechatData = {
  561. qi: materialInfo.order,
  562. status: wxConst.status.check,
  563. tips: wxConst.tips.check,
  564. begin_time: Date.parse(begin_audit.begin_time),
  565. 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)),
  566. 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)),
  567. };
  568. await this.ctx.helper.sendWechat(preAuditor.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  569. await transaction.insert(this.tableName, newAuditors);
  570. await transaction.commit();
  571. } catch (error) {
  572. await transaction.rollback();
  573. throw error;
  574. }
  575. }
  576. /**
  577. * 审批
  578. * @param {Number} materialId - 材料调差期id
  579. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  580. * @param {Number} times - 第几次审批
  581. * @return {Promise<void>}
  582. */
  583. async check(materialId, checkData, times = 1) {
  584. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  585. throw '提交数据错误';
  586. }
  587. const pid = this.ctx.session.sessionProject.id;
  588. switch (checkData.checkType) {
  589. case auditConst.status.checked:
  590. await this._checked(pid, materialId, checkData, times);
  591. break;
  592. case auditConst.status.checkNo:
  593. await this._checkNo(pid, materialId, checkData, times);
  594. break;
  595. case auditConst.status.checkNoPre:
  596. await this._checkNoPre(pid, materialId, checkData, times);
  597. break;
  598. default:
  599. throw '无效审批操作';
  600. }
  601. }
  602. /**
  603. * 用于添加推送所需的content内容
  604. * @param {Number} pid 项目id
  605. * @param {Number} tid 台账id
  606. * @param {Number} mid 期id
  607. * @param {Number} uid 审批人id
  608. */
  609. async getNoticeContent(pid, tid, mid, uid, opinion = '') {
  610. const noticeSql = 'SELECT * FROM (SELECT ' +
  611. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  612. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  613. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  614. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  615. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  616. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  617. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  618. const content = await this.db.query(noticeSql, noticeSqlParam);
  619. if (content.length) {
  620. content[0].opinion = opinion;
  621. }
  622. return content.length ? JSON.stringify(content[0]) : '';
  623. }
  624. /**
  625. * 审批
  626. * @param {Number} materialId - 材料调差期id
  627. * @param {Number} times - 第几次审批
  628. * @return {Promise<void>}
  629. */
  630. async checkAgain(materialId, times = 1) {
  631. const time = new Date();
  632. // 整理当前流程审核人状态更新
  633. const audit = (await this.getAllDataByCondition({ where: { mid: materialId, times }, orders: [['order', 'desc']], limit: 1, offset: 0 }))[0];
  634. if (!audit || audit.order < 1) {
  635. throw '审核数据错误';
  636. }
  637. const transaction = await this.db.beginTransaction();
  638. try {
  639. // 当前审批人2次添加至流程中
  640. const newAuditors = [];
  641. newAuditors.push({
  642. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  643. times: audit.times, order: audit.order + 1, status: auditConst.status.checkAgain,
  644. begin_time: time, end_time: time, opinion: '',
  645. });
  646. newAuditors.push({
  647. tid: audit.tid, mid: audit.mid, aid: audit.aid,
  648. times: audit.times, order: audit.order + 2, status: auditConst.status.checking,
  649. begin_time: time,
  650. });
  651. await transaction.insert(this.tableName, newAuditors);
  652. // 本期结束
  653. // 同步 期信息
  654. await transaction.update(this.ctx.service.material.tableName, {
  655. id: materialId, status: auditConst.status.checking,
  656. });
  657. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  658. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  659. // 微信模板通知
  660. const wechatData = {
  661. qi: materialInfo.order,
  662. status: wxConst.status.check,
  663. tips: wxConst.tips.check,
  664. begin_time: Date.parse(new Date()),
  665. 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)),
  666. 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)),
  667. };
  668. await this.ctx.helper.sendWechat(audit.aid, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  669. await transaction.commit();
  670. } catch (err) {
  671. await transaction.rollback();
  672. throw err;
  673. }
  674. }
  675. /**
  676. * 获取审核人需要审核的期列表
  677. *
  678. * @param auditorId
  679. * @return {Promise<*>}
  680. */
  681. async getAuditMaterial(auditorId) {
  682. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  683. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  684. ' t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  685. ' FROM ?? AS ma, ?? AS m, ?? As t ' +
  686. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' +
  687. ' and ma.`mid` = m.`id` and ma.`tid` = t.`id` ORDER BY ma.`begin_time` DESC';
  688. 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];
  689. return await this.db.query(sql, sqlParam);
  690. }
  691. /**
  692. * 获取审核人审核的次数
  693. *
  694. * @param auditorId
  695. * @return {Promise<*>}
  696. */
  697. async getCountByChecked(auditorId) {
  698. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  699. }
  700. /**
  701. * 获取最近一次审批结束时间
  702. *
  703. * @param auditorId
  704. * @return {Promise<*>}
  705. */
  706. async getLastEndTimeByChecked(auditorId) {
  707. const sql = 'SELECT `end_time` FROM ?? WHERE `aid` = ? ' +
  708. 'AND `status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ') ORDER BY `end_time` DESC';
  709. const sqlParam = [this.tableName, auditorId];
  710. const result = await this.db.queryOne(sql, sqlParam);
  711. return result ? result.end_time : null;
  712. }
  713. /**
  714. * 获取 某时间后 审批进度 更新的期
  715. * @param {Number} pid - 查询标段
  716. * @param {Number} uid - 查询人
  717. * @param {Date} time - 查询时间
  718. * @return {Promise<*>}
  719. */
  720. async getNoticeMaterial(pid, uid, time) {
  721. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  722. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  723. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  724. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  725. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  726. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  727. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  728. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  729. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  730. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  731. // ' ORDER BY new_t.`end_time`';
  732. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  733. // this.ctx.service.projectAccount.tableName, time, pid];
  734. // return await this.db.query(sql, sqlParam);
  735. let notice = await this.db.select('zh_notice', {
  736. where: { pid, type: pushType.material, uid },
  737. orders: [['create_time', 'desc']],
  738. limit: 10, offset: 0,
  739. });
  740. notice = notice.map(v => {
  741. const extra = JSON.parse(v.content);
  742. delete v.content;
  743. return { ...v, ...extra };
  744. });
  745. return notice;
  746. }
  747. /**
  748. * 获取审核人流程列表
  749. *
  750. * @param auditorId
  751. * @return {Promise<*>}
  752. */
  753. async getAuditGroupByList(materialId, times) {
  754. const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  755. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  756. ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  757. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  758. return await this.db.query(sql, sqlParam);
  759. }
  760. /**
  761. * 获取审核人流程列表(包括原报)
  762. * @param {Number} materialId 调差id
  763. * @param {Number} times 审核次数
  764. * @return {Promise<Array>} 查询结果集(包括原报)
  765. */
  766. async getAuditorsWithOwner(materialId, times = 1) {
  767. const result = await this.getAuditGroupByList(materialId, times);
  768. const sql =
  769. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  770. ' FROM ' +
  771. this.ctx.service.material.tableName +
  772. ' As s' +
  773. ' LEFT JOIN ' +
  774. this.ctx.service.projectAccount.tableName +
  775. ' As pa' +
  776. ' ON s.user_id = pa.id' +
  777. ' WHERE s.id = ?';
  778. const sqlParam = [times, materialId, materialId];
  779. const user = await this.db.queryOne(sql, sqlParam);
  780. result.unshift(user);
  781. return result;
  782. }
  783. /**
  784. * 复制上一期的审批人列表给最新一期
  785. *
  786. * @param transaction - 新增一期的事务
  787. * @param {Object} preMaterial - 上一期
  788. * @param {Object} newaMaterial - 最新一期
  789. * @return {Promise<*>}
  790. */
  791. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  792. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  793. const newAuditors = [];
  794. for (const a of auditors) {
  795. const na = {
  796. tid: preMaterial.tid,
  797. mid: newMaterial.id,
  798. aid: a.aid,
  799. times: newMaterial.times,
  800. order: newAuditors.length + 1,
  801. status: auditConst.status.uncheck,
  802. };
  803. newAuditors.push(na);
  804. }
  805. const result = await transaction.insert(this.tableName, newAuditors);
  806. return result.affectedRows === auditors.length;
  807. }
  808. /**
  809. * 移除审核人
  810. *
  811. * @param {Number} materialId - 材料调差期id
  812. * @param {Number} status - 期状态
  813. * @param {Number} status - 期次数
  814. * @return {Promise<boolean>}
  815. */
  816. async getAuditorByStatus(materialId, status, times = 1) {
  817. let auditor = null;
  818. let sql = '';
  819. let sqlParam = '';
  820. switch (status) {
  821. case auditConst.status.checking :
  822. case auditConst.status.checked :
  823. case auditConst.status.checkNoPre :
  824. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  825. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  826. ' WHERE la.`mid` = ? and la.`status` = ? ' +
  827. ' ORDER BY la.`times` desc, la.`order` desc';
  828. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  829. auditor = await this.db.queryOne(sql, sqlParam);
  830. break;
  831. case auditConst.status.checkNo :
  832. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  833. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  834. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  835. ' ORDER BY la.`times` desc, la.`order` desc';
  836. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  837. auditor = await this.db.queryOne(sql, sqlParam);
  838. break;
  839. case auditConst.status.uncheck :
  840. default:break;
  841. }
  842. return auditor;
  843. }
  844. async getAllAuditors(tenderId) {
  845. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  846. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  847. ' WHERE t.id = ?' +
  848. ' GROUP BY ma.aid';
  849. const sqlParam = [tenderId];
  850. return this.db.query(sql, sqlParam);
  851. }
  852. async updateNewAuditList(material, newIdList) {
  853. const transaction = await this.db.beginTransaction();
  854. try {
  855. // 先删除旧的审批流,再添加新的
  856. await transaction.delete(this.tableName, { mid: material.id, times: material.times });
  857. const newAuditors = [];
  858. let order = 1;
  859. for (const aid of newIdList) {
  860. newAuditors.push({
  861. tid: material.tid, mid: material.id, aid,
  862. times: material.times, order, status: auditConst.status.uncheck,
  863. });
  864. order++;
  865. }
  866. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  867. await transaction.commit();
  868. } catch (err) {
  869. await transaction.rollback();
  870. throw err;
  871. }
  872. }
  873. async updateLastAudit(material, auditList, lastId) {
  874. const transaction = await this.db.beginTransaction();
  875. try {
  876. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  877. const idList = this._.map(auditList, 'aid');
  878. let order = idList.length + 1;
  879. if (idList.indexOf(lastId) !== -1) {
  880. await transaction.delete(this.tableName, { mid: material.id, times: material.times, aid: lastId });
  881. const audit = this._.find(auditList, { 'aid': lastId });
  882. // 顺移之后审核人流程顺序
  883. await this._syncOrderByDelete(transaction, material.id, audit.order, material.times);
  884. order = order - 1;
  885. }
  886. // 添加终审
  887. const newAuditor = {
  888. tid: material.tid, mid: material.id, aid: lastId,
  889. times: material.times, order, status: auditConst.status.uncheck,
  890. };
  891. await transaction.insert(this.tableName, newAuditor);
  892. await transaction.commit();
  893. } catch (err) {
  894. await transaction.rollback();
  895. throw err;
  896. }
  897. }
  898. async getNumByMonth(tid, startMonth, endMonth) {
  899. 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 ?';
  900. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  901. const result = await this.db.queryOne(sql, sqlParam);
  902. return result ? result.num : 0;
  903. }
  904. }
  905. return MaterialAudit;
  906. };