stage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. module.exports = app => {
  14. class Stage extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.tableName = 'stage';
  24. }
  25. /**
  26. * 获取 最新一期 期计量
  27. * @param tenderId
  28. * @param includeUnCheck
  29. * @returns {Promise<*>}
  30. */
  31. async getLastestStage(tenderId, includeUnCheck = false) {
  32. this.initSqlBuilder();
  33. this.sqlBuilder.setAndWhere('tid', {
  34. value: tenderId,
  35. operate: '=',
  36. });
  37. if (!includeUnCheck) {
  38. this.sqlBuilder.setAndWhere('status', {
  39. value: auditConst.status.uncheck,
  40. operate: '!=',
  41. });
  42. }
  43. this.sqlBuilder.orderBy = [['order', 'desc']];
  44. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  45. const stage = await this.db.queryOne(sql, sqlParam);
  46. return stage;
  47. }
  48. /**
  49. * 获取 最新一期 审批完成的 期计量
  50. * @param tenderId
  51. * @returns {Promise<*>}
  52. */
  53. async getLastestCompleteStage(tenderId) {
  54. this.initSqlBuilder();
  55. this.sqlBuilder.setAndWhere('tid', {
  56. value: tenderId,
  57. operate: '=',
  58. });
  59. this.sqlBuilder.setAndWhere('status', {
  60. value: auditConst.status.checked,
  61. operate: '=',
  62. });
  63. this.sqlBuilder.orderBy = [['order', 'desc']];
  64. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  65. const stage = await this.db.queryOne(sql, sqlParam);
  66. return stage;
  67. }
  68. /**
  69. * 获取标段下的期列表(报表选择用,只需要一些key信息,不需要计算详细数据,也懒得一个个字段写了,用*来处理)
  70. * @param tenderId
  71. * @returns {Promise<void>}
  72. */
  73. async getValidStagesShort(tenderId) {
  74. console.log('tenderId: ' + 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) {
  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_detail: true,
  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. /**
  281. * 更新 check_detail 标识
  282. * @param {Integer}sid - 期id
  283. * @param {Boolean}check - 标记
  284. * @returns {Promise<void>}
  285. */
  286. async updateCheckDetailFlag(sid, check) {
  287. const result = await this.db.update(this.tableName, {id: sid, check_detail: check});
  288. return result.affectedRows === 1;
  289. }
  290. /**
  291. * 删除计量期
  292. *
  293. * @param {Number} id - 期Id
  294. * @returns {Promise<void>}
  295. */
  296. async deleteStage(id) {
  297. const transaction = await this.db.beginTransaction();
  298. try {
  299. await transaction.delete(this.tableName, { id });
  300. await transaction.delete(this.ctx.service.stageAudit.tableName, { sid: id });
  301. await transaction.delete(this.ctx.service.stageBills.tableName, { sid: id });
  302. await transaction.delete(this.ctx.service.stageChange.tableName, { sid: id });
  303. await transaction.delete(this.ctx.service.stagePos.tableName, { sid: id });
  304. await transaction.delete(this.ctx.service.stageDetail.tableName, { sid: id });
  305. await transaction.delete(this.ctx.service.stagePosFinal.tableName, { sid: id });
  306. await transaction.delete(this.ctx.service.stageBillsFinal.tableName, { sid: id });
  307. // 删除计量合同支付附件
  308. const payList = await this.ctx.service.stagePay.getAllDataByCondition({ where: { sid: id } });
  309. if (payList) {
  310. for (const pt of payList) {
  311. if (pt.attachment !== null && pt.attachment !== '') {
  312. const payAttList = JSON.parse(pt.attachment);
  313. for (const pat of payAttList) {
  314. if (fs.existsSync(path.join(this.app.baseDir, pat.filepath))) {
  315. await fs.unlinkSync(path.join(this.app.baseDir, pat.filepath));
  316. }
  317. }
  318. }
  319. }
  320. }
  321. await transaction.delete(this.ctx.service.stagePay.tableName, { sid: id });
  322. await transaction.delete(this.ctx.service.pay.tableName, { csid: id });
  323. // 删除计量附件文件
  324. const attList = await this.ctx.service.stageAtt.getAllDataByCondition({ where: { sid: id } });
  325. if (attList.length !== 0) {
  326. for (const att of attList) {
  327. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  328. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  329. }
  330. }
  331. }
  332. await transaction.delete(this.ctx.service.stageAtt.tableName, { sid: id });
  333. await transaction.commit();
  334. return true;
  335. } catch (err) {
  336. await transaction.rollback();
  337. throw err;
  338. }
  339. }
  340. }
  341. return Stage;
  342. };