material_audit.js 54 KB

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