quality_controller.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. 'use strict';
  2. /**
  3. * 标段管理控制器
  4. *
  5. * @author Mai
  6. * @date 2025/7/17
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const qualityInspectionConst = require('../const/quality_inspection');
  11. const auditType = require('../const/audit').auditType;
  12. const shenpiConst = require('../const/shenpi');
  13. const codeRuleConst = require('../const/code_rule');
  14. const contractConst = require('../const/contract');
  15. const moment = require('moment');
  16. const sendToWormhole = require('stream-wormhole');
  17. const fs = require('fs');
  18. const path = require('path');
  19. const PermissionCheck = require('../const/account_permission').PermissionCheck;
  20. module.exports = app => {
  21. class QualityController extends app.BaseController {
  22. constructor(ctx) {
  23. super(ctx);
  24. ctx.showProject = true;
  25. // ctx.showTitle = true;
  26. }
  27. loadMenu(ctx) {
  28. super.loadMenu(ctx);
  29. // 虚拟menu,以保证标题显示正确
  30. ctx.menu = {
  31. name: '质量管理',
  32. display: false,
  33. caption: '质量管理',
  34. controller: 'quality',
  35. };
  36. }
  37. async tender(ctx) {
  38. try {
  39. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  40. const renderData = {
  41. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  42. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.tender),
  43. };
  44. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  45. renderData.accountList = accountList;
  46. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  47. const accountGroupList = unitList.map(item => {
  48. const groupList = accountList.filter(item1 => item1.company === item.name);
  49. return { groupName: item.name, groupList };
  50. }).filter(x => { return x.groupList.length > 0; });
  51. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  52. // renderData.accountGroup = unitList.map(item => {
  53. // const groupList = accountList.filter(item1 => item1.company === item.name);
  54. // return { groupName: item.name, groupList };
  55. // });
  56. renderData.accountGroup = accountGroupList;
  57. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  58. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'quality', ctx.session.sessionUser.is_admin ? 'all' : '');
  59. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  60. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  61. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('quality');
  62. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('quality');
  63. await this.layout('quality/tender.ejs', renderData, 'quality/tender_modal.ejs');
  64. } catch (err) {
  65. ctx.log(err);
  66. ctx.postError(err, '无法查看质量管理数据');
  67. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  68. }
  69. }
  70. async inspectionTender(ctx) {
  71. try {
  72. if (!ctx.subProject.page_show.qualityInspection) throw '该功能已关闭';
  73. const renderData = {
  74. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  75. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.tender),
  76. };
  77. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  78. renderData.accountList = accountList;
  79. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  80. const accountGroupList = unitList.map(item => {
  81. const groupList = accountList.filter(item1 => item1.company === item.name);
  82. return { groupName: item.name, groupList };
  83. }).filter(x => { return x.groupList.length > 0; });
  84. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  85. // renderData.accountGroup = unitList.map(item => {
  86. // const groupList = accountList.filter(item1 => item1.company === item.name);
  87. // return { groupName: item.name, groupList };
  88. // });
  89. renderData.accountGroup = accountGroupList;
  90. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  91. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'inspection', ctx.session.sessionUser.is_admin ? 'all' : '');
  92. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  93. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  94. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('inspection');
  95. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('inspection');
  96. await this.layout('quality/tender.ejs', renderData, 'quality/tender_modal.ejs');
  97. } catch (err) {
  98. ctx.log(err);
  99. ctx.postError(err, '无法查看质量巡检数据');
  100. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  101. }
  102. }
  103. async member(ctx) {
  104. try {
  105. const data = JSON.parse(ctx.request.body.data);
  106. const result = await ctx.service.tenderPermission.getPartsPermission(data.tid, data.parts);
  107. ctx.body = { err: 0, msg: '', data: result };
  108. } catch (err) {
  109. ctx.log(err);
  110. ctx.ajaxErrorBody(err, '查询标段权限错误');
  111. }
  112. }
  113. async memberSave(ctx) {
  114. try {
  115. const data = JSON.parse(ctx.request.body.data);
  116. await ctx.service.tenderPermission.savePermission(data.tid, data.member, data.permissionBlock);
  117. ctx.body = { err: 0, msg: '', data: '' };
  118. } catch (err) {
  119. ctx.log(err);
  120. ctx.ajaxErrorBody(err, '保存标段权限错误');
  121. }
  122. }
  123. async auditSave(ctx) {
  124. try {
  125. if (ctx.session.sessionUser.is_admin === 0) throw '没有设置权限';
  126. const tid = parseInt(ctx.params.tid);
  127. const responseData = {
  128. err: 0, msg: '', data: null,
  129. };
  130. const tenderInfo = await ctx.service.tender.getDataById(tid);
  131. if (!tenderInfo) throw '标段不存在';
  132. const data = JSON.parse(ctx.request.body.data);
  133. if (!data.type) {
  134. throw '提交数据错误';
  135. }
  136. let uids;
  137. let auditList = [];
  138. const tenderPermissionKeys = ['quality', 'inspection', 'safe_inspection', 'safe_payment', 'schedule'];
  139. const tPsKeys = {
  140. quality: ['quality', 'inspection'],
  141. safe: ['safe_inspection', 'safe_payment'],
  142. };
  143. switch (data.type) {
  144. case 'add-audit':
  145. if (!data.key || (!tenderPermissionKeys.includes(data.key) && !Object.keys(tPsKeys).includes(data.key))) throw '参数有误';
  146. // // 判断用户是单个还是数组
  147. uids = data.id instanceof Array ? data.id : [data.id];
  148. // // 判断该用户的组是否已加入到表中,已加入则提示无需添加
  149. const insertKeys = data.together ? tPsKeys[data.key] : [data.key];
  150. auditList = await ctx.service.tenderPermission.getPartsPermission(tenderInfo.id, insertKeys);
  151. const addAidList = ctx.helper._.difference(uids, ctx.helper._.map(auditList, 'uid'));
  152. if (addAidList.length === 0) {
  153. throw '用户已存在成员管理中,无需重复添加';
  154. }
  155. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { id: addAidList } });
  156. const insert_members = [];
  157. for (const account of accountList) {
  158. const insert_member = { uid: account.id };
  159. for (const key of insertKeys) {
  160. insert_member[key] = ['1'];
  161. }
  162. insert_members.push(insert_member);
  163. }
  164. await ctx.service.tenderPermission.saveOnePermission(tenderInfo.id, ctx.helper._.map(accountList, 'id'), insert_members, insertKeys);
  165. responseData.data = await ctx.service.tenderPermission.getPartsPermission(tenderInfo.id, insertKeys);
  166. break;
  167. case 'del-audit':
  168. if (!data.key || (!tenderPermissionKeys.includes(data.key) && !Object.keys(tPsKeys).includes(data.key))) throw '参数有误';
  169. uids = data.id instanceof Array ? data.id : [data.id];
  170. if (uids.length === 0) throw '没有选择要移除的用户';
  171. const deleteKeys = data.together ? tPsKeys[data.key] : [data.key];
  172. auditList = await ctx.service.tenderPermission.getPartsPermission(tenderInfo.id, deleteKeys);
  173. // 判断uids和auditList中是否有相同的uid
  174. const commonUids = ctx.helper._.intersection(uids, ctx.helper._.map(auditList, 'uid'));
  175. if (commonUids.length === 0) {
  176. throw '该用户已不在成员管理中,移除失败';
  177. }
  178. const del_members = [];
  179. for (const uid of uids) {
  180. const del_member = { uid };
  181. for (const key of deleteKeys) {
  182. del_member[key] = [];
  183. }
  184. del_members.push(del_member);
  185. }
  186. await ctx.service.tenderPermission.saveOnePermission(tenderInfo.id, uids, del_members, deleteKeys);
  187. responseData.data = await ctx.service.tenderPermission.getPartsPermission(tenderInfo.id, deleteKeys);
  188. break;
  189. case 'save-permission':
  190. if (!data.key || !tenderPermissionKeys.includes(data.key)) throw '参数有误';
  191. uids = data.uid instanceof Array ? data.uid : [data.uid];
  192. await ctx.service.tenderPermission.saveOnePermission(tenderInfo.id, uids, data.members, [data.key]);
  193. break;
  194. case 'list':
  195. if (!data.key || !tenderPermissionKeys.includes(data.key)) throw '参数有误';
  196. responseData.data = await ctx.service.tenderPermission.getPartsPermission(tenderInfo.id, [data.key]);
  197. break;
  198. default: throw '参数有误';
  199. }
  200. ctx.body = responseData;
  201. } catch (err) {
  202. this.log(err);
  203. ctx.body = { err: 1, msg: err.toString(), data: null };
  204. }
  205. }
  206. async info(ctx) {
  207. try {
  208. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  209. const renderData = {
  210. thirdParty: {
  211. gxby: ctx.session.sessionProject.gxby_status,
  212. dagl: ctx.session.sessionProject.dagl_status,
  213. },
  214. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.info),
  215. };
  216. await this.layout('quality/info.ejs', renderData, 'quality/info_modal.ejs');
  217. } catch (err) {
  218. ctx.log(err);
  219. ctx.postError(err, '无法查看质量管理数据');
  220. ctx.redirect(`/sp/${ctx.subProject.id}/quality`);
  221. }
  222. }
  223. async flaw(ctx) {
  224. try {
  225. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  226. const renderData = {
  227. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.flaw),
  228. };
  229. await this.layout('quality/flaw.ejs', renderData, 'quality/flaw_modal.ejs');
  230. } catch (err) {
  231. ctx.log(err);
  232. ctx.postError(err, '无法查看质量管理数据');
  233. ctx.redirect(`/sp/${ctx.subProject.id}/quality`);
  234. }
  235. }
  236. async lab(ctx) {
  237. try {
  238. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  239. const renderData = {
  240. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.lab),
  241. };
  242. await this.layout('quality/lab.ejs', renderData, 'quality/lab_modal.ejs');
  243. } catch (err) {
  244. ctx.log(err);
  245. ctx.postError(err, '无法查看质量管理数据');
  246. ctx.redirect(`/sp/${ctx.subProject.id}/quality`);
  247. }
  248. }
  249. async _loadXmjData(spec) {
  250. const xmj = await this.ctx.service.ledger.getAllDataByCondition({
  251. where: { tender_id: this.ctx.tender.id },
  252. columns: ['id', 'tender_id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name'],
  253. });
  254. // const quality = spec && spec.loadStatus ? await this.ctx.service.quality.getAllDataByCondition({
  255. // where: { tid: this.ctx.tender.id, rela_type: 'xmj' },
  256. // columns: ['rela_id', 'gxby_status', 'gxby_date', 'dagl_status'],
  257. // }) : [];
  258. // this.ctx.helper.assignRelaData(xmj, [
  259. // { data: quality, fields: ['gxby_status', 'gxby_date', 'dagl_status'], prefix: '', relaId: 'rela_id' },
  260. // ]);
  261. return xmj; // .filter(x => { return !x.b_code; });
  262. }
  263. async _loadPosData(condition, spec) {
  264. const pos = await this.ctx.service.pos.getAllDataByCondition({
  265. where: condition,
  266. columns: ['id', 'tid', 'lid', 'name'],
  267. orders: [['porder', 'ASC']],
  268. });
  269. // const quality = spec && spec.loadStatus ? await this.ctx.service.quality.getAllDataByCondition({
  270. // where: { tid: this.ctx.tender.id, rela_type: 'pos' },
  271. // columns: ['rela_id', 'gxby_status', 'gxby_date', 'dagl_status'],
  272. // }) : [];
  273. // this.ctx.helper.assignRelaData(pos, [
  274. // { data: quality, fields: ['gxby_status', 'gxby_date', 'dagl_status'], prefix: '', relaId: 'rela_id' },
  275. // ]);
  276. return pos;
  277. }
  278. async _loadDetailData(rela_type, rela_id, rela_name) {
  279. const result = await this.ctx.service.quality.getDataByCondition({ tid: this.ctx.tender.id, rela_type, rela_id, rela_name });
  280. if (!result) return result;
  281. await this.ctx.service.quality.loadQualityDetail(result);
  282. await this.ctx.service.quality.checkQualityStatusAndSave(result);
  283. return result;
  284. }
  285. async _loadData(type, data, result) {
  286. if (result[type]) return result[type];
  287. switch (type) {
  288. case 'xmj':
  289. return this._loadXmjData(data.spec);
  290. case 'pos':
  291. return this._loadPosData({ tid: this.ctx.tender.id }, data.spec);
  292. case 'detail':
  293. if (!data.rela_id && !data.rela_type) throw '缺少参数';
  294. return this._loadDetailData(data.rela_type, data.rela_id, data.rela_name);
  295. case 'quality':
  296. return await this.ctx.service.quality.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  297. case 'lab':
  298. return await this.ctx.service.qualityLab.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  299. default: throw '未知数据类型';
  300. }
  301. }
  302. async _load(data) {
  303. if (!data.filter) throw '参数错误';
  304. const filter = data.filter.split(';');
  305. const result = {};
  306. for (const f of filter) {
  307. result[f] = await this._loadData(f, data, result);
  308. }
  309. return result;
  310. }
  311. async load(ctx) {
  312. try {
  313. const data = JSON.parse(ctx.request.body.data);
  314. const result = await this._load(data);
  315. ctx.body = { err: 0, msg: '', data: result };
  316. } catch (err) {
  317. ctx.log(err);
  318. ctx.ajaxErrorBody(err, '获取数据错误');
  319. }
  320. }
  321. async _saveKaigong(ctx, data) {
  322. if (data.add) {
  323. if (!ctx.permission.quality.add) throw '您无权新增开工';
  324. await ctx.service.quality.kaigong(data, data.add.name, data.add.date);
  325. } else if (data.update) {
  326. await ctx.service.quality.kaigong(data, data.update.name, data.update.date);
  327. } else if (data.del) {
  328. await ctx.service.quality.delKaigong(data);
  329. }
  330. }
  331. async _saveGongxu(ctx, data) {
  332. if (data.add) {
  333. if (!ctx.permission.quality.add) throw '您无权新增工序';
  334. await ctx.service.qualityGongxu.add(data, data.add.name);
  335. } else if (data.update) {
  336. await ctx.service.qualityGongxu.update(data.update.id, data.update.name);
  337. } else if (data.del) {
  338. await ctx.service.qualityGongxu.del(data.del);
  339. }
  340. }
  341. async _saveYinbi(ctx, data) {
  342. if (data.add) {
  343. if (!ctx.permission.quality.add) throw '您无权新增隐蔽工程';
  344. await ctx.service.qualityYinbi.add(data, data.add);
  345. } else if (data.update) {
  346. await ctx.service.qualityYinbi.update(data.update.id, data.update);
  347. } else if (data.del) {
  348. await ctx.service.qualityYinbi.del(data.del);
  349. }
  350. }
  351. async save(ctx) {
  352. try {
  353. const data = JSON.parse(ctx.request.body.data);
  354. if (!data.quality_id && (!data.rela_type && !data.rela_id)) throw '参数错误';
  355. switch (ctx.params.type) {
  356. case 'kaigong':
  357. await this._saveKaigong(ctx, data);
  358. break;
  359. case 'gongxu':
  360. await this._saveGongxu(ctx, data);
  361. break;
  362. case 'yinbi':
  363. await this._saveYinbi(ctx, data);
  364. break;
  365. default: throw '请求错误';
  366. }
  367. const quality = await this._loadDetailData(data.rela_type, data.rela_id, data.rela_name);
  368. ctx.body = { err: 0, msg: '', data: quality };
  369. } catch (err) {
  370. ctx.log(err);
  371. ctx.ajaxErrorBody(err, '保存数据错误');
  372. }
  373. }
  374. async checkCanUpload(ctx) {
  375. if (!ctx.permission.quality.upload) {
  376. throw '您无权上传、删除文件';
  377. }
  378. }
  379. async uploadFile(ctx) {
  380. let stream;
  381. try {
  382. await this.checkCanUpload(ctx);
  383. const parts = ctx.multipart({ autoFields: true });
  384. let index = 0;
  385. const create_time = Date.parse(new Date()) / 1000;
  386. let stream = await parts();
  387. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  388. const quality = await ctx.service.quality.getDataById(parts.field.quality_id);
  389. if (!quality) throw '工程未开工';
  390. const blockType = parts.field.block_type;
  391. const blockId = parts.field.block_id || '';
  392. const specType = parts.field.spec_type || '';
  393. if (!blockType) throw '参数错误';
  394. const uploadfiles = [];
  395. while (stream !== undefined) {
  396. if (!stream.filename) throw '未发现上传文件!';
  397. const fileInfo = path.parse(stream.filename);
  398. const filepath = `${ctx.session.sessionProject.id}/${ctx.tender.id}/quality/${blockType}/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  399. // 保存文件
  400. await ctx.fujianOss.put(ctx.fujianOssPath + filepath, stream);
  401. await sendToWormhole(stream);
  402. // 插入到stage_pay对应的附件列表中
  403. uploadfiles.push({
  404. filename: fileInfo.name,
  405. fileext: fileInfo.ext,
  406. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  407. filepath,
  408. });
  409. ++index;
  410. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  411. stream = await parts();
  412. } else {
  413. stream = undefined;
  414. }
  415. }
  416. const result = await ctx.service.qualityFile.addFiles(user, quality, uploadfiles, blockType, blockId, specType);
  417. ctx.body = { err: 0, msg: '', data: result };
  418. } catch (error) {
  419. ctx.log(error);
  420. // 失败需要消耗掉stream 以防卡死
  421. if (stream) await sendToWormhole(stream);
  422. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  423. }
  424. }
  425. async delFile(ctx) {
  426. try {
  427. const data = JSON.parse(ctx.request.body.data);
  428. if (!data.del) throw '缺少参数';
  429. const result = await ctx.service.qualityFile.delFiles(data.del);
  430. ctx.body = { err: 0, msg: '', data: result };
  431. } catch (error) {
  432. this.log(error);
  433. ctx.ajaxErrorBody(error, '删除附件失败');
  434. }
  435. }
  436. async saveFile(ctx) {
  437. try {
  438. const data = JSON.parse(ctx.request.body.data);
  439. if (!data.id) throw '缺少参数';
  440. const result = await ctx.service.file.qualityFile(data.id, data.filename);
  441. ctx.body = { err: 0, msg: '', data: result };
  442. } catch (error) {
  443. this.log(error);
  444. ctx.ajaxErrorBody(error, '编辑附件失败');
  445. }
  446. }
  447. async rule(ctx) {
  448. try {
  449. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  450. const ruleGroups = await ctx.service.qualityRule.getRuleGroups(ctx.subProject.id);
  451. const tenderList = await ctx.service.tender.getAllDataByCondition({ where: { spid: ctx.subProject.id } });
  452. const renderData = {
  453. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.rule),
  454. ruleGroups,
  455. thirdParty: {
  456. gxby: ctx.session.sessionProject.gxby_status,
  457. dagl: ctx.session.sessionProject.dagl_status,
  458. },
  459. tenderList,
  460. };
  461. await this.layout('quality/rule.ejs', renderData, 'quality/rule_modal.ejs');
  462. } catch (err) {
  463. ctx.log(err);
  464. ctx.postError(err, '无法查看质量管理数据');
  465. ctx.redirect(`/sp/${ctx.subProject.id}/quality`);
  466. }
  467. }
  468. async ruleSave(ctx) {
  469. try {
  470. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  471. const data = JSON.parse(ctx.request.body.data);
  472. if (!data.group && !data.rule && !data.quality) throw '参数错误';
  473. if (data.group) {
  474. const result = await ctx.service.qualityRule.saveGroup(data.group);
  475. ctx.body = { err: 0, msg: '', data: result };
  476. } else if (data.rule) {
  477. const result = await ctx.service.qualityRule.saveRule(data.rule);
  478. ctx.body = { err: 0, msg: '', data: result };
  479. } else if (data.quality) {
  480. const result = await ctx.service.quality.saveQualityManage(this.ctx.tender.id, data.quality);
  481. ctx.body = { err: 0, msg: '', data: result };
  482. }
  483. } catch (err) {
  484. ctx.log(err);
  485. ctx.ajaxErrorBody(err, '保存状态推送规则错误');
  486. }
  487. }
  488. async _pushIncStatus(select) {
  489. // todo 向其他系统推送
  490. return await this.ctx.service.quality.pushIncStatus(select);
  491. }
  492. async _pushAllStatus(select) {
  493. // todo 向其他系统推送
  494. return await this.ctx.service.quality.pushAllStatus(select);
  495. }
  496. async pushStatus(ctx) {
  497. try {
  498. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  499. const data = JSON.parse(ctx.request.body.data);
  500. if (!data.push_type) throw '参数错误';
  501. let count;
  502. switch (data.push_type) {
  503. case 'inc':
  504. count = await this._pushIncStatus(data.select);
  505. break;
  506. case 'all':
  507. count = await this._pushAllStatus();
  508. break;
  509. default: throw '参数错误';
  510. }
  511. ctx.body = { err: 0, msg: '', data: count };
  512. } catch (err) {
  513. ctx.log(err);
  514. ctx.ajaxErrorBody(err, '推送失败');
  515. }
  516. }
  517. async _forceRefreshStatus() {
  518. const qualities = await this.ctx.service.quality.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  519. for (const q of qualities) {
  520. await this.ctx.service.quality.loadQualityDetail(q);
  521. await this.ctx.service.quality.checkQualityStatusAndSave(q);
  522. }
  523. }
  524. async force(ctx) {
  525. try {
  526. if (!ctx.subProject.page_show.quality) throw '该功能已关闭';
  527. const data = JSON.parse(ctx.request.body.data);
  528. if (!data.type) throw '参数错误';
  529. switch (data.type) {
  530. case 'all':
  531. await this._forceRefreshStatus();
  532. break;
  533. default: throw '参数错误';
  534. }
  535. const result = data.load ? this._load(data.load) : {};
  536. ctx.body = { err: 0, msg: '', data: result };
  537. } catch (err) {
  538. console.log(err);
  539. ctx.log(err);
  540. ctx.ajaxErrorBody(err, '操作失败');
  541. }
  542. }
  543. /**
  544. * 变更管理 页面 (Get)
  545. *
  546. * @param {Object} ctx - egg全局变量
  547. * @return {void}
  548. */
  549. async inspection(ctx) {
  550. try {
  551. if (!ctx.subProject.page_show.qualityInspection) throw '该功能已关闭';
  552. const status = parseInt(ctx.query.status) || 0;
  553. await this._filterInspection(ctx, status);
  554. } catch (err) {
  555. ctx.log(err);
  556. ctx.postError(err, '无法查看质量管理数据');
  557. ctx.redirect(`/sp/${ctx.subProject.id}/quality/tender`);
  558. }
  559. }
  560. // 质量巡检单功能
  561. async _filterInspection(ctx, status = 0) {
  562. try {
  563. ctx.session.sessionUser.tenderId = ctx.tender.id;
  564. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  565. const orders = ctx.query.order ? ctx.query.order : 0;
  566. const filter = JSON.parse(JSON.stringify(qualityInspectionConst.filter));
  567. filter.count = [];
  568. filter.count[filter.status.pending] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.pending);
  569. filter.count[filter.status.uncheck] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.uncheck);
  570. filter.count[filter.status.checking] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.checking);
  571. filter.count[filter.status.reply] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.reply);
  572. filter.count[filter.status.rectification] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.rectification);
  573. filter.count[filter.status.checked] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.checked);
  574. filter.count[filter.status.checkStop] = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, filter.status.checkStop);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  575. const inspectionList = await ctx.service.qualityInspection.getListByStatus(ctx.tender.id, status, 1, sorts, orders);
  576. const total = await ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, status);
  577. const allAttList = inspectionList.length > 0 ? await ctx.service.qualityInspectionAtt.getAllAtt(ctx.tender.id, ctx.helper._.map(inspectionList, 'id')) : [];
  578. for (const c of inspectionList) {
  579. c.attList = ctx.helper._.filter(allAttList, { qiid: c.id });
  580. c.curAuditors = await ctx.service.qualityInspectionAudit.getAuditorsByStatus(c.id, c.status, c.times);
  581. if (c.status === auditConst.inspection.status.checkNoPre) {
  582. c.curAuditors2 = await ctx.service.qualityInspectionAudit.getAuditorsByStatus(c.id, auditConst.inspection.status.checking, c.times);
  583. }
  584. }
  585. // 分页相关
  586. const page = ctx.page;
  587. const pageSize = ctx.pageSize;
  588. const pageInfo = {
  589. page,
  590. pageSizeSelect: 1,
  591. pageSize,
  592. total_num: total,
  593. total: Math.ceil(total / pageSize),
  594. queryData: JSON.stringify(ctx.urlInfo.query),
  595. };
  596. let codeRule = [];
  597. let c_connector = '1';
  598. let c_rule_first = 1;
  599. const rule_type = 'inspection';
  600. const tender = await this.service.tender.getDataById(ctx.tender.id);
  601. if (tender.c_code_rules) {
  602. const c_code_rules = JSON.parse(tender.c_code_rules);
  603. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  604. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  605. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  606. }
  607. for (const rule of codeRule) {
  608. switch (rule.rule_type) {
  609. case codeRuleConst.measure.ruleType.dealCode:
  610. rule.preview = ctx.tender.info.deal_info.dealCode;
  611. break;
  612. case codeRuleConst.measure.ruleType.tenderName:
  613. rule.preview = tender.name;
  614. break;
  615. case codeRuleConst.measure.ruleType.inDate:
  616. rule.preview = moment().format('YYYY');
  617. break;
  618. case codeRuleConst.measure.ruleType.text:
  619. rule.preview = rule.text;
  620. break;
  621. case codeRuleConst.measure.ruleType.addNo:
  622. const s = '0000000000';
  623. rule.preview = s.substr(s.length - rule.format);
  624. break;
  625. default: break;
  626. }
  627. }
  628. const renderData = {
  629. moment,
  630. tender,
  631. permission: ctx.permission.inspection,
  632. rule_type,
  633. codeRule,
  634. dealCode: ctx.tender.info.deal_info.dealCode,
  635. c_connector,
  636. c_rule_first,
  637. ruleType: codeRuleConst.ruleType[rule_type],
  638. ruleConst: codeRuleConst.measure,
  639. filter,
  640. inspectionList,
  641. auditType,
  642. auditConst: qualityInspectionConst,
  643. status,
  644. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.inspection),
  645. pageInfo,
  646. };
  647. await this.layout('quality/inspection.ejs', renderData, 'quality/inspection_modal.ejs');
  648. } catch (err) {
  649. ctx.log(err);
  650. ctx.postError(err, '无法查看质量巡检数据');
  651. ctx.redirect(`/sp/${ctx.subProject.id}/quality/inspection`);
  652. }
  653. }
  654. /**
  655. * 新增变更申请 (Post)
  656. *
  657. * @param {Object} ctx - egg全局变量
  658. * @return {void}
  659. */
  660. async inspectionSave(ctx) {
  661. try {
  662. const data = JSON.parse(ctx.request.body.data);
  663. const reponseData = {
  664. err: 0, msg: '', data: {},
  665. };
  666. switch (data.type) {
  667. case 'add':
  668. if (!data.code || data.code === '') {
  669. throw '编号不能为空';
  670. }
  671. if (!data.check_item || !data.check_date) {
  672. throw '请填写检查项和日期';
  673. }
  674. reponseData.data = await ctx.service.qualityInspection.add(ctx.tender.id, ctx.session.sessionUser.accountId, data.code, data.check_item, data.check_date);
  675. break;
  676. default:throw '参数有误';
  677. }
  678. ctx.body = reponseData;
  679. } catch (err) {
  680. this.log(err);
  681. ctx.body = { err: 1, msg: err.toString() };
  682. }
  683. }
  684. /**
  685. * 获取审批界面所需的 原报、审批人数据等
  686. * @param ctx
  687. * @return {Promise<void>}
  688. * @private
  689. */
  690. async _getInspectionAuditViewData(ctx) {
  691. await ctx.service.qualityInspection.loadAuditViewData(ctx.inspection);
  692. }
  693. async inspectionInformation(ctx) {
  694. try {
  695. const whiteList = this.ctx.app.config.multipart.whitelist;
  696. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  697. await this._getInspectionAuditViewData(ctx);
  698. // 获取附件列表
  699. const fileList = await ctx.service.qualityInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  700. // 获取用户人验证手机号
  701. const renderData = {
  702. moment,
  703. tender,
  704. inspection: ctx.inspection,
  705. auditConst: qualityInspectionConst,
  706. inspectionMenuCounts: await this._getInspectionMenuCounts(ctx),
  707. fileList,
  708. whiteList,
  709. auditType,
  710. shenpiConst,
  711. deleteFilePermission: PermissionCheck.delFile(this.ctx.session.sessionUser.permission),
  712. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.inspection_information),
  713. preUrl: `/sp/${ctx.subProject.id}/quality/tender/${ctx.tender.id}/inspection/${ctx.inspection.id}/information`,
  714. };
  715. // data.accountGroup = accountGroup;
  716. // 获取所有项目参与者
  717. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  718. renderData.accountList = accountList;
  719. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  720. renderData.accountGroup = unitList.map(item => {
  721. const groupList = accountList.filter(item1 => item1.company === item.name);
  722. return { groupName: item.name, groupList };
  723. }).filter(x => { return x.groupList.length > 0; });
  724. if (ctx.inspection.flow_version === 1) {
  725. const groupedIds = new Set(renderData.accountGroup.reduce((ids, group) => ids.concat(group.groupList.map(user => user.id)), []));
  726. const others = accountList.filter(user => !groupedIds.has(user.id));
  727. if (others.length) renderData.accountGroup.push({ groupName: '其他用户', groupList: others });
  728. }
  729. await this.layout('quality/inspection_information.ejs', renderData, 'quality/inspection_information_modal.ejs');
  730. } catch (err) {
  731. this.log(err);
  732. ctx.redirect(`/sp/${ctx.subProject.id}/quality/tender/${ctx.tender.id}/inspection`);
  733. }
  734. }
  735. async inspectionInformationSave(ctx) {
  736. try {
  737. const data = JSON.parse(ctx.request.body.data);
  738. const reponseData = {
  739. err: 0, msg: '', data: {},
  740. };
  741. if (ctx.inspection.flow_version === 1) {
  742. reponseData.data = await ctx.service.qualityInspectionAudit.processFlow(ctx.inspection, data);
  743. ctx.body = reponseData;
  744. return;
  745. }
  746. switch (data.type) {
  747. case 'update-field':
  748. if (!(!ctx.inspection.readOnly || ctx.inspection.rectificationPower)) {
  749. throw '当前状态不可修改';
  750. }
  751. if (data.update.check_item !== undefined && data.update.check_item === '') {
  752. throw '检查项不能为空';
  753. }
  754. if (data.update.check_date !== undefined && data.update.check_date === '') {
  755. throw '请填写检查日期';
  756. }
  757. if (data.update.rectification_item !== undefined && data.update.rectification_item === '') {
  758. throw '整改情况不能为空';
  759. }
  760. if (data.update.rectification_date !== undefined && data.update.rectification_date === '') {
  761. throw '请填写整改日期';
  762. }
  763. const fields = ['id', 'code', 'check_item', 'check_situation', 'action', 'check_date', 'inspector', 'rectification_item', 'rectification_date'];
  764. if (!this.checkFieldExists(data.update, fields)) {
  765. throw '参数有误';
  766. }
  767. reponseData.data = await ctx.service.qualityInspection.defaultUpdate(data.update);
  768. break;
  769. case 'add-audit':
  770. const id = this.app._.toInteger(data.auditorId);
  771. if (isNaN(id) || id <= 0) {
  772. throw '参数错误';
  773. }
  774. // 检查权限等
  775. if (ctx.inspection.uid !== ctx.session.sessionUser.accountId) {
  776. throw '您无权添加审核人';
  777. }
  778. if (ctx.inspection.status !== auditConst.inspection.status.uncheck && ctx.inspection.status !== auditConst.inspection.status.checkNo) {
  779. throw '当前不允许添加审核人';
  780. }
  781. ctx.inspection.auditorList = await ctx.service.qualityInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  782. // 检查审核人是否已存在
  783. const exist = this.app._.find(ctx.inspection.auditorList, { aid: id });
  784. if (exist) {
  785. throw '该审核人已存在,请勿重复添加';
  786. }
  787. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.inspection, sp_status: shenpiConst.sp_status.gdzs });
  788. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.inspection === shenpiConst.sp_status.gdzs ? 1 : 0;
  789. const result = await ctx.service.qualityInspectionAudit.addAuditor(ctx.inspection.id, id, ctx.inspection.times, is_gdzs);
  790. if (!result) {
  791. throw '添加审核人失败';
  792. }
  793. reponseData.data = await ctx.service.qualityInspectionAudit.getUserGroup(ctx.inspection.id, ctx.inspection.times);
  794. break;
  795. case 'del-audit':
  796. const id2 = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  797. if (isNaN(id2) || id2 <= 0) {
  798. throw '参数错误';
  799. }
  800. const result2 = await ctx.service.qualityInspectionAudit.deleteAuditor(ctx.inspection.id, id2, ctx.inspection.times);
  801. if (!result2) {
  802. throw '移除审核人失败';
  803. }
  804. reponseData.data = await ctx.service.qualityInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  805. break;
  806. case 'start-inspection':
  807. if (ctx.inspection.readOnly) {
  808. throw '当前状态不可提交';
  809. }
  810. await ctx.service.qualityInspectionAudit.start(ctx.inspection.id, ctx.inspection.times);
  811. break;
  812. case 'del-inspection':
  813. if (ctx.inspection.readOnly) {
  814. throw '当前状态不可删除';
  815. }
  816. await ctx.service.qualityInspection.delInspection(ctx.inspection.id);
  817. break;
  818. case 'check':
  819. if (!ctx.inspection || !(ctx.inspection.status === auditConst.inspection.status.checking || ctx.inspection.status === auditConst.inspection.status.checkNoPre)) {
  820. throw '当前质量巡检数据有误';
  821. }
  822. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  823. throw '您无权进行该操作';
  824. }
  825. await ctx.service.qualityInspectionAudit.check(ctx.inspection, data);
  826. break;
  827. case 'rectification':
  828. if (!ctx.inspection || ctx.inspection.status !== auditConst.inspection.status.rectification) {
  829. throw '当前质量巡检数据有误';
  830. }
  831. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  832. throw '您无权进行该操作';
  833. }
  834. await ctx.service.qualityInspectionAudit.rectification(ctx.inspection, data);
  835. break;
  836. default:throw '参数有误';
  837. }
  838. ctx.body = reponseData;
  839. } catch (err) {
  840. this.log(err);
  841. ctx.body = { err: 1, msg: err.toString() };
  842. }
  843. }
  844. checkFieldExists(update, fields) {
  845. for (const field of Object.keys(update)) {
  846. if (!fields.includes(field)) {
  847. return false;
  848. }
  849. }
  850. return true;
  851. }
  852. /**
  853. * 上传附件
  854. * @param {*} ctx 上下文
  855. */
  856. async uploadInspectionFile(ctx) {
  857. let stream;
  858. try {
  859. if (ctx.inspection.flow_version === 1 && !ctx.inspection.canUpload) throw '当前不可上传附件';
  860. const parts = this.ctx.multipart({
  861. autoFields: true,
  862. });
  863. const files = [];
  864. const create_time = Date.parse(new Date()) / 1000;
  865. let idx = 0;
  866. const extra_upload = ctx.inspection.status === auditConst.inspection.status.checked;
  867. while ((stream = await parts()) !== undefined) {
  868. if (!stream.filename) {
  869. // 如果没有传入直接返回
  870. return;
  871. }
  872. const fileInfo = path.parse(stream.filename);
  873. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/quality_inspection/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  874. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  875. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  876. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  877. ++idx;
  878. stream && (await sendToWormhole(stream));
  879. }
  880. const in_time = new Date();
  881. const payload = files.map(file => {
  882. let idx;
  883. if (Array.isArray(parts.field.name)) {
  884. idx = parts.field.name.findIndex(name => name === file.name);
  885. } else {
  886. idx = 'isString';
  887. }
  888. const newFile = {
  889. tid: ctx.tender.id,
  890. qiid: ctx.inspection.id,
  891. uid: ctx.session.sessionUser.accountId,
  892. filename: file.name,
  893. fileext: file.ext,
  894. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  895. filepath: file.filepath,
  896. upload_time: in_time,
  897. extra_upload,
  898. };
  899. return newFile;
  900. });
  901. // 执行文件信息写入数据库
  902. await ctx.service.qualityInspectionAtt.saveFileMsgToDb(payload);
  903. // 将最新的当前标段的所有文件信息返回
  904. const data = await ctx.service.qualityInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  905. ctx.body = { err: 0, msg: '', data };
  906. } catch (err) {
  907. stream && (await sendToWormhole(stream));
  908. this.log(err);
  909. ctx.body = { err: 1, msg: err.toString(), data: null };
  910. }
  911. }
  912. /**
  913. * 删除附件
  914. * @param {Ojbect} ctx 上下文
  915. */
  916. async deleteInspectionFile(ctx) {
  917. try {
  918. const { id } = JSON.parse(ctx.request.body.data);
  919. const fileInfo = await ctx.service.qualityInspectionAtt.getDataById(id);
  920. if (ctx.inspection.flow_version === 1) {
  921. if (!fileInfo || fileInfo.qiid !== ctx.inspection.id || !ctx.inspection.canUpload || fileInfo.uid !== ctx.session.sessionUser.accountId) throw '您无权删除该附件';
  922. if (ctx.inspection.status === qualityInspectionConst.status.checked && !fileInfo.extra_upload && !PermissionCheck.delFile(ctx.session.sessionUser.permission)) throw '该附件不可删除';
  923. }
  924. if (fileInfo && Object.keys(fileInfo).length) {
  925. // 先删除文件
  926. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  927. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  928. // 再删除数据库
  929. await ctx.service.qualityInspectionAtt.delete(id);
  930. } else {
  931. throw '不存在该文件';
  932. }
  933. const data = await ctx.service.qualityInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  934. ctx.body = { err: 0, msg: '请求成功', data };
  935. } catch (err) {
  936. this.log(err);
  937. ctx.body = { err: 1, msg: err.toString(), data: null };
  938. }
  939. }
  940. /**
  941. * 下载附件
  942. * @param {Object} ctx - egg全局变量
  943. * @return {void}
  944. */
  945. async downloadInspectionFile(ctx) {
  946. const id = ctx.params.fid;
  947. if (id) {
  948. try {
  949. const fileInfo = await ctx.service.qualityInspectionAtt.getDataById(id);
  950. if (fileInfo !== undefined && fileInfo !== '') {
  951. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  952. // 解决中文无法下载问题
  953. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  954. let disposition = '';
  955. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  956. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  957. } else if (userAgent.indexOf('firefox') >= 0) {
  958. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  959. } else {
  960. /* safari等其他非主流浏览器只能自求多福了 */
  961. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  962. }
  963. ctx.response.set({
  964. 'Content-Type': 'application/octet-stream',
  965. 'Content-Disposition': disposition,
  966. 'Content-Length': fileInfo.filesize,
  967. });
  968. // ctx.body = await fs.createReadStream(fileName);
  969. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  970. } else {
  971. throw '不存在该文件';
  972. }
  973. } catch (err) {
  974. this.log(err);
  975. this.setMessage(err.toString(), this.messageType.ERROR);
  976. }
  977. }
  978. }
  979. async inspectionOverview(ctx) {
  980. try {
  981. if (!ctx.subProject.page_show.qualityInspection) throw '该功能已关闭';
  982. const tender = await this.service.tender.getDataById(ctx.tender.id);
  983. const year = moment().year();
  984. const renderData = {
  985. moment,
  986. tender,
  987. permission: ctx.permission.inspection,
  988. overview: await ctx.service.qualityInspection.getOverviewData(ctx.tender.id, year),
  989. inspectionMenuCounts: await this._getInspectionMenuCounts(ctx),
  990. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.quality.inspection_overview),
  991. };
  992. await this.layout('quality/inspection_overview.ejs', renderData);
  993. } catch (err) {
  994. ctx.log(err);
  995. ctx.postError(err, '无法查看巡检概况数据');
  996. ctx.redirect(`/sp/${ctx.subProject.id}/quality/tender/${ctx.tender.id}/inspection`);
  997. }
  998. }
  999. async inspectionRectification(ctx) {
  1000. try {
  1001. if (!ctx.subProject.page_show.qualityInspection) throw '该功能已关闭';
  1002. const status = qualityInspectionConst.filter.status.rectification; // 正在整改
  1003. await this._filterInspection(ctx, status);
  1004. } catch (err) {
  1005. ctx.log(err);
  1006. ctx.postError(err, '无法查看巡检整改数据');
  1007. ctx.redirect(`/sp/${ctx.subProject.id}/quality/tender/${ctx.tender.id}/inspection`);
  1008. }
  1009. }
  1010. async inspectionApproval(ctx) {
  1011. try {
  1012. if (!ctx.subProject.page_show.qualityInspection) throw '该功能已关闭';
  1013. const status = qualityInspectionConst.filter.status.reply; // 正在批复
  1014. await this._filterInspection(ctx, status);
  1015. } catch (err) {
  1016. ctx.log(err);
  1017. ctx.postError(err, '无法查看巡检批复数据');
  1018. ctx.redirect(`/sp/${ctx.subProject.id}/quality/tender/${ctx.tender.id}/inspection`);
  1019. }
  1020. }
  1021. async _getInspectionMenuCounts(ctx) {
  1022. const status = qualityInspectionConst.filter.status;
  1023. const [rectification, reply] = await Promise.all([
  1024. ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, status.rectification),
  1025. ctx.service.qualityInspection.getCountByStatus(ctx.tender.id, status.reply),
  1026. ]);
  1027. return { rectification, reply };
  1028. }
  1029. }
  1030. return QualityController;
  1031. };