safe_controller.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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 auditType = require('../const/audit').auditType;
  11. const shenpiConst = require('../const/shenpi');
  12. const codeRuleConst = require('../const/code_rule');
  13. const contractConst = require('../const/contract');
  14. const moment = require('moment');
  15. const sendToWormhole = require('stream-wormhole');
  16. const fs = require('fs');
  17. const path = require('path');
  18. const PermissionCheck = require('../const/account_permission').PermissionCheck;
  19. module.exports = app => {
  20. class SafeController extends app.BaseController {
  21. constructor(ctx) {
  22. super(ctx);
  23. ctx.showProject = true;
  24. // ctx.showTitle = true;
  25. }
  26. loadMenu(ctx) {
  27. super.loadMenu(ctx);
  28. // 虚拟menu,以保证标题显示正确
  29. ctx.menu = {
  30. name: '安全管理',
  31. display: false,
  32. caption: '安全管理',
  33. controller: 'safe',
  34. };
  35. }
  36. async tender(ctx) {
  37. try {
  38. if (!ctx.subProject.page_show.safePayment) throw '该功能已关闭';
  39. const renderData = {
  40. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  41. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.tender),
  42. };
  43. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  44. renderData.accountList = accountList;
  45. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  46. const accountGroupList = unitList.map(item => {
  47. const groupList = accountList.filter(item1 => item1.company === item.name);
  48. return { groupName: item.name, groupList };
  49. }).filter(x => { return x.groupList.length > 0; });
  50. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  51. // renderData.accountGroup = unitList.map(item => {
  52. // const groupList = accountList.filter(item1 => item1.company === item.name);
  53. // return { groupName: item.name, groupList };
  54. // });
  55. renderData.accountGroup = accountGroupList;
  56. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  57. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'safe_payment', ctx.session.sessionUser.is_admin ? 'all' : '');
  58. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  59. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  60. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('safe_payment');
  61. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('safe_payment');
  62. await this.layout('safe/tender.ejs', renderData, 'safe/tender_modal.ejs');
  63. } catch (err) {
  64. ctx.log(err);
  65. ctx.postError(err, '无法查看安全计量数据');
  66. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  67. }
  68. }
  69. async stage(ctx) {
  70. try {
  71. if (!ctx.subProject.page_show.safePayment) throw '该功能已关闭';
  72. const renderData = {
  73. auditType: auditConst.auditType,
  74. auditConst: auditConst.common,
  75. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.stage),
  76. };
  77. renderData.stages = await this.ctx.service.safeStage.getAllStages(ctx.tender.id, 'DESC');
  78. for (const s of renderData.stages) {
  79. if (s.audit_status !== auditConst.common.status.checked) await this.ctx.service.safeStage.loadUser(s);
  80. s.can_del = (s.create_user_id === ctx.session.sessionUser.accountId || ctx.session.sessionUser.is_admin)
  81. && (s.audit_status === auditConst.common.status.uncheck || s.audit_status === auditConst.common.status.checkNo);
  82. }
  83. await this.layout('safe_calc/stage.ejs', renderData, 'safe_calc/stage_modal.ejs');
  84. } catch (err) {
  85. ctx.log(err);
  86. ctx.postError(err, '查看安全计量数据错误');
  87. ctx.redirect(`/sp/${ctx.subProject.id}/safe`);
  88. }
  89. }
  90. async addStage(ctx) {
  91. try {
  92. if (!ctx.permission.safe_payment.add) throw '您无权创建计量期';
  93. const stage_date = ctx.request.body.stage_date;
  94. if (!stage_date) throw '请选择日期';
  95. const stage_code = ctx.request.body.stage_code;
  96. const stages = await ctx.service.safeStage.getAllStages(ctx.tender.id, 'DESC');
  97. const unCompleteCount = stages.filter(s => { return s.status !== auditConst.common.status.checked; }).length;
  98. if (unCompleteCount.length > 0) throw `最新一起未审批通过,请审批通过后再新增`;
  99. const newStage = await ctx.service.safeStage.add(ctx.tender.id, stage_code, stage_date);
  100. if (!newStage) throw '新增期失败';
  101. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage/${newStage.stage_order}/bills`);
  102. } catch (err) {
  103. ctx.log(err);
  104. ctx.postError(err, '新增期失败');
  105. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  106. }
  107. }
  108. async delStage(ctx) {
  109. try {
  110. // if (!ctx.session.sessionUser.is_admin && ctx.request.body.confirm !== '确认删除本期') throw '请输入文本确认删除本期';
  111. const stage_id = ctx.request.body.stage_id;
  112. const stage = await ctx.service.safeStage.getDataById(stage_id);
  113. if (!stage) throw '删除的期不存在,请刷新页面';
  114. if (!ctx.session.sessionUser.is_admin && stage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权删除本期';
  115. // 获取最新的期数
  116. const stageCount = await ctx.service.safeStage.count({ tid: ctx.tender.id });
  117. if (stage.stage_order !== stageCount) throw '非最新一期,不可删除';
  118. await ctx.service.safeStage.delete(stage_id);
  119. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  120. } catch (err) {
  121. ctx.log(err);
  122. ctx.postError(err, '删除期失败');
  123. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  124. }
  125. }
  126. async saveStage(ctx) {
  127. try {
  128. const stage_id = ctx.request.body.stage_id;
  129. const data = {
  130. stage_date: ctx.request.body.stage_date,
  131. stage_code: ctx.request.body.stage_code,
  132. };
  133. const stage = await ctx.service.safeStage.getStage(stage_id);
  134. if (!stage) throw '删除的期不存在,请刷新页面';
  135. if (!ctx.session.sessionUser.is_admin && stage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权修改该数据';
  136. await this.ctx.service.safeStage.save(stage, data);
  137. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  138. } catch (err) {
  139. ctx.log(err);
  140. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  141. }
  142. }
  143. async _getStageAuditViewData(ctx) {
  144. await this.ctx.service.safeStage.loadAuditViewData(ctx.safeStage);
  145. }
  146. async safeBills(ctx) {
  147. try {
  148. await this._getStageAuditViewData(ctx);
  149. // 获取附件列表
  150. const attList = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.safeStage.id, 'desc');
  151. const stdBills = await ctx.service.stdGclList.getSafeGcl();
  152. // 流程审批人相关数据
  153. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  154. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  155. const accountGroup = unitList.map(item => {
  156. const groupList = accountList.filter(item1 => item1.company === item.name);
  157. return { groupName: item.name, groupList };
  158. }).filter(x => { return x.groupList.length > 0; });
  159. // 是否已验证手机短信
  160. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  161. const renderData = {
  162. auditConst: auditConst.common,
  163. accountList,
  164. accountGroup,
  165. shenpiConst,
  166. auditType: auditConst.auditType,
  167. authMobile: pa.auth_mobile,
  168. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.bills),
  169. stdBills,
  170. attList,
  171. };
  172. await this.layout('safe_calc/index.ejs', renderData, 'safe_calc/modal.ejs');
  173. } catch (err) {
  174. ctx.log(err);
  175. ctx.postError(err, '读取安全生产费错误');
  176. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  177. }
  178. }
  179. async safeCompare(ctx) {
  180. try {
  181. const renderData = {
  182. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.compare),
  183. auditConst,
  184. };
  185. await this.layout('safe_calc/compare.ejs', renderData);
  186. } catch (err) {
  187. ctx.log(err);
  188. ctx.postError(err, '读取安全生产费错误');
  189. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  190. }
  191. }
  192. async safeLoad(ctx) {
  193. try {
  194. const data = JSON.parse(ctx.request.body.data);
  195. const filter = data.filter.split(';');
  196. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  197. for (const f of filter) {
  198. switch (f) {
  199. case 'bills':
  200. responseData.data.bills = ctx.safeStage.readOnly
  201. ? await ctx.service.safeStageBills.getReadData(ctx.safeStage)
  202. : await ctx.service.safeStageBills.getEditData(ctx.safeStage);
  203. break;
  204. case 'billsCompare':
  205. responseData.data[f] = await ctx.service.safeStageBills.getCompareData(ctx.safeStage);
  206. break;
  207. case 'auditFlow':
  208. responseData.data[f] = await ctx.service.safeStageAudit.getViewFlow(ctx.safeStage);
  209. break;
  210. case 'att':
  211. responseData.data[f] = await ctx.service.safeStageFile.getData(ctx.safeStage.id, 'bills', 'DESC');
  212. break;
  213. default:
  214. responseData.data[f] = [];
  215. break;
  216. }
  217. }
  218. ctx.body = responseData;
  219. } catch (err) {
  220. this.log(err);
  221. ctx.body = { err: 1, msg: err.toString(), data: null };
  222. }
  223. }
  224. async _billsBase(stage, type, data) {
  225. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  226. if (type !== 'add') {
  227. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  228. }
  229. switch (type) {
  230. case 'add':
  231. return await this.ctx.service.safeStageBills.addSafeBillsNode(stage, data.id, data.count);
  232. case 'delete':
  233. return await this.ctx.service.safeStageBills.delete(stage.id, data.id, data.count);
  234. case 'up-move':
  235. return await this.ctx.service.safeStageBills.upMoveNode(stage.id, data.id, data.count);
  236. case 'down-move':
  237. return await this.ctx.service.safeStageBills.downMoveNode(stage.id, data.id, data.count);
  238. case 'up-level':
  239. return await this.ctx.service.safeStageBills.upLevelNode(stage.id, data.id, data.count);
  240. case 'down-level':
  241. return await this.ctx.service.safeStageBills.downLevelNode(stage.id, data.id, data.count);
  242. }
  243. }
  244. async safeUpdate(ctx) {
  245. try {
  246. const data = JSON.parse(ctx.request.body.data);
  247. if (!data.postType || !data.postData) throw '数据错误';
  248. const responseData = { err: 0, msg: '', data: {} };
  249. switch (data.postType) {
  250. case 'add':
  251. case 'delete':
  252. case 'up-move':
  253. case 'down-move':
  254. case 'up-level':
  255. case 'down-level':
  256. responseData.data = await this._billsBase(ctx.safeStage, data.postType, data.postData);
  257. break;
  258. case 'update':
  259. responseData.data = await this.ctx.service.safeStageBills.updateCalc(ctx.safeStage, data.postData);
  260. break;
  261. case 'add-std':
  262. responseData.data = await this.ctx.service.safeStageBills.addStdNodeWithParent(ctx.safeStage, data.postData.id, data.postData.stdData);
  263. break;
  264. default:
  265. throw '未知操作';
  266. }
  267. ctx.body = responseData;
  268. } catch (err) {
  269. this.log(err);
  270. ctx.body = this.ajaxErrorBody(err, '数据错误');
  271. }
  272. }
  273. async safeDecimal(ctx) {
  274. try {
  275. const data = JSON.parse(ctx.request.body.data);
  276. const result = await this.ctx.service.safeStageBills.setDecimal(data.decimal);
  277. ctx.body = { err: 0, msg: '', data: result };
  278. } catch (err) {
  279. this.log(err);
  280. ctx.body = this.ajaxErrorBody(err, '设置小数位数错误');
  281. }
  282. }
  283. async uploadStageFile(ctx) {
  284. let stream;
  285. try {
  286. const parts = ctx.multipart({autoFields: true});
  287. let index = 0;
  288. const create_time = Date.parse(new Date()) / 1000;
  289. let stream = await parts();
  290. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  291. const rela_type = parts.fields.type;
  292. const rela_id = parts.field.rela_id;
  293. const uploadfiles = [];
  294. while (stream !== undefined) {
  295. if (!stream.filename) throw '未发现上传文件!';
  296. const fileInfo = path.parse(stream.filename);
  297. const filepath = `app/public/upload/${ctx.safeStage.tid}/safeStage/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  298. // 保存文件
  299. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  300. await sendToWormhole(stream);
  301. // 插入到stage_pay对应的附件列表中
  302. uploadfiles.push({
  303. rela_id,
  304. filename: fileInfo.name,
  305. fileext: fileInfo.ext,
  306. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  307. filepath,
  308. });
  309. ++index;
  310. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  311. stream = await parts();
  312. } else {
  313. stream = undefined;
  314. }
  315. }
  316. const result = await ctx.service.safeStageFile.addFiles(ctx.safeStage, 'bills', uploadfiles, user);
  317. ctx.body = {err: 0, msg: '', data: result};
  318. } catch (error) {
  319. ctx.log(error);
  320. // 失败需要消耗掉stream 以防卡死
  321. if (stream) await sendToWormhole(stream);
  322. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  323. }
  324. }
  325. async deleteStageFile(ctx) {
  326. try{
  327. const data = JSON.parse(ctx.request.body.data);
  328. if (!data && !data.id) throw '缺少参数';
  329. const result = await ctx.service.safeStageFile.delFiles(data.id);
  330. ctx.body = { err: 0, msg: '', data: result };
  331. } catch(error) {
  332. ctx.log(error);
  333. ctx.ajaxErrorBody(error, '删除附件失败');
  334. }
  335. }
  336. /**
  337. * 添加审批人
  338. * @param ctx
  339. * @return {Promise<void>}
  340. */
  341. async addStageAudit(ctx) {
  342. try {
  343. const data = JSON.parse(ctx.request.body.data);
  344. const id = this.app._.toInteger(data.auditorId);
  345. if (isNaN(id) || id <= 0) throw '参数错误';
  346. // 检查权限等
  347. if (ctx.safeStage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权添加审核人';
  348. if (ctx.safeStage.audit_status !== auditConst.common.status.uncheck && ctx.safeStage.audit_status !== auditConst.common.status.checkNo) {
  349. throw '当前不允许添加审核人';
  350. }
  351. // 检查审核人是否已存在
  352. const exist = await ctx.service.safeStageAudit.getDataByCondition({ stage_id: ctx.safeStage.id, audit_times: ctx.safeStage.audit_times, audit_id: id });
  353. if (exist) throw '该审核人已存在,请勿重复添加';
  354. const auditorInfo = await this.ctx.service.projectAccount.getDataById(id);
  355. if (!auditorInfo) throw '添加的审批人不存在';
  356. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.safe_payment, sp_status: shenpiConst.sp_status.gdzs });
  357. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.safe_payment === shenpiConst.sp_status.gdzs ? 1 : 0;
  358. const result = await ctx.service.safeStageAudit.addAuditor(ctx.safeStage.id, auditorInfo, ctx.safeStage.audit_times, is_gdzs);
  359. if (!result) throw '添加审核人失败';
  360. const auditors = await ctx.service.safeStageAudit.getAuditorGroup(ctx.safeStage.id, ctx.safeStage.audit_times);
  361. ctx.body = { err: 0, msg: '', data: auditors };
  362. } catch (err) {
  363. ctx.log(err);
  364. ctx.body = { err: 1, msg: err.toString(), data: null };
  365. }
  366. }
  367. /**
  368. * 移除审批人
  369. * @param ctx
  370. * @return {Promise<void>}
  371. */
  372. async deleteStageAudit(ctx) {
  373. try {
  374. const data = JSON.parse(ctx.request.body.data);
  375. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  376. if (isNaN(id) || id <= 0) throw '参数错误';
  377. const result = await ctx.service.safeStageAudit.deleteAuditor(ctx.safeStage.id, id, ctx.safeStage.audit_times);
  378. if (!result) throw '移除审核人失败';
  379. const auditors = await ctx.service.safeStageAudit.getAuditors(ctx.safeStage.id, ctx.safeStage.audit_times);
  380. ctx.body = { err: 0, msg: '', data: auditors };
  381. } catch (err) {
  382. ctx.log(err);
  383. ctx.body = { err: 1, msg: err.toString(), data: null };
  384. }
  385. }
  386. async stageAuditStart(ctx) {
  387. try {
  388. if (ctx.safeStage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权上报该期数据';
  389. if (ctx.safeStage.revising) throw '台账修订中,不可上报';
  390. if (ctx.safeStage.audit_status !== auditConst.common.status.uncheck && ctx.safeStage.audit_status !== auditConst.common.status.checkNo) throw '该期数据当前无法上报';
  391. await ctx.service.safeStageAudit.start(ctx.safeStage);
  392. ctx.redirect(ctx.request.header.referer);
  393. } catch (err) {
  394. ctx.log(err);
  395. ctx.postError(err, '上报失败');
  396. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage/${ctx.safeStage.stage_order}/bills`);
  397. }
  398. }
  399. async stageAuditCheck(ctx) {
  400. try {
  401. if (!ctx.safeStage || (ctx.safeStage.audit_status !== auditConst.common.status.checking && ctx.safeStage.audit_status !== auditConst.common.status.checkNoPre)) {
  402. throw '当前期数据有误';
  403. }
  404. if (ctx.safeStage.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) < 0) {
  405. throw '您无权进行该操作';
  406. }
  407. if (ctx.safeStage.revising) throw '台账修订中,不可审批';
  408. const checkType = parseInt(ctx.request.body.checkType);
  409. const opinion = ctx.request.body.opinion.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  410. await ctx.service.safeStageAudit.check(ctx.safeStage, checkType, opinion);
  411. } catch (err) {
  412. ctx.log(err);
  413. ctx.postError(err, '审批失败');
  414. }
  415. ctx.redirect(ctx.request.header.referer);
  416. }
  417. async stageAuditCheckAgain(ctx) {
  418. try {
  419. if (!ctx.safeStage.isLatest) throw '非最新一期,不可重新审批';
  420. if (ctx.safeStage.audit_status !== auditConst.common.status.checked) throw '未审批完成,不可重新审批';
  421. if (ctx.safeStage.revising) throw '台账修订中,不可重审';
  422. if (ctx.session.sessionUser.loginStatus === 0) {
  423. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  424. if (!user.auth_mobile) throw '未绑定手机号';
  425. const code = ctx.request.body.code;
  426. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  427. const cacheCode = await app.redis.get(cacheKey);
  428. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  429. throw '验证码不正确!';
  430. }
  431. }
  432. const adminCheckAgain = ctx.request.body.confirm === '确认设置终审审批' && ctx.session.sessionUser.is_admin;
  433. if (ctx.safeStage.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) < 0 && !adminCheckAgain) throw '您无权重新审批';
  434. await ctx.service.safeStageAudit.checkAgain(ctx.safeStage, adminCheckAgain);
  435. } catch (err) {
  436. ctx.log(err);
  437. ctx.postError(err, '重新审批失败');
  438. }
  439. ctx.redirect(ctx.request.header.referer);
  440. }
  441. async stageAuditCheckCancel(ctx) {
  442. try {
  443. if (ctx.safeStage.revising) throw '台账修订中,不可撤回';
  444. if (!ctx.safeStage.cancancel) throw '您无权进行该操作';
  445. await ctx.service.safeStageAudit.checkCancel(ctx.safeStage);
  446. } catch (err) {
  447. ctx.log(err);
  448. ctx.postError(err, '撤回失败');
  449. }
  450. ctx.redirect(ctx.request.header.referer);
  451. }
  452. async inspectionTender(ctx) {
  453. try {
  454. if (!ctx.subProject.page_show.safeInspection) throw '该功能已关闭';
  455. const renderData = {
  456. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  457. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.tender),
  458. };
  459. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  460. renderData.accountList = accountList;
  461. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  462. const accountGroupList = unitList.map(item => {
  463. const groupList = accountList.filter(item1 => item1.company === item.name);
  464. return { groupName: item.name, groupList };
  465. }).filter(x => { return x.groupList.length > 0; });
  466. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  467. // renderData.accountGroup = unitList.map(item => {
  468. // const groupList = accountList.filter(item1 => item1.company === item.name);
  469. // return { groupName: item.name, groupList };
  470. // });
  471. renderData.accountGroup = accountGroupList;
  472. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  473. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'safe_inspection', ctx.session.sessionUser.is_admin ? 'all' : '');
  474. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  475. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  476. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('safe_inspection');
  477. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('safe_inspection');
  478. await this.layout('safe/tender.ejs', renderData, 'safe/tender_modal.ejs');
  479. } catch (err) {
  480. ctx.log(err);
  481. ctx.postError(err, '无法查看安全巡检数据');
  482. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  483. }
  484. }
  485. /**
  486. * 变更管理 页面 (Get)
  487. *
  488. * @param {Object} ctx - egg全局变量
  489. * @return {void}
  490. */
  491. async inspection(ctx) {
  492. try {
  493. if (!ctx.subProject.page_show.safeInspection) throw '该功能已关闭';
  494. const status = parseInt(ctx.query.status) || 0;
  495. await this._filterInspection(ctx, status);
  496. } catch (err) {
  497. ctx.log(err);
  498. ctx.postError(err, '无法查看质量管理数据');
  499. ctx.redirect(`/sp/${ctx.subProject.id}/safe/inspection`);
  500. }
  501. }
  502. // 质量巡检单功能
  503. async _filterInspection(ctx, status = 0) {
  504. try {
  505. ctx.session.sessionUser.tenderId = ctx.tender.id;
  506. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  507. const orders = ctx.query.order ? ctx.query.order : 0;
  508. const filter = JSON.parse(JSON.stringify(auditConst.inspection.filter));
  509. filter.count = [];
  510. filter.count[filter.status.pending] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.pending);
  511. filter.count[filter.status.uncheck] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.uncheck);
  512. filter.count[filter.status.checking] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checking);
  513. filter.count[filter.status.rectification] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.rectification);
  514. filter.count[filter.status.checked] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checked);
  515. filter.count[filter.status.checkStop] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checkStop);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  516. const inspectionList = await ctx.service.safeInspection.getListByStatus(ctx.tender.id, status, 1, sorts, orders);
  517. const total = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, status);
  518. const allAttList = inspectionList.length > 0 ? await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.helper._.map(inspectionList, 'id')) : [];
  519. for (const c of inspectionList) {
  520. c.attList = ctx.helper._.filter(allAttList, { qiid: c.id });
  521. c.curAuditors = await ctx.service.safeInspectionAudit.getAuditorsByStatus(c.id, c.status, c.times);
  522. if (c.status === auditConst.inspection.status.checkNoPre) {
  523. c.curAuditors2 = await ctx.service.safeInspectionAudit.getAuditorsByStatus(c.id, auditConst.inspection.status.checking, c.times);
  524. }
  525. }
  526. // 分页相关
  527. const page = ctx.page;
  528. const pageSize = ctx.pageSize;
  529. const pageInfo = {
  530. page,
  531. pageSizeSelect: 1,
  532. pageSize,
  533. total_num: total,
  534. total: Math.ceil(total / pageSize),
  535. queryData: JSON.stringify(ctx.urlInfo.query),
  536. };
  537. let codeRule = [];
  538. let c_connector = '1';
  539. let c_rule_first = 1;
  540. const rule_type = 'safe_inspection';
  541. const tender = await this.service.tender.getDataById(ctx.tender.id);
  542. if (tender.c_code_rules) {
  543. const c_code_rules = JSON.parse(tender.c_code_rules);
  544. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  545. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  546. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  547. }
  548. for (const rule of codeRule) {
  549. switch (rule.rule_type) {
  550. case codeRuleConst.measure.ruleType.dealCode:
  551. rule.preview = ctx.tender.info.deal_info.dealCode;
  552. break;
  553. case codeRuleConst.measure.ruleType.tenderName:
  554. rule.preview = tender.name;
  555. break;
  556. case codeRuleConst.measure.ruleType.inDate:
  557. rule.preview = moment().format('YYYY');
  558. break;
  559. case codeRuleConst.measure.ruleType.text:
  560. rule.preview = rule.text;
  561. break;
  562. case codeRuleConst.measure.ruleType.addNo:
  563. const s = '0000000000';
  564. rule.preview = s.substr(s.length - rule.format);
  565. break;
  566. default: break;
  567. }
  568. }
  569. const renderData = {
  570. moment,
  571. tender,
  572. permission: ctx.permission.safe_inspection,
  573. rule_type,
  574. codeRule,
  575. dealCode: ctx.tender.info.deal_info.dealCode,
  576. c_connector,
  577. c_rule_first,
  578. ruleType: codeRuleConst.ruleType[rule_type],
  579. ruleConst: codeRuleConst.measure,
  580. filter,
  581. inspectionList,
  582. auditType,
  583. auditConst: auditConst.inspection,
  584. status,
  585. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.inspection),
  586. pageInfo,
  587. };
  588. await this.layout('safe/inspection.ejs', renderData, 'safe/inspection_modal.ejs');
  589. } catch (err) {
  590. ctx.log(err);
  591. ctx.postError(err, '无法查看安全巡检数据');
  592. ctx.redirect(`/sp/${ctx.subProject.id}/safe/inspection`);
  593. }
  594. }
  595. /**
  596. * 新增变更申请 (Post)
  597. *
  598. * @param {Object} ctx - egg全局变量
  599. * @return {void}
  600. */
  601. async inspectionSave(ctx) {
  602. try {
  603. const data = JSON.parse(ctx.request.body.data);
  604. const reponseData = {
  605. err: 0, msg: '', data: {},
  606. };
  607. switch (data.type) {
  608. case 'add':
  609. if (!data.code || data.code === '') {
  610. throw '编号不能为空';
  611. }
  612. if (!data.check_item || !data.check_date) {
  613. throw '请填写检查项和日期';
  614. }
  615. reponseData.data = await ctx.service.safeInspection.add(ctx.tender.id, ctx.session.sessionUser.accountId, data.code, data.check_item, data.check_date);
  616. break;
  617. default:throw '参数有误';
  618. }
  619. ctx.body = reponseData;
  620. } catch (err) {
  621. this.log(err);
  622. ctx.body = { err: 1, msg: err.toString() };
  623. }
  624. }
  625. /**
  626. * 获取审批界面所需的 原报、审批人数据等
  627. * @param ctx
  628. * @return {Promise<void>}
  629. * @private
  630. */
  631. async _getInspectionAuditViewData(ctx) {
  632. await ctx.service.safeInspection.loadAuditViewData(ctx.inspection);
  633. }
  634. async inspectionInformation(ctx) {
  635. try {
  636. const whiteList = this.ctx.app.config.multipart.whitelist;
  637. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  638. await this._getInspectionAuditViewData(ctx);
  639. // 获取附件列表
  640. const fileList = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  641. // 获取用户人验证手机号
  642. const renderData = {
  643. moment,
  644. tender,
  645. inspection: ctx.inspection,
  646. auditConst: auditConst.inspection,
  647. fileList,
  648. whiteList,
  649. auditType,
  650. shenpiConst,
  651. deleteFilePermission: PermissionCheck.delFile(this.ctx.session.sessionUser.permission),
  652. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.inspection_information),
  653. preUrl: `/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/inspection/${ctx.inspection.id}/information`,
  654. };
  655. // data.accountGroup = accountGroup;
  656. // 获取所有项目参与者
  657. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  658. renderData.accountList = accountList;
  659. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  660. renderData.accountGroup = unitList.map(item => {
  661. const groupList = accountList.filter(item1 => item1.company === item.name);
  662. return { groupName: item.name, groupList };
  663. }).filter(x => { return x.groupList.length > 0; });
  664. await this.layout('safe/inspection_information.ejs', renderData, 'safe/inspection_information_modal.ejs');
  665. } catch (err) {
  666. this.log(err);
  667. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/inspection`);
  668. }
  669. }
  670. async inspectionInformationSave(ctx) {
  671. try {
  672. const data = JSON.parse(ctx.request.body.data);
  673. const reponseData = {
  674. err: 0, msg: '', data: {},
  675. };
  676. switch (data.type) {
  677. case 'update-field':
  678. if (!(!ctx.inspection.readOnly || ctx.inspection.rectificationPower)) {
  679. throw '当前状态不可修改';
  680. }
  681. if (data.update.check_item !== undefined && data.update.check_item === '') {
  682. throw '检查项不能为空';
  683. }
  684. if (data.update.check_date !== undefined && data.update.check_date === '') {
  685. throw '请填写检查日期';
  686. }
  687. if (data.update.rectification_item !== undefined && data.update.rectification_item === '') {
  688. throw '整改情况不能为空';
  689. }
  690. if (data.update.rectification_date !== undefined && data.update.rectification_date === '') {
  691. throw '请填写整改日期';
  692. }
  693. const fields = ['id', 'check_item', 'check_situation', 'action', 'check_date', 'inspector', 'rectification_item', 'rectification_date'];
  694. if (!this.checkFieldExists(data.update, fields)) {
  695. throw '参数有误';
  696. }
  697. reponseData.data = await ctx.service.safeInspection.defaultUpdate(data.update);
  698. break;
  699. case 'add-audit':
  700. const id = this.app._.toInteger(data.auditorId);
  701. if (isNaN(id) || id <= 0) {
  702. throw '参数错误';
  703. }
  704. // 检查权限等
  705. if (ctx.inspection.uid !== ctx.session.sessionUser.accountId) {
  706. throw '您无权添加审核人';
  707. }
  708. if (ctx.inspection.status !== auditConst.inspection.status.uncheck && ctx.inspection.status !== auditConst.inspection.status.checkNo) {
  709. throw '当前不允许添加审核人';
  710. }
  711. ctx.inspection.auditorList = await ctx.service.safeInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  712. // 检查审核人是否已存在
  713. const exist = this.app._.find(ctx.inspection.auditorList, { aid: id });
  714. if (exist) {
  715. throw '该审核人已存在,请勿重复添加';
  716. }
  717. const result = await ctx.service.safeInspectionAudit.addAuditor(ctx.inspection.id, id, ctx.inspection.times);
  718. if (!result) {
  719. throw '添加审核人失败';
  720. }
  721. reponseData.data = await ctx.service.safeInspectionAudit.getUserGroup(ctx.inspection.id, ctx.inspection.times);
  722. break;
  723. case 'del-audit':
  724. const id2 = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  725. if (isNaN(id2) || id2 <= 0) {
  726. throw '参数错误';
  727. }
  728. const result2 = await ctx.service.safeInspectionAudit.deleteAuditor(ctx.inspection.id, id2, ctx.inspection.times);
  729. if (!result2) {
  730. throw '移除审核人失败';
  731. }
  732. reponseData.data = await ctx.service.safeInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  733. break;
  734. case 'start-inspection':
  735. if (ctx.inspection.readOnly) {
  736. throw '当前状态不可提交';
  737. }
  738. await ctx.service.safeInspectionAudit.start(ctx.inspection.id, ctx.inspection.times);
  739. break;
  740. case 'del-inspection':
  741. if (ctx.inspection.readOnly) {
  742. throw '当前状态不可删除';
  743. }
  744. await ctx.service.safeInspection.delInspection(ctx.inspection.id);
  745. break;
  746. case 'check':
  747. if (!ctx.inspection || !(ctx.inspection.status === auditConst.inspection.status.checking || ctx.inspection.status === auditConst.inspection.status.checkNoPre)) {
  748. throw '当前质量巡检数据有误';
  749. }
  750. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  751. throw '您无权进行该操作';
  752. }
  753. await ctx.service.safeInspectionAudit.check(ctx.inspection, data);
  754. break;
  755. case 'rectification':
  756. if (!ctx.inspection || ctx.inspection.status !== auditConst.inspection.status.rectification) {
  757. throw '当前质量巡检数据有误';
  758. }
  759. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  760. throw '您无权进行该操作';
  761. }
  762. await ctx.service.safeInspectionAudit.rectification(ctx.inspection, data);
  763. break;
  764. default:throw '参数有误';
  765. }
  766. ctx.body = reponseData;
  767. } catch (err) {
  768. this.log(err);
  769. ctx.body = { err: 1, msg: err.toString() };
  770. }
  771. }
  772. checkFieldExists(update, fields) {
  773. for (const field of Object.keys(update)) {
  774. if (!fields.includes(field)) {
  775. return false;
  776. }
  777. }
  778. return true;
  779. }
  780. /**
  781. * 上传附件
  782. * @param {*} ctx 上下文
  783. */
  784. async uploadInspectionFile(ctx) {
  785. let stream;
  786. try {
  787. // this._checkAdvanceFileCanModify(ctx);
  788. const parts = this.ctx.multipart({
  789. autoFields: true,
  790. });
  791. const files = [];
  792. const create_time = Date.parse(new Date()) / 1000;
  793. let idx = 0;
  794. const extra_upload = ctx.inspection.status === auditConst.inspection.status.checked;
  795. while ((stream = await parts()) !== undefined) {
  796. if (!stream.filename) {
  797. // 如果没有传入直接返回
  798. return;
  799. }
  800. const fileInfo = path.parse(stream.filename);
  801. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/safe_inspection/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  802. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  803. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  804. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  805. ++idx;
  806. stream && (await sendToWormhole(stream));
  807. }
  808. const in_time = new Date();
  809. const payload = files.map(file => {
  810. let idx;
  811. if (Array.isArray(parts.field.name)) {
  812. idx = parts.field.name.findIndex(name => name === file.name);
  813. } else {
  814. idx = 'isString';
  815. }
  816. const newFile = {
  817. tid: ctx.tender.id,
  818. qiid: ctx.inspection.id,
  819. uid: ctx.session.sessionUser.accountId,
  820. filename: file.name,
  821. fileext: file.ext,
  822. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  823. filepath: file.filepath,
  824. upload_time: in_time,
  825. extra_upload,
  826. };
  827. return newFile;
  828. });
  829. // 执行文件信息写入数据库
  830. await ctx.service.safeInspectionAtt.saveFileMsgToDb(payload);
  831. // 将最新的当前标段的所有文件信息返回
  832. const data = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  833. ctx.body = { err: 0, msg: '', data };
  834. } catch (err) {
  835. stream && (await sendToWormhole(stream));
  836. this.log(err);
  837. ctx.body = { err: 1, msg: err.toString(), data: null };
  838. }
  839. }
  840. /**
  841. * 删除附件
  842. * @param {Ojbect} ctx 上下文
  843. */
  844. async deleteInspectionFile(ctx) {
  845. try {
  846. const { id } = JSON.parse(ctx.request.body.data);
  847. const fileInfo = await ctx.service.safeInspectionAtt.getDataById(id);
  848. if (fileInfo || Object.keys(fileInfo).length) {
  849. // 先删除文件
  850. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  851. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  852. // 再删除数据库
  853. await ctx.service.safeInspectionAtt.delete(id);
  854. } else {
  855. throw '不存在该文件';
  856. }
  857. const data = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  858. ctx.body = { err: 0, msg: '请求成功', data };
  859. } catch (err) {
  860. this.log(err);
  861. ctx.body = { err: 1, msg: err.toString(), data: null };
  862. }
  863. }
  864. /**
  865. * 下载附件
  866. * @param {Object} ctx - egg全局变量
  867. * @return {void}
  868. */
  869. async downloadInspectionFile(ctx) {
  870. const id = ctx.params.fid;
  871. if (id) {
  872. try {
  873. const fileInfo = await ctx.service.safeInspectionAtt.getDataById(id);
  874. if (fileInfo !== undefined && fileInfo !== '') {
  875. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  876. // 解决中文无法下载问题
  877. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  878. let disposition = '';
  879. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  880. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  881. } else if (userAgent.indexOf('firefox') >= 0) {
  882. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  883. } else {
  884. /* safari等其他非主流浏览器只能自求多福了 */
  885. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  886. }
  887. ctx.response.set({
  888. 'Content-Type': 'application/octet-stream',
  889. 'Content-Disposition': disposition,
  890. 'Content-Length': fileInfo.filesize,
  891. });
  892. // ctx.body = await fs.createReadStream(fileName);
  893. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  894. } else {
  895. throw '不存在该文件';
  896. }
  897. } catch (err) {
  898. this.log(err);
  899. this.setMessage(err.toString(), this.messageType.ERROR);
  900. }
  901. }
  902. }
  903. }
  904. return SafeController;
  905. };