spss_controller.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. 'use strict';
  2. /**
  3. * 标段对比 控制器
  4. *
  5. * @author Mai
  6. * @date 2020/2/21
  7. * @version
  8. */
  9. const measureType = require('../const/tender').measureType;
  10. const status = require('../const/audit').stage.status;
  11. module.exports = app => {
  12. class SpssController extends app.BaseController {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局context
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. ctx.showProject = true;
  22. }
  23. async _getTzData(tid, includePos = false) {
  24. console.log(tid);
  25. const tender = await this.ctx.service.tender.getTender(tid);
  26. if (!tender || tender.project_id !== this.ctx.session.sessionProject.id) {
  27. throw '不存在该标段';
  28. }
  29. const bills = await this.ctx.service.ledger.getData(tid);
  30. const pos = tender.measure_type === measureType.tz.value || includePos
  31. ? await this.ctx.service.pos.getPosData({tid: tid})
  32. : [];
  33. return { id: tid, name: tender.name, bills: bills, pos: pos };
  34. }
  35. async _checkStage(tid, sorder) {
  36. const _ = this.ctx.helper._;
  37. const stage = await this.service.stage.getDataByCondition({ tid: tid, order: sorder });
  38. if (!stage) throw '期数据错误';
  39. // 读取原报、审核人数据
  40. stage.auditors = await this.service.stageAudit.getAuditors(stage.id, stage.times);
  41. stage.curAuditor = await this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  42. const accountId = this.ctx.session.sessionUser.accountId, auditorIds = _.map(stage.auditors, 'aid'), shareIds = [];
  43. const permission = this.ctx.session.sessionUser.permission;
  44. if (accountId === stage.user_id) { // 原报
  45. stage.curTimes = stage.times;
  46. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  47. stage.curOrder = 0;
  48. } else if (stage.status === status.checked) {
  49. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  50. } else {
  51. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  52. }
  53. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  54. if (stage.status === status.uncheck) {
  55. throw '您无权查看该数据';
  56. }
  57. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  58. if (stage.status === status.checked) {
  59. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  60. } else if (stage.status === status.checkNo) {
  61. const audit = await this.service.stageAudit.getDataByCondition({
  62. sid: stage.id, times: stage.times - 1, status: status.checkNo
  63. });
  64. stage.curOrder = audit.order;
  65. } else {
  66. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  67. }
  68. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  69. if (stage.status === status.uncheck) {
  70. throw '您无权查看该数据';
  71. }
  72. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  73. stage.curOrder = stage.status === status.checked ? _.max(_.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  74. } else { // 其他不可见
  75. throw '您无权查看该数据';
  76. }
  77. return stage;
  78. }
  79. async _getStageData(tid, sorder) {
  80. console.log(tid);
  81. const data = await this._getTzData(tid, true);
  82. const stage = await this._checkStage(tid, sorder);
  83. const bills = await this.ctx.service.stageBills.getAuditorStageData(tid, stage.id, stage.curTimes, stage.curOrder);
  84. const pos = await this.ctx.service.stagePos.getAuditorStageData(tid, stage.id, stage.curTimes, stage.curOrder);
  85. data.stage = {
  86. sid: stage.id, sorder: stage.order, curTimes: stage.curTimes, curOrder: stage.curOrder,
  87. bills: bills, pos: pos
  88. };
  89. return data;
  90. }
  91. /**
  92. * 台账 对比 页面
  93. *
  94. * @param {Object} ctx - egg全局变量
  95. * @return {void}
  96. */
  97. async compareTz(ctx) {
  98. try {
  99. const renderData = {
  100. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.compare.tz)
  101. };
  102. await this.layout('spss/compare_tz.ejs', renderData, 'spss/compare_tz_modal.ejs');
  103. } catch (err) {
  104. ctx.helper.log(err);
  105. }
  106. }
  107. /**
  108. * 获取 台账 对比 数据(Ajax)
  109. * @param ctx
  110. * @returns {Promise<void>}
  111. */
  112. async loadCompareTz(ctx) {
  113. try {
  114. const data = JSON.parse(ctx.request.body.data);
  115. const responseData = {err: 0, msg: '', data: {}};
  116. // const tender1 = await ctx.service.tender.getTender(data.tid1);
  117. // responseData.data.tender1 = {
  118. // name: tender1.name,
  119. // bills: await ctx.service.ledger.getData(data.tid1),
  120. // pos: tender1.measure_type === measureType.tz.value
  121. // ? await ctx.service.pos.getPosData({tid: data.tid1}) : []
  122. // };
  123. // const tender2 = await ctx.service.tender.getTender(data.tid2);
  124. // responseData.data.tender2 = {
  125. // name: tender2.name,
  126. // bills: await ctx.service.ledger.getData(data.tid2),
  127. // pos: tender2.measure_type === measureType.tz.value
  128. // ? await ctx.service.pos.getPosData({tid: data.tid2}) : []
  129. // };
  130. responseData.data.tender1 = await this._getTzData(data.tid1);
  131. responseData.data.tender2 = await this._getTzData(data.tid2);
  132. ctx.body = responseData;
  133. } catch (err) {
  134. ctx.helper.log(err);
  135. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  136. }
  137. }
  138. /**
  139. * 期计量 对比 页面
  140. *
  141. * @param {Object} ctx - egg全局变量
  142. * @return {void}
  143. */
  144. async compareStage(ctx) {
  145. try {
  146. const renderData = {
  147. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.compare.stage)
  148. };
  149. await this.layout('spss/compare_tz.ejs', renderData, 'spss/compare_stage_modal.ejs');
  150. } catch (err) {
  151. ctx.helper.log(err);
  152. }
  153. }
  154. /**
  155. * 获取 期计量 对比 数据(Ajax)
  156. * @param ctx
  157. * @returns {Promise<void>}
  158. */
  159. async loadCompareStage(ctx) {
  160. try {
  161. const data = JSON.parse(ctx.request.body.data);
  162. console.log(data);
  163. const responseData = {err: 0, msg: '', data: {}};
  164. responseData.data.tender1 = await this._getStageData(data.tid1, data.sorder1);
  165. responseData.data.tender2 = await this._getStageData(data.tid2, data.sorder2);
  166. ctx.body = responseData;
  167. } catch (err) {
  168. ctx.helper.log(err);
  169. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  170. }
  171. }
  172. /**
  173. * 台账 汇总 页面
  174. *
  175. * @param {Object} ctx - egg全局变量
  176. * @return {void}
  177. */
  178. async gatherTz(ctx) {
  179. try {
  180. const renderData = {
  181. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.gather.tz)
  182. };
  183. await this.layout('spss/compare_tz.ejs', renderData, 'spss/gather_tz_modal.ejs');
  184. } catch (err) {
  185. ctx.helper.log(err);
  186. }
  187. }
  188. /**
  189. * 获取 台账 汇总 数据(Ajax)
  190. * @param ctx
  191. * @returns {Promise<void>}
  192. */
  193. async loadGatherTz(ctx) {
  194. try {
  195. const data = JSON.parse(ctx.request.body.data);
  196. const responseData = {err: 0, msg: '', data: []};
  197. for (const t of data.tenders) {
  198. responseData.data.push(await this._getTzData(t.id));
  199. }
  200. ctx.body = responseData;
  201. } catch (err) {
  202. ctx.helper.log(err);
  203. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  204. }
  205. }
  206. /**
  207. * 期计量 汇总 页面
  208. *
  209. * @param {Object} ctx - egg全局变量
  210. * @return {void}
  211. */
  212. async gatherStage(ctx) {
  213. try {
  214. const renderData = {
  215. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.gather.stage)
  216. };
  217. await this.layout('spss/compare_tz.ejs', renderData, 'spss/gather_stage_modal.ejs');
  218. } catch (err) {
  219. ctx.helper.log(err);
  220. }
  221. }
  222. /**
  223. * 获取 期计量 汇总 数据(Ajax)
  224. * @param ctx
  225. * @returns {Promise<void>}
  226. */
  227. async loadGatherStage(ctx) {
  228. try {
  229. const data = JSON.parse(ctx.request.body.data);
  230. const responseData = {err: 0, msg: '', data: []};
  231. for (const t of data.tenders) {
  232. responseData.data.push(await this._getStageData(t.id, t.sorder));
  233. }
  234. ctx.body = responseData;
  235. } catch (err) {
  236. ctx.helper.log(err);
  237. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  238. }
  239. }
  240. }
  241. return SpssController;
  242. };