schedule_controller.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/7/2
  7. * @version
  8. */
  9. const moment = require('moment');
  10. const measureType = require('../const/tender').measureType;
  11. const scheduleConst = require('../const/schedule');
  12. const billsPosConvert = require('../lib/bills_pos_convert');
  13. const _ = require('lodash');
  14. module.exports = app => {
  15. class ScheduleController extends app.BaseController {
  16. async _getSelectedLedgerList(ctx) {
  17. const scheduleLedgerList = await ctx.service.scheduleLedger.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  18. return _.map(scheduleLedgerList, 'ledger_id');
  19. }
  20. async _getLastPlanMonth(ctx) {
  21. const lastMonth = await ctx.service.scheduleMonth.getLastPlanMonth();
  22. return lastMonth && lastMonth[0] && lastMonth[0].yearmonth ? lastMonth[0].yearmonth : null;
  23. }
  24. async index(ctx) {
  25. try {
  26. const scheduleInfo = await ctx.service.schedule.getDataByCondition({ tid: ctx.tender.id });
  27. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: ctx.tender.id }, orders: [['yearmonth', 'asc']] });
  28. const renderData = {
  29. scheduleInfo,
  30. scheduleMonth,
  31. tender: ctx.tender.data,
  32. tenderMenu: this.menu.tenderMenu,
  33. planMonth: await this._getLastPlanMonth(ctx),
  34. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  35. preUrl: '/tender/' + ctx.tender.id,
  36. };
  37. await this.layout('schedule/index.ejs', renderData, 'schedule/modal.ejs');
  38. } catch (err) {
  39. this.log(err);
  40. ctx.redirect(this.menu.menu.dashboard.url);
  41. }
  42. }
  43. async ledger(ctx) {
  44. const tender = ctx.tender;
  45. const scheduleLedgerList = await this._getSelectedLedgerList(ctx);
  46. const allSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  47. const hadDataLidList = [];
  48. for (const sl of scheduleLedgerList) {
  49. const info = _.find(allSlmList, function(item) {
  50. return item.lid === sl && ((item.plan_tp !== null && item.plan_tp !== 0) ||
  51. (item.plan_gcl !== null && item.plan_gcl !== 0) ||
  52. (item.sj_tp !== null && item.sj_tp !== 0) ||
  53. (item.sj_gcl !== null && item.sj_gcl !== 0));
  54. });
  55. if (info) {
  56. hadDataLidList.push(info.lid);
  57. }
  58. }
  59. const renderData = {
  60. tender: tender.data,
  61. tenderInfo: tender.info,
  62. measureType,
  63. scheduleLedgerList,
  64. hadDataLidList,
  65. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.ledger),
  66. };
  67. await this.layout('schedule/ledger.ejs', renderData);
  68. }
  69. async plan(ctx) {
  70. const tender = ctx.tender;
  71. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  72. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  73. const renderData = {
  74. tender: tender.data,
  75. tenderInfo: tender.info,
  76. schedule,
  77. scheduleMonth,
  78. planMonth: await this._getLastPlanMonth(ctx),
  79. measureType,
  80. mode: scheduleConst.plan_mode,
  81. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  82. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.plan),
  83. };
  84. await this.layout('schedule/plan.ejs', renderData, 'schedule/plan_modal.ejs');
  85. }
  86. async stageTp(ctx) {
  87. const tender = ctx.tender;
  88. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  89. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  90. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  91. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  92. let curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  93. let slmList = [];
  94. let nextSlmList = [];
  95. let endSlmList = [];
  96. let yearSlmList = [];
  97. let curYearStageData = [];
  98. if (ctx.params.order && scheduleStage.length > 0) {
  99. curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  100. }
  101. if (curScheduleStage) {
  102. const newSS = _.sortBy(scheduleStage, 'yearmonth');
  103. const nowScheduleStage = _.findIndex(newSS, { yearmonth: curScheduleStage.yearmonth });
  104. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  105. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSS.length - 1 ? newSS[nowScheduleStage + 1] : null;
  106. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  107. if (nowScheduleStage === 0) {
  108. endSlmList = slmList;
  109. } else if (nowScheduleStage > 0) {
  110. const endYearmonthCollection = _.map(_.take(newSS, nowScheduleStage + 1), 'yearmonth');
  111. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  112. }
  113. const yearConllection = _.map(_.filter(newSS, function(item) {
  114. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  115. }), 'yearmonth');
  116. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  117. // 获取本年完成计量数据
  118. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  119. const stageList = _.filter(stageOrderList, function(item) {
  120. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  121. });
  122. const stageIdList = _.map(_.filter(stageList, function(item) {
  123. return _.find(newSS, { order: item.order });
  124. }), 'id');
  125. curYearStageData = await ctx.service.stageBills.getStagesData(ctx.tender.id, stageIdList.join(','));
  126. }
  127. const renderData = {
  128. tender: tender.data,
  129. tenderInfo: tender.info,
  130. schedule,
  131. scheduleMonth,
  132. measureType,
  133. stageOrderList,
  134. scheduleStage,
  135. curScheduleStage,
  136. slmList,
  137. nextSlmList,
  138. endSlmList,
  139. yearSlmList,
  140. curYearStageData,
  141. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  142. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageTp),
  143. };
  144. await this.layout('schedule/stage_tp.ejs', renderData, 'schedule/stage_tp_modal.ejs');
  145. }
  146. async stageGcl(ctx) {
  147. const tender = ctx.tender;
  148. const schedule = await ctx.service.schedule.getDataByCondition({ tid: tender.id });
  149. const scheduleMonth = await ctx.service.scheduleMonth.getAllDataByCondition({ where: { tid: tender.id }, orders: [['yearmonth', 'asc']] });
  150. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  151. const curScheduleStage = scheduleStage.length > 0 ? _.maxBy(scheduleStage, 'order') : null;
  152. const renderData = {
  153. tender: tender.data,
  154. tenderInfo: tender.info,
  155. schedule,
  156. scheduleMonth,
  157. measureType,
  158. scheduleStage,
  159. curScheduleStage,
  160. scheduleLedgerList: await this._getSelectedLedgerList(ctx),
  161. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.schedule.stageGcl),
  162. };
  163. await this.layout('schedule/stage_gcl.ejs', renderData, 'schedule/stage_gcl_modal.ejs');
  164. }
  165. /**
  166. * 获取金额模式下台账数据(Ajax)
  167. *
  168. * @param ctx
  169. * @return {Promise<void>}
  170. */
  171. async loadTpLedgerData(ctx) {
  172. try {
  173. const tender = ctx.tender;
  174. // const ledgerData = await ctx.controller.stage._getStageLedgerData(ctx);
  175. // console.log(ledgerData);
  176. const stageOrderList = await ctx.service.stage.getAllDataByCondition({ columns: ['id', 's_time', 'order'], where: { tid: tender.id } });
  177. const scheduleStage = await ctx.service.scheduleStage.getAllDataByCondition({ where: { tid: tender.id }, orders: [['order', 'desc']] });
  178. const curScheduleStage = _.find(scheduleStage, { order: parseInt(ctx.params.order) });
  179. let slmList = [];
  180. let nextSlmList = [];
  181. let endSlmList = [];
  182. let yearSlmList = [];
  183. let curYearStageData = [];
  184. if (curScheduleStage) {
  185. const newSS = _.sortBy(scheduleStage, 'yearmonth');
  186. const nowScheduleStage = _.findIndex(newSS, { yearmonth: curScheduleStage.yearmonth });
  187. slmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: curScheduleStage.yearmonth } });
  188. const nextScheduleStage = nowScheduleStage >= 0 && nowScheduleStage + 1 <= newSS.length - 1 ? newSS[nowScheduleStage + 1] : null;
  189. if (nextScheduleStage) nextSlmList = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: tender.id, yearmonth: nextScheduleStage.yearmonth } });
  190. if (nowScheduleStage === 0) {
  191. endSlmList = slmList;
  192. } else if (nowScheduleStage > 0) {
  193. const endYearmonthCollection = _.map(_.take(newSS, nowScheduleStage + 1), 'yearmonth');
  194. endSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, endYearmonthCollection);
  195. }
  196. const yearConllection = _.map(_.filter(newSS, function(item) {
  197. return item.yearmonth.indexOf(curScheduleStage.yearmonth.split('-')[0]) !== -1;
  198. }), 'yearmonth');
  199. yearSlmList = await ctx.service.scheduleLedgerMonth.getConllectionList(tender.id, yearConllection);
  200. // 获取本年完成计量数据
  201. const curStage = _.find(stageOrderList, { order: curScheduleStage.order });
  202. const stageList = _.filter(stageOrderList, function(item) {
  203. return item.s_time.indexOf(curStage.s_time.split('-')[0]) !== -1;
  204. });
  205. const stageIdList = _.map(_.filter(stageList, function(item) {
  206. return _.find(newSS, { order: item.order });
  207. }), 'id');
  208. curYearStageData = await ctx.service.stageBills.getStagesData(tender.id, stageIdList.join(','));
  209. }
  210. ctx.body = { err: 0, msg: '', data: {
  211. // ledgerData,
  212. slmList,
  213. nextSlmList,
  214. endSlmList,
  215. yearSlmList,
  216. curYearStageData,
  217. } };
  218. } catch (err) {
  219. this.log(err);
  220. ctx.body = { err: 1, msg: err.toString(), data: [] };
  221. }
  222. }
  223. /**
  224. * 获取台账数据(Ajax)
  225. *
  226. * @param ctx
  227. * @return {Promise<void>}
  228. */
  229. async loadLedgerData(ctx) {
  230. try {
  231. const ledgerData = await ctx.service.ledger.getData(ctx.tender.id);
  232. // const posData = ctx.tender.data.measure_type === measureType.tz.value
  233. // ? await ctx.service.pos.getPosData({ tid: ctx.tender.id }) : [];
  234. // const convert = new billsPosConvert(ctx);
  235. // convert.loadData(ledgerData, posData, []);
  236. // const result = await convert.convert();
  237. const scheduleLedgerMonthData = await ctx.service.scheduleLedgerMonth.getAllDataByCondition({ where: { tid: ctx.tender.id } });
  238. ctx.body = { err: 0, msg: '', data: { bills: ledgerData, slm: scheduleLedgerMonthData } };
  239. } catch (err) {
  240. this.log(err);
  241. ctx.body = { err: 1, msg: err.toString(), data: [] };
  242. }
  243. }
  244. /**
  245. * 台账选中提交(Ajax)
  246. *
  247. * @param ctx
  248. * @return {Promise<void>}
  249. */
  250. async saveLedger(ctx) {
  251. try {
  252. const data = JSON.parse(ctx.request.body.data);
  253. const result = await ctx.service.scheduleLedger.saveLedger(data);
  254. ctx.body = { err: 0, msg: '', data: result };
  255. } catch (err) {
  256. this.log(err);
  257. ctx.body = { err: 1, msg: err.toString(), data: [] };
  258. }
  259. }
  260. /**
  261. * 计划进度计算方式提交(Ajax)
  262. *
  263. * @param ctx
  264. * @return {Promise<void>}
  265. */
  266. async savePlan(ctx) {
  267. try {
  268. const data = JSON.parse(ctx.request.body.data);
  269. const responseData = {
  270. err: 0,
  271. msg: '',
  272. data: {},
  273. };
  274. switch (data.type) {
  275. case 'mode':
  276. responseData.data = await ctx.service.schedule.saveMode(data.postData);
  277. break;
  278. case 'addmonth':
  279. responseData.data = await ctx.service.scheduleMonth.add(data.postData);
  280. break;
  281. case 'delmonth':
  282. responseData.data = await ctx.service.scheduleMonth.del(data.postData);
  283. break;
  284. case 'ledger_edit':
  285. responseData.data = await ctx.service.scheduleLedgerMonth.save(data.postData);
  286. break;
  287. default: throw '参数有误';
  288. }
  289. ctx.body = responseData;
  290. } catch (err) {
  291. this.log(err);
  292. ctx.body = { err: 1, msg: err.toString(), data: null };
  293. }
  294. }
  295. /**
  296. * 计量进度金额模式计算方式提交(Ajax)
  297. *
  298. * @param ctx
  299. * @return {Promise<void>}
  300. */
  301. async saveStageTp(ctx) {
  302. try {
  303. const data = JSON.parse(ctx.request.body.data);
  304. const responseData = {
  305. err: 0,
  306. msg: '',
  307. data: {},
  308. };
  309. switch (data.type) {
  310. case 'add_stage':
  311. responseData.data = await ctx.service.scheduleStage.add(data.postData);
  312. break;
  313. case 'del_stage':
  314. responseData.data = await ctx.service.scheduleStage.del(data.postData);
  315. break;
  316. case 'reload_stage':
  317. responseData.data = await ctx.service.scheduleStage.changeOrder(data.postData);
  318. break;
  319. default: throw '参数有误';
  320. }
  321. ctx.body = responseData;
  322. } catch (err) {
  323. this.log(err);
  324. ctx.body = { err: 1, msg: err.toString(), data: null };
  325. }
  326. }
  327. /**
  328. * 计量进度工程量模式计算方式提交(Ajax)
  329. *
  330. * @param ctx
  331. * @return {Promise<void>}
  332. */
  333. async saveStageGcl(ctx) {
  334. try {
  335. const data = JSON.parse(ctx.request.body.data);
  336. const responseData = {
  337. err: 0,
  338. msg: '',
  339. data: {},
  340. };
  341. switch (data.type) {
  342. case 'add_stage':
  343. responseData.data = await ctx.service.scheduleMonth.addStageUsed(data.postData);
  344. break;
  345. case 'del_stage':
  346. responseData.data = await ctx.service.scheduleMonth.delStageUsed(data.postData);
  347. break;
  348. case 'ledger_edit':
  349. responseData.data = await ctx.service.scheduleLedgerMonth.saveSj(data.postData);
  350. break;
  351. default: throw '参数有误';
  352. }
  353. ctx.body = responseData;
  354. } catch (err) {
  355. this.log(err);
  356. ctx.body = { err: 1, msg: err.toString(), data: null };
  357. }
  358. }
  359. }
  360. return ScheduleController;
  361. };