stage.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const payConst = require('../const/deal_pay.js');
  11. const fs = require('fs');
  12. const path = require('path');
  13. const _ = require('lodash');
  14. module.exports = app => {
  15. class Stage extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'stage';
  25. }
  26. /**
  27. * 获取 最新一期 期计量
  28. * @param tenderId
  29. * @param includeUnCheck
  30. * @returns {Promise<*>}
  31. */
  32. async getLastestStage(tenderId, includeUnCheck = false) {
  33. this.initSqlBuilder();
  34. this.sqlBuilder.setAndWhere('tid', {
  35. value: tenderId,
  36. operate: '=',
  37. });
  38. if (!includeUnCheck) {
  39. this.sqlBuilder.setAndWhere('status', {
  40. value: auditConst.status.uncheck,
  41. operate: '!=',
  42. });
  43. }
  44. this.sqlBuilder.orderBy = [['order', 'desc']];
  45. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  46. const stage = await this.db.queryOne(sql, sqlParam);
  47. return stage;
  48. }
  49. /**
  50. * 获取 最新一期 审批完成的 期计量
  51. * @param tenderId
  52. * @returns {Promise<*>}
  53. */
  54. async getLastestCompleteStage(tenderId) {
  55. this.initSqlBuilder();
  56. this.sqlBuilder.setAndWhere('tid', {
  57. value: tenderId,
  58. operate: '=',
  59. });
  60. this.sqlBuilder.setAndWhere('status', {
  61. value: auditConst.status.checked,
  62. operate: '=',
  63. });
  64. this.sqlBuilder.orderBy = [['order', 'desc']];
  65. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  66. const stage = await this.db.queryOne(sql, sqlParam);
  67. return stage;
  68. }
  69. /**
  70. * 获取标段下的期列表(报表选择用,只需要一些key信息,不需要计算详细数据,也懒得一个个字段写了,用*来处理)
  71. * @param tenderId
  72. * @returns {Promise<void>}
  73. */
  74. async getValidStagesShort(tenderId) {
  75. const sql = 'select * from zh_stage where tid = ? order by zh_stage.order';
  76. const sqlParam = [tenderId];
  77. return await this.db.query(sql, sqlParam);
  78. }
  79. /**
  80. * 获取某一期信息(报表用)
  81. * @param stageId
  82. * @returns {Promise<void>}
  83. */
  84. async getStageById(stageId) {
  85. const sql = 'select * from zh_stage where id = ? order by zh_stage.order';
  86. const sqlParam = [stageId];
  87. return await this.db.query(sql, sqlParam);
  88. }
  89. async checkStageGatherData(stage) {
  90. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  91. if (stage.status !== auditConst.status.checked && stage.check_calc) {
  92. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  93. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : stage.user_id === this.ctx.session.sessionUser.accountId;
  94. stage.curTimes = stage.status === auditConst.status.checkNo ? stage.times - 1 : stage.times;
  95. stage.curOrder = curAuditor ? curAuditor.order : 0;
  96. if (isActive) {
  97. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  98. stage.contract_tp = tpData.contract_tp;
  99. stage.qc_tp = tpData.qc_tp;
  100. stage.yf_tp = await this.ctx.service.stagePay.getYfTotalPrice(stage);
  101. }
  102. }
  103. }
  104. /**
  105. * 获取标段下的全部计量期,按倒序
  106. * @param tenderId
  107. * @returns {Promise<void>}
  108. */
  109. async getValidStages(tenderId) {
  110. const stages = await this.db.select(this.tableName, {
  111. where: {tid: tenderId},
  112. orders: [['order', 'desc']],
  113. });
  114. for (const s of stages) {
  115. s.tp = this.ctx.helper.add(s.contract_tp, s.qc_tp);
  116. s.pre_tp = this.ctx.helper.add(s.pre_contract_tp, s.pre_qc_tp);
  117. s.end_tp = this.ctx.helper.add(s.pre_tp, s.tp);
  118. }
  119. if (stages.length !== 0) {
  120. const lastStage = stages[stages.length - 1];
  121. if (lastStage.status === auditConst.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  122. stages.splice(stages.length - 1, 1);
  123. }
  124. }
  125. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  126. if (stages.length > 0 && stages[0].status !== auditConst.status.checked) {
  127. const stage = stages[0];
  128. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  129. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : stage.user_id === this.ctx.session.sessionUser.accountId;
  130. if (isActive) {
  131. stage.curTimes = stage.times;
  132. stage.curOrder = curAuditor ? curAuditor.order : 0;
  133. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  134. stage.contract_tp = tpData.contract_tp;
  135. stage.qc_tp = tpData.qc_tp;
  136. stage.yf_tp = await this.ctx.service.stagePay.getYfTotalPrice(stage);
  137. stage.tp = this.ctx.helper.add(stage.contract_tp, stage.qc_tp);
  138. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  139. }
  140. }
  141. return stages;
  142. }
  143. /**
  144. *
  145. * @param tenderId - 标段id
  146. * @param date - 计量年月
  147. * @param period - 开始-截止日期
  148. * @returns {Promise<void>}
  149. */
  150. async addStage(tenderId, date, period) {
  151. const stages = await this.getAllDataByCondition({
  152. where: {tid: tenderId},
  153. order: ['order'],
  154. });
  155. const preStage = stages[stages.length - 1];
  156. if (stages.length > 0 && stages[stages.length - 1].status !== auditConst.status.checked) {
  157. throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
  158. };
  159. const order = stages.length + 1;
  160. const newStage = {
  161. sid: this.uuid.v4(),
  162. tid: tenderId,
  163. order: order,
  164. in_time: new Date(),
  165. s_time: date,
  166. period: period,
  167. times: 1,
  168. status: auditConst.status.uncheck,
  169. user_id: this.ctx.session.sessionUser.accountId,
  170. check_calc: false,
  171. };
  172. if (preStage) {
  173. newStage.im_type = preStage.im_type;
  174. newStage.im_pre = preStage.im_pre;
  175. newStage.im_gather = preStage.im_gather;
  176. newStage.im_gather_node = preStage.im_gather_node;
  177. newStage.pre_contract_tp = this.ctx.helper.add(preStage.pre_contract_tp, preStage.contract_tp);
  178. newStage.pre_qc_tp = this.ctx.helper.add(preStage.pre_qc_tp, preStage.qc_tp);
  179. newStage.pre_yf_tp = this.ctx.helper.add(preStage.pre_yf_tp, preStage.yf_tp);
  180. }
  181. const transaction = await this.db.beginTransaction();
  182. try {
  183. // 新增期记录
  184. const result = await transaction.insert(this.tableName, newStage);
  185. if (result.affectedRows === 1) {
  186. newStage.id = result.insertId;
  187. } else {
  188. throw '新增期数据失败';
  189. }
  190. // 存在上一期时,复制上一期审批流程
  191. if (preStage) {
  192. const auditResult = await this.ctx.service.stageAudit.copyPreStageAuditors(transaction, preStage, newStage);
  193. if (!auditResult) {
  194. throw '复制上一期审批流程失败';
  195. }
  196. }
  197. // 新增期合同支付数据
  198. const dealResult = await this.ctx.service.stagePay.addInitialStageData(newStage, transaction);
  199. if (!dealResult) {
  200. throw '新增期合同支付数据失败';
  201. }
  202. await transaction.commit();
  203. return newStage;
  204. } catch (err) {
  205. await transaction.rollback();
  206. throw err;
  207. }
  208. }
  209. /**
  210. * 编辑计量期
  211. *
  212. * @param {Number} tenderId - 标段Id
  213. * @param {Number} order - 第N期
  214. * @param {String} date - 计量年月
  215. * @param {String} period - 开始-截止时间
  216. * @returns {Promise<void>}
  217. */
  218. async saveStage(tenderId, order, date, period) {
  219. await this.db.update(this.tableName, {
  220. s_time: date,
  221. period: period,
  222. }, { where: { tid: tenderId, order: order } });
  223. }
  224. /**
  225. * 设置 中间计量 生成规则,并生成数据
  226. * @param {Number} tenderId - 标段id
  227. * @param {Number} order - 期序号
  228. * @param {Number} data - 中间计量生成规则
  229. * @returns {Promise<void>}
  230. */
  231. async buildDetailData(tenderId, order, data) {
  232. const conn = await this.db.beginTransaction();
  233. try {
  234. await conn.update(this.tableName, { im_type: data.im_type, im_pre: data.im_pre }, { where: { tid: tenderId, order: order } });
  235. // to do 生成中间计量数据
  236. await conn.commit();
  237. } catch (err) {
  238. await conn.rollback();
  239. throw err;
  240. }
  241. }
  242. /**
  243. * 获取 当期的 计算基数
  244. * @returns {Promise<any>}
  245. */
  246. async getStagePayCalcBase(stage, tenderInfo) {
  247. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  248. const param = tenderInfo.deal_param;
  249. for (const cb of calcBase) {
  250. switch (cb.code) {
  251. case 'htj':
  252. cb.value = param.contractPrice;
  253. break;
  254. case 'zlje':
  255. cb.value = param.zanLiePrice;
  256. break;
  257. case 'htjszl':
  258. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  259. break;
  260. case 'kgyfk':
  261. cb.value = param.startAdvance;
  262. break;
  263. case 'clyfk':
  264. cb.value = param.materialAdvance;
  265. break;
  266. case 'bqwc':
  267. const sum = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  268. cb.value = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  269. break;
  270. case 'ybbqwc':
  271. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(stage, '^1[0-9]{2}-');
  272. cb.value = this.ctx.helper.add(sumGcl.contract_tp, sumGcl.qc_tp);
  273. break;
  274. default:
  275. cb.value = 0;
  276. }
  277. }
  278. return calcBase;
  279. }
  280. async updateCheckCalcFlag(sid, check) {
  281. const result = await this.db.update(this.tableName, {id: sid, check_calc: check});
  282. return result.affectedRows === 1;
  283. }
  284. /**
  285. * 删除计量期
  286. *
  287. * @param {Number} id - 期Id
  288. * @returns {Promise<void>}
  289. */
  290. async deleteStage(id) {
  291. const transaction = await this.db.beginTransaction();
  292. try {
  293. await transaction.delete(this.tableName, { id });
  294. await transaction.delete(this.ctx.service.stageAudit.tableName, { sid: id });
  295. await transaction.delete(this.ctx.service.stageBills.tableName, { sid: id });
  296. await transaction.delete(this.ctx.service.stageChange.tableName, { sid: id });
  297. await transaction.delete(this.ctx.service.stagePos.tableName, { sid: id });
  298. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: id });
  299. await transaction.delete(this.ctx.service.stagePosFinal.tableName, { sid: id });
  300. await transaction.delete(this.ctx.service.stageBillsFinal.tableName, { sid: id });
  301. // 删除计量合同支付附件
  302. const payList = await this.ctx.service.stagePay.getAllDataByCondition({ where: { sid: id } });
  303. if (payList) {
  304. for (const pt of payList) {
  305. if (pt.attachment !== null && pt.attachment !== '') {
  306. const payAttList = JSON.parse(pt.attachment);
  307. for (const pat of payAttList) {
  308. if (fs.existsSync(path.join(this.app.baseDir, pat.filepath))) {
  309. await fs.unlinkSync(path.join(this.app.baseDir, pat.filepath));
  310. }
  311. }
  312. }
  313. }
  314. }
  315. await transaction.delete(this.ctx.service.stagePay.tableName, { sid: id });
  316. await transaction.delete(this.ctx.service.pay.tableName, { csid: id });
  317. // 删除计量附件文件
  318. const attList = await this.ctx.service.stageAtt.getAllDataByCondition({ where: { sid: id } });
  319. if (attList.length !== 0) {
  320. for (const att of attList) {
  321. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  322. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  323. }
  324. }
  325. }
  326. await transaction.delete(this.ctx.service.stageAtt.tableName, { sid: id });
  327. await transaction.commit();
  328. return true;
  329. } catch (err) {
  330. await transaction.rollback();
  331. throw err;
  332. }
  333. }
  334. /**
  335. * 获取 多期的 计算基数 -(材料调差调用)
  336. * @returns {Promise<any>}
  337. */
  338. async getMaterialCalcBase(stage_list, tenderInfo) {
  339. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  340. const param = tenderInfo.deal_param;
  341. for (const cb of calcBase) {
  342. switch (cb.code) {
  343. case 'htj':
  344. cb.value = param.contractPrice;
  345. break;
  346. case 'zlje':
  347. cb.value = param.zanLiePrice;
  348. break;
  349. case 'htjszl':
  350. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  351. break;
  352. case 'kgyfk':
  353. cb.value = param.startAdvance;
  354. break;
  355. case 'clyfk':
  356. cb.value = param.materialAdvance;
  357. break;
  358. case 'bqwc':
  359. const sum = await this.ctx.service.stageBills.getSumTotalPriceByMaterial(stage_list);
  360. cb.value = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  361. break;
  362. case 'ybbqwc':
  363. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGclByMaterial(stage_list, '^1[0-9]{2}-');
  364. cb.value = this.ctx.helper.add(sumGcl.contract_tp, sumGcl.qc_tp);
  365. break;
  366. default:
  367. cb.value = 0;
  368. }
  369. }
  370. return calcBase;
  371. }
  372. /**
  373. * 获取必要的stage信息调用curTimes, curOrder, id , times, curAuditor(材料调差)
  374. * @param stage_id_list
  375. * @returns {Promise<void>}
  376. */
  377. async getStageMsgByStageId(stage_id_list) {
  378. const list = [];
  379. stage_id_list = stage_id_list.split(',');
  380. for (const sid of stage_id_list) {
  381. const stage = await this.getDataById(sid);
  382. stage.auditors = await this.service.stageAudit.getAuditors(stage.id, stage.times);
  383. stage.curAuditor = await this.service.stageAudit.getCurAuditor(stage.id, stage.times);
  384. stage.curOrder = _.max(_.map(stage.auditors, 'order'));
  385. stage.curTimes = stage.times;
  386. list.push(stage);
  387. }
  388. return list;
  389. }
  390. }
  391. return Stage;
  392. };