dashboard_controller.js 44 KB

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