spss_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. this.ctx.query_tender = tender;
  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. this.ctx.query_tender = null;
  34. return { id: tid, name: tender.name, bills: bills, pos: pos };
  35. }
  36. async _checkStage(tid, sorder) {
  37. const _ = this.ctx.helper._;
  38. const stage = await this.service.stage.getDataByCondition({ tid: tid, order: sorder });
  39. if (!stage) throw '期数据错误';
  40. // 读取原报、审核人数据
  41. stage.auditors = await this.service.stageAudit.getAuditors(stage.id, stage.times);
  42. stage.curAuditor = await this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  43. const accountId = this.ctx.session.sessionUser.accountId, auditorIds = _.map(stage.auditors, 'aid'), shareIds = [];
  44. const permission = this.ctx.session.sessionUser.permission;
  45. if (accountId === stage.user_id) { // 原报
  46. stage.curTimes = stage.times;
  47. if (stage.status === status.uncheck || stage.status === status.checkNo) {
  48. stage.curOrder = 0;
  49. } else if (stage.status === status.checked) {
  50. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  51. } else {
  52. stage.curOrder = stage.curAuditor.aid === accountId ? stage.curAuditor.order : stage.curAuditor.order - 1;
  53. }
  54. } else if (auditorIds.indexOf(accountId) !== -1) { // 审批人
  55. if (stage.status === status.uncheck) {
  56. throw '您无权查看该数据';
  57. }
  58. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  59. if (stage.status === status.checked) {
  60. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  61. } else if (stage.status === status.checkNo) {
  62. const audit = await this.service.stageAudit.getDataByCondition({
  63. sid: stage.id, times: stage.times - 1, status: status.checkNo
  64. });
  65. stage.curOrder = audit.order;
  66. } else {
  67. stage.curOrder = accountId === stage.curAuditor.aid ? stage.curAuditor.order : stage.curAuditor.order - 1;
  68. }
  69. } else if (shareIds.indexOf(accountId) !== -1 || (permission !== null && permission.tender !== undefined && permission.tender.indexOf('2') !== -1)) { // 分享人
  70. if (stage.status === status.uncheck) {
  71. throw '您无权查看该数据';
  72. }
  73. stage.curTimes = stage.status === status.checkNo ? stage.times - 1 : stage.times;
  74. stage.curOrder = stage.status === status.checked ? _.max(_.map(stage.auditors, 'order')) : stage.curAuditor.order - 1;
  75. } else { // 其他不可见
  76. throw '您无权查看该数据';
  77. }
  78. return stage;
  79. }
  80. async _getStageData(tid, sorder) {
  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. const responseData = {err: 0, msg: '', data: {}};
  163. responseData.data.tender1 = await this._getStageData(data.tid1, data.sorder1);
  164. responseData.data.tender2 = await this._getStageData(data.tid2, data.sorder2);
  165. ctx.body = responseData;
  166. } catch (err) {
  167. ctx.helper.log(err);
  168. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  169. }
  170. }
  171. /**
  172. * 台账 汇总 页面
  173. *
  174. * @param {Object} ctx - egg全局变量
  175. * @return {void}
  176. */
  177. async gatherTz(ctx) {
  178. try {
  179. const renderData = {
  180. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.gather.tz)
  181. };
  182. await this.layout('spss/compare_tz.ejs', renderData, 'spss/gather_tz_modal.ejs');
  183. } catch (err) {
  184. ctx.helper.log(err);
  185. }
  186. }
  187. /**
  188. * 获取 台账 汇总 数据(Ajax)
  189. * @param ctx
  190. * @returns {Promise<void>}
  191. */
  192. async loadGatherTz(ctx) {
  193. try {
  194. const data = JSON.parse(ctx.request.body.data);
  195. const responseData = {err: 0, msg: '', data: []};
  196. for (const t of data.tenders) {
  197. responseData.data.push(await this._getTzData(t.id));
  198. }
  199. ctx.body = responseData;
  200. } catch (err) {
  201. ctx.helper.log(err);
  202. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  203. }
  204. }
  205. /**
  206. * 期计量 汇总 页面
  207. *
  208. * @param {Object} ctx - egg全局变量
  209. * @return {void}
  210. */
  211. async gatherStage(ctx) {
  212. try {
  213. const renderData = {
  214. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.gather.stage)
  215. };
  216. await this.layout('spss/compare_tz.ejs', renderData, 'spss/gather_stage_modal.ejs');
  217. } catch (err) {
  218. ctx.helper.log(err);
  219. }
  220. }
  221. /**
  222. * 获取 期计量 汇总 数据(Ajax)
  223. * @param ctx
  224. * @returns {Promise<void>}
  225. */
  226. async loadGatherStage(ctx) {
  227. try {
  228. const data = JSON.parse(ctx.request.body.data);
  229. const responseData = {err: 0, msg: '', data: []};
  230. for (const t of data.tenders) {
  231. responseData.data.push(await this._getStageData(t.id, t.sorder));
  232. }
  233. ctx.body = responseData;
  234. } catch (err) {
  235. ctx.helper.log(err);
  236. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  237. }
  238. }
  239. /**
  240. * 检测台账 页面
  241. *
  242. * @param {Object} ctx - egg全局变量
  243. * @return {void}
  244. */
  245. async checkTz(ctx) {
  246. try {
  247. const renderData = {
  248. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.tools.checkTz)
  249. };
  250. await this.layout('spss/check_tz.ejs', renderData);
  251. } catch (err) {
  252. ctx.helper.log(err);
  253. }
  254. }
  255. /**
  256. * 获取 期计量 汇总 数据(Ajax)
  257. * @param ctx
  258. * @returns {Promise<void>}
  259. */
  260. async loadBaseData(ctx) {
  261. try {
  262. const data = JSON.parse(ctx.request.body.data);
  263. const responseData = {err: 0, msg: '', data: []};
  264. responseData.data = await this._getTzData(data.id);
  265. ctx.body = responseData;
  266. } catch (err) {
  267. ctx.helper.log(err);
  268. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  269. }
  270. }
  271. }
  272. return SpssController;
  273. };