schedule_controller.js 18 KB

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