material_audit.js 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410
  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 auditType = require('../const/audit').auditType;
  11. const pushType = require('../const/audit').pushType;
  12. const pushOperate = require('../const/spec_3f').pushOperate;
  13. const smsTypeConst = require('../const/sms_type');
  14. const wxConst = require('../const/wechat_template');
  15. const shenpiConst = require('../const/shenpi');
  16. const materialConst = require('../const/material');
  17. module.exports = app => {
  18. class MaterialAudit extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'material_audit';
  28. }
  29. /**
  30. * 获取 审核人信息
  31. *
  32. * @param {Number} materialId - 材料调差期id
  33. * @param {Number} auditorId - 审核人id
  34. * @param {Number} times - 第几次审批
  35. * @return {Promise<*>}
  36. */
  37. async getAuditor(materialId, auditorId, times = 1) {
  38. 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` ' +
  39. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  40. ' WHERE la.`mid` = ? and la.`aid` = ? and la.`times` = ? ORDER BY la.`order` DESC';
  41. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditorId, times];
  42. return await this.db.queryOne(sql, sqlParam);
  43. }
  44. /**
  45. * 获取 审核列表信息
  46. *
  47. * @param {Number} materialId - 材料调差期id
  48. * @param {Number} times - 第几次审批
  49. * @return {Promise<*>}
  50. */
  51. async getAuditors(materialId, times = 1, order_sort = 'asc') {
  52. const sql = 'SELECT la.id, la.aid, la.times, la.order, la.status, la.opinion, la.is_old, la.begin_time, la.end_time, la.audit_type, la.audit_order,' +
  53. ' pa.name, pa.company, pa.role, pa.mobile, pa.telephone, pa.sign_path' +
  54. ` FROM ${this.tableName} la LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON la.aid = pa.id` +
  55. ' WHERE la.mid = ? AND la.times = ?' +
  56. ' ORDER BY la.order ' + order_sort;
  57. const sqlParam = [materialId, times];
  58. const result = await this.db.query(sql, sqlParam);
  59. const max_sort = this._.max(result.map(x => { return x.audit_order; }));
  60. for (const i in result) {
  61. result[i].max_sort = max_sort;
  62. }
  63. return result;
  64. }
  65. async getLastestAuditors(materialId, times, status) {
  66. const sql =
  67. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, la.audit_type, la.audit_order,' +
  68. ' la.`times`, la.`order`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time` ' +
  69. ' FROM ' + this.tableName + ' AS la' +
  70. ' Left Join ' + this.ctx.service.projectAccount.tableName + ' AS pa ON la.`aid` = pa.`id`' +
  71. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  72. ' ORDER BY `order` DESC';
  73. const sqlParam = [materialId, status, times ? times: 1];
  74. const result = await this.db.query(sql, sqlParam);
  75. if (result.length === 0) return [];
  76. return result.filter(x => { return x.order === result[0].order });
  77. }
  78. async getFinalAuditGroup(materialId, times = 1) {
  79. const sql =
  80. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, pa.`sign_path`, la.`times`, la.`mid`, la.`audit_order`, la.`audit_type`, Max(la.`order`) as max_order, GROUP_CONCAT(la.tp_data) as tp_data ' +
  81. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  82. ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  83. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  84. const result = await this.db.query(sql, sqlParam);
  85. for (const r of result) {
  86. const auditor = await this.getDataByCondition({mid: materialId, times: r.times, order: r.max_order});
  87. r.status = auditor.status;
  88. r.opinion = auditor.opinion;
  89. r.begin_time = auditor.begin_time;
  90. r.end_time = auditor.end_time;
  91. }
  92. return result;
  93. }
  94. /**
  95. * 获取 当前审核人
  96. *
  97. * @param {Number} materialId - 材料调差期id
  98. * @param {Number} times - 第几次审批
  99. * @return {Promise<*>}
  100. */
  101. async getCurAuditor(materialId, times = 1) {
  102. 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`, la.audit_order ' +
  103. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  104. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?';
  105. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  106. return await this.db.queryOne(sql, sqlParam);
  107. }
  108. async getCurAuditors(materialId, times = 1) {
  109. const sql =
  110. '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`, la.audit_type, la.audit_order ' +
  111. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  112. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?';
  113. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checking, times];
  114. return await this.db.query(sql, sqlParam);
  115. }
  116. /**
  117. * 获取 最新审核顺序
  118. *
  119. * @param {Number} materialId - 材料调差期id
  120. * @param {Number} times - 第几次审批
  121. * @return {Promise<number>}
  122. */
  123. async getNewOrder(materialId, times = 1) {
  124. const sql = 'SELECT Max(??) As max_order, Max(audit_order) As max_audit_order FROM ?? Where `mid` = ? and `times` = ?';
  125. const sqlParam = ['order', this.tableName, materialId, times];
  126. const result = await this.db.queryOne(sql, sqlParam);
  127. return result && result.max_order ? [result.max_order + 1, result.max_audit_order + 1] : [1, 1];
  128. }
  129. /**
  130. * 新增审核人
  131. *
  132. * @param {Number} materialId - 材料调差期id
  133. * @param {Number} auditorId - 审核人id
  134. * @param {Number} times - 第几次审批
  135. * @return {Promise<number>}
  136. */
  137. async addAuditor(materialId, auditorId, times = 1, is_gdzs = 0) {
  138. const transaction = await this.db.beginTransaction();
  139. try {
  140. let [newOrder, newAuditOrder] = await this.getNewOrder(materialId, times);
  141. // 判断是否存在固定终审,存在则newOrder - 1并使终审order+1
  142. newOrder = is_gdzs === 1 ? newOrder - 1 : newOrder;
  143. newAuditOrder = is_gdzs === 1 ? newAuditOrder - 1 : newAuditOrder;
  144. if (is_gdzs) await this._syncOrderByDelete(transaction, materialId, newOrder, times, '+');
  145. const data = {
  146. tid: this.ctx.tender.id,
  147. mid: materialId,
  148. aid: auditorId,
  149. times,
  150. order: newOrder,
  151. status: auditConst.status.uncheck,
  152. audit_order: newAuditOrder,
  153. };
  154. const result = await transaction.insert(this.tableName, data);
  155. await transaction.commit();
  156. return result.effectRows = 1;
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. return false;
  162. }
  163. /**
  164. * 移除审核人时,同步其后审核人order
  165. * @param transaction - 事务
  166. * @param {Number} materialId - 材料调差期id
  167. * @param {Number} auditorId - 审核人id
  168. * @param {Number} times - 第几次审批
  169. * @return {Promise<*>}
  170. * @private
  171. */
  172. async _syncOrderByDelete(transaction, materialId, order, times, selfOperate = '-') {
  173. this.initSqlBuilder();
  174. this.sqlBuilder.setAndWhere('mid', {
  175. value: materialId,
  176. operate: '=',
  177. });
  178. this.sqlBuilder.setAndWhere('order', {
  179. value: order,
  180. operate: '>=',
  181. });
  182. this.sqlBuilder.setAndWhere('times', {
  183. value: times,
  184. operate: '=',
  185. });
  186. this.sqlBuilder.setUpdateData('order', {
  187. value: 1,
  188. selfOperate: selfOperate,
  189. });
  190. this.sqlBuilder.setUpdateData('audit_order', {
  191. value: 1,
  192. selfOperate: selfOperate,
  193. });
  194. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  195. const data = await transaction.query(sql, sqlParam);
  196. return data;
  197. }
  198. /**
  199. * 移除审核人
  200. *
  201. * @param {Number} materialId - 材料调差期id
  202. * @param {Number} auditorId - 审核人id
  203. * @param {Number} times - 第几次审批
  204. * @return {Promise<boolean>}
  205. */
  206. async deleteAuditor(materialId, auditorId, times = 1) {
  207. const transaction = await this.db.beginTransaction();
  208. try {
  209. const condition = { mid: materialId, aid: auditorId, times };
  210. const auditor = await this.getDataByCondition(condition);
  211. if (!auditor) {
  212. throw '该审核人不存在';
  213. }
  214. await transaction.delete(this.tableName, { mid: materialId, order: auditor.order, times});
  215. await this._syncOrderByDelete(transaction, materialId, auditor.order, times);
  216. await transaction.commit();
  217. } catch (err) {
  218. await transaction.rollback();
  219. throw err;
  220. }
  221. return true;
  222. }
  223. async getTpData(transaction, materialId, decimal = (this.ctx.material.decimal ? this.ctx.material.decimal : materialConst.decimal)) {
  224. const materialInfo = await transaction.get(this.ctx.service.material.tableName, { id: materialId });
  225. const tp_data = {
  226. m_tp: materialInfo.m_tp !== null ? this.ctx.helper.round(materialInfo.m_tp, decimal.tp) : null,
  227. tax_tp: materialInfo.material_tax ? (materialInfo.m_tax_tp ? materialInfo.m_tax_tp : materialInfo.m_tp) :
  228. (materialInfo.m_tp !== null ? this.ctx.helper.round(this.ctx.helper.mul(materialInfo.m_tp, 1+materialInfo.rate/100), decimal.tp) : null),
  229. ex_tp: materialInfo.ex_tp !== null ? this.ctx.helper.round(materialInfo.ex_tp, decimal.tp) : null,
  230. 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,
  231. }
  232. tp_data.total_tp = this.ctx.helper.add(tp_data.m_tp, tp_data.ex_tp);
  233. 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;
  234. if (materialInfo.is_stage_self) {
  235. const materialStageList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: materialId } });
  236. const stage_tp = [];
  237. for (const ms of materialStageList) {
  238. stage_tp.push({
  239. id: ms.id,
  240. sid: ms.sid,
  241. order: ms.order,
  242. m_tp: this.ctx.helper.round(ms.m_tp, decimal.tp),
  243. m_tax_tp: this.ctx.helper.round(ms.m_tax_tp, decimal.tp),
  244. });
  245. }
  246. tp_data.stage_tp = stage_tp;
  247. }
  248. return tp_data;
  249. }
  250. /**
  251. * 开始审批
  252. * @param {Number} materialId - 材料调差期id
  253. * @param {Number} times - 第几次审批
  254. * @return {Promise<boolean>}
  255. */
  256. async start(materialId, times = 1) {
  257. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: 1 } });
  258. if (audits.length === 0) {
  259. if(this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  260. throw '请联系管理员添加审批人';
  261. } else {
  262. throw '请先选择审批人,再上报数据';
  263. }
  264. }
  265. const transaction = await this.db.beginTransaction();
  266. try {
  267. const tp_data = await this.getTpData(transaction, materialId);
  268. const begin_time = new Date();
  269. const updateData = audits.map(x => { return { id: x.id, status: auditConst.status.checking, begin_time, tp_data: JSON.stringify(tp_data) } });
  270. await transaction.updateRows(this.tableName, updateData);
  271. await transaction.update(this.ctx.service.material.tableName, {
  272. id: materialId, status: auditConst.status.checking, tp_data: JSON.stringify(tp_data),
  273. });
  274. // 本期一些必要数据(如应耗数量和上期调差金额)插入到material_bills_history表里
  275. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  276. if (materialBillsData.length === 0 && this.ctx.material.ex_expr === null) {
  277. throw '请至少使用一种调差方式';
  278. }
  279. // 微信模板通知
  280. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  281. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  282. const users = this._.map(audits, 'aid');
  283. const wechatData = {
  284. qi: materialInfo.order,
  285. status: wxConst.status.check,
  286. tips: wxConst.tips.check,
  287. begin_time: Date.parse(new Date()),
  288. 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)),
  289. 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)),
  290. };
  291. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  292. for (const audit of audits) {
  293. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  294. pid: this.ctx.session.sessionProject.id,
  295. tid: this.ctx.tender.id,
  296. uid: audit.aid,
  297. sp_type: 'material',
  298. sp_id: audit.id,
  299. table_name: this.tableName,
  300. template: wxConst.template.material,
  301. wx_data: wechatData,
  302. });
  303. }
  304. // todo 更新标段tender状态 ?
  305. // 检查三方特殊推送
  306. await this.ctx.service.specMsg.addMaterialMsg(transaction, this.ctx.session.sessionProject.id, materialInfo, pushOperate.material.flow);
  307. await transaction.commit();
  308. } catch (err) {
  309. await transaction.rollback();
  310. throw err;
  311. }
  312. return true;
  313. }
  314. async _checked(pid, materialId, checkData, times) {
  315. const accountId = this.ctx.session.sessionUser.accountId;
  316. const time = new Date();
  317. // 整理当前流程审核人状态更新
  318. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  319. if (audits.length === 0) throw '审核数据错误';
  320. const selfAudit = audits.find(x => { return x.aid === accountId; });
  321. if (!selfAudit) throw '当前标段您无权审批';
  322. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: selfAudit.order } });
  323. const nextAudits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: selfAudit.order + 1 } });
  324. const transaction = await this.db.beginTransaction();
  325. try {
  326. // 获取当前总金额及独立单价期的金额,添加到tp_data中,报表使用
  327. const tp_data = await this.getTpData(transaction, materialId);
  328. await transaction.update(this.tableName, {
  329. id: selfAudit.id,
  330. status: checkData.checkType,
  331. opinion: checkData.opinion,
  332. end_time: time,
  333. tp_data: JSON.stringify(tp_data),
  334. });
  335. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  336. // 获取推送必要信息
  337. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, accountId, checkData.opinion);
  338. const auditors = await this.getAuditorsWithOwner(materialId, times);
  339. // 添加推送
  340. const records = [];
  341. // const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, content: noticeContent }];
  342. auditors.forEach(audit => {
  343. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  344. });
  345. await transaction.insert('zh_notice', records);
  346. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  347. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  348. if (audits.length === 1 || selfAudit.audit_type === auditType.key.or) {
  349. // 或签更新他人审批状态
  350. if (selfAudit.audit_type === auditType.key.or) {
  351. const updateOther = [];
  352. for (const audit of audits) {
  353. if (audit.aid === selfAudit.aid) continue;
  354. updateOther.push({
  355. id: audit.id,
  356. status: auditConst.status.checkSkip,
  357. opinion: '',
  358. end_time: time,
  359. });
  360. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  361. }
  362. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  363. }
  364. // 无下一审核人表示,审核结束
  365. if (nextAudits.length > 0) {
  366. // 复制一份下一审核人数据
  367. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  368. // 流程至下一审批人
  369. const updateData = nextAudits.map(x => {
  370. return {
  371. id: x.id,
  372. status: auditConst.status.checking,
  373. begin_time: time,
  374. tp_data: JSON.stringify(tp_data)
  375. };
  376. });
  377. await transaction.updateRows(this.tableName, updateData);
  378. // 同步 期信息
  379. await transaction.update(this.ctx.service.material.tableName, {
  380. id: materialId, status: auditConst.status.checking,
  381. });
  382. const begin_audit = await this.getDataByCondition({
  383. mid: materialId,
  384. order: 1,
  385. });
  386. // 微信模板通知
  387. const wechatData = {
  388. qi: materialInfo.order,
  389. status: wxConst.status.check,
  390. tips: wxConst.tips.check,
  391. begin_time: Date.parse(begin_audit.begin_time),
  392. 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)),
  393. 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)),
  394. };
  395. await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  396. // 重新发送配置
  397. for (const a of nextAudits) {
  398. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  399. pid: this.ctx.session.sessionProject.id,
  400. tid: this.ctx.tender.id,
  401. uid: a.aid,
  402. sp_type: 'material',
  403. sp_id: a.id,
  404. table_name: this.tableName,
  405. template: wxConst.template.material,
  406. wx_data: wechatData,
  407. });
  408. }
  409. // 检查三方特殊推送
  410. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  411. } else {
  412. // 本期结束
  413. // 同步 期信息
  414. await transaction.update(this.ctx.service.material.tableName, {
  415. id: materialId, status: checkData.checkType,
  416. });
  417. // 处理旧数据,防止重复插入到历史表
  418. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  419. tid: this.ctx.tender.id,
  420. order: this.ctx.material.order
  421. });
  422. const mbhList = [];
  423. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({where: {tid: this.ctx.tender.id}});
  424. for (const mb of materialBillsData) {
  425. if (mb.code === '') {
  426. throw '调差工料编号不能为空';
  427. }
  428. const newMbh = {
  429. tid: this.ctx.tender.id,
  430. mid: this.ctx.material.id,
  431. order: this.ctx.material.order,
  432. mb_id: mb.id,
  433. quantity: mb.quantity,
  434. expr: mb.expr,
  435. msg_tp: mb.msg_tp,
  436. msg_times: mb.msg_times,
  437. msg_spread: mb.msg_spread,
  438. m_up_risk: mb.m_up_risk,
  439. m_down_risk: mb.m_down_risk,
  440. m_spread: mb.m_spread,
  441. m_tp: mb.m_tp,
  442. pre_tp: mb.pre_tp,
  443. m_tax_tp: mb.m_tax_tp,
  444. tax_pre_tp: mb.tax_pre_tp,
  445. origin: mb.origin,
  446. is_summary: mb.is_summary,
  447. m_tax: mb.m_tax,
  448. };
  449. mbhList.push(newMbh);
  450. }
  451. if (mbhList.length !== 0) await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  452. // 处理旧数据,防止重复插入到历史表
  453. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, {
  454. tid: this.ctx.tender.id,
  455. order: this.ctx.material.order
  456. });
  457. const materialExponentData = await this.ctx.service.materialExponent.getAllDataByCondition({where: {tid: this.ctx.tender.id}});
  458. const mehList = [];
  459. for (const me of materialExponentData) {
  460. const newMeh = {
  461. tid: this.ctx.tender.id,
  462. mid: this.ctx.material.id,
  463. order: this.ctx.material.order,
  464. me_id: me.id,
  465. type: me.type,
  466. weight_num: me.weight_num,
  467. basic_price: me.basic_price,
  468. basic_times: me.basic_times,
  469. m_price: me.m_price,
  470. calc_num: me.calc_num,
  471. is_summary: me.is_summary,
  472. };
  473. mehList.push(newMeh);
  474. }
  475. if (mehList.length !== 0) await transaction.insert(this.ctx.service.materialExponentHistory.tableName, mehList);
  476. // 微信模板通知
  477. const begin_audit = await this.getDataByCondition({
  478. mid: materialId,
  479. order: 1,
  480. });
  481. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  482. const wechatData = {
  483. qi: materialInfo.order,
  484. status: wxConst.status.success,
  485. tips: wxConst.tips.success,
  486. begin_time: Date.parse(begin_audit.begin_time),
  487. 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)),
  488. 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)),
  489. };
  490. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  491. // 检查三方特殊推送
  492. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  493. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.checked);
  494. }
  495. } else {
  496. // 同步 期信息
  497. await transaction.update(this.ctx.service.material.tableName, {
  498. id: materialId,
  499. status: auditConst.status.checking,
  500. });
  501. }
  502. await transaction.commit();
  503. } catch (err) {
  504. await transaction.rollback();
  505. throw err;
  506. }
  507. }
  508. async _checkNo(pid, materialId, checkData, times) {
  509. const accountId = this.ctx.session.sessionUser.accountId;
  510. const time = new Date();
  511. // 整理当前流程审核人状态更新
  512. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  513. if (!audits) throw '审核数据错误';
  514. const selfAudit = audits.find(x => { return x.aid === accountId; });
  515. if (!selfAudit) throw '当前标段您无权审批';
  516. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times: selfAudit.times, order: selfAudit.order }});
  517. const auditors = await this.getUniqAuditor(materialId, times); // 全部参与的审批人
  518. const newAuditors = auditors.map(x => {
  519. return {
  520. aid: x.aid, tid: selfAudit.tid, mid: selfAudit.mid,
  521. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  522. audit_type: x.audit_type, audit_order: x.audit_order,
  523. }
  524. });
  525. const transaction = await this.db.beginTransaction();
  526. try {
  527. const tp_data = await this.getTpData(transaction, materialId);
  528. const updateData = [];
  529. audits.forEach(x => {
  530. updateData.push({
  531. id: x.id,
  532. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  533. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  534. end_time: x.aid === selfAudit.aid ? time : null,
  535. tp_data: JSON.stringify(tp_data),
  536. });
  537. });
  538. await transaction.updateRows(this.tableName, updateData);
  539. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  540. // 添加到消息推送表
  541. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, selfAudit.aid, checkData.opinion);
  542. const records = [{ pid, tid: selfAudit.tid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  543. auditors.forEach(audit => {
  544. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  545. });
  546. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  547. // 同步期信息
  548. await transaction.update(this.ctx.service.material.tableName, {
  549. id: materialId, status: checkData.checkType,
  550. times: times + 1,
  551. tp_data: JSON.stringify(tp_data),
  552. });
  553. // 拷贝新一次审核流程列表
  554. await transaction.insert(this.tableName, newAuditors);
  555. // 微信模板通知
  556. const begin_audit = await this.getDataByCondition({
  557. mid: materialId,
  558. order: 1,
  559. });
  560. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  561. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  562. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  563. const wechatData = {
  564. qi: materialInfo.order,
  565. status: wxConst.status.back,
  566. tips: wxConst.tips.back,
  567. begin_time: Date.parse(begin_audit.begin_time),
  568. 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)),
  569. 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)),
  570. };
  571. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  572. // 检查三方特殊推送
  573. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  574. await transaction.commit();
  575. } catch (err) {
  576. await transaction.rollback();
  577. throw err;
  578. }
  579. }
  580. async _checkNoPre(pid, materialId, checkData, times) {
  581. const accountId = this.ctx.session.sessionUser.accountId;
  582. const time = new Date();
  583. // 整理当前流程审核人状态更新
  584. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  585. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  586. const selfAudit = audits.find(x => { return x.aid === accountId; });
  587. if (!selfAudit) throw '当前标段您无权审批';
  588. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times: selfAudit.times, order: selfAudit.order }});
  589. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  590. const auditors2 = await this.getAuditGroupByList(materialId, times);
  591. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  592. const transaction = await this.db.beginTransaction();
  593. try {
  594. const tp_data = await this.getTpData(transaction, materialId);
  595. // 添加到消息推送表
  596. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, selfAudit.aid, checkData.opinion);
  597. const records = [{ pid, tid: selfAudit.tid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  598. auditors2.forEach(audit => {
  599. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  600. });
  601. await transaction.insert('zh_notice', records);
  602. const updateData = [];
  603. for (const audit of audits) {
  604. if (audit.aid === selfAudit.aid) {
  605. updateData.push({
  606. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time, tp_data: JSON.stringify(tp_data)
  607. });
  608. } else {
  609. updateData.push({
  610. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null, tp_data: JSON.stringify(tp_data)
  611. });
  612. }
  613. }
  614. await transaction.updateRows(this.tableName, updateData);
  615. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  616. // 顺移其后审核人流程顺序
  617. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE mid = ? AND times = ? AND `order` > ?';
  618. await transaction.query(sql, [materialId, selfAudit.times, selfAudit.order]);
  619. const newAuditors = [];
  620. preAuditors.forEach(x => {
  621. newAuditors.push({
  622. tid: selfAudit.tid, mid: materialId, aid: x.aid,
  623. times: x.times, order: selfAudit.order + 1,
  624. status: auditConst.status.checking, begin_time: time,
  625. audit_type: x.audit_type, audit_order: x.audit_order,
  626. tp_data: JSON.stringify(tp_data),
  627. });
  628. });
  629. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  630. // 获取刚批量添加的所有list
  631. for (let j = 0; j < preAuditors.length; j++) {
  632. newAuditors[j].id = newAuditors_result.insertId + j;
  633. }
  634. const newFlowAuditors = [];
  635. flowAudits.forEach(x => {
  636. newFlowAuditors.push({
  637. tid: selfAudit.tid, mid: materialId, aid: x.aid,
  638. times: x.times, order: selfAudit.order + 2,
  639. status: auditConst.status.uncheck,
  640. audit_type: x.audit_type, audit_order: x.audit_order,
  641. });
  642. });
  643. await transaction.insert(this.tableName, newFlowAuditors);
  644. // 微信模板通知
  645. const begin_audit = await this.getDataByCondition({
  646. mid: materialId,
  647. order: 1,
  648. });
  649. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  650. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  651. const wechatData = {
  652. qi: materialInfo.order,
  653. status: wxConst.status.check,
  654. tips: wxConst.tips.check,
  655. begin_time: Date.parse(begin_audit.begin_time),
  656. 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)),
  657. 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)),
  658. };
  659. await this.ctx.helper.sendWechat(this._.map(preAuditors, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  660. for (const a of newAuditors) {
  661. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  662. pid: this.ctx.session.sessionProject.id,
  663. tid: this.ctx.tender.id,
  664. uid: a.aid,
  665. sp_type: 'material',
  666. sp_id: a.id,
  667. table_name: this.tableName,
  668. template: wxConst.template.material,
  669. wx_data: wechatData,
  670. });
  671. }
  672. // 检查三方特殊推送
  673. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  674. await transaction.commit();
  675. } catch (error) {
  676. await transaction.rollback();
  677. throw error;
  678. }
  679. }
  680. /**
  681. * 审批
  682. * @param {Number} materialId - 材料调差期id
  683. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  684. * @param {Number} times - 第几次审批
  685. * @return {Promise<void>}
  686. */
  687. async check(materialId, checkData, times = 1) {
  688. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  689. throw '提交数据错误';
  690. }
  691. const pid = this.ctx.session.sessionProject.id;
  692. switch (checkData.checkType) {
  693. case auditConst.status.checked:
  694. await this._checked(pid, materialId, checkData, times);
  695. break;
  696. case auditConst.status.checkNo:
  697. await this._checkNo(pid, materialId, checkData, times);
  698. break;
  699. case auditConst.status.checkNoPre:
  700. await this._checkNoPre(pid, materialId, checkData, times);
  701. break;
  702. default:
  703. throw '无效审批操作';
  704. }
  705. }
  706. /**
  707. * 用于添加推送所需的content内容
  708. * @param {Number} pid 项目id
  709. * @param {Number} tid 台账id
  710. * @param {Number} mid 期id
  711. * @param {Number} uid 审批人id
  712. */
  713. async getNoticeContent(pid, tid, mid, uid, opinion = '') {
  714. const noticeSql = 'SELECT * FROM (SELECT ' +
  715. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  716. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  717. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  718. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  719. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  720. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  721. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  722. const content = await this.db.query(noticeSql, noticeSqlParam);
  723. if (content.length) {
  724. content[0].opinion = opinion;
  725. }
  726. return content.length ? JSON.stringify(content[0]) : '';
  727. }
  728. /**
  729. * 审批
  730. * @param {Number} materialId - 材料调差期id
  731. * @param {Number} times - 第几次审批
  732. * @return {Promise<void>}
  733. */
  734. async checkAgain(materialId, times = 1) {
  735. const accountId = this.ctx.session.sessionUser.accountId;
  736. const time = new Date();
  737. // 整理当前流程审核人状态更新
  738. const auditors = await this.getAllDataByCondition({
  739. where: { mid: materialId, times },
  740. orders: [['order', 'asc']],
  741. });
  742. const maxOrder = auditors[auditors.length - 1].order;
  743. const audits = auditors.filter(x => { return x.order === maxOrder});
  744. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  745. const selfAudit = audits.find(x => { return x.aid === accountId; });
  746. if (!selfAudit) throw '当前标段您无权审批';
  747. const finalAudit = selfAudit || audits[0];
  748. const transaction = await this.db.beginTransaction();
  749. try {
  750. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  751. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  752. const tp_data = await this.getTpData(transaction, materialId, material_decimal);
  753. const checkAgainAuditors = [], checkingAuditors = [];
  754. audits.forEach(x => {
  755. checkAgainAuditors.push({
  756. tid: x.tid, mid: x.mid, aid: x.aid,
  757. times: x.times, order: x.order + 1,
  758. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  759. begin_time: time, end_time: time, opinion: '',
  760. audit_type: x.audit_type, audit_order: x.audit_order,
  761. });
  762. });
  763. audits.forEach(x => {
  764. checkingAuditors.push({
  765. tid: x.tid, mid: x.mid, aid: x.aid,
  766. times: x.times, order: x.order + 2,
  767. status: auditConst.status.checking,
  768. begin_time: time, end_time: time, opinion: '',
  769. audit_type: x.audit_type, audit_order: x.audit_order,
  770. tp_data: JSON.stringify(tp_data),
  771. });
  772. });
  773. await transaction.insert(this.tableName, checkAgainAuditors);
  774. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  775. // 获取刚批量添加的所有list
  776. for (let j = 0; j < checkingAuditors.length; j++) {
  777. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  778. }
  779. // 本期结束
  780. // 同步 期信息
  781. await transaction.update(this.ctx.service.material.tableName, {
  782. id: materialId, status: auditConst.status.checking,
  783. final_auditor_str: '',
  784. });
  785. // 微信模板通知
  786. const wechatData = {
  787. qi: materialInfo.order,
  788. status: wxConst.status.check,
  789. tips: wxConst.tips.check,
  790. begin_time: Date.parse(new Date()),
  791. 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)),
  792. 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)),
  793. };
  794. await this.ctx.helper.sendWechat(this._.map(checkingAuditors, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  795. for (const a of checkingAuditors) {
  796. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  797. pid: this.ctx.session.sessionProject.id,
  798. tid: this.ctx.tender.id,
  799. uid: a.aid,
  800. sp_type: 'material',
  801. sp_id: a.id,
  802. table_name: this.tableName,
  803. template: wxConst.template.material,
  804. wx_data: wechatData,
  805. });
  806. }
  807. // 检查三方特殊推送
  808. await this.ctx.service.specMsg.addMaterialMsg(transaction, this.ctx.session.sessionProject.id, materialInfo, pushOperate.material.flow);
  809. await transaction.commit();
  810. } catch (err) {
  811. await transaction.rollback();
  812. throw err;
  813. }
  814. }
  815. /**
  816. * 获取审核人需要审核的期列表
  817. *
  818. * @param auditorId
  819. * @return {Promise<*>}
  820. */
  821. async getAuditMaterial(auditorId, spid = '') {
  822. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  823. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  824. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  825. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid` ' +
  826. ' FROM ?? AS ma' +
  827. ' LEFT JOIN ?? AS m On ma.mid = m.id' +
  828. ' LEFT JOIN ?? As t On m.tid = t.id' +
  829. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' + spSql +
  830. ' ORDER BY ma.`begin_time` DESC';
  831. 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];
  832. const result = await this.db.query(sql, sqlParam);
  833. // 过滤result中存在重复sid的值, 保留最新的一条
  834. const filterResult = [];
  835. const midArr = [];
  836. for (const r of result) {
  837. if (midArr.indexOf(r.mid) === -1) {
  838. filterResult.push(r);
  839. midArr.push(r.mid);
  840. }
  841. }
  842. return filterResult;
  843. }
  844. /**
  845. * 获取审核人审核的次数
  846. *
  847. * @param auditorId
  848. * @return {Promise<*>}
  849. */
  850. async getCountByChecked(auditorId, spid = '') {
  851. if (spid) {
  852. const sql = 'SELECT count(*) AS count FROM ?? AS a LEFT JOIN ?? AS t ON a.tid = t.id WHERE a.`aid` = ? AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre]) + ') AND t.`spid` = ?';
  853. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  854. const result = await this.db.queryOne(sql, sqlParam);
  855. return result.count ? result.count : 0;
  856. }
  857. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  858. }
  859. /**
  860. * 获取最近一次审批结束时间
  861. *
  862. * @param auditorId
  863. * @return {Promise<*>}
  864. */
  865. async getLastEndTimeByChecked(auditorId, spid = '') {
  866. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  867. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`aid` = ? ' +
  868. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
  869. ' ORDER BY a.`end_time` DESC';
  870. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  871. const result = await this.db.queryOne(sql, sqlParam);
  872. return result ? result.end_time : null;
  873. }
  874. /**
  875. * 获取 某时间后 审批进度 更新的期
  876. * @param {Number} pid - 查询标段
  877. * @param {Number} uid - 查询人
  878. * @param {Date} time - 查询时间
  879. * @return {Promise<*>}
  880. */
  881. async getNoticeMaterial(pid, uid, time) {
  882. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  883. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  884. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  885. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  886. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  887. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  888. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  889. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  890. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  891. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  892. // ' ORDER BY new_t.`end_time`';
  893. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  894. // this.ctx.service.projectAccount.tableName, time, pid];
  895. // return await this.db.query(sql, sqlParam);
  896. let notice = await this.db.select('zh_notice', {
  897. where: { pid, type: pushType.material, uid },
  898. orders: [['create_time', 'desc']],
  899. limit: 10, offset: 0,
  900. });
  901. notice = notice.map(v => {
  902. const extra = JSON.parse(v.content);
  903. delete v.content;
  904. return { ...v, ...extra };
  905. });
  906. return notice;
  907. }
  908. /**
  909. * 获取审核人流程列表
  910. *
  911. * @param auditorId
  912. * @return {Promise<*>}
  913. */
  914. async getAuditGroupByList(materialId, times) {
  915. // const sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`tid`, la.`mid`, la.`aid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  916. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  917. // ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  918. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  919. const sql =
  920. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  921. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `mid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  922. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  923. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`mid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  924. const sqlParam = [this.tableName, materialId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, materialId, times, 0];
  925. return await this.db.query(sql, sqlParam);
  926. }
  927. /**
  928. * 获取审核人流程列表(包括原报)
  929. * @param {Number} materialId 调差id
  930. * @param {Number} times 审核次数
  931. * @return {Promise<Array>} 查询结果集(包括原报)
  932. */
  933. async getAuditorsWithOwner(materialId, times = 1) {
  934. const result = await this.getAuditGroupByList(materialId, times);
  935. const sql =
  936. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  937. ' FROM ' +
  938. this.ctx.service.material.tableName +
  939. ' As s' +
  940. ' LEFT JOIN ' +
  941. this.ctx.service.projectAccount.tableName +
  942. ' As pa' +
  943. ' ON s.user_id = pa.id' +
  944. ' WHERE s.id = ?';
  945. const sqlParam = [times, materialId, materialId];
  946. const user = await this.db.queryOne(sql, sqlParam);
  947. result.unshift(user);
  948. return result;
  949. }
  950. /**
  951. * 获取tender下所有的审核列表(报表用)
  952. * @param {Number} tenderId
  953. */
  954. async getAuditorsByTender(tenderId) {
  955. const sql = 'SELECT la.`tid`, la.`mid`, la.`aid`, la.`order`, la.`times`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`' +
  956. ' FROM ?? AS la ' +
  957. ' WHERE la.`tid` = ?';
  958. const sqlParam = [this.tableName, tenderId];
  959. return await this.db.query(sql, sqlParam);
  960. }
  961. /**
  962. * 复制上一期的审批人列表给最新一期
  963. *
  964. * @param transaction - 新增一期的事务
  965. * @param {Object} preMaterial - 上一期
  966. * @param {Object} newaMaterial - 最新一期
  967. * @return {Promise<*>}
  968. */
  969. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  970. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  971. const newAuditors = [];
  972. for (const a of auditors) {
  973. const na = {
  974. tid: preMaterial.tid,
  975. mid: newMaterial.id,
  976. aid: a.aid,
  977. times: newMaterial.times,
  978. order: a.audit_order,
  979. status: auditConst.status.uncheck,
  980. audit_type: a.audit_type,
  981. audit_order: a.audit_order,
  982. };
  983. newAuditors.push(na);
  984. }
  985. if (newAuditors.length > 0) {
  986. const result = await transaction.insert(this.tableName, newAuditors);
  987. return (result.effectRows = auditors.length);
  988. } else {
  989. return true;
  990. }
  991. }
  992. /**
  993. * 移除审核人
  994. *
  995. * @param {Number} materialId - 材料调差期id
  996. * @param {Number} status - 期状态
  997. * @param {Number} status - 期次数
  998. * @return {Promise<boolean>}
  999. */
  1000. async getAuditorByStatus(materialId, status, times = 1) {
  1001. let auditor = null;
  1002. let sql = '';
  1003. let sqlParam = '';
  1004. switch (status) {
  1005. case auditConst.status.checking :
  1006. case auditConst.status.checked :
  1007. case auditConst.status.checkNoPre :
  1008. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  1009. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1010. ' WHERE la.`mid` = ? and la.`status` = ? ' +
  1011. ' ORDER BY la.`times` desc, la.`order` desc';
  1012. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  1013. auditor = await this.db.queryOne(sql, sqlParam);
  1014. break;
  1015. case auditConst.status.checkNo :
  1016. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  1017. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1018. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  1019. ' ORDER BY la.`times` desc, la.`order` desc';
  1020. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  1021. auditor = await this.db.queryOne(sql, sqlParam);
  1022. break;
  1023. case auditConst.status.uncheck :
  1024. default:break;
  1025. }
  1026. return auditor;
  1027. }
  1028. async getAuditorsByStatus(materialId, status, times = 1) {
  1029. let auditor = [];
  1030. let sql = '';
  1031. let sqlParam = '';
  1032. let cur;
  1033. switch (status) {
  1034. case auditConst.status.checking:
  1035. case auditConst.status.checked:
  1036. case auditConst.status.checkNoPre:
  1037. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where mid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [materialId, times, status]);
  1038. if (!cur) return [];
  1039. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_order, la.audit_type ' +
  1040. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1041. ' WHERE la.`mid` = ? and la.`order` = ? and times = ?';
  1042. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, cur.order, times];
  1043. auditor = await this.db.query(sql, sqlParam);
  1044. break;
  1045. case auditConst.status.checkNo:
  1046. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where mid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [materialId, parseInt(times) - 1, status]);
  1047. if (!cur) return [];
  1048. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_order, la.audit_type ' +
  1049. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1050. ' WHERE la.`mid` = ? and la.`order` = ? and la.`times` = ?';
  1051. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, cur.order, parseInt(times) - 1];
  1052. auditor = await this.db.query(sql, sqlParam);
  1053. break;
  1054. case auditConst.status.uncheck:
  1055. default:
  1056. break;
  1057. }
  1058. return auditor;
  1059. }
  1060. async getAllAuditors(tenderId) {
  1061. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  1062. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  1063. ' WHERE t.id = ?' +
  1064. ' GROUP BY ma.aid';
  1065. const sqlParam = [tenderId];
  1066. return this.db.query(sql, sqlParam);
  1067. }
  1068. async updateNewAuditList(material, newList) {
  1069. const transaction = await this.db.beginTransaction();
  1070. try {
  1071. // 先删除旧的审批流,再添加新的
  1072. await transaction.delete(this.tableName, { mid: material.id, times: material.times });
  1073. const newAuditors = [];
  1074. for (const auditor of newList) {
  1075. newAuditors.push({
  1076. tid: material.tid, mid: material.id, aid: auditor.audit_id,
  1077. times: material.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1078. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1079. });
  1080. }
  1081. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1082. await transaction.commit();
  1083. } catch (err) {
  1084. await transaction.rollback();
  1085. throw err;
  1086. }
  1087. }
  1088. async updateLastAudit(material, auditList, lastId) {
  1089. const transaction = await this.db.beginTransaction();
  1090. try {
  1091. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1092. const existAudit = auditList.find(x => { return x.aid === lastId });
  1093. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1094. // const idList = this._.map(auditList, 'aid');
  1095. // let order = idList.length + 1;
  1096. if (existAudit) {
  1097. await transaction.delete(this.tableName, {mid: material.id, times: material.times, aid: lastId});
  1098. const sameOrder = auditList.filter(x => { return x.order === existAudit.order });
  1099. if (sameOrder.length === 1) {
  1100. const updateData = [];
  1101. auditList.forEach(x => {
  1102. if (x.order <= existAudit.order) return;
  1103. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1104. });
  1105. if (updateData.length > 0) {
  1106. await transaction.updateRows(updateData);
  1107. }
  1108. order = order - 1;
  1109. }
  1110. }
  1111. // 添加终审
  1112. const newAuditor = {
  1113. tid: material.tid, mid: material.id, aid: lastId,
  1114. times: material.times, order, status: auditConst.status.uncheck,
  1115. audit_type: auditType.key.common, audit_order: order,
  1116. };
  1117. await transaction.insert(this.tableName, newAuditor);
  1118. await transaction.commit();
  1119. } catch (err) {
  1120. await transaction.rollback();
  1121. throw err;
  1122. }
  1123. }
  1124. async getNumByMonth(tid, startMonth, endMonth) {
  1125. const sql = 'SELECT COUNT(*) as num FROM ?? t1 JOIN (SELECT MAX(id) as max_id FROM ?? WHERE tid = ? GROUP BY mid) t2 ON t1.id = t2.max_id WHERE t1.status = ? AND t1.end_time between ? and ?';
  1126. // 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 ?';
  1127. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1128. const result = await this.db.queryOne(sql, sqlParam);
  1129. return result ? result.num : 0;
  1130. }
  1131. /**
  1132. * 删除本次审批流程
  1133. * @param {Number} materialId - 调差id
  1134. * @param {Number} times - 第几次审批
  1135. * @param {Object} data - 更改参数
  1136. * @return {Promise<void>}
  1137. */
  1138. async saveAudit(materialId, times, sp_group, data) {
  1139. const transaction = await this.db.beginTransaction();
  1140. try {
  1141. const auditors = await this.getAuditGroupByList(materialId, times);
  1142. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1143. if (data.operate !== 'del') {
  1144. const exist = await this.getDataByCondition({ mid: materialId, times, aid: data.new_aid, is_old: 0 });
  1145. if (exist) throw '该审核人已存在,请勿重复添加';
  1146. }
  1147. if (data.operate === 'add') {
  1148. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1149. throw '当前人下无法操作新增';
  1150. }
  1151. const newAudit = {
  1152. tid: this.ctx.tender.id,
  1153. mid: materialId,
  1154. aid: data.new_aid,
  1155. order: now_audit.order + 1,
  1156. audit_order: now_audit.audit_order + 1,
  1157. audit_type: auditType.key.common,
  1158. times: times,
  1159. status: 1
  1160. };
  1161. // order+1
  1162. await this._syncOrderByDelete(transaction, materialId, now_audit.order+1, times, '+');
  1163. await transaction.insert(this.tableName, newAudit);
  1164. // 更新审批流程页数据,如果存在
  1165. } else if (data.operate === 'add-sibling') {
  1166. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1167. throw '当前人下无法操作新增';
  1168. }
  1169. const newAudit = {
  1170. tid: this.ctx.tender.id,
  1171. mid: materialId,
  1172. aid: data.new_aid,
  1173. order: now_audit.order,
  1174. audit_order: now_audit.audit_order,
  1175. audit_type: now_audit.audit_type,
  1176. times: times,
  1177. status: 1
  1178. };
  1179. await transaction.insert(this.tableName, newAudit);
  1180. } else if (data.operate === 'del') {
  1181. if (now_audit.status !== auditConst.status.uncheck) {
  1182. throw '当前人无法操作删除';
  1183. }
  1184. const flowAuditors = auditors.filter(x => { return x.order === now_audit.order; });
  1185. await transaction.delete(this.tableName, { mid: materialId, times, aid: now_audit.aid, order: now_audit.order });
  1186. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, materialId, now_audit.order, times);
  1187. // 旧的更新为is_old为1
  1188. await transaction.update(this.tableName, { is_old: 1 }, {
  1189. where: {
  1190. mid: materialId,
  1191. times,
  1192. aid: data.old_aid,
  1193. }
  1194. });
  1195. } else if (data.operate === 'change') {
  1196. const nowAudit = await this.getDataByCondition({ mid: materialId, times, aid: now_audit.aid, order: now_audit.order });
  1197. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1198. throw '当前人无法操作替换';
  1199. }
  1200. nowAudit.aid = data.new_aid;
  1201. await transaction.update(this.tableName, nowAudit);
  1202. // 旧的更新为is_old为1
  1203. await transaction.update(this.tableName, { is_old: 1 }, {
  1204. where: {
  1205. mid: materialId,
  1206. times,
  1207. aid: data.old_aid,
  1208. }
  1209. });
  1210. }
  1211. if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  1212. const newAuditors = await transaction.select(this.tableName, { where: { mid: materialId, times } });
  1213. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors.filter(x => { return x.is_old === 0; }));
  1214. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1215. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.material, shenpiConst.sp_type.material, uniqNewAuditorGroup, sp_group);
  1216. } else if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs) {
  1217. const newAuditors = await this.getAuditGroupByList(materialId, times, transaction);
  1218. await this.ctx.service.shenpiAudit.updateAuditList(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.material, shenpiConst.sp_type.material, this._.map(newAuditors, 'aid'));
  1219. }
  1220. // 更新到审批流程方法
  1221. await transaction.commit();
  1222. } catch (err) {
  1223. await transaction.rollback();
  1224. throw err;
  1225. }
  1226. }
  1227. async getAuditorGroup(materialId, times) {
  1228. const auditors = await this.getAuditors(materialId, times); // 全部参与的审批人
  1229. const newAuditors = auditors.filter(x => { return x.is_old === 0; });
  1230. return this.ctx.helper.groupAuditors(newAuditors);
  1231. }
  1232. async getUserGroup(materialId, times) {
  1233. const group = await this.getAuditorGroup(materialId, times);
  1234. const sql =
  1235. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1236. ' FROM ' + this.ctx.service.material.tableName + ' As m' +
  1237. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1238. ' ON m.user_id = pa.id' +
  1239. ' WHERE m.id = ?';
  1240. const sqlParam = [times, materialId, materialId];
  1241. const user = await this.db.queryOne(sql, sqlParam);
  1242. group.unshift([ user ]);
  1243. return group;
  1244. }
  1245. async getUniqUserGroup(materialId, times) {
  1246. const group = await this.getAuditorGroup(materialId, times);
  1247. const sql =
  1248. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`, 1 As audit_type, 0 As audit_order' +
  1249. ' FROM ' + this.ctx.service.material.tableName + ' As m' +
  1250. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1251. ' ON m.user_id = pa.id' +
  1252. ' WHERE m.id = ?';
  1253. const sqlParam = [times, materialId, materialId];
  1254. const user = await this.db.queryOne(sql, sqlParam);
  1255. user.audit_order = 0;
  1256. group.unshift([ user ]);
  1257. return this.ctx.helper.groupAuditorsUniq(group);
  1258. }
  1259. async getAuditorHistory(materialId, times, reverse = false) {
  1260. const history = [];
  1261. if (times >= 1) {
  1262. for (let i = 1; i <= times; i++) {
  1263. const auditors = await this.getAuditors(materialId, i);
  1264. const group = this.ctx.helper.groupAuditors(auditors);
  1265. const historyGroup = [];
  1266. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  1267. for (const g of group) {
  1268. const his = {
  1269. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1270. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1271. auditors: g
  1272. };
  1273. if (his.audit_type === auditType.key.common) {
  1274. his.name = g[0].name;
  1275. } else {
  1276. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1277. }
  1278. his.is_final = his.audit_order === max_order;
  1279. if (g[0].begin_time) {
  1280. his.begin_time = g[0].begin_time;
  1281. const beginTime = this.ctx.moment(g[0].begin_time);
  1282. his.beginYear = beginTime.format('YYYY');
  1283. his.beginDate = beginTime.format('MM-DD');
  1284. his.beginTime = beginTime.format('HH:mm:ss');
  1285. }
  1286. let end_time;
  1287. g.forEach(x => {
  1288. if (x.status === auditConst.status.checkSkip) return;
  1289. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1290. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1291. end_time = x.end_time;
  1292. if (his.status !== auditConst.status.checking) his.status = x.status;
  1293. }
  1294. });
  1295. if (end_time) {
  1296. his.end_time = end_time;
  1297. const endTime = this.ctx.moment(end_time);
  1298. his.endYear = endTime.format('YYYY');
  1299. his.endDate = endTime.format('MM-DD');
  1300. his.endTime = endTime.format('HH:mm:ss');
  1301. }
  1302. historyGroup.push(his);
  1303. }
  1304. if (reverse) {
  1305. history.push(historyGroup.reverse());
  1306. } else {
  1307. history.push(historyGroup);
  1308. }
  1309. }
  1310. }
  1311. return history;
  1312. }
  1313. async getUniqAuditor(materialId, times) {
  1314. const auditors = await this.getAuditors(materialId, times); // 全部参与的审批人
  1315. const result = [];
  1316. auditors.forEach(x => {
  1317. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  1318. result.push(x);
  1319. }
  1320. });
  1321. return result;
  1322. }
  1323. /**
  1324. * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
  1325. *
  1326. * @param auditorId
  1327. * @return {Promise<*>}
  1328. */
  1329. async getDonesByAudit(auditorId, spid = '') {
  1330. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  1331. const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre, auditConst.status.checkAgain];
  1332. const sql =
  1333. 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`mid`, t.`id`, m.`order` As `morder`, t.`name`, t.`spid` ' +
  1334. ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS m ON la.`mid` = m.`id`' +
  1335. ' WHERE la.`aid` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) +')' + spSql +
  1336. ' ORDER BY la.`end_time` DESC';
  1337. const sqlParam = [
  1338. this.tableName,
  1339. this.ctx.service.tender.tableName,
  1340. this.ctx.service.material.tableName,
  1341. auditorId,
  1342. ];
  1343. return await this.db.query(sql, sqlParam);
  1344. }
  1345. }
  1346. return MaterialAudit;
  1347. };