dashboard_controller.js 40 KB

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