safe_controller.js 50 KB

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