spss_controller.js 11 KB

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