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. 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. console.log(tid);
  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. console.log(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. };