safe_inspection_audit.js 66 KB

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