dashboard_controller.js 40 KB

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