material_audit.js 52 KB

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