spss_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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.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. 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. const data = await this._getTzData(tid, true);
  81. const stage = await this._checkStage(tid, sorder);
  82. const bills = await this.ctx.service.stageBills.getAuditorStageData(tid, stage.id, stage.curTimes, stage.curOrder);
  83. const pos = await this.ctx.service.stagePos.getAuditorStageData(tid, stage.id, stage.curTimes, stage.curOrder);
  84. data.stage = {
  85. sid: stage.id, sorder: stage.order, curTimes: stage.curTimes, curOrder: stage.curOrder,
  86. bills: bills, pos: pos
  87. };
  88. return data;
  89. }
  90. /**
  91. * 台账 对比 页面
  92. *
  93. * @param {Object} ctx - egg全局变量
  94. * @return {void}
  95. */
  96. async compareTz(ctx) {
  97. try {
  98. const renderData = {
  99. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.compare.tz)
  100. };
  101. await this.layout('spss/compare_tz.ejs', renderData, 'spss/compare_tz_modal.ejs');
  102. } catch (err) {
  103. ctx.helper.log(err);
  104. }
  105. }
  106. /**
  107. * 获取 台账 对比 数据(Ajax)
  108. * @param ctx
  109. * @returns {Promise<void>}
  110. */
  111. async loadCompareTz(ctx) {
  112. try {
  113. const data = JSON.parse(ctx.request.body.data);
  114. const responseData = {err: 0, msg: '', data: {}};
  115. // const tender1 = await ctx.service.tender.getTender(data.tid1);
  116. // responseData.data.tender1 = {
  117. // name: tender1.name,
  118. // bills: await ctx.service.ledger.getData(data.tid1),
  119. // pos: tender1.measure_type === measureType.tz.value
  120. // ? await ctx.service.pos.getPosData({tid: data.tid1}) : []
  121. // };
  122. // const tender2 = await ctx.service.tender.getTender(data.tid2);
  123. // responseData.data.tender2 = {
  124. // name: tender2.name,
  125. // bills: await ctx.service.ledger.getData(data.tid2),
  126. // pos: tender2.measure_type === measureType.tz.value
  127. // ? await ctx.service.pos.getPosData({tid: data.tid2}) : []
  128. // };
  129. responseData.data.tender1 = await this._getTzData(data.tid1);
  130. responseData.data.tender2 = await this._getTzData(data.tid2);
  131. ctx.body = responseData;
  132. } catch (err) {
  133. ctx.helper.log(err);
  134. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  135. }
  136. }
  137. /**
  138. * 期计量 对比 页面
  139. *
  140. * @param {Object} ctx - egg全局变量
  141. * @return {void}
  142. */
  143. async compareStage(ctx) {
  144. try {
  145. const renderData = {
  146. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.compare.stage)
  147. };
  148. await this.layout('spss/compare_tz.ejs', renderData, 'spss/compare_stage_modal.ejs');
  149. } catch (err) {
  150. ctx.helper.log(err);
  151. }
  152. }
  153. /**
  154. * 获取 期计量 对比 数据(Ajax)
  155. * @param ctx
  156. * @returns {Promise<void>}
  157. */
  158. async loadCompareStage(ctx) {
  159. try {
  160. const data = JSON.parse(ctx.request.body.data);
  161. const responseData = {err: 0, msg: '', data: {}};
  162. responseData.data.tender1 = await this._getStageData(data.tid1, data.sorder1);
  163. responseData.data.tender2 = await this._getStageData(data.tid2, data.sorder2);
  164. ctx.body = responseData;
  165. } catch (err) {
  166. ctx.helper.log(err);
  167. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  168. }
  169. }
  170. /**
  171. * 台账 汇总 页面
  172. *
  173. * @param {Object} ctx - egg全局变量
  174. * @return {void}
  175. */
  176. async gatherTz(ctx) {
  177. try {
  178. const renderData = {
  179. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.gather.tz)
  180. };
  181. await this.layout('spss/compare_tz.ejs', renderData, 'spss/gather_tz_modal.ejs');
  182. } catch (err) {
  183. ctx.helper.log(err);
  184. }
  185. }
  186. /**
  187. * 获取 台账 汇总 数据(Ajax)
  188. * @param ctx
  189. * @returns {Promise<void>}
  190. */
  191. async loadGatherTz(ctx) {
  192. try {
  193. const data = JSON.parse(ctx.request.body.data);
  194. const responseData = {err: 0, msg: '', data: []};
  195. for (const t of data.tenders) {
  196. responseData.data.push(await this._getTzData(t.id));
  197. }
  198. ctx.body = responseData;
  199. } catch (err) {
  200. ctx.helper.log(err);
  201. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  202. }
  203. }
  204. /**
  205. * 期计量 汇总 页面
  206. *
  207. * @param {Object} ctx - egg全局变量
  208. * @return {void}
  209. */
  210. async gatherStage(ctx) {
  211. try {
  212. const renderData = {
  213. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.gather.stage)
  214. };
  215. await this.layout('spss/compare_tz.ejs', renderData, 'spss/gather_stage_modal.ejs');
  216. } catch (err) {
  217. ctx.helper.log(err);
  218. }
  219. }
  220. /**
  221. * 获取 期计量 汇总 数据(Ajax)
  222. * @param ctx
  223. * @returns {Promise<void>}
  224. */
  225. async loadGatherStage(ctx) {
  226. try {
  227. const data = JSON.parse(ctx.request.body.data);
  228. const responseData = {err: 0, msg: '', data: []};
  229. for (const t of data.tenders) {
  230. responseData.data.push(await this._getStageData(t.id, t.sorder));
  231. }
  232. ctx.body = responseData;
  233. } catch (err) {
  234. ctx.helper.log(err);
  235. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  236. }
  237. }
  238. /**
  239. * 检测台账 页面
  240. *
  241. * @param {Object} ctx - egg全局变量
  242. * @return {void}
  243. */
  244. async checkTz(ctx) {
  245. try {
  246. const renderData = {
  247. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.tools.checkTz)
  248. };
  249. await this.layout('spss/check_tz.ejs', renderData);
  250. } catch (err) {
  251. ctx.helper.log(err);
  252. }
  253. }
  254. /**
  255. * 获取 期计量 汇总 数据(Ajax)
  256. * @param ctx
  257. * @returns {Promise<void>}
  258. */
  259. async loadBaseData(ctx) {
  260. try {
  261. const data = JSON.parse(ctx.request.body.data);
  262. const responseData = {err: 0, msg: '', data: []};
  263. responseData.data = await this._getTzData(data.id);
  264. ctx.body = responseData;
  265. } catch (err) {
  266. ctx.helper.log(err);
  267. ctx.body = this.ajaxErrorBody(err, '查询数据错误');
  268. }
  269. }
  270. }
  271. return SpssController;
  272. };