dashboard_controller.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. 'use strict';
  2. /**
  3. * 控制面板
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/23
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const officeList = require('../const/cld_office').list;
  11. const maintainConst = require('../const/maintain');
  12. const typeColMap = require('../const/advance').typeColMap;
  13. const moment = require('moment');
  14. const fs = require('fs');
  15. const path = require('path');
  16. const sendToWormhole = require('stream-wormhole');
  17. module.exports = app => {
  18. class DashboardController extends app.BaseController {
  19. /**
  20. * 控制面板页面
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. async workspace(ctx) {
  26. const allAuditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId);
  27. const allAuditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId);
  28. const allAuditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId);
  29. const allAuditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId);
  30. const allAuditMaterial = await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId);
  31. const allAuditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId);
  32. const allAuditChangeProject = await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId);
  33. const allAuditChangeApply = await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId);
  34. const allAuditChangePlan = await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId);
  35. const allAuditPayments = await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId);
  36. const allAuditStageAss = await ctx.service.stageAuditAss.getAuditStageAss(ctx.session.sessionUser.accountId);
  37. const allAuditFinancials = await ctx.service.financialPayAudit.getAuditFinancial(ctx.session.sessionUser.accountId);
  38. const allAuditInspections = await ctx.service.qualityInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId);
  39. const allAuditSafeInspections = await ctx.service.safeInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId);
  40. const allAuditSafeStage = await ctx.service.safeStageAudit.getAuditStage(ctx.session.sessionUser.accountId);
  41. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  42. const auditShenpiTenders = await ctx.service.ledgerAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  43. const auditShenpiStages = await ctx.service.stageAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  44. const auditShenpiChanges = await ctx.service.changeAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  45. const auditShenpiRevise = await ctx.service.reviseAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  46. const auditShenpiMaterial = await ctx.service.materialAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  47. const auditShenpiAdvance = await ctx.service.advanceAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  48. const auditShenpiChangeProject = await ctx.service.changeProjectAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  49. const auditShenpiChangeApply = await ctx.service.changeApplyAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  50. const auditShenpiChangePlan = await ctx.service.changePlanAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  51. const auditShenpiPayment = await ctx.service.paymentDetailAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  52. const auditShenpiFinancial = await ctx.service.financialPayAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  53. const auditShenpiInspection = await ctx.service.qualityInspectionAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  54. const auditShenpiSafeInspection = await ctx.service.safeInspectionAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  55. const auditShenpiSafeStage = await ctx.service.safeStageAudit.getDonesByAudit(ctx.session.sessionUser.accountId);
  56. const dashboardStatus = {
  57. all: 0,
  58. dashboard: 0,
  59. worry: 0,
  60. early: 0,
  61. shenpi: {
  62. ledger: 0,
  63. stage: 0,
  64. change: 0,
  65. revise: 0,
  66. material: 0,
  67. advance: 0,
  68. changeProject: 0,
  69. changeApply: 0,
  70. changePlan: 0,
  71. payment: 0,
  72. financial: 0,
  73. inspection: 0,
  74. safeInspection: 0,
  75. safeStage: 0,
  76. },
  77. };
  78. let noticeList = [];
  79. // 还要考虑功能关闭不展示对应类型情况
  80. const subProjects = await ctx.service.subProject.getSubProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin, true);
  81. for (const subProject of subProjects) {
  82. subProject.page_show = ctx.service.subProject.getPageShow(subProject.page_show);
  83. const spTenders = ctx.helper._.filter(auditShenpiTenders, { spid: subProject.id });
  84. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spTenders, 'shenpi_type', 'ledger'));
  85. const spStages = ctx.helper._.filter(auditShenpiStages, { spid: subProject.id });
  86. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spStages, 'shenpi_type', 'stage'));
  87. const spChanges = ctx.helper._.filter(auditShenpiChanges, { spid: subProject.id });
  88. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChanges, 'shenpi_type', 'change'));
  89. const spRevise = ctx.helper._.filter(auditShenpiRevise, { spid: subProject.id });
  90. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spRevise, 'shenpi_type', 'revise'));
  91. const spMaterial = subProject.page_show.openMaterial ? ctx.helper._.filter(auditShenpiMaterial, { spid: subProject.id }) : [];
  92. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spMaterial, 'shenpi_type', 'material'));
  93. const spAdvance = ctx.helper._.filter(auditShenpiAdvance, { spid: subProject.id });
  94. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spAdvance, 'shenpi_type', 'advance'));
  95. const spChangeProject = subProject.page_show.openChangeProject ? ctx.helper._.filter(auditShenpiChangeProject, { spid: subProject.id }) : [];
  96. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangeProject, 'shenpi_type', 'changeProject'));
  97. const spChangeApply = subProject.page_show.openChangeApply ? ctx.helper._.filter(auditShenpiChangeApply, { spid: subProject.id }) : [];
  98. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangeApply, 'shenpi_type', 'changeApply'));
  99. const spChangePlan = subProject.page_show.openChangePlan ? ctx.helper._.filter(auditShenpiChangePlan, { spid: subProject.id }) : [];
  100. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spChangePlan, 'shenpi_type', 'changePlan'));
  101. const spPayment = subProject.page_show.openPayment ? ctx.helper._.filter(auditShenpiPayment, { spid: subProject.id }) : [];
  102. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spPayment, 'shenpi_type', 'payment'));
  103. const spFinancial = subProject.page_show.openFinancial ? ctx.helper._.filter(auditShenpiFinancial, { spid: subProject.id }) : [];
  104. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spFinancial, 'shenpi_type', 'financial'));
  105. const spInspection = subProject.page_show.qualityInspection ? ctx.helper._.filter(auditShenpiInspection, { spid: subProject.id }) : [];
  106. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spInspection, 'shenpi_type', 'inspection'));
  107. const spSafeInspection = subProject.page_show.safeInspection ? ctx.helper._.filter(auditShenpiSafeInspection, { spid: subProject.id }) : [];
  108. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spSafeInspection, 'shenpi_type', 'safeInspection'));
  109. const spSafeStage = subProject.page_show.safeInspection ? ctx.helper._.filter(auditShenpiSafeStage, { spid: subProject.id }) : [];
  110. noticeList.push(...ctx.helper.addKeyValue4ObjArray(spSafeStage, 'shenpi_type', 'safeStage'));
  111. }
  112. const noticeDayList = [];
  113. noticeList = ctx.helper._.orderBy(noticeList, ['shenpi_time'], ['desc']);
  114. for (const notice of noticeList) {
  115. const noticeDay = moment(new Date(notice.shenpi_time)).format('YYYY-MM-DD');
  116. if (noticeDayList.indexOf(noticeDay) === -1) noticeDayList.push(noticeDay);
  117. }
  118. const dashboardShenpis = [];
  119. // 审批时间状态提示,0:无预警,1,预警(当前时间大于创建时间常高于stageAuditEarly天时,触发提示),2,紧急(当前时间大于审批发起时常高于stageAuditWorry天时,触发提示)
  120. const auditTenders = await this.auditSet(ctx, allAuditTenders, subProjects, dashboardStatus, 'ledger');
  121. dashboardShenpis.push(...auditTenders);
  122. const auditStages = await this.auditSet(ctx, allAuditStages, subProjects, dashboardStatus, 'stage');
  123. dashboardShenpis.push(...auditStages);
  124. const auditChanges = await this.auditSet(ctx, allAuditChanges, subProjects, dashboardStatus, 'change');
  125. dashboardShenpis.push(...auditChanges);
  126. const auditRevise = await this.auditSet(ctx, allAuditRevise, subProjects, dashboardStatus, 'revise');
  127. dashboardShenpis.push(...auditRevise);
  128. const auditMaterial = await this.auditSet(ctx, allAuditMaterial, subProjects, dashboardStatus, 'material');
  129. dashboardShenpis.push(...auditMaterial);
  130. const auditAdvance = await this.auditSet(ctx, allAuditAdvance, subProjects, dashboardStatus, 'advance');
  131. dashboardShenpis.push(...auditAdvance);
  132. const auditChangeProject = await this.auditSet(ctx, allAuditChangeProject, subProjects, dashboardStatus, 'changeProject');
  133. dashboardShenpis.push(...auditChangeProject);
  134. const auditChangeApply = await this.auditSet(ctx, allAuditChangeApply, subProjects, dashboardStatus, 'changeApply');
  135. dashboardShenpis.push(...auditChangeApply);
  136. const auditChangePlan = await this.auditSet(ctx, allAuditChangePlan, subProjects, dashboardStatus, 'changePlan');
  137. dashboardShenpis.push(...auditChangePlan);
  138. const auditPayments = await this.auditSet(ctx, allAuditPayments, subProjects, dashboardStatus, 'payment');
  139. dashboardShenpis.push(...auditPayments);
  140. const auditStageAss = await this.auditSet(ctx, allAuditStageAss, subProjects, dashboardStatus, 'stageAss');
  141. dashboardShenpis.push(...auditStageAss);
  142. const auditFinancials = await this.auditSet(ctx, allAuditFinancials, subProjects, dashboardStatus, 'financial');
  143. dashboardShenpis.push(...auditFinancials);
  144. const auditInspections = await this.auditSet(ctx, allAuditInspections, subProjects, dashboardStatus, 'inspection');
  145. dashboardShenpis.push(...auditInspections);
  146. const auditSafeInspections = await this.auditSet(ctx, allAuditSafeInspections, subProjects, dashboardStatus, 'safeInspection');
  147. dashboardShenpis.push(...auditSafeInspections);
  148. const auditSafeStage = await this.auditSet(ctx, allAuditSafeStage, subProjects, dashboardStatus, 'safeStage');
  149. dashboardShenpis.push(...auditSafeStage);
  150. // console.log(ctx.helper._.orderBy(dashboardShenpis, ['start_audit', 'shenpi_time'], ['desc', 'desc']));
  151. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  152. // 获取销售人员数据
  153. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  154. const officeName = officeList[salesmanData.office];
  155. // 获取版本信息
  156. const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 });
  157. // 获取项目通知
  158. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.helper._.map(subProjects, 'id'), 10);
  159. // 获取系统通知
  160. const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, '', 1, 0, 2);
  161. // 获取系统维护信息
  162. const maintainData = await ctx.service.maintain.getDataById(1);
  163. // 最近使用项目列表
  164. const recentProjects = await ctx.service.subProjPermission.getRecentProjects(ctx.session.sessionUser.accountId);
  165. const renderData = {
  166. dashboardShenpis: ctx.helper._.orderBy(dashboardShenpis, ['start_audit', 'shenpi_time'], ['desc', 'asc']),
  167. dashboardStatus,
  168. role: pa.role,
  169. authMobile: pa.auth_mobile,
  170. acLedger: auditConst.ledger,
  171. acStage: auditConst.stage,
  172. acChange: auditConst.change,
  173. acRevise: auditConst.revise,
  174. acMaterial: auditConst.material,
  175. acAdvance: auditConst.advance,
  176. acChangeProject: auditConst.changeProject,
  177. acChangeApply: auditConst.changeApply,
  178. acChangePlan: auditConst.changeApply,
  179. acFinancial: auditConst.financial,
  180. acInspection: auditConst.inspection,
  181. acSafeStage: auditConst.safeStage,
  182. noticeList,
  183. noticeDayList,
  184. pushType: auditConst.pushType,
  185. projectData,
  186. salesmanData,
  187. officeName,
  188. versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  189. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  190. sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  191. uid: ctx.session.sessionUser.accountId,
  192. maintainData,
  193. maintainConst,
  194. typeColMap,
  195. recentProjects,
  196. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.dashboard.workspace),
  197. };
  198. await this.layout('dashboard/workspace.ejs', renderData, 'dashboard/modal.ejs');
  199. await ctx.service.projectAccount.defaultUpdate({
  200. id: this.ctx.session.sessionUser.accountId,
  201. last_notice: new Date(),
  202. });
  203. }
  204. async loadTenders(ctx) {
  205. try {
  206. const responseData = { err: 0, msg: '', data: {} };
  207. const subProjects = await ctx.service.subProject.getSubProject(ctx.session.sessionProject.id, ctx.session.sessionUser.accountId, ctx.session.sessionUser.is_admin, true);
  208. const tenders = [];
  209. const accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  210. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  211. for (const subProject of subProjects) {
  212. subProject.page_show = ctx.service.subProject.getPageShow(subProject.page_show);
  213. // 获取每个项目下参与的标段
  214. const subProjectTenders = await ctx.service.tender.getList('', userPermission, ctx.session.sessionUser.is_admin, '', subProject);
  215. tenders.push(...subProjectTenders);
  216. }
  217. for (const t of tenders) {
  218. // 用标段管理的方法获取t数据
  219. await this.ctx.service.tenderCache.loadTenderCache(t, '');
  220. t.total_price = t.ledger_tp && t.ledger_tp.total_price ? t.ledger_tp.total_price : 0;
  221. // [t.change_tp, t.change_p_tp, t.change_n_tp, t.change_valuation_tp, t.change_unvaluation_tp] = await ctx.service.change.getChangeTp(t.id);
  222. }
  223. responseData.data.tenderList = tenders;
  224. ctx.body = responseData;
  225. } catch (err) {
  226. this.log(err);
  227. ctx.body = { err: 1, msg: err.toString(), data: null };
  228. }
  229. }
  230. async auditSet(ctx, allAudits, subProjects, dashboardStatus, type) {
  231. const audits = [];
  232. for (const t of allAudits) {
  233. const sp = subProjects.find(sp => sp.id === t.spid);
  234. if (sp) {
  235. t.sp_name = sp.name;
  236. t.start_audit = 0;
  237. let calcTime;
  238. let closeType = false;
  239. switch (type) {
  240. case 'ledger':
  241. calcTime = t.ledger_status === auditConst[type].status.checking ? t.begin_time : t.end_time;
  242. break;
  243. case 'revise':
  244. calcTime = t.status === auditConst[type].status.checking ? t.begin_time : t.end_time;
  245. break;
  246. case 'stage':
  247. calcTime = t.sstatus === auditConst[type].status.checkNo ? t.end_time : t.begin_time;
  248. break;
  249. case 'stageAss':
  250. calcTime = t.begin_time;
  251. break;
  252. case 'change':
  253. calcTime = t.begin_time ? t.begin_time : t.cin_time ? new Date(ctx.moment.unix(t.cin_time).format('YYYY-MM-DD HH:mm:ss')) : '';
  254. break;
  255. case 'changeProject':
  256. if (!sp.page_show.openChangeProject) closeType = true;
  257. calcTime = t.status !== auditConst[type].status.back ? t.begin_time : t.end_time;
  258. break;
  259. case 'changeApply':
  260. if (!sp.page_show.openChangeApply) closeType = true;
  261. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  262. break;
  263. case 'changePlan':
  264. if (!sp.page_show.openChangePlan) closeType = true;
  265. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  266. break;
  267. case 'material':
  268. if (!sp.page_show.openMaterial) closeType = true;
  269. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  270. break;
  271. case 'advance':
  272. calcTime = t.mstatus !== auditConst[type].status.checkNo ? t.create_time : t.end_time;
  273. break;
  274. case 'payment':
  275. if (!sp.page_show.openPayment) closeType = true;
  276. calcTime = t.sstatus !== auditConst.stage.status.checkNo ? t.begin_time : t.end_time;
  277. break;
  278. case 'financial':
  279. if (!sp.page_show.openFinancial) closeType = true;
  280. calcTime = t.fpcstatus !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  281. break;
  282. case 'inspection':
  283. if (!sp.page_show.qualityInspection) closeType = true;
  284. calcTime = t.status !== auditConst[type].status.checkNo ? t.begin_time : t.end_time;
  285. break;
  286. case 'safeInspection':
  287. if (!sp.page_show.safeInspection) closeType = true;
  288. calcTime = t.status !== auditConst.inspection.status.checkNo ? t.begin_time : t.end_time;
  289. break;
  290. case 'safeStage':
  291. if (!sp.page_show.safePayment) closeType = true;
  292. calcTime = t.create_time;
  293. break;
  294. default:
  295. closeType = true;
  296. }
  297. if (closeType) continue;
  298. if (sp.page_show.openStageAudit) {
  299. const now = new Date();
  300. const calcDay = ctx.helper.calculateDaysBetween(now, calcTime);
  301. if (sp.page_show.stageAuditWorry && calcDay >= sp.page_show.stageAuditWorry) {
  302. t.start_audit = 2;
  303. dashboardStatus.worry += 1;
  304. } else if (sp.page_show.stageAuditEarly && calcDay >= sp.page_show.stageAuditEarly) {
  305. t.start_audit = 1;
  306. dashboardStatus.early += 1;
  307. }
  308. }
  309. const calcType = type === 'stageAss' ? 'stage' : type;
  310. dashboardStatus.shenpi[calcType] += 1;
  311. t.shenpi_time = calcTime;
  312. t.shenpi_type = type;
  313. dashboardStatus.dashboard += 1;
  314. audits.push(t);
  315. }
  316. }
  317. return audits;
  318. }
  319. /**
  320. * 控制面板页面
  321. *
  322. * @param {Object} ctx - egg全局变量
  323. * @return {void}
  324. */
  325. async index(ctx) {
  326. const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId, ctx.subProject.id);
  327. const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId, ctx.subProject.id);
  328. const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId, ctx.subProject.id);
  329. const auditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId, ctx.subProject.id);
  330. const auditMaterial = ctx.subProject.page_show.openMaterial ? await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  331. const auditAdvance = await ctx.service.advanceAudit.getAuditAdvance(ctx.session.sessionUser.accountId, ctx.subProject.id);
  332. const auditChangeProject = ctx.subProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getAuditChangeProject(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  333. const auditChangeApply = ctx.subProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getAuditChangeApply(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  334. const auditChangePlan = ctx.subProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getAuditChangePlan(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  335. const auditPayments = ctx.subProject.page_show.openPayment ? await ctx.service.paymentDetailAudit.getAuditPayment(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  336. const auditStageAss = await ctx.service.stageAuditAss.getAuditStageAss(ctx.session.sessionUser.accountId, ctx.subProject.id);
  337. const auditFinancials = ctx.subProject.page_show.openFinancial ? await ctx.service.financialPayAudit.getAuditFinancial(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  338. const auditInspections = ctx.subProject.page_show.qualityInspection ? await ctx.service.qualityInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  339. const auditSafeInspections = ctx.subProject.page_show.safeInspection ? await ctx.service.safeInspectionAudit.getAuditInspection(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  340. const auditSafeStage = ctx.subProject.page_show.safePayment ? await ctx.service.safeStageAudit.getAuditStage(ctx.session.sessionUser.accountId, ctx.subProject.id) : [];
  341. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  342. const noticeList = await ctx.service.noticePush.getNotice(ctx.session.sessionProject.id, pa.id, ctx.subProject.id);
  343. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  344. // 获取销售人员数据
  345. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  346. const officeName = officeList[salesmanData.office];
  347. // 获取版本信息
  348. const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 });
  349. // 获取项目通知
  350. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.subProject.id);
  351. const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
  352. const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
  353. // 获取系统通知
  354. const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, '', 1, 0, 2);
  355. // 获取系统维护信息
  356. const maintainData = await ctx.service.maintain.getDataById(1);
  357. // 获取各个审批的次数及最后的审批时间
  358. const shenpi_count = [
  359. { count: await ctx.service.advanceAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '预付款' },
  360. { count: await ctx.service.ledgerAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '台账审批' },
  361. { count: await ctx.service.reviseAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '台账修订' },
  362. { count: await ctx.service.stageAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '计量审批' },
  363. { count: await ctx.service.changeAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更审批' },
  364. ];
  365. if (ctx.subProject.page_show.openChangeProject) shenpi_count.push({ count: await ctx.service.changeProjectAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更立项' });
  366. if (ctx.subProject.page_show.openChangeApply) shenpi_count.push({ count: await ctx.service.changeApplyAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更申请' });
  367. if (ctx.subProject.page_show.openChangePlan) shenpi_count.push({ count: await ctx.service.changePlanAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '变更方案' });
  368. if (ctx.subProject.page_show.openMaterial) shenpi_count.push({ count: await ctx.service.materialAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '材料调差' });
  369. if (ctx.subProject.page_show.openFinancial) shenpi_count.push({ count: await ctx.service.financialPayAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '资金支付' });
  370. if (ctx.subProject.page_show.qualityInspection) shenpi_count.push({ count: await ctx.service.qualityInspectionAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '质量巡检' });
  371. if (ctx.subProject.page_show.safeInspection) shenpi_count.push({ count: await ctx.service.safeInspectionAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '安全巡检' });
  372. if (ctx.subProject.page_show.safePayment) shenpi_count.push({ count: await ctx.service.safeStageAudit.getCountByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id), name: '安全计量' });
  373. // shenpi_count.push({ count: await ctx.service.advanceAudit.getCountByChecked(ctx.session.sessionUser.accountId), name: '预付款' });
  374. const total_count = ctx.app._.sumBy(shenpi_count, 'count');
  375. const shenpi_lastime = [
  376. await ctx.service.advanceAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  377. await ctx.service.ledgerAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  378. await ctx.service.reviseAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  379. await ctx.service.stageAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  380. await ctx.service.changeAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id),
  381. ctx.subProject.page_show.openChangeProject ? await ctx.service.changeProjectAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  382. ctx.subProject.page_show.openChangeApply ? await ctx.service.changeApplyAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  383. ctx.subProject.page_show.openChangePlan ? await ctx.service.changePlanAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  384. ctx.subProject.page_show.openMaterial ? await ctx.service.materialAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  385. ctx.subProject.page_show.openFinancial ? await ctx.service.financialPayAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  386. ctx.subProject.page_show.qualityInspection ? await ctx.service.qualityInspectionAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  387. ctx.subProject.page_show.safeInspection ? await ctx.service.safeInspectionAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  388. ctx.subProject.page_show.safeInspection ? await ctx.service.safeStageAudit.getLastEndTimeByChecked(ctx.session.sessionUser.accountId, ctx.subProject.id) : null,
  389. ];
  390. const last_time = ctx.app._.max(shenpi_lastime);
  391. // console.log(ctx.app._.max(shenpi_lastime), ctx.helper.calcDayNum(last_time));
  392. const renderData = {
  393. auditTenders,
  394. auditStages,
  395. auditChanges,
  396. auditRevise,
  397. auditMaterial,
  398. auditAdvance,
  399. auditChangeProject,
  400. auditChangeApply,
  401. auditChangePlan,
  402. auditPayments,
  403. auditStageAss,
  404. auditFinancials,
  405. auditInspections,
  406. auditSafeInspections,
  407. auditSafeStage,
  408. shenpi_count,
  409. total_count,
  410. last_day: ctx.helper.calcDayNum(last_time),
  411. role: pa.role,
  412. authMobile: pa.auth_mobile,
  413. acLedger: auditConst.ledger,
  414. acStage: auditConst.stage,
  415. acChange: auditConst.change,
  416. acRevise: auditConst.revise,
  417. acMaterial: auditConst.material,
  418. acAdvance: auditConst.advance,
  419. acChangeProject: auditConst.changeProject,
  420. acChangeApply: auditConst.changeApply,
  421. acChangePlan: auditConst.changeApply,
  422. acFinancial: auditConst.financial,
  423. acInspection: auditConst.inspection,
  424. acSafeStage: auditConst.safeStage,
  425. noticeList,
  426. pushType: auditConst.pushType,
  427. projectData,
  428. salesmanData,
  429. officeName,
  430. versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  431. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  432. sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  433. userMsgPermission,
  434. uid: ctx.session.sessionUser.accountId,
  435. maintainData,
  436. maintainConst,
  437. typeColMap,
  438. };
  439. await this.layout('dashboard/index.ejs', renderData, 'dashboard/modal.ejs');
  440. await ctx.service.projectAccount.defaultUpdate({
  441. id: this.ctx.session.sessionUser.accountId,
  442. last_notice: new Date(),
  443. });
  444. }
  445. /**
  446. * 控制面板-通知页面
  447. *
  448. * @param {Object} ctx - egg全局变量
  449. * @return {void}
  450. */
  451. async msg(ctx) {
  452. try {
  453. const page = ctx.page;
  454. const msgId = parseInt(ctx.params.mid) || 0;
  455. let msgInfo = msgId ? await ctx.service.message.getDataById(msgId) : null;
  456. const type = msgInfo ? msgInfo.type : ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;
  457. if (msgInfo && msgInfo.type === 1 && msgInfo.project_id !== ctx.session.sessionProject.id) {
  458. throw '非该项目通知无权查看';
  459. }
  460. if (msgInfo) {
  461. msgInfo.content = JSON.parse(JSON.stringify(msgInfo.content).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;'));
  462. msgInfo.files = await this.ctx.service.messageAtt.getAtt(msgInfo.id);
  463. }
  464. const total = type === 1 ?
  465. await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, spid: [ctx.subProject.id, ''], type }) :
  466. await ctx.service.message.count({ status: 1, type });
  467. const limit = 5;
  468. const offset = limit * (this.ctx.page - 1);
  469. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, ctx.subProject.id, limit, offset, type);
  470. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  471. const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
  472. const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
  473. if (!msgInfo) {
  474. msgInfo = msgList[0];
  475. }
  476. // 分页相关
  477. const pageInfo = {
  478. page,
  479. total: Math.ceil(total / limit),
  480. queryData: JSON.stringify(ctx.urlInfo.query),
  481. };
  482. const renderData = {
  483. msgInfo,
  484. uid: ctx.session.sessionUser.accountId,
  485. type,
  486. pageInfo,
  487. userMsgPermission,
  488. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  489. };
  490. await this.layout('dashboard/msg.ejs', renderData);
  491. } catch (error) {
  492. console.log(error);
  493. this.log(error);
  494. ctx.session.postError = error.toString();
  495. ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard');
  496. }
  497. }
  498. /**
  499. * 控制面板-通知添加和编辑页面
  500. *
  501. * @param {Object} ctx - egg全局变量
  502. * @return {void}
  503. */
  504. async msgAdd(ctx) {
  505. let id = ctx.params.mid;
  506. id = parseInt(id);
  507. try {
  508. if (isNaN(id) || id < 0) {
  509. throw '参数错误';
  510. }
  511. const rule = ctx.service.message.rule();
  512. const jsValidator = await this.jsValidator.convert(rule).build();
  513. const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id);
  514. const files = await ctx.service.messageAtt.getAtt(id);
  515. const renderData = {
  516. jsValidator,
  517. msgInfo,
  518. files,
  519. whiteList: ctx.app.config.multipart.whitelist,
  520. moment,
  521. };
  522. await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');
  523. } catch (error) {
  524. console.log(error);
  525. // this.setMessage(error.toString(), this.messageType.ERROR);
  526. ctx.redirect(ctx.request.header.referer);
  527. }
  528. }
  529. /**
  530. * 控制面板-通知保存
  531. *
  532. * @param {Object} ctx - egg全局变量
  533. * @return {void}
  534. */
  535. async msgSet(ctx) {
  536. try {
  537. let id = ctx.params.mid;
  538. id = parseInt(id);
  539. if (isNaN(id) || id < 0) {
  540. throw '参数错误';
  541. }
  542. const rule = ctx.service.message.rule();
  543. ctx.helper.validate(rule);
  544. const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id, ctx.subProject.id);
  545. if (result) {
  546. // 新增的项目通知会发送微信模版消息通知客户
  547. if (id === 0) {
  548. // 获取该项目所有的openid,发送信息
  549. const wechats = await ctx.service.projectAccount.getOpenIdListByPid(ctx.session.sessionProject.id);
  550. if (wechats.length > 0) {
  551. const msgInfo = await ctx.service.message.getDataById(result);
  552. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  553. // 绑定成功通知
  554. const templateId = 'VKUo4us4lt2dQY0EaaJxcui2jkjmriN3A0K7i4kpZwY';
  555. const url = ctx.protocol + '://' + ctx.host + '/wx/url2wap?project=' + ctx.session.sessionProject.code + '&url=' + ctx.protocol + '://' + ctx.host + '/wap/dashboard/msg/' + msgInfo.id;
  556. const msgData = {
  557. thing21: {
  558. value: ctx.helper.contentChange(projectData.name),
  559. },
  560. thing2: {
  561. value: ctx.helper.contentChange(msgInfo.title),
  562. },
  563. thing8: {
  564. value: msgInfo.creator,
  565. },
  566. time3: {
  567. value: moment(msgInfo.release_time * 1000).format('YYYY-MM-DD'),
  568. },
  569. };
  570. for (const wx of wechats) {
  571. const result = await app.wechat.api.sendTemplate(wx.wx_openid, templateId, url, '', msgData);
  572. }
  573. }
  574. }
  575. ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard/msg');
  576. }
  577. } catch (error) {
  578. console.log(error);
  579. ctx.redirect(ctx.request.header.referer);
  580. }
  581. }
  582. /**
  583. * 控制面板-通知删除
  584. *
  585. * @param {Object} ctx - egg全局变量
  586. * @return {void}
  587. */
  588. async msgDelete(ctx) {
  589. try {
  590. let id = ctx.params.mid;
  591. id = parseInt(id);
  592. if (isNaN(id) || id <= 0) {
  593. throw '参数错误';
  594. }
  595. const msgInfo = await ctx.service.message.getDataById(id);
  596. if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) {
  597. throw '通知不存在或无权限操作';
  598. }
  599. const result = await ctx.service.message.deleteMsg(msgInfo.id);
  600. if (result) {
  601. ctx.redirect('/sp/' + ctx.subProject.id + '/dashboard/msg');
  602. }
  603. } catch (error) {
  604. console.log(error);
  605. ctx.redirect(ctx.request.header.referer);
  606. }
  607. }
  608. /**
  609. * 将推送记录设置为已读
  610. * @param {Object} ctx 上下文
  611. */
  612. async pushSet(ctx) {
  613. try {
  614. const { id } = JSON.parse(ctx.request.body.data);
  615. const data = await ctx.service.noticePush.set(id);
  616. ctx.body = { err: 0, msg: '' };
  617. } catch (err) {
  618. this.log(err);
  619. ctx.body = { err: 1, msg: err.toString(), data: null };
  620. }
  621. }
  622. /**
  623. * 上传附件
  624. * @param {*} ctx 上下文
  625. */
  626. async msgUploadFile(ctx) {
  627. let stream;
  628. try {
  629. const responseData = { err: 0, msg: '', data: {} };
  630. const mid = ctx.params.mid || 0;
  631. if (!mid) throw '参数有误';
  632. const parts = this.ctx.multipart({
  633. autoFields: true,
  634. });
  635. const files = [];
  636. const create_time = Date.parse(new Date()) / 1000;
  637. let idx = 0;
  638. while ((stream = await parts()) !== undefined) {
  639. if (!stream.filename) {
  640. // 如果没有传入直接返回
  641. return;
  642. }
  643. const fileInfo = path.parse(stream.filename);
  644. const filepath = `app/public/upload/message/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  645. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  646. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  647. ++idx;
  648. stream && (await sendToWormhole(stream));
  649. }
  650. const in_time = new Date();
  651. const payload = files.map(file => {
  652. let idx;
  653. if (Array.isArray(parts.field.name)) {
  654. idx = parts.field.name.findIndex(name => name === file.name);
  655. } else {
  656. idx = 'isString';
  657. }
  658. const newFile = {
  659. project_id: ctx.session.sessionProject.id,
  660. mid,
  661. uid: ctx.session.sessionUser.accountId,
  662. filename: file.name,
  663. fileext: file.ext,
  664. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  665. filepath: file.filepath,
  666. upload_time: in_time,
  667. };
  668. return newFile;
  669. });
  670. // 执行文件信息写入数据库
  671. await ctx.service.messageAtt.saveFileMsgToDb(payload);
  672. // 将最新的当前标段的所有文件信息返回
  673. responseData.data = await ctx.service.messageAtt.getAtt(mid);
  674. ctx.body = responseData;
  675. } catch (err) {
  676. stream && (await sendToWormhole(stream));
  677. this.log(err);
  678. ctx.body = { err: 1, msg: err.toString(), data: null };
  679. }
  680. }
  681. /**
  682. * 删除附件
  683. * @param {Ojbect} ctx 上下文
  684. */
  685. async msgDeleteFile(ctx) {
  686. try {
  687. const mid = ctx.params.mid || 0;
  688. const responseData = { err: 0, msg: '', data: {} };
  689. const data = JSON.parse(ctx.request.body.data);
  690. const fileInfo = await ctx.service.messageAtt.getDataById(data.id);
  691. if (fileInfo) {
  692. // 先删除文件
  693. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  694. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  695. // 再删除数据库
  696. await ctx.service.messageAtt.delete(data.id);
  697. } else {
  698. throw '不存在该文件';
  699. }
  700. responseData.data = await ctx.service.messageAtt.getAtt(mid);
  701. ctx.body = responseData;
  702. } catch (err) {
  703. this.log(err);
  704. ctx.body = { err: 1, msg: err.toString(), data: null };
  705. }
  706. }
  707. }
  708. return DashboardController;
  709. };