material_audit.js 53 KB

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