quality_inspection.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 auditType = require('../const/audit').auditType;
  11. module.exports = app => {
  12. class QualityInspection extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'quality_inspection';
  22. }
  23. async loadUser(inspection) {
  24. const status = auditConst.status;
  25. const accountId = this.ctx.session.sessionUser.accountId;
  26. inspection.user = await this.ctx.service.projectAccount.getAccountInfoById(inspection.uid);
  27. if (inspection.rectification_uid) {
  28. inspection.rectification_user = await this.ctx.service.projectAccount.getAccountInfoById(inspection.rectification_uid);
  29. }
  30. inspection.auditors = await this.ctx.service.qualityInspectionAudit.getAuditors(inspection.id, inspection.times); // 全部参与的审批人
  31. inspection.auditorIds = this._.map(inspection.auditors, 'aid');
  32. inspection.curAuditors = inspection.auditors.filter(x => { return x.status === status.checking || x.status === status.rectification; }); // 当前流程中审批中的审批人
  33. inspection.curAuditorIds = this._.map(inspection.curAuditors, 'aid');
  34. inspection.flowAuditors = inspection.curAuditors.length > 0 ? inspection.auditors.filter(x => { return x.order === inspection.curAuditors[0].order; }) : []; // 当前流程中参与的审批人(包含会签时,审批通过的人)
  35. inspection.flowAuditorIds = this._.map(inspection.flowAuditors, 'aid');
  36. inspection.nextAuditors = inspection.curAuditors.length > 0 ? inspection.auditors.filter(x => { return x.order === inspection.curAuditors[0].order + 1; }) : [];
  37. inspection.nextAuditorIds = this._.map(inspection.nextAuditors, 'aid');
  38. const newAuditors = inspection.auditors.filter(x => { return x.is_old === 0 && x.is_rectification === 0; });
  39. inspection.auditorGroups = this.ctx.helper.groupAuditors(newAuditors);
  40. inspection.userGroups = this.ctx.helper.groupAuditorsUniq(inspection.auditorGroups);
  41. inspection.userGroups.unshift([{
  42. aid: inspection.user.id, order: 0, times: inspection.times, audit_order: 0, audit_type: auditType.key.common,
  43. name: inspection.user.name, role: inspection.user.role, company: inspection.user.company,
  44. }]);
  45. inspection.finalAuditorIds = inspection.userGroups[inspection.userGroups.length - 1].map(x => { return x.aid; });
  46. }
  47. async loadAuditViewData(inspection) {
  48. const times = inspection.status === auditConst.status.checkNo ? inspection.times - 1 : inspection.times;
  49. if (!inspection.user) inspection.user = await this.ctx.service.projectAccount.getAccountInfoById(inspection.uid);
  50. inspection.auditHistory = await this.ctx.service.qualityInspectionAudit.getAuditorHistory(inspection.id, times);
  51. // 获取审批流程中左边列表
  52. if (inspection.status === auditConst.status.checkNo && inspection.uid !== this.ctx.session.sessionUser.accountId) {
  53. const auditors = await this.ctx.service.qualityInspectionAudit.getAuditors(inspection.id, times); // 全部参与的审批人
  54. const newAuditors = auditors.filter(x => { return x.is_old === 0 && x.is_rectification === 0; });
  55. const auditorGroups = this.ctx.helper.groupAuditors(newAuditors);
  56. inspection.auditors2 = this.ctx.helper.groupAuditorsUniq(auditorGroups);
  57. inspection.auditors2.unshift([{
  58. aid: inspection.user.id, order: 0, times: inspection.times - 1, audit_order: 0, audit_type: auditType.key.common,
  59. name: inspection.user.name, role: inspection.user.role, company: inspection.user.company,
  60. }]);
  61. } else {
  62. inspection.auditors2 = inspection.userGroups;
  63. }
  64. if (inspection.status === auditConst.status.uncheck || inspection.status === auditConst.status.checkNo) {
  65. inspection.auditorList = await this.ctx.service.qualityInspectionAudit.getAuditors(inspection.id, inspection.times);
  66. }
  67. }
  68. async add(tenderId, userId, code, check_item, check_date) {
  69. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND `code` = ?';
  70. const sqlParam = [this.tableName, tenderId, code];
  71. const codeCount = await this.db.queryOne(sql, sqlParam);
  72. const count = codeCount.count;
  73. if (count > 0) {
  74. throw '编号重复';
  75. }
  76. // 初始化事务
  77. const transaction = await this.db.beginTransaction();
  78. let result = false;
  79. try {
  80. const inspection = {
  81. tid: tenderId,
  82. uid: userId,
  83. status: auditConst.status.uncheck,
  84. times: 1,
  85. code,
  86. check_item,
  87. check_date,
  88. inspector: this.ctx.session.sessionUser.name,
  89. create_time: new Date(),
  90. };
  91. const operate = await transaction.insert(this.tableName, inspection);
  92. if (operate.affectedRows <= 0) {
  93. throw '新建质量巡检数据失败';
  94. }
  95. inspection.id = operate.insertId;
  96. // 先找出标段最近存在的变更令审批人的变更令info
  97. const preChangeInfo = await this.getHaveAuditLastInfo(tenderId);
  98. if (preChangeInfo) {
  99. // 并把之前存在的变更令审批人添加到zh_change_audit
  100. const auditResult = await this.ctx.service.qualityInspectionAudit.copyPreAuditors(transaction, preChangeInfo, inspection);
  101. if (!auditResult) {
  102. throw '复制上一次审批流程失败';
  103. }
  104. }
  105. result = inspection;
  106. await transaction.commit();
  107. } catch (error) {
  108. console.log(error);
  109. // 回滚
  110. await transaction.rollback();
  111. }
  112. return result;
  113. }
  114. async delInspection(id) {
  115. const transaction = await this.db.beginTransaction();
  116. try {
  117. // 删除预付款记录
  118. await transaction.delete(this.tableName, { id });
  119. // 删除附件
  120. const fileInfo = await this.db.select(this.ctx.service.qualityInspectionAtt.tableName, { where: { qiid: id } });
  121. await transaction.delete(this.ctx.service.qualityInspectionAtt.tableName, { qiid: id });
  122. await this.ctx.helper.delFiles(fileInfo);
  123. // 先删除文件
  124. // for (let i = 0; i < fileInfo.length; i++) {
  125. // const file = fileInfo[i];
  126. // if (fs.existsSync(path.resolve(this.app.baseDir, './app', file.filepath))) {
  127. // fs.unlinkSync(path.resolve(this.app.baseDir, './app', file.filepath));
  128. // // fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  129. // }
  130. // await this.ctx.app.fujianOss.delete(file.filepath);
  131. // }
  132. // 删除审批记录
  133. await transaction.delete(this.ctx.service.qualityInspectionAudit.tableName, { qiid: id });
  134. await transaction.commit();
  135. } catch (err) {
  136. await transaction.rollback();
  137. throw err;
  138. }
  139. }
  140. async getHaveAuditLastInfo(tenderId) {
  141. const sql = 'SELECT a.* FROM ?? as a LEFT JOIN ?? as b ON a.`id` = b.`qiid` WHERE a.`tid` = ? ORDER BY a.`create_time` DESC';
  142. const sqlParam = [this.tableName, this.ctx.service.qualityInspectionAudit.tableName, tenderId];
  143. return await this.db.queryOne(sql, sqlParam);
  144. }
  145. async getListByStatus(tid, status, hadlimit = 0, sortBy = '', orderBy = '') {
  146. let sql = '';
  147. let sqlParam = '';
  148. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin) && status === 0) {
  149. sql = 'SELECT a.* FROM ?? As a WHERE a.tid = ?';
  150. sqlParam = [this.tableName, tid];
  151. } else {
  152. switch (status) {
  153. case 0: // 所有
  154. sql =
  155. 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND' +
  156. ' (a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid))' +
  157. ' OR a.status = ?)';
  158. sqlParam = [
  159. this.tableName,
  160. tid,
  161. this.ctx.session.sessionUser.accountId,
  162. auditConst.status.uncheck,
  163. this.ctx.service.qualityInspectionAudit.tableName,
  164. this.ctx.session.sessionUser.accountId,
  165. auditConst.status.checked,
  166. ];
  167. break;
  168. case auditConst.filter.status.pending: // 待处理(你的)
  169. sql = 'SELECT a.* FROM ?? as a WHERE a.tid = ? AND (a.id in(SELECT b.qiid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND (b.status = ? OR b.status = ?)) OR (a.uid = ? AND (a.status = ? OR a.status = ?)))';
  170. sqlParam = [this.tableName, tid, this.ctx.service.qualityInspectionAudit.tableName, tid, this.ctx.session.sessionUser.accountId, auditConst.status.checking, auditConst.status.rectification, this.ctx.session.sessionUser.accountId, auditConst.status.uncheck, auditConst.status.checkNo];
  171. break;
  172. case auditConst.filter.status.uncheck: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  173. sql =
  174. 'SELECT a.* FROM ?? AS a WHERE ' +
  175. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  176. sqlParam = [
  177. this.tableName,
  178. this.ctx.session.sessionUser.accountId,
  179. tid,
  180. auditConst.status.uncheck,
  181. auditConst.status.checkNo,
  182. ];
  183. break;
  184. case auditConst.filter.status.checking: // 进行中(所有的)
  185. sql =
  186. 'SELECT a.* FROM ?? AS a WHERE ' +
  187. '(a.status = ? OR a.status = ?) AND a.tid = ?' +
  188. (this.ctx.session.sessionUser.is_admin ? '' : ' AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid)');
  189. sqlParam = [this.tableName, status, auditConst.status.checkNoPre, tid, this.ctx.service.qualityInspectionAudit.tableName, this.ctx.session.sessionUser.accountId];
  190. break;
  191. case auditConst.filter.status.rectification: // 整改中(所有的)
  192. case auditConst.filter.status.checkStop: // 终止(所有的)
  193. sql =
  194. 'SELECT a.* FROM ?? AS a WHERE ' +
  195. 'a.status = ? AND a.tid = ?' +
  196. (this.ctx.session.sessionUser.is_admin ? '' : ' AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid)');
  197. sqlParam = [this.tableName, status, tid, this.ctx.service.qualityInspectionAudit.tableName, this.ctx.session.sessionUser.accountId];
  198. break;
  199. case auditConst.filter.status.checked: // 已完成(所有的)
  200. sql = 'SELECT a.* FROM ?? as a WHERE a.status = ? AND a.tid = ?';
  201. sqlParam = [this.tableName, status, tid];
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. if (sortBy && orderBy) {
  208. if (sortBy === 'code') {
  209. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  210. } else {
  211. sql += ' ORDER BY a.create_time ' + orderBy;
  212. }
  213. } else {
  214. sql += ' ORDER BY a.create_time DESC';
  215. }
  216. if (hadlimit) {
  217. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  218. const offset = limit * (this.ctx.page - 1);
  219. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  220. sql += ' LIMIT ' + limitString;
  221. }
  222. const list = await this.db.query(sql, sqlParam);
  223. return list;
  224. }
  225. /**
  226. * 获取支付个数
  227. * @param {int} spid - 项目id
  228. * @param {int} status - 状态
  229. * @return {void}
  230. */
  231. async getCountByStatus(tid, status = 0) {
  232. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin) && status === 0) {
  233. const sql5 = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ?';
  234. const sqlParam5 = [this.tableName, tid];
  235. const result5 = await this.db.query(sql5, sqlParam5);
  236. return result5[0].count;
  237. }
  238. switch (status) {
  239. case 0: // 所有
  240. const sql =
  241. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  242. '(a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid)) OR a.status = ?)';
  243. const sqlParam = [
  244. this.tableName,
  245. tid,
  246. this.ctx.session.sessionUser.accountId,
  247. auditConst.status.uncheck,
  248. this.ctx.service.qualityInspectionAudit.tableName,
  249. this.ctx.session.sessionUser.accountId,
  250. auditConst.status.checked,
  251. ];
  252. const result = await this.db.query(sql, sqlParam);
  253. return result[0].count;
  254. case auditConst.filter.status.pending: // 待处理(你的)
  255. const sql6 = 'SELECT count(*) AS count FROM ?? as a WHERE a.tid = ? AND (a.id in(SELECT b.qiid FROM ?? as b WHERE b.tid = ? AND b.aid = ? AND (b.status = ? OR b.status = ?)) OR (a.uid = ? AND (a.status = ? OR a.status = ?)))';
  256. const sqlParam6 = [this.tableName, tid, this.ctx.service.qualityInspectionAudit.tableName, tid, this.ctx.session.sessionUser.accountId, auditConst.status.checking, auditConst.status.rectification, this.ctx.session.sessionUser.accountId, auditConst.status.uncheck, auditConst.status.checkNo];
  257. const result6 = await this.db.query(sql6, sqlParam6);
  258. return result6[0].count;
  259. case auditConst.filter.status.uncheck: // 待上报(所有的)PS:取未上报,退回的变更立项
  260. const sql2 =
  261. 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  262. 'a.uid = ? AND a.tid = ? AND (a.status = ? OR a.status = ?)';
  263. const sqlParam2 = [
  264. this.tableName,
  265. this.ctx.session.sessionUser.accountId,
  266. tid,
  267. auditConst.status.uncheck,
  268. auditConst.status.checkNo,
  269. ];
  270. const result2 = await this.db.query(sql2, sqlParam2);
  271. return result2[0].count;
  272. case auditConst.filter.status.checking: // 进行中(所有的)
  273. const sql7 =
  274. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  275. '(a.status = ? OR a.status = ?) AND a.tid = ?' +
  276. (this.ctx.session.sessionUser.is_admin ? '' : ' AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid)');
  277. const sqlParam7 = [this.tableName, status, auditConst.status.checkNoPre, tid, this.ctx.service.qualityInspectionAudit.tableName, this.ctx.session.sessionUser.accountId];
  278. const result7 = await this.db.query(sql7, sqlParam7);
  279. return result7[0].count;
  280. case auditConst.filter.status.rectification: // 整改中(所有的)
  281. case auditConst.filter.status.checkStop: // 终止(所有的)
  282. const sql3 =
  283. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  284. 'a.status = ? AND a.tid = ?' +
  285. (this.ctx.session.sessionUser.is_admin ? '' : ' AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid)');
  286. const sqlParam3 = [this.tableName, status, tid, this.ctx.service.qualityInspectionAudit.tableName, this.ctx.session.sessionUser.accountId];
  287. const result3 = await this.db.query(sql3, sqlParam3);
  288. return result3[0].count;
  289. case auditConst.filter.status.checked: // 已完成(所有的)
  290. const sql4 = 'SELECT count(*) AS count FROM ?? as a WHERE a.status = ? AND a.tid = ?';
  291. const sqlParam4 = [this.tableName, status, tid];
  292. const result4 = await this.db.query(sql4, sqlParam4);
  293. return result4[0].count;
  294. default:
  295. break;
  296. }
  297. }
  298. }
  299. return QualityInspection;
  300. };