stage_pay.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const timesLen = require('../const/audit').stage.timesLen;
  10. const payConst = require('../const/deal_pay');
  11. module.exports = app => {
  12. class StagePay extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'stage_pay';
  22. }
  23. /**
  24. * 查询 某期 某轮审批 某审核人数据
  25. *
  26. * @param {Number} pid - 合同支付id
  27. * @param {Number} sid - 期id
  28. * @param {Number} times - 期第几轮审批
  29. * @param {Number} order - 审核人顺序
  30. * @param {Number|Array|Null} pid - 部位明细id - 为空则查询全部
  31. * @returns {Promise<*>}
  32. */
  33. async getAuditorStagePay(pid, sid, times, order) {
  34. const pidSql = pid instanceof Array ? ' And SP.`pid` in (' + pid.join(',') + ')' : 'And SP.`pid` = ' + pid;
  35. const sql = 'SELECT SP.*, P.`csorder`, P.`cstimes`, P.`csaorder`, P.`order`, P.uid, P.minus, P.ptype, P.sprice, P.sexpr, P.rprice, P.rexpr, P.is_yf, P.dl_type, P.dl_count, P.dl_tp_type, P.dl_tp ' +
  36. ' FROM ?? As SP, ?? As P ' +
  37. ' WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? AND SP.`pid` = P.`id` AND P.`valid`' + pidSql +
  38. ' ORDER BY P.`order`';
  39. const sqlParam = [this.tableName, this.ctx.service.pay.tableName, sid, times, order, pid];
  40. if (pid instanceof Array) {
  41. return await this.db.query(sql, sqlParam);
  42. } else {
  43. return await this.db.queryOne(sql, sqlParam);
  44. }
  45. }
  46. /**
  47. * 查询 某期 某轮审批 某审核人数据
  48. * @param {Number} sid - 期id
  49. * @param {Number} times - 期第几轮审批
  50. * @param {Number} order - 审核人顺序
  51. * @param {Number|Array|Null} pid - 部位明细id - 为空则查询全部
  52. * @returns {Promise<*>}
  53. */
  54. async getAuditorStageData(sid, times, order) {
  55. const sql = 'SELECT SP.*,' +
  56. ' P.`csorder`, P.`cstimes`, P.`csaorder`, P.`order`, P.uid, P.minus, P.ptype, P.sprice, P.sexpr, P.rprice, P.rexpr, P.is_yf, P.dl_type, P.dl_count, P.dl_tp_type, P.dl_tp, P.name as pName ' +
  57. ' FROM ?? As SP, ?? As P ' +
  58. ' WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? AND SP.`pid` = P.`id` AND P.`valid`' +
  59. ' ORDER BY P.`order`';
  60. const sqlParam = [this.tableName, this.ctx.service.pay.tableName, sid, times, order];
  61. return await this.db.query(sql, sqlParam);
  62. }
  63. /**
  64. * 新增期时,初始化 期--合同支付 数据
  65. * @param {Object} stage - 新增的期
  66. * @param transaction - 事务
  67. * @returns {Promise<void>}
  68. */
  69. async addInitialStageData(stage, transaction) {
  70. if (!stage) {
  71. throw '初始化期合同支付数据失败';
  72. }
  73. let pays = await this.ctx.service.pay.getAllDataByCondition({where: { tid: this.ctx.tender.id } });
  74. // 兼容旧项目,无初始化合同支付数据
  75. if (!pays || pays.length === 0) {
  76. const result = await this.ctx.service.pay.addDefaultPayData(this.ctx.tender.id, transaction);
  77. if (!result) {
  78. throw '初始化合同支付数据失败';
  79. }
  80. pays = await transaction.select(this.ctx.service.pay.tableName, {where: { tid: this.ctx.tender.id } });
  81. }
  82. const stagePays = [];
  83. for (const p of pays) {
  84. stagePays.push({
  85. tid: p.tid, sid: stage.id, pid: p.id,
  86. stimes: stage.times, sorder: 0, expr: p.expr, name: p.name,
  87. });
  88. }
  89. // 获取截止上期数据
  90. if (stage.order > 1) {
  91. const preStage = await this.ctx.service.stage.getDataByCondition({tid: stage.tid, order: stage.order - 1});
  92. if (!preStage) {
  93. throw '标段数据有误';
  94. }
  95. const prePays = await this.getStageLastestPays(preStage.id);
  96. for (const pp of prePays) {
  97. const sp = this._.find(stagePays, {pid: pp.pid});
  98. sp.name = pp.name;
  99. sp.expr = pp.expr;
  100. sp.pre_tp = pp.end_tp;
  101. sp.pause = pp.pause;
  102. sp.pre_used = pp.pre_used || !this.ctx.helper.checkZero(pp.tp);
  103. sp.pre_finish = pp.rprice ? pp.end_tp === pp.rprice : false;
  104. }
  105. }
  106. let result;
  107. if (transaction) {
  108. result = await transaction.insert(this.tableName, stagePays);
  109. } else {
  110. result = await this.db.insert(this.tableName, stagePays);
  111. }
  112. return result.affectedRows === pays.length;
  113. }
  114. async getStagePay(stage, pid) {
  115. return await this.getAuditorStagePay(pid, stage.id, stage.curTimes, stage.curOrder);
  116. }
  117. /**
  118. * 获取某期合同支付数据
  119. *
  120. * @param {Object} stage - 期数据(通过StageController._getStageData()获取)
  121. * @returns {Promise<*>}
  122. */
  123. async getStagePays(stage) {
  124. return await this.getAuditorStageData(stage.id, stage.curTimes, stage.curOrder);
  125. }
  126. /**
  127. * 获取某期最后数据(不限制该期是否已经审批通过)
  128. *
  129. * @param {Number} sid - 期id
  130. * @returns {Promise<*>}
  131. */
  132. async getStageLastestPays(sid) {
  133. const sql = 'SELECT SP.*, P.`order`, P.uid, P.minus, P.ptype, P.sprice, P.sexpr, P.rprice, P.rexpr, P.is_yf, P.dl_type, P.dl_count, P.dl_tp_type, P.dl_tp ' +
  134. ' FROM ' + this.tableName + ' As SP' +
  135. ' INNER JOIN ' + this.ctx.service.pay.tableName + ' As P ON SP.pid = P.id' +
  136. ' INNER JOIN ( ' +
  137. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `sprogress` ' +
  138. ' FROM ' + this.tableName +
  139. ' WHERE `sid` = ? ' +
  140. ' GROUP BY `sid`) As M ON (SP.`stimes` * ' + timesLen + ' + SP.`sorder`) = M.`sprogress`' +
  141. ' WHERE SP.`sid` = ? AND P.`valid` = true' +
  142. ' ORDER BY P.`order`';
  143. const sqlParam = [sid, sid];
  144. return await this.db.query(sql, sqlParam);
  145. }
  146. /**
  147. * 同步新增 (仅供合同支付新增时调用)
  148. * @param {Object} pay - 合同支付项数据
  149. * @param transaction - 新增合同支付的事务
  150. * @returns {Promise<{tid, sid: number|*, pid, stimes: number|*, sorder: number|*}>}
  151. */
  152. async syncAdd(pay, transaction) {
  153. const stagePay = {
  154. tid: pay.tid,
  155. sid: pay.csid,
  156. pid: pay.id,
  157. stimes: pay.cstimes,
  158. sorder: pay.csaorder,
  159. };
  160. const result = await transaction.insert(this.tableName, stagePay);
  161. if (result.affectedRows !== 1) {
  162. throw '新增数据失败';
  163. }
  164. stagePay.id = result.insertId;
  165. return stagePay;
  166. }
  167. /**
  168. * 保存本期金额/表达式
  169. * @param data
  170. * @returns {Promise<void>}
  171. */
  172. async save(saveData) {
  173. const datas = saveData instanceof Array ? saveData : [saveData], updateDatas = [];
  174. const stagePays = await this.getStagePay(this.ctx.stage, this._.map(datas, 'pid'));
  175. for (const data of datas) {
  176. const stagePay = stagePays.find(function (x) {
  177. return x.pid === data.pid;
  178. });
  179. const updateData = {id: stagePay.id};
  180. if (data.name !== undefined) { updateData.name = data.name }
  181. if (data.expr !== undefined) { updateData.expr = data.expr }
  182. if (data.tp !== undefined) { updateData.tp = data.tp }
  183. if (data.pause !== undefined) { updateData.pause = data.pause }
  184. updateDatas.push(updateData);
  185. }
  186. const result = await this.db.updateRows(this.tableName, updateDatas);
  187. if (result.affectedRows !== updateDatas.length) {
  188. throw '保存数据失败';
  189. }
  190. }
  191. /**
  192. * 计算
  193. * @param stage
  194. * @param transaction
  195. * @returns {Promise<boolean>}
  196. */
  197. async calcAllStagePays(stage, transaction) {
  198. if (!stage || !transaction) {
  199. throw '计算数据错误';
  200. }
  201. const stagePays = await this.getStagePays(stage);
  202. const PayCalculator = require('../lib/pay_calc');
  203. const payCalculator = new PayCalculator(this.ctx, stage, this.ctx.tender.info);
  204. await payCalculator.calculateAll(stagePays);
  205. const srUpdate = [], update = [];
  206. for (const sp of stagePays) {
  207. update.push({
  208. id: sp.id,
  209. tp: sp.tp,
  210. end_tp: sp.end_tp,
  211. });
  212. if (stage.order === 1 || sp.csorder >= stage.order) {
  213. srUpdate.push({
  214. id: sp.pid,
  215. sprice: sp.sprice,
  216. rprice: sp.rprice,
  217. });
  218. }
  219. }
  220. await transaction.updateRows(this.tableName, update);
  221. if (srUpdate.length > 0) {
  222. await transaction.updateRows(this.ctx.service.pay.tableName, srUpdate);
  223. }
  224. const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
  225. return yf;
  226. }
  227. async getYfTotalPrice(stage) {
  228. if (!stage) throw '计算数据错误';
  229. const stagePays = await this.getStagePays(stage);
  230. const PayCalculator = require('../lib/pay_calc');
  231. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(stage.tid);
  232. const payCalculator = new PayCalculator(this.ctx, stage, tenderInfo);
  233. await payCalculator.calculateAll(stagePays);
  234. const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
  235. return yf ? yf.tp : 0;
  236. }
  237. /**
  238. * 拷贝上一操作人数据 为 下一操作人数据
  239. * @param stage - 期数据
  240. * @param times - 下一操作人 该期第几次
  241. * @param order - 下一操作人顺序
  242. * @param transaction - 事务
  243. * @returns {Promise<*>}
  244. */
  245. async copyAuditStagePays(stage, times, order, transaction) {
  246. if (!stage || !transaction || !times || order === undefined) {
  247. throw '数据错误';
  248. }
  249. const sql = 'INSERT INTO ?? (`tid`, `sid`, `pid`, `stimes`, `sorder`, `name`, `tp`, `expr`, `pause`, `attachment`, `pre_tp`) ' +
  250. ' SELECT SP.`tid`, SP.`sid`, SP.`pid`, ?, ?, SP.name, SP.`tp`, SP.`expr`, SP.`pause`, SP.`attachment`, SP.`pre_tp` ' +
  251. ' FROM ?? As SP, ?? As P ' +
  252. ' WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? And SP.`pid` = P.`id` And P.`valid`';
  253. const sqlParam = [this.tableName, times, order, this.tableName, this.ctx.service.pay.tableName,
  254. stage.id, stage.curTimes, stage.curOrder];
  255. return await transaction.query(sql, sqlParam);
  256. }
  257. /**
  258. * 保存附件
  259. * @param data
  260. * @returns {Promise<void>}
  261. */
  262. async saveAtt(id, att) {
  263. // 获取attachment
  264. const info = await this.getDataById(id);
  265. let attachment = null;
  266. if (info.attachment !== null && JSON.parse(info.attachment).length !== 0) {
  267. attachment = JSON.parse(info.attachment);
  268. attachment.unshift(att);
  269. } else {
  270. attachment = new Array(att);
  271. }
  272. const result = await this.db.update(this.tableName, { id, attachment: JSON.stringify(attachment) });
  273. return result.affectedRows === 1;
  274. }
  275. /**
  276. * 删除附件
  277. * @param data
  278. * @returns {Promise<void>}
  279. */
  280. async deleteAtt(id, attachment) {
  281. const result = await this.db.update(this.tableName, { id, attachment: JSON.stringify(attachment) });
  282. return result.affectedRows === 1;
  283. }
  284. }
  285. return StagePay;
  286. };