dashboard_controller.js 47 KB

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