material_audit.js 50 KB

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