material_audit.js 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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, aid: r.aid, 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), exponent_decimal = (this.ctx.material.exponent_decimal ? this.ctx.material.exponent_decimal : materialConst.exponent_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, exponent_decimal.tp) : null,
  230. ex_tax_tp: materialInfo.ex_tax_tp !== null ? this.ctx.helper.round(materialInfo.ex_tax_tp, exponent_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 shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  282. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  283. const users = this._.map(audits, 'aid');
  284. const wechatData = {
  285. wap_url: shenpiUrl,
  286. qi: materialInfo.order,
  287. status: wxConst.status.check,
  288. tips: wxConst.tips.check,
  289. begin_time: Date.parse(new Date()),
  290. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), materialInfo.ex_tp),
  291. 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), materialInfo.ex_tax_tp),
  292. };
  293. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  294. for (const audit of audits) {
  295. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  296. pid: this.ctx.session.sessionProject.id,
  297. tid: this.ctx.tender.id,
  298. uid: audit.aid,
  299. sp_type: 'material',
  300. sp_id: audit.id,
  301. table_name: this.tableName,
  302. template: wxConst.template.material,
  303. wx_data: wechatData,
  304. });
  305. }
  306. // todo 更新标段tender状态 ?
  307. // 检查三方特殊推送
  308. await this.ctx.service.specMsg.addMaterialMsg(transaction, this.ctx.session.sessionProject.id, materialInfo, pushOperate.material.flow);
  309. await transaction.commit();
  310. } catch (err) {
  311. await transaction.rollback();
  312. throw err;
  313. }
  314. return true;
  315. }
  316. async _checked(pid, materialId, checkData, times) {
  317. const accountId = this.ctx.session.sessionUser.accountId;
  318. const time = new Date();
  319. // 整理当前流程审核人状态更新
  320. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  321. if (audits.length === 0) throw '审核数据错误';
  322. const selfAudit = audits.find(x => { return x.aid === accountId; });
  323. if (!selfAudit) throw '当前标段您无权审批';
  324. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: selfAudit.order } });
  325. const nextAudits = await this.getAllDataByCondition({ where: { mid: materialId, times, order: selfAudit.order + 1 } });
  326. const transaction = await this.db.beginTransaction();
  327. try {
  328. // 获取当前总金额及独立单价期的金额,添加到tp_data中,报表使用
  329. const tp_data = await this.getTpData(transaction, materialId);
  330. await transaction.update(this.tableName, {
  331. id: selfAudit.id,
  332. status: checkData.checkType,
  333. opinion: checkData.opinion,
  334. end_time: time,
  335. tp_data: JSON.stringify(tp_data),
  336. });
  337. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, selfAudit.id);
  338. // 获取推送必要信息
  339. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, accountId, checkData.opinion);
  340. const auditors = await this.getAuditorsWithOwner(materialId, times);
  341. // 添加推送
  342. const records = [];
  343. // const records = [{ pid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checked, content: noticeContent }];
  344. auditors.forEach(audit => {
  345. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checked, content: noticeContent });
  346. });
  347. await transaction.insert('zh_notice', records);
  348. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  349. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  350. if (audits.length === 1 || selfAudit.audit_type === auditType.key.or) {
  351. // 或签更新他人审批状态
  352. if (selfAudit.audit_type === auditType.key.or) {
  353. const updateOther = [];
  354. for (const audit of audits) {
  355. if (audit.aid === selfAudit.aid) continue;
  356. updateOther.push({
  357. id: audit.id,
  358. status: auditConst.status.checkSkip,
  359. opinion: '',
  360. end_time: time,
  361. });
  362. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, audit.id);
  363. }
  364. if (updateOther.length > 0) transaction.updateRows(this.tableName, updateOther);
  365. }
  366. // 无下一审核人表示,审核结束
  367. if (nextAudits.length > 0) {
  368. // 复制一份下一审核人数据
  369. // await this.ctx.service.stagePay.copyAuditStagePays(this.ctx.stage, this.ctx.stage.times, nextAudit.order, transaction);
  370. // 流程至下一审批人
  371. const updateData = nextAudits.map(x => {
  372. return {
  373. id: x.id,
  374. status: auditConst.status.checking,
  375. begin_time: time,
  376. tp_data: JSON.stringify(tp_data)
  377. };
  378. });
  379. await transaction.updateRows(this.tableName, updateData);
  380. // 同步 期信息
  381. await transaction.update(this.ctx.service.material.tableName, {
  382. id: materialId, status: auditConst.status.checking,
  383. });
  384. const begin_audit = await this.getDataByCondition({
  385. mid: materialId,
  386. order: 1,
  387. });
  388. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  389. // 微信模板通知
  390. const wechatData = {
  391. wap_url: shenpiUrl,
  392. qi: materialInfo.order,
  393. status: wxConst.status.check,
  394. tips: wxConst.tips.check,
  395. begin_time: Date.parse(begin_audit.begin_time),
  396. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), materialInfo.ex_tp),
  397. 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), materialInfo.ex_tax_tp),
  398. };
  399. await this.ctx.helper.sendWechat(this._.map(nextAudits, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  400. // 重新发送配置
  401. for (const a of nextAudits) {
  402. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  403. pid: this.ctx.session.sessionProject.id,
  404. tid: this.ctx.tender.id,
  405. uid: a.aid,
  406. sp_type: 'material',
  407. sp_id: a.id,
  408. table_name: this.tableName,
  409. template: wxConst.template.material,
  410. wx_data: wechatData,
  411. });
  412. }
  413. // 检查三方特殊推送
  414. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  415. } else {
  416. // 本期结束
  417. // 同步 期信息
  418. await transaction.update(this.ctx.service.material.tableName, {
  419. id: materialId, status: checkData.checkType,
  420. });
  421. // 处理旧数据,防止重复插入到历史表
  422. await transaction.delete(this.ctx.service.materialBillsHistory.tableName, {
  423. tid: this.ctx.tender.id,
  424. order: this.ctx.material.order
  425. });
  426. const mbhList = [];
  427. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({where: {tid: this.ctx.tender.id}});
  428. for (const mb of materialBillsData) {
  429. if (mb.code === '') {
  430. throw '调差工料编号不能为空';
  431. }
  432. const newMbh = {
  433. tid: this.ctx.tender.id,
  434. mid: this.ctx.material.id,
  435. order: this.ctx.material.order,
  436. mb_id: mb.id,
  437. quantity: mb.quantity,
  438. expr: mb.expr,
  439. msg_tp: mb.msg_tp,
  440. msg_times: mb.msg_times,
  441. msg_spread: mb.msg_spread,
  442. m_up_risk: mb.m_up_risk,
  443. m_down_risk: mb.m_down_risk,
  444. m_spread: mb.m_spread,
  445. m_tp: mb.m_tp,
  446. pre_tp: mb.pre_tp,
  447. m_tax_tp: mb.m_tax_tp,
  448. tax_pre_tp: mb.tax_pre_tp,
  449. origin: mb.origin,
  450. is_summary: mb.is_summary,
  451. m_tax: mb.m_tax,
  452. };
  453. mbhList.push(newMbh);
  454. }
  455. if (mbhList.length !== 0) await transaction.insert(this.ctx.service.materialBillsHistory.tableName, mbhList);
  456. // 处理旧数据,防止重复插入到历史表
  457. await transaction.delete(this.ctx.service.materialExponentHistory.tableName, {
  458. tid: this.ctx.tender.id,
  459. order: this.ctx.material.order
  460. });
  461. const materialExponentData = await this.ctx.service.materialExponent.getAllDataByCondition({where: {tid: this.ctx.tender.id}});
  462. const mehList = [];
  463. for (const me of materialExponentData) {
  464. const newMeh = {
  465. tid: this.ctx.tender.id,
  466. mid: this.ctx.material.id,
  467. order: this.ctx.material.order,
  468. me_id: me.id,
  469. type: me.type,
  470. weight_num: me.weight_num,
  471. basic_price: me.basic_price,
  472. basic_times: me.basic_times,
  473. m_price: me.m_price,
  474. calc_num: me.calc_num,
  475. is_summary: me.is_summary,
  476. };
  477. mehList.push(newMeh);
  478. }
  479. if (mehList.length !== 0) await transaction.insert(this.ctx.service.materialExponentHistory.tableName, mehList);
  480. // 微信模板通知
  481. const begin_audit = await this.getDataByCondition({
  482. mid: materialId,
  483. order: 1,
  484. });
  485. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  486. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  487. const wechatData = {
  488. wap_url: shenpiUrl,
  489. qi: materialInfo.order,
  490. status: wxConst.status.success,
  491. tips: wxConst.tips.success,
  492. begin_time: Date.parse(begin_audit.begin_time),
  493. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), materialInfo.ex_tp),
  494. 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), materialInfo.ex_tax_tp),
  495. };
  496. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  497. // 检查三方特殊推送
  498. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  499. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.checked);
  500. }
  501. } else {
  502. // 同步 期信息
  503. await transaction.update(this.ctx.service.material.tableName, {
  504. id: materialId,
  505. status: auditConst.status.checking,
  506. });
  507. }
  508. await transaction.commit();
  509. } catch (err) {
  510. await transaction.rollback();
  511. throw err;
  512. }
  513. }
  514. async _checkNo(pid, materialId, checkData, times) {
  515. const accountId = this.ctx.session.sessionUser.accountId;
  516. const time = new Date();
  517. // 整理当前流程审核人状态更新
  518. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  519. if (!audits) throw '审核数据错误';
  520. const selfAudit = audits.find(x => { return x.aid === accountId; });
  521. if (!selfAudit) throw '当前标段您无权审批';
  522. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times: selfAudit.times, order: selfAudit.order }});
  523. const auditors = await this.getUniqAuditor(materialId, times); // 全部参与的审批人
  524. const newAuditors = auditors.map(x => {
  525. return {
  526. aid: x.aid, tid: selfAudit.tid, mid: selfAudit.mid,
  527. times: times + 1, order: x.audit_order, status: auditConst.status.uncheck,
  528. audit_type: x.audit_type, audit_order: x.audit_order,
  529. }
  530. });
  531. const transaction = await this.db.beginTransaction();
  532. try {
  533. const tp_data = await this.getTpData(transaction, materialId);
  534. const updateData = [];
  535. audits.forEach(x => {
  536. updateData.push({
  537. id: x.id,
  538. status: x.aid === selfAudit.aid ? checkData.checkType : auditConst.status.checkSkip,
  539. opinion: x.aid === selfAudit.aid ? checkData.opinion : '',
  540. end_time: x.aid === selfAudit.aid ? time : null,
  541. tp_data: JSON.stringify(tp_data),
  542. });
  543. });
  544. await transaction.updateRows(this.tableName, updateData);
  545. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  546. // 添加到消息推送表
  547. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, selfAudit.aid, checkData.opinion);
  548. const records = [{ pid, tid: selfAudit.tid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNo, content: noticeContent }];
  549. auditors.forEach(audit => {
  550. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNo, content: noticeContent });
  551. });
  552. await transaction.insert(this.ctx.service.noticePush.tableName, records);
  553. // 同步期信息
  554. await transaction.update(this.ctx.service.material.tableName, {
  555. id: materialId, status: checkData.checkType,
  556. times: times + 1,
  557. tp_data: JSON.stringify(tp_data),
  558. });
  559. // 拷贝新一次审核流程列表
  560. await transaction.insert(this.tableName, newAuditors);
  561. // 微信模板通知
  562. const begin_audit = await this.getDataByCondition({
  563. mid: materialId,
  564. order: 1,
  565. });
  566. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  567. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  568. const users = this._.uniq(this._.concat(this._.map(auditors, 'aid'), materialInfo.user_id));
  569. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  570. const wechatData = {
  571. wap_url: shenpiUrl,
  572. qi: materialInfo.order,
  573. status: wxConst.status.back,
  574. tips: wxConst.tips.back,
  575. begin_time: Date.parse(begin_audit.begin_time),
  576. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), materialInfo.ex_tp),
  577. 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), materialInfo.ex_tax_tp),
  578. };
  579. await this.ctx.helper.sendWechat(users, smsTypeConst.const.TC, smsTypeConst.judge.result.toString(), wxConst.template.material, wechatData);
  580. // 检查三方特殊推送
  581. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  582. await transaction.commit();
  583. } catch (err) {
  584. await transaction.rollback();
  585. throw err;
  586. }
  587. }
  588. async _checkNoPre(pid, materialId, checkData, times) {
  589. const accountId = this.ctx.session.sessionUser.accountId;
  590. const time = new Date();
  591. // 整理当前流程审核人状态更新
  592. const audits = await this.getAllDataByCondition({ where: { mid: materialId, times, status: auditConst.status.checking } });
  593. if (audits.length === 0 || audits[0].order <= 1) throw '审核数据错误';
  594. const selfAudit = audits.find(x => { return x.aid === accountId; });
  595. if (!selfAudit) throw '当前标段您无权审批';
  596. const flowAudits = await this.getAllDataByCondition({ where: { mid: materialId, times: selfAudit.times, order: selfAudit.order }});
  597. // 添加重新审批后,不能用order-1,取groupby值里的上一个才对
  598. const auditors2 = await this.getAuditGroupByList(materialId, times);
  599. const preAuditors = auditors2.filter(x => { return x.audit_order === selfAudit.audit_order - 1});
  600. const transaction = await this.db.beginTransaction();
  601. try {
  602. const tp_data = await this.getTpData(transaction, materialId);
  603. // 添加到消息推送表
  604. const noticeContent = await this.getNoticeContent(pid, selfAudit.tid, materialId, selfAudit.aid, checkData.opinion);
  605. const records = [{ pid, tid: selfAudit.tid, type: pushType.material, uid: this.ctx.material.user_id, status: auditConst.status.checkNoPre, content: noticeContent }];
  606. auditors2.forEach(audit => {
  607. records.push({ pid, tid: selfAudit.tid, type: pushType.material, uid: audit.aid, status: auditConst.status.checkNoPre, content: noticeContent });
  608. });
  609. await transaction.insert('zh_notice', records);
  610. const updateData = [];
  611. for (const audit of audits) {
  612. if (audit.aid === selfAudit.aid) {
  613. updateData.push({
  614. id: audit.id, status: checkData.checkType, opinion: checkData.opinion, end_time: time, tp_data: JSON.stringify(tp_data)
  615. });
  616. } else {
  617. updateData.push({
  618. id: audit.id, status: auditConst.status.checkSkip, opinion: '', end_time: null, tp_data: JSON.stringify(tp_data)
  619. });
  620. }
  621. }
  622. await transaction.updateRows(this.tableName, updateData);
  623. await this.ctx.service.noticeAgain.stopNoticeAgain(transaction, this.tableName, this._.map(updateData, 'id'));
  624. // 顺移其后审核人流程顺序
  625. const sql = 'UPDATE ' + this.tableName + ' SET `order` = `order` + 2 WHERE mid = ? AND times = ? AND `order` > ?';
  626. await transaction.query(sql, [materialId, selfAudit.times, selfAudit.order]);
  627. const newAuditors = [];
  628. preAuditors.forEach(x => {
  629. newAuditors.push({
  630. tid: selfAudit.tid, mid: materialId, aid: x.aid,
  631. times: x.times, order: selfAudit.order + 1,
  632. status: auditConst.status.checking, begin_time: time,
  633. audit_type: x.audit_type, audit_order: x.audit_order,
  634. tp_data: JSON.stringify(tp_data),
  635. });
  636. });
  637. const newAuditors_result = await transaction.insert(this.tableName, newAuditors);
  638. // 获取刚批量添加的所有list
  639. for (let j = 0; j < preAuditors.length; j++) {
  640. newAuditors[j].id = newAuditors_result.insertId + j;
  641. }
  642. const newFlowAuditors = [];
  643. flowAudits.forEach(x => {
  644. newFlowAuditors.push({
  645. tid: selfAudit.tid, mid: materialId, aid: x.aid,
  646. times: x.times, order: selfAudit.order + 2,
  647. status: auditConst.status.uncheck,
  648. audit_type: x.audit_type, audit_order: x.audit_order,
  649. });
  650. });
  651. await transaction.insert(this.tableName, newFlowAuditors);
  652. // 微信模板通知
  653. const begin_audit = await this.getDataByCondition({
  654. mid: materialId,
  655. order: 1,
  656. });
  657. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  658. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  659. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  660. const wechatData = {
  661. wap_url: shenpiUrl,
  662. qi: materialInfo.order,
  663. status: wxConst.status.check,
  664. tips: wxConst.tips.check,
  665. begin_time: Date.parse(begin_audit.begin_time),
  666. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), materialInfo.ex_tp),
  667. 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), materialInfo.ex_tax_tp),
  668. };
  669. await this.ctx.helper.sendWechat(this._.map(preAuditors, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  670. for (const a of newAuditors) {
  671. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  672. pid: this.ctx.session.sessionProject.id,
  673. tid: this.ctx.tender.id,
  674. uid: a.aid,
  675. sp_type: 'material',
  676. sp_id: a.id,
  677. table_name: this.tableName,
  678. template: wxConst.template.material,
  679. wx_data: wechatData,
  680. });
  681. }
  682. // 检查三方特殊推送
  683. await this.ctx.service.specMsg.addMaterialMsg(transaction, pid, materialInfo, pushOperate.material.flow);
  684. await transaction.commit();
  685. } catch (error) {
  686. await transaction.rollback();
  687. throw error;
  688. }
  689. }
  690. /**
  691. * 审批
  692. * @param {Number} materialId - 材料调差期id
  693. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  694. * @param {Number} times - 第几次审批
  695. * @return {Promise<void>}
  696. */
  697. async check(materialId, checkData, times = 1) {
  698. if (checkData.checkType !== auditConst.status.checked && checkData.checkType !== auditConst.status.checkNo && checkData.checkType !== auditConst.status.checkNoPre) {
  699. throw '提交数据错误';
  700. }
  701. const pid = this.ctx.session.sessionProject.id;
  702. switch (checkData.checkType) {
  703. case auditConst.status.checked:
  704. await this._checked(pid, materialId, checkData, times);
  705. break;
  706. case auditConst.status.checkNo:
  707. await this._checkNo(pid, materialId, checkData, times);
  708. break;
  709. case auditConst.status.checkNoPre:
  710. await this._checkNoPre(pid, materialId, checkData, times);
  711. break;
  712. default:
  713. throw '无效审批操作';
  714. }
  715. }
  716. /**
  717. * 用于添加推送所需的content内容
  718. * @param {Number} pid 项目id
  719. * @param {Number} tid 台账id
  720. * @param {Number} mid 期id
  721. * @param {Number} uid 审批人id
  722. */
  723. async getNoticeContent(pid, tid, mid, uid, opinion = '') {
  724. const noticeSql = 'SELECT * FROM (SELECT ' +
  725. ' t.`id` As `tid`, ma.`mid`, t.`name`, m.`order`, pa.`name` As `su_name`, pa.role As `su_role`' +
  726. ' FROM (SELECT * FROM ?? WHERE `id` = ? ) As t' +
  727. ' LEFT JOIN ?? As m On t.`id` = m.`tid` AND m.`id` = ?' +
  728. ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  729. ' LEFT JOIN ?? As pa ON pa.`id` = ?' +
  730. ' WHERE t.`project_id` = ? ) as new_t GROUP BY new_t.`tid`';
  731. const noticeSqlParam = [this.ctx.service.tender.tableName, tid, this.ctx.service.material.tableName, mid, this.tableName, this.ctx.service.projectAccount.tableName, uid, pid];
  732. const content = await this.db.query(noticeSql, noticeSqlParam);
  733. if (content.length) {
  734. content[0].opinion = opinion;
  735. }
  736. return content.length ? JSON.stringify(content[0]) : '';
  737. }
  738. /**
  739. * 审批
  740. * @param {Number} materialId - 材料调差期id
  741. * @param {Number} times - 第几次审批
  742. * @return {Promise<void>}
  743. */
  744. async checkAgain(materialId, times = 1) {
  745. const accountId = this.ctx.session.sessionUser.accountId;
  746. const time = new Date();
  747. // 整理当前流程审核人状态更新
  748. const auditors = await this.getAllDataByCondition({
  749. where: { mid: materialId, times },
  750. orders: [['order', 'asc']],
  751. });
  752. const maxOrder = auditors[auditors.length - 1].order;
  753. const audits = auditors.filter(x => { return x.order === maxOrder});
  754. if (!audits || audits.length === 0 || maxOrder < 1) throw '审核数据错误';
  755. const selfAudit = audits.find(x => { return x.aid === accountId; });
  756. if (!selfAudit) throw '当前标段您无权审批';
  757. const finalAudit = selfAudit || audits[0];
  758. const transaction = await this.db.beginTransaction();
  759. try {
  760. const materialInfo = await this.ctx.service.material.getDataById(materialId);
  761. const material_decimal = materialInfo && materialInfo.decimal ? JSON.parse(materialInfo.decimal) : materialConst.decimal;
  762. const material_exponent_decimal = materialInfo && materialInfo.exponent_decimal ? JSON.parse(materialInfo.exponent_decimal) : materialConst.exponent_decimal;
  763. const tp_data = await this.getTpData(transaction, materialId, material_decimal, material_exponent_decimal);
  764. const checkAgainAuditors = [], checkingAuditors = [];
  765. audits.forEach(x => {
  766. checkAgainAuditors.push({
  767. tid: x.tid, mid: x.mid, aid: x.aid,
  768. times: x.times, order: x.order + 1,
  769. status: !selfAudit || x.aid === selfAudit.aid ? auditConst.status.checkAgain : auditConst.status.checkSkip,
  770. begin_time: time, end_time: time, opinion: '',
  771. audit_type: x.audit_type, audit_order: x.audit_order,
  772. });
  773. });
  774. audits.forEach(x => {
  775. checkingAuditors.push({
  776. tid: x.tid, mid: x.mid, aid: x.aid,
  777. times: x.times, order: x.order + 2,
  778. status: auditConst.status.checking,
  779. begin_time: time, end_time: time, opinion: '',
  780. audit_type: x.audit_type, audit_order: x.audit_order,
  781. tp_data: JSON.stringify(tp_data),
  782. });
  783. });
  784. await transaction.insert(this.tableName, checkAgainAuditors);
  785. const checkingAuditors_result = await transaction.insert(this.tableName, checkingAuditors);
  786. // 获取刚批量添加的所有list
  787. for (let j = 0; j < checkingAuditors.length; j++) {
  788. checkingAuditors[j].id = checkingAuditors_result.insertId + j;
  789. }
  790. // 本期结束
  791. // 同步 期信息
  792. await transaction.update(this.ctx.service.material.tableName, {
  793. id: materialId, status: auditConst.status.checking,
  794. final_auditor_str: '',
  795. });
  796. // 微信模板通知
  797. const shenpiUrl = await this.ctx.helper.urlToShort(this.ctx.protocol + '://' + this.ctx.host + '/wap/tender/' + this.ctx.tender.id + '/measure/material/' + materialInfo.order);
  798. const wechatData = {
  799. wap_url: shenpiUrl,
  800. qi: materialInfo.order,
  801. status: wxConst.status.check,
  802. tips: wxConst.tips.check,
  803. begin_time: Date.parse(new Date()),
  804. m_tp: this.ctx.helper.add(this.ctx.helper.round(materialInfo.m_tp, material_decimal.tp), materialInfo.ex_tp),
  805. 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), materialInfo.ex_tax_tp),
  806. };
  807. await this.ctx.helper.sendWechat(this._.map(checkingAuditors, 'aid'), smsTypeConst.const.TC, smsTypeConst.judge.approval.toString(), wxConst.template.material, wechatData);
  808. for (const a of checkingAuditors) {
  809. await this.ctx.service.noticeAgain.addNoticeAgain(transaction, smsTypeConst.const.TC, {
  810. pid: this.ctx.session.sessionProject.id,
  811. tid: this.ctx.tender.id,
  812. uid: a.aid,
  813. sp_type: 'material',
  814. sp_id: a.id,
  815. table_name: this.tableName,
  816. template: wxConst.template.material,
  817. wx_data: wechatData,
  818. });
  819. }
  820. // 检查三方特殊推送
  821. await this.ctx.service.specMsg.addMaterialMsg(transaction, this.ctx.session.sessionProject.id, materialInfo, pushOperate.material.flow);
  822. await transaction.commit();
  823. } catch (err) {
  824. await transaction.rollback();
  825. throw err;
  826. }
  827. }
  828. /**
  829. * 获取审核人需要审核的期列表
  830. *
  831. * @param auditorId
  832. * @return {Promise<*>}
  833. */
  834. async getAuditMaterial(auditorId, spid = '') {
  835. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  836. const sql = 'SELECT ma.`aid`, ma.`times`, ma.`order`, ma.`begin_time`, ma.`end_time`, ma.`tid`, ma.`mid`,' +
  837. ' m.`order` As `morder`, m.`status` As `mstatus`,' +
  838. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid` ' +
  839. ' FROM ?? AS ma' +
  840. ' LEFT JOIN ?? AS m On ma.mid = m.id' +
  841. ' LEFT JOIN ?? As t On m.tid = t.id' +
  842. ' WHERE ((ma.`aid` = ? and ma.`status` = ?) OR (m.`user_id` = ? and ma.`status` = ? and m.`status` = ? and ma.`times` = (m.`times`-1)))' + spSql +
  843. ' ORDER BY ma.`begin_time` DESC';
  844. 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];
  845. const result = await this.db.query(sql, sqlParam);
  846. // 过滤result中存在重复sid的值, 保留最新的一条
  847. const filterResult = [];
  848. const midArr = [];
  849. for (const r of result) {
  850. if (midArr.indexOf(r.mid) === -1) {
  851. filterResult.push(r);
  852. midArr.push(r.mid);
  853. }
  854. }
  855. return filterResult;
  856. }
  857. /**
  858. * 获取审核人审核的次数
  859. *
  860. * @param auditorId
  861. * @return {Promise<*>}
  862. */
  863. async getCountByChecked(auditorId, spid = '') {
  864. if (spid) {
  865. 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` = ?';
  866. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, spid];
  867. const result = await this.db.queryOne(sql, sqlParam);
  868. return result.count ? result.count : 0;
  869. }
  870. return await this.db.count(this.tableName, { aid: auditorId, status: [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre] });
  871. }
  872. /**
  873. * 获取最近一次审批结束时间
  874. *
  875. * @param auditorId
  876. * @return {Promise<*>}
  877. */
  878. async getLastEndTimeByChecked(auditorId, spid = '') {
  879. const sqSql = spid ? ' AND t.`spid` = "' + spid + '"' : '';
  880. const sql = 'SELECT a.`end_time` FROM ?? AS a LEFT JOIN ?? AS t ON a.`tid` = t.`id` WHERE a.`aid` = ? ' +
  881. 'AND a.`status` in (' + this.ctx.helper.getInArrStrSqlFilter([auditConst.status.checked, auditConst.status.checkNo]) + ')' + sqSql +
  882. ' ORDER BY a.`end_time` DESC';
  883. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId];
  884. const result = await this.db.queryOne(sql, sqlParam);
  885. return result ? result.end_time : null;
  886. }
  887. /**
  888. * 获取 某时间后 审批进度 更新的期
  889. * @param {Number} pid - 查询标段
  890. * @param {Number} uid - 查询人
  891. * @param {Date} time - 查询时间
  892. * @return {Promise<*>}
  893. */
  894. async getNoticeMaterial(pid, uid, time) {
  895. // const sql = 'SELECT * FROM (SELECT t.`name`, t.`project_id`, t.`type`, t.`user_id`, ' +
  896. // ' m.`order` As `m_order`, m.`status` As `m_status`, ' +
  897. // ' ma.`aid`, ma.`times`, ma.`order`, ma.`end_time`, ma.`tid`, ma.`mid`, ma.`status`, ' +
  898. // ' pa.`name` As `su_name`, pa.role As `su_role`, pa.company As `su_company`' +
  899. // ' FROM (SELECT * FROM ?? WHERE `user_id` = ? OR `id` in (SELECT `tid` FROM ?? WHERE `aid` = ? GROUP BY `tid`)) As t' +
  900. // ' LEFT JOIN ?? As m On t.`id` = m.`tid`' +
  901. // ' LEFT JOIN ?? As ma ON m.`id` = ma.`mid`' +
  902. // ' LEFT JOIN ?? As pa ON ma.`aid` = pa.`id`' +
  903. // ' WHERE ma.`end_time` > ? and t.`project_id` = ?' +
  904. // ' ORDER By ma.`end_time` DESC LIMIT 1000) as new_t GROUP BY new_t.`tid`' +
  905. // ' ORDER BY new_t.`end_time`';
  906. // const sqlParam = [this.ctx.service.tender.tableName, uid, this.tableName, uid, this.ctx.service.material.tableName, this.tableName,
  907. // this.ctx.service.projectAccount.tableName, time, pid];
  908. // return await this.db.query(sql, sqlParam);
  909. let notice = await this.db.select('zh_notice', {
  910. where: { pid, type: pushType.material, uid },
  911. orders: [['create_time', 'desc']],
  912. limit: 10, offset: 0,
  913. });
  914. notice = notice.map(v => {
  915. const extra = JSON.parse(v.content);
  916. delete v.content;
  917. return { ...v, ...extra };
  918. });
  919. return notice;
  920. }
  921. /**
  922. * 获取审核人流程列表
  923. *
  924. * @param auditorId
  925. * @return {Promise<*>}
  926. */
  927. async getAuditGroupByList(materialId, times) {
  928. // 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 ' +
  929. // ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  930. // ' WHERE la.`mid` = ? and la.`times` = ? GROUP BY la.`aid` ORDER BY la.`order`';
  931. // const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, times];
  932. const sql =
  933. 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_type, la.audit_order ' +
  934. ' FROM (SELECT `aid`, max(`order`) `order` FROM ?? WHERE `mid` = ? and `times` = ? and `is_old` = ? GROUP BY aid) sa' +
  935. ' LEFT JOIN ?? la ON sa.`aid` = la.`aid` AND sa.`order` = la.`order`' +
  936. ' Left JOIN ?? AS pa On la.`aid` = pa.`id` WHERE la.`mid` = ? and la.`times` = ? and la.`is_old` = ? order BY la.`order`';
  937. const sqlParam = [this.tableName, materialId, times, 0, this.tableName, this.ctx.service.projectAccount.tableName, materialId, times, 0];
  938. return await this.db.query(sql, sqlParam);
  939. }
  940. /**
  941. * 获取审核人流程列表(包括原报)
  942. * @param {Number} materialId 调差id
  943. * @param {Number} times 审核次数
  944. * @return {Promise<Array>} 查询结果集(包括原报)
  945. */
  946. async getAuditorsWithOwner(materialId, times = 1) {
  947. const result = await this.getAuditGroupByList(materialId, times);
  948. const sql =
  949. 'SELECT pa.`id` As aid, pa.`name`, pa.`company`, pa.`role`, ? As times, ? As mid, 0 As `order`' +
  950. ' FROM ' +
  951. this.ctx.service.material.tableName +
  952. ' As s' +
  953. ' LEFT JOIN ' +
  954. this.ctx.service.projectAccount.tableName +
  955. ' As pa' +
  956. ' ON s.user_id = pa.id' +
  957. ' WHERE s.id = ?';
  958. const sqlParam = [times, materialId, materialId];
  959. const user = await this.db.queryOne(sql, sqlParam);
  960. result.unshift(user);
  961. return result;
  962. }
  963. /**
  964. * 获取tender下所有的审核列表(报表用)
  965. * @param {Number} tenderId
  966. */
  967. async getAuditorsByTender(tenderId) {
  968. const sql = 'SELECT la.`tid`, la.`mid`, la.`aid`, la.`order`, la.`times`, la.`status`, la.`opinion`, la.`begin_time`, la.`end_time`' +
  969. ' FROM ?? AS la ' +
  970. ' WHERE la.`tid` = ?';
  971. const sqlParam = [this.tableName, tenderId];
  972. return await this.db.query(sql, sqlParam);
  973. }
  974. /**
  975. * 复制上一期的审批人列表给最新一期
  976. *
  977. * @param transaction - 新增一期的事务
  978. * @param {Object} preMaterial - 上一期
  979. * @param {Object} newaMaterial - 最新一期
  980. * @return {Promise<*>}
  981. */
  982. async copyPreMaterialAuditors(transaction, preMaterial, newMaterial) {
  983. const auditors = await this.getAuditGroupByList(preMaterial.id, preMaterial.times);
  984. const newAuditors = [];
  985. for (const a of auditors) {
  986. const na = {
  987. tid: preMaterial.tid,
  988. mid: newMaterial.id,
  989. aid: a.aid,
  990. times: newMaterial.times,
  991. order: a.audit_order,
  992. status: auditConst.status.uncheck,
  993. audit_type: a.audit_type,
  994. audit_order: a.audit_order,
  995. };
  996. newAuditors.push(na);
  997. }
  998. if (newAuditors.length > 0) {
  999. const result = await transaction.insert(this.tableName, newAuditors);
  1000. return (result.effectRows = auditors.length);
  1001. } else {
  1002. return true;
  1003. }
  1004. }
  1005. /**
  1006. * 移除审核人
  1007. *
  1008. * @param {Number} materialId - 材料调差期id
  1009. * @param {Number} status - 期状态
  1010. * @param {Number} status - 期次数
  1011. * @return {Promise<boolean>}
  1012. */
  1013. async getAuditorByStatus(materialId, status, times = 1) {
  1014. let auditor = null;
  1015. let sql = '';
  1016. let sqlParam = '';
  1017. switch (status) {
  1018. case auditConst.status.checking :
  1019. case auditConst.status.checked :
  1020. case auditConst.status.checkNoPre :
  1021. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  1022. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1023. ' WHERE la.`mid` = ? and la.`status` = ? ' +
  1024. ' ORDER BY la.`times` desc, la.`order` desc';
  1025. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, status];
  1026. auditor = await this.db.queryOne(sql, sqlParam);
  1027. break;
  1028. case auditConst.status.checkNo :
  1029. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`aid`, la.`order` ' +
  1030. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id`' +
  1031. ' WHERE la.`mid` = ? and la.`status` = ? and la.`times` = ?' +
  1032. ' ORDER BY la.`times` desc, la.`order` desc';
  1033. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, auditConst.status.checkNo, parseInt(times) - 1];
  1034. auditor = await this.db.queryOne(sql, sqlParam);
  1035. break;
  1036. case auditConst.status.uncheck :
  1037. default:break;
  1038. }
  1039. return auditor;
  1040. }
  1041. async getAuditorsByStatus(materialId, status, times = 1) {
  1042. let auditor = [];
  1043. let sql = '';
  1044. let sqlParam = '';
  1045. let cur;
  1046. switch (status) {
  1047. case auditConst.status.checking:
  1048. case auditConst.status.checked:
  1049. case auditConst.status.checkNoPre:
  1050. cur = await this.db.queryOne(`SELECT * From ${this.tableName} where mid = ? AND times = ? AND status = ? ORDER By times DESC, ` + '`order` DESC', [materialId, times, status]);
  1051. if (!cur) return [];
  1052. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_order, la.audit_type ' +
  1053. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1054. ' WHERE la.`mid` = ? and la.`order` = ? and times = ?';
  1055. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, cur.order, times];
  1056. auditor = await this.db.query(sql, sqlParam);
  1057. break;
  1058. case auditConst.status.checkNo:
  1059. 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]);
  1060. if (!cur) return [];
  1061. sql = 'SELECT la.`aid`, pa.`name`, pa.`company`, pa.`role`, la.`times`, la.`mid`, la.`order`, la.`status`, la.audit_order, la.audit_type ' +
  1062. ' FROM ?? AS la Left Join ?? AS pa On la.`aid` = pa.`id` ' +
  1063. ' WHERE la.`mid` = ? and la.`order` = ? and la.`times` = ?';
  1064. sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, materialId, cur.order, parseInt(times) - 1];
  1065. auditor = await this.db.query(sql, sqlParam);
  1066. break;
  1067. case auditConst.status.uncheck:
  1068. default:
  1069. break;
  1070. }
  1071. return auditor;
  1072. }
  1073. async getAllAuditors(tenderId) {
  1074. const sql = 'SELECT ma.aid, ma.tid FROM ' + this.tableName + ' ma' +
  1075. ' LEFT JOIN ' + this.ctx.service.tender.tableName + ' t On ma.tid = t.id' +
  1076. ' WHERE t.id = ?' +
  1077. ' GROUP BY ma.aid';
  1078. const sqlParam = [tenderId];
  1079. return this.db.query(sql, sqlParam);
  1080. }
  1081. /**
  1082. * 取待审批期列表(wap用)
  1083. *
  1084. * @param auditorId
  1085. * @return {Promise<*>}
  1086. */
  1087. async getAuditMaterialByWap(auditorId) {
  1088. const sql =
  1089. 'SELECT sa.`aid`, sa.`times`, sa.`begin_time`, sa.`end_time`, sa.`tid`, sa.`mid`,' +
  1090. // ' s.`order` As `sorder`, s.`status` As `sstatus`, s.`s_time`, s.`contract_tp`, s.`qc_tp`, s.`pre_contract_tp`, s.`pre_qc_tp`, s.`yf_tp`, s.`pre_yf_tp`, ' +
  1091. ' s.*,' +
  1092. ' t.`name`, t.`project_id`, t.`type`, t.`user_id`, t.`spid`,' +
  1093. ' ti.`deal_info` ' +
  1094. ' FROM ?? AS sa' +
  1095. ' Left Join ?? AS s On sa.`mid` = s.`id`' +
  1096. ' Left Join ?? As t On sa.`tid` = t.`id`' +
  1097. ' Left Join ?? AS ti ON ti.`tid` = t.`id`' +
  1098. ' WHERE sa.`aid` = ? and sa.`status` = ?';
  1099. const sqlParam = [
  1100. this.tableName,
  1101. this.ctx.service.material.tableName,
  1102. this.ctx.service.tender.tableName,
  1103. this.ctx.service.tenderInfo.tableName,
  1104. auditorId,
  1105. auditConst.status.checking,
  1106. ];
  1107. return await this.db.query(sql, sqlParam);
  1108. }
  1109. async updateNewAuditList(material, newList) {
  1110. const transaction = await this.db.beginTransaction();
  1111. try {
  1112. // 先删除旧的审批流,再添加新的
  1113. await transaction.delete(this.tableName, { mid: material.id, times: material.times });
  1114. const newAuditors = [];
  1115. for (const auditor of newList) {
  1116. newAuditors.push({
  1117. tid: material.tid, mid: material.id, aid: auditor.audit_id,
  1118. times: material.times, order: auditor.audit_order, status: auditConst.status.uncheck,
  1119. audit_type: auditor.audit_type, audit_order: auditor.audit_order,
  1120. });
  1121. }
  1122. if(newAuditors.length > 0) await transaction.insert(this.tableName, newAuditors);
  1123. await transaction.commit();
  1124. } catch (err) {
  1125. await transaction.rollback();
  1126. throw err;
  1127. }
  1128. }
  1129. async updateLastAudit(material, auditList, lastId) {
  1130. const transaction = await this.db.beginTransaction();
  1131. try {
  1132. // 先判断auditList里的aid是否与lastId相同,相同则删除并重新更新order
  1133. const existAudit = auditList.find(x => { return x.aid === lastId });
  1134. let order = auditList.length > 0 ? auditList.reduce((rst, a) => { return Math.max(rst, a.order)}, 0) + 1 : 1; // 最大值 + 1
  1135. // const idList = this._.map(auditList, 'aid');
  1136. // let order = idList.length + 1;
  1137. if (existAudit) {
  1138. await transaction.delete(this.tableName, {mid: material.id, times: material.times, aid: lastId});
  1139. const sameOrder = auditList.filter(x => { return x.order === existAudit.order });
  1140. if (sameOrder.length === 1) {
  1141. const updateData = [];
  1142. auditList.forEach(x => {
  1143. if (x.order <= existAudit.order) return;
  1144. updateData.push({id: x.id, order: x.order - 1, audit_order: x.audit_order - 1});
  1145. });
  1146. if (updateData.length > 0) {
  1147. await transaction.updateRows(this.tableName, updateData);
  1148. }
  1149. order = order - 1;
  1150. }
  1151. }
  1152. // 添加终审
  1153. const newAuditor = {
  1154. tid: material.tid, mid: material.id, aid: lastId,
  1155. times: material.times, order, status: auditConst.status.uncheck,
  1156. audit_type: auditType.key.common, audit_order: order,
  1157. };
  1158. await transaction.insert(this.tableName, newAuditor);
  1159. await transaction.commit();
  1160. } catch (err) {
  1161. await transaction.rollback();
  1162. throw err;
  1163. }
  1164. }
  1165. async getNumByMonth(tid, startMonth, endMonth) {
  1166. 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 ?';
  1167. // 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 ?';
  1168. const sqlParam = [this.tableName, this.tableName, tid, auditConst.status.checked, startMonth, endMonth];
  1169. const result = await this.db.queryOne(sql, sqlParam);
  1170. return result ? result.num : 0;
  1171. }
  1172. /**
  1173. * 删除本次审批流程
  1174. * @param {Number} materialId - 调差id
  1175. * @param {Number} times - 第几次审批
  1176. * @param {Object} data - 更改参数
  1177. * @return {Promise<void>}
  1178. */
  1179. async saveAudit(materialId, times, sp_group, data) {
  1180. const transaction = await this.db.beginTransaction();
  1181. try {
  1182. const auditors = await this.getAuditGroupByList(materialId, times);
  1183. const now_audit = this._.find(auditors, { aid: data.old_aid });
  1184. if (data.operate !== 'del') {
  1185. const exist = await this.getDataByCondition({ mid: materialId, times, aid: data.new_aid, is_old: 0 });
  1186. if (exist) throw '该审核人已存在,请勿重复添加';
  1187. }
  1188. if (data.operate === 'add') {
  1189. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1190. throw '当前人下无法操作新增';
  1191. }
  1192. const newAudit = {
  1193. tid: this.ctx.tender.id,
  1194. mid: materialId,
  1195. aid: data.new_aid,
  1196. order: now_audit.order + 1,
  1197. audit_order: now_audit.audit_order + 1,
  1198. audit_type: auditType.key.common,
  1199. times: times,
  1200. status: 1
  1201. };
  1202. // order+1
  1203. await this._syncOrderByDelete(transaction, materialId, now_audit.order+1, times, '+');
  1204. await transaction.insert(this.tableName, newAudit);
  1205. // 更新审批流程页数据,如果存在
  1206. } else if (data.operate === 'add-sibling') {
  1207. if (now_audit.status !== auditConst.status.uncheck && now_audit.status !== auditConst.status.checking) {
  1208. throw '当前人下无法操作新增';
  1209. }
  1210. const newAudit = {
  1211. tid: this.ctx.tender.id,
  1212. mid: materialId,
  1213. aid: data.new_aid,
  1214. order: now_audit.order,
  1215. audit_order: now_audit.audit_order,
  1216. audit_type: now_audit.audit_type,
  1217. times: times,
  1218. status: 1
  1219. };
  1220. await transaction.insert(this.tableName, newAudit);
  1221. } else if (data.operate === 'del') {
  1222. if (now_audit.status !== auditConst.status.uncheck) {
  1223. throw '当前人无法操作删除';
  1224. }
  1225. const flowAuditors = auditors.filter(x => { return x.order === now_audit.order; });
  1226. await transaction.delete(this.tableName, { mid: materialId, times, aid: now_audit.aid, order: now_audit.order });
  1227. if (flowAuditors.length === 1) await this._syncOrderByDelete(transaction, materialId, now_audit.order, times);
  1228. // 旧的更新为is_old为1
  1229. await transaction.update(this.tableName, { is_old: 1 }, {
  1230. where: {
  1231. mid: materialId,
  1232. times,
  1233. aid: data.old_aid,
  1234. }
  1235. });
  1236. } else if (data.operate === 'change') {
  1237. const nowAudit = await this.getDataByCondition({ mid: materialId, times, aid: now_audit.aid, order: now_audit.order });
  1238. if (now_audit.status !== auditConst.status.uncheck || !nowAudit) {
  1239. throw '当前人无法操作替换';
  1240. }
  1241. nowAudit.aid = data.new_aid;
  1242. await transaction.update(this.tableName, nowAudit);
  1243. // 旧的更新为is_old为1
  1244. await transaction.update(this.tableName, { is_old: 1 }, {
  1245. where: {
  1246. mid: materialId,
  1247. times,
  1248. aid: data.old_aid,
  1249. }
  1250. });
  1251. }
  1252. if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdspl) {
  1253. const newAuditors = await transaction.select(this.tableName, { where: { mid: materialId, times } });
  1254. const newAuditorGroup = this.ctx.helper.groupAuditors(newAuditors.filter(x => { return x.is_old === 0; }));
  1255. const uniqNewAuditorGroup = this.ctx.helper.groupAuditorsUniq(newAuditorGroup);
  1256. await this.ctx.service.shenpiAudit.updateAuditListWithAuditType(transaction, this.ctx.tender.id, this.ctx.tender.info.shenpi.material, shenpiConst.sp_type.material, uniqNewAuditorGroup, sp_group);
  1257. } else if (this.ctx.tender.info.shenpi.material === shenpiConst.sp_status.gdzs) {
  1258. const newAuditors = await this.getAuditGroupByList(materialId, times, transaction);
  1259. 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'));
  1260. }
  1261. // 更新到审批流程方法
  1262. await transaction.commit();
  1263. } catch (err) {
  1264. await transaction.rollback();
  1265. throw err;
  1266. }
  1267. }
  1268. async getAuditorGroup(materialId, times) {
  1269. const auditors = await this.getAuditors(materialId, times); // 全部参与的审批人
  1270. const newAuditors = auditors.filter(x => { return x.is_old === 0; });
  1271. return this.ctx.helper.groupAuditors(newAuditors);
  1272. }
  1273. async getUserGroup(materialId, times) {
  1274. const group = await this.getAuditorGroup(materialId, times);
  1275. const sql =
  1276. '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' +
  1277. ' FROM ' + this.ctx.service.material.tableName + ' As m' +
  1278. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1279. ' ON m.user_id = pa.id' +
  1280. ' WHERE m.id = ?';
  1281. const sqlParam = [times, materialId, materialId];
  1282. const user = await this.db.queryOne(sql, sqlParam);
  1283. group.unshift([ user ]);
  1284. return group;
  1285. }
  1286. async getUniqUserGroup(materialId, times) {
  1287. const group = await this.getAuditorGroup(materialId, times);
  1288. const sql =
  1289. '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' +
  1290. ' FROM ' + this.ctx.service.material.tableName + ' As m' +
  1291. ' LEFT JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa' +
  1292. ' ON m.user_id = pa.id' +
  1293. ' WHERE m.id = ?';
  1294. const sqlParam = [times, materialId, materialId];
  1295. const user = await this.db.queryOne(sql, sqlParam);
  1296. user.audit_order = 0;
  1297. group.unshift([ user ]);
  1298. return this.ctx.helper.groupAuditorsUniq(group);
  1299. }
  1300. async getAuditorHistory(materialId, times, reverse = false) {
  1301. const history = [];
  1302. if (times >= 1) {
  1303. for (let i = 1; i <= times; i++) {
  1304. const auditors = await this.getAuditors(materialId, i);
  1305. const group = this.ctx.helper.groupAuditors(auditors);
  1306. const historyGroup = [];
  1307. const max_order = group.length > 0 && group[group.length - 1].length > 0 ? group[group.length - 1][0].audit_order : -1;
  1308. for (const g of group) {
  1309. const his = {
  1310. beginYear: '', beginDate: '', beginTime: '', endYear: '', endDate: '', endTime: '', begin_time: null, end_time: null,
  1311. audit_type: g[0].audit_type, audit_order: g[0].audit_order,
  1312. auditors: g
  1313. };
  1314. if (his.audit_type === auditType.key.common) {
  1315. his.name = g[0].name;
  1316. } else {
  1317. his.name = this.ctx.helper.transFormToChinese(his.audit_order) + '审';
  1318. }
  1319. his.is_final = his.audit_order === max_order;
  1320. if (g[0].begin_time) {
  1321. his.begin_time = g[0].begin_time;
  1322. const beginTime = this.ctx.moment(g[0].begin_time);
  1323. his.beginYear = beginTime.format('YYYY');
  1324. his.beginDate = beginTime.format('MM-DD');
  1325. his.beginTime = beginTime.format('HH:mm:ss');
  1326. }
  1327. let end_time;
  1328. g.forEach(x => {
  1329. if (x.status === auditConst.status.checkSkip) return;
  1330. if (!his.status || x.status === auditConst.status.checking) his.status = x.status;
  1331. if (x.end_time && (!end_time || x.end_time > end_time)) {
  1332. end_time = x.end_time;
  1333. if (his.status !== auditConst.status.checking) his.status = x.status;
  1334. }
  1335. });
  1336. if (end_time) {
  1337. his.end_time = end_time;
  1338. const endTime = this.ctx.moment(end_time);
  1339. his.endYear = endTime.format('YYYY');
  1340. his.endDate = endTime.format('MM-DD');
  1341. his.endTime = endTime.format('HH:mm:ss');
  1342. }
  1343. historyGroup.push(his);
  1344. }
  1345. if (reverse) {
  1346. history.push(historyGroup.reverse());
  1347. } else {
  1348. history.push(historyGroup);
  1349. }
  1350. }
  1351. }
  1352. return history;
  1353. }
  1354. async getUniqAuditor(materialId, times) {
  1355. const auditors = await this.getAuditors(materialId, times); // 全部参与的审批人
  1356. const result = [];
  1357. auditors.forEach(x => {
  1358. if (result.findIndex(r => { return x.aid === r.aid && x.audit_order === r.audit_order; }) < 0) {
  1359. result.push(x);
  1360. }
  1361. });
  1362. return result;
  1363. }
  1364. /**
  1365. * 获取审核人已经审核过的审批信息(包括退回,通过,重新审批等)
  1366. *
  1367. * @param auditorId
  1368. * @return {Promise<*>}
  1369. */
  1370. async getDonesByAudit(auditorId, spid = '') {
  1371. const spSql = spid ? ' and t.`spid` = "' + spid + '"' : '';
  1372. const status = [auditConst.status.checked, auditConst.status.checkNo, auditConst.status.checkNoPre, auditConst.status.checkAgain];
  1373. const sql =
  1374. 'SELECT la.`status`, la.`end_time` as `shenpi_time`, la.`mid`, t.`id`, m.`order` As `morder`, t.`name`, t.`spid` ' +
  1375. ' FROM ?? AS la Left Join ?? AS t ON la.`tid` = t.`id` LEFT JOIN ?? AS m ON la.`mid` = m.`id`' +
  1376. ' WHERE la.`aid` = ? AND la.`status` in (' + this.ctx.helper.getInArrStrSqlFilter(status) +')' + spSql +
  1377. ' ORDER BY la.`end_time` DESC';
  1378. const sqlParam = [
  1379. this.tableName,
  1380. this.ctx.service.tender.tableName,
  1381. this.ctx.service.material.tableName,
  1382. auditorId,
  1383. ];
  1384. return await this.db.query(sql, sqlParam);
  1385. }
  1386. }
  1387. return MaterialAudit;
  1388. };