quality_inspection.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 addByWap(tid, data) {
  115. const sql = 'SELECT COUNT(*) as count FROM ?? WHERE `tid` = ? AND `code` = ?';
  116. const sqlParam = [this.tableName, tid, data.code];
  117. const codeCount = await this.db.queryOne(sql, sqlParam);
  118. const count = codeCount.count;
  119. if (count > 0) {
  120. throw '巡检编号已存在,请修改巡检编号';
  121. }
  122. // 初始化事务
  123. const transaction = await this.db.beginTransaction();
  124. let result = false;
  125. try {
  126. const inspection = {
  127. tid,
  128. uid: this.ctx.session.sessionUser.accountId,
  129. status: auditConst.status.uncheck,
  130. times: 1,
  131. code: data.code,
  132. check_item: data.check_item,
  133. check_date: data.check_date || null,
  134. check_situation: data.check_situation || '',
  135. action: data.action || '',
  136. inspector: data.inspector || this.ctx.session.sessionUser.name,
  137. create_time: new Date(),
  138. };
  139. const operate = await transaction.insert(this.tableName, inspection);
  140. if (operate.affectedRows <= 0) {
  141. throw '新建质量巡检数据失败';
  142. }
  143. inspection.id = operate.insertId;
  144. // 先找出标段最近存在的变更令审批人的变更令info
  145. const preChangeInfo = await this.getHaveAuditLastInfo(tid);
  146. if (preChangeInfo) {
  147. // 并把之前存在的变更令审批人添加到zh_change_audit
  148. const auditResult = await this.ctx.service.qualityInspectionAudit.copyPreAuditors(transaction, preChangeInfo, inspection);
  149. if (!auditResult) {
  150. throw '复制上一次审批流程失败';
  151. }
  152. }
  153. result = inspection;
  154. await transaction.commit();
  155. } catch (error) {
  156. console.log(error);
  157. // 回滚
  158. await transaction.rollback();
  159. }
  160. return result;
  161. }
  162. async delInspection(id) {
  163. const transaction = await this.db.beginTransaction();
  164. try {
  165. // 删除预付款记录
  166. await transaction.delete(this.tableName, { id });
  167. // 删除附件
  168. const fileInfo = await this.db.select(this.ctx.service.qualityInspectionAtt.tableName, { where: { qiid: id } });
  169. await transaction.delete(this.ctx.service.qualityInspectionAtt.tableName, { qiid: id });
  170. await this.ctx.helper.delFiles(fileInfo);
  171. // 先删除文件
  172. // for (let i = 0; i < fileInfo.length; i++) {
  173. // const file = fileInfo[i];
  174. // if (fs.existsSync(path.resolve(this.app.baseDir, './app', file.filepath))) {
  175. // fs.unlinkSync(path.resolve(this.app.baseDir, './app', file.filepath));
  176. // // fs.unlinkSync(path.resolve(this.app.baseDir, zipPath));
  177. // }
  178. // await this.ctx.app.fujianOss.delete(file.filepath);
  179. // }
  180. // 删除审批记录
  181. await transaction.delete(this.ctx.service.qualityInspectionAudit.tableName, { qiid: id });
  182. await transaction.commit();
  183. } catch (err) {
  184. await transaction.rollback();
  185. throw err;
  186. }
  187. }
  188. async getHaveAuditLastInfo(tenderId) {
  189. 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';
  190. const sqlParam = [this.tableName, this.ctx.service.qualityInspectionAudit.tableName, tenderId];
  191. return await this.db.queryOne(sql, sqlParam);
  192. }
  193. async getListByStatus(tid, status, hadlimit = 0, sortBy = '', orderBy = '') {
  194. let sql = '';
  195. let sqlParam = '';
  196. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all) && status === 0) {
  197. sql = 'SELECT a.* FROM ?? As a WHERE a.tid = ?';
  198. sqlParam = [this.tableName, tid];
  199. } else {
  200. switch (status) {
  201. case 0: // 所有
  202. sql =
  203. 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND' +
  204. ' (a.uid = ? OR (a.status != ? AND a.id IN (SELECT b.qiid FROM ?? AS b WHERE b.aid = ? GROUP BY b.qiid))' +
  205. ' OR a.status = ?)';
  206. sqlParam = [
  207. this.tableName,
  208. tid,
  209. this.ctx.session.sessionUser.accountId,
  210. auditConst.status.uncheck,
  211. this.ctx.service.qualityInspectionAudit.tableName,
  212. this.ctx.session.sessionUser.accountId,
  213. auditConst.status.checked,
  214. ];
  215. break;
  216. case auditConst.filter.status.pending: // 待处理(你的)
  217. 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 = ?)))';
  218. 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];
  219. break;
  220. case auditConst.filter.status.uncheck: // 待上报(所有的)PS:取未上报,退回,修订的变更令
  221. sql =
  222. 'SELECT a.* FROM ?? AS a WHERE ' +
  223. 'a.tid = ? AND (a.status = ? OR a.status = ?)' +
  224. (this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all ? '' : ' AND a.uid = ' + this.ctx.session.sessionUser.accountId);
  225. sqlParam = [
  226. this.tableName,
  227. tid,
  228. auditConst.status.uncheck,
  229. auditConst.status.checkNo,
  230. ];
  231. break;
  232. case auditConst.filter.status.checking: // 进行中(所有的)
  233. sql =
  234. 'SELECT a.* FROM ?? AS a WHERE ' +
  235. '(a.status = ? OR a.status = ?) AND a.tid = ?' +
  236. (this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all ? '' : ' AND (a.uid = ' + this.ctx.session.sessionUser.accountId + ' OR a.id IN (SELECT b.qiid FROM ' + this.ctx.service.qualityInspectionAudit.tableName + ' AS b WHERE b.aid = ' + this.ctx.session.sessionUser.accountId + ' GROUP BY b.qiid))');
  237. sqlParam = [this.tableName, status, auditConst.status.checkNoPre, tid];
  238. break;
  239. case auditConst.filter.status.rectification: // 整改中(所有的)
  240. case auditConst.filter.status.checkStop: // 终止(所有的)
  241. sql =
  242. 'SELECT a.* FROM ?? AS a WHERE ' +
  243. 'a.status = ? AND a.tid = ?' +
  244. (this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all ? '' : ' AND (a.uid = ' + this.ctx.session.sessionUser.accountId + ' OR a.id IN (SELECT b.qiid FROM ' + this.ctx.service.qualityInspectionAudit.tableName + ' AS b WHERE b.aid = ' + this.ctx.session.sessionUser.accountId + ' GROUP BY b.qiid))');
  245. sqlParam = [this.tableName, status, tid];
  246. break;
  247. case auditConst.filter.status.checked: // 已完成(所有的)
  248. sql = 'SELECT a.* FROM ?? as a WHERE a.status = ? AND a.tid = ?';
  249. sqlParam = [this.tableName, status, tid];
  250. break;
  251. default:
  252. break;
  253. }
  254. }
  255. if (sortBy && orderBy) {
  256. if (sortBy === 'code') {
  257. sql += ' ORDER BY CHAR_LENGTH(a.code) ' + orderBy + ',convert(a.code using gbk) ' + orderBy;
  258. } else {
  259. sql += ' ORDER BY a.create_time ' + orderBy;
  260. }
  261. } else {
  262. sql += ' ORDER BY a.create_time DESC';
  263. }
  264. if (hadlimit) {
  265. const limit = this.ctx.pageSize ? this.ctx.pageSize : this.app.config.pageSize;
  266. const offset = limit * (this.ctx.page - 1);
  267. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  268. sql += ' LIMIT ' + limitString;
  269. }
  270. const list = await this.db.query(sql, sqlParam);
  271. return list;
  272. }
  273. /**
  274. * 获取支付个数
  275. * @param {int} spid - 项目id
  276. * @param {int} status - 状态
  277. * @return {void}
  278. */
  279. async getCountByStatus(tid, status = 0) {
  280. if ((this.ctx.tender.isTourist || this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all) && status === 0) {
  281. const sql5 = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ?';
  282. const sqlParam5 = [this.tableName, tid];
  283. const result5 = await this.db.query(sql5, sqlParam5);
  284. return result5[0].count;
  285. }
  286. switch (status) {
  287. case 0: // 所有
  288. const sql =
  289. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  290. '(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 = ?)';
  291. const sqlParam = [
  292. this.tableName,
  293. tid,
  294. this.ctx.session.sessionUser.accountId,
  295. auditConst.status.uncheck,
  296. this.ctx.service.qualityInspectionAudit.tableName,
  297. this.ctx.session.sessionUser.accountId,
  298. auditConst.status.checked,
  299. ];
  300. const result = await this.db.query(sql, sqlParam);
  301. return result[0].count;
  302. case auditConst.filter.status.pending: // 待处理(你的)
  303. 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 = ?)))';
  304. 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];
  305. const result6 = await this.db.query(sql6, sqlParam6);
  306. return result6[0].count;
  307. case auditConst.filter.status.uncheck: // 待上报(所有的)PS:取未上报,退回的变更立项
  308. const sql2 =
  309. 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND (a.status = ? OR a.status = ?)' +
  310. (this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all ? '' : ' AND a.uid = ' + this.ctx.session.sessionUser.accountId);
  311. const sqlParam2 = [
  312. this.tableName,
  313. tid,
  314. auditConst.status.uncheck,
  315. auditConst.status.checkNo,
  316. ];
  317. const result2 = await this.db.query(sql2, sqlParam2);
  318. return result2[0].count;
  319. case auditConst.filter.status.checking: // 进行中(所有的)
  320. const sql7 =
  321. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  322. '(a.status = ? OR a.status = ?) AND a.tid = ?' +
  323. (this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all ? '' : ' AND (a.uid = ' + this.ctx.session.sessionUser.accountId + ' OR a.id IN (SELECT b.qiid FROM ' + this.ctx.service.qualityInspectionAudit.tableName + ' AS b WHERE b.aid = ' + this.ctx.session.sessionUser.accountId + ' GROUP BY b.qiid))');
  324. const sqlParam7 = [this.tableName, status, auditConst.status.checkNoPre, tid];
  325. const result7 = await this.db.query(sql7, sqlParam7);
  326. return result7[0].count;
  327. case auditConst.filter.status.rectification: // 整改中(所有的)
  328. case auditConst.filter.status.checkStop: // 终止(所有的)
  329. const sql3 =
  330. 'SELECT count(*) AS count FROM ?? as a WHERE ' +
  331. 'a.status = ? AND a.tid = ?' +
  332. (this.ctx.session.sessionUser.is_admin || this.ctx.permission.inspection.view_all ? '' : ' AND (a.uid = ' + this.ctx.session.sessionUser.accountId + ' OR a.id IN (SELECT b.qiid FROM ' + this.ctx.service.qualityInspectionAudit.tableName + ' AS b WHERE b.aid = ' + this.ctx.session.sessionUser.accountId + ' GROUP BY b.qiid))');
  333. const sqlParam3 = [this.tableName, status, tid];
  334. const result3 = await this.db.query(sql3, sqlParam3);
  335. return result3[0].count;
  336. case auditConst.filter.status.checked: // 已完成(所有的)
  337. const sql4 = 'SELECT count(*) AS count FROM ?? as a WHERE a.status = ? AND a.tid = ?';
  338. const sqlParam4 = [this.tableName, status, tid];
  339. const result4 = await this.db.query(sql4, sqlParam4);
  340. return result4[0].count;
  341. default:
  342. break;
  343. }
  344. }
  345. }
  346. return QualityInspection;
  347. };