stage_pay.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. }
  104. }
  105. let result;
  106. if (transaction) {
  107. result = await transaction.insert(this.tableName, stagePays);
  108. } else {
  109. result = await this.db.insert(this.tableName, stagePays);
  110. }
  111. return result.affectedRows === pays.length;
  112. }
  113. async getStagePay(stage, pid) {
  114. return await this.getAuditorStagePay(pid, stage.id, stage.curTimes, stage.curOrder);
  115. }
  116. /**
  117. * 获取某期合同支付数据
  118. *
  119. * @param {Object} stage - 期数据(通过StageController._getStageData()获取)
  120. * @returns {Promise<*>}
  121. */
  122. async getStagePays(stage) {
  123. return await this.getAuditorStageData(stage.id, stage.curTimes, stage.curOrder);
  124. }
  125. /**
  126. * 获取某期最后数据(不限制该期是否已经审批通过)
  127. *
  128. * @param {Number} sid - 期id
  129. * @returns {Promise<*>}
  130. */
  131. async getStageLastestPays(sid) {
  132. 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 ' +
  133. ' FROM ' + this.tableName + ' As SP' +
  134. ' INNER JOIN ' + this.ctx.service.pay.tableName + ' As P ON SP.pid = P.id' +
  135. ' INNER JOIN ( ' +
  136. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `sprogress` ' +
  137. ' FROM ' + this.tableName +
  138. ' WHERE `sid` = ? ' +
  139. ' GROUP BY `sid`) As M ON (SP.`stimes` * ' + timesLen + ' + SP.`sorder`) = M.`sprogress`' +
  140. ' WHERE SP.`sid` = ? AND P.`valid` = true' +
  141. ' ORDER BY P.`order`';
  142. const sqlParam = [sid, sid];
  143. return await this.db.query(sql, sqlParam);
  144. }
  145. /**
  146. * 同步新增 (仅供合同支付新增时调用)
  147. * @param {Object} pay - 合同支付项数据
  148. * @param transaction - 新增合同支付的事务
  149. * @returns {Promise<{tid, sid: number|*, pid, stimes: number|*, sorder: number|*}>}
  150. */
  151. async syncAdd(pay, transaction) {
  152. const stagePay = {
  153. tid: pay.tid,
  154. sid: pay.csid,
  155. pid: pay.id,
  156. stimes: pay.cstimes,
  157. sorder: pay.csaorder,
  158. };
  159. const result = await transaction.insert(this.tableName, stagePay);
  160. if (result.affectedRows !== 1) {
  161. throw '新增数据失败';
  162. }
  163. stagePay.id = result.insertId;
  164. return stagePay;
  165. }
  166. /**
  167. * 保存本期金额/表达式
  168. * @param data
  169. * @returns {Promise<void>}
  170. */
  171. async save(saveData) {
  172. const datas = saveData instanceof Array ? saveData : [saveData], updateDatas = [];
  173. const stagePays = await this.getStagePay(this.ctx.stage, this._.map(datas, 'pid'));
  174. for (const data of datas) {
  175. const stagePay = stagePays.find(function (x) {
  176. return x.pid === data.pid;
  177. });
  178. const updateData = {id: stagePay.id};
  179. if (data.name !== undefined) { updateData.name = data.name }
  180. if (data.expr !== undefined) { updateData.expr = data.expr }
  181. if (data.tp !== undefined) { updateData.tp = data.tp }
  182. if (data.pause !== undefined) { updateData.pause = data.pause }
  183. updateDatas.push(updateData);
  184. }
  185. const result = await this.db.updateRows(this.tableName, updateDatas);
  186. if (result.affectedRows !== updateDatas.length) {
  187. throw '保存数据失败';
  188. }
  189. }
  190. /**
  191. * 计算
  192. * @param stage
  193. * @param transaction
  194. * @returns {Promise<boolean>}
  195. */
  196. async calcAllStagePays(stage, transaction) {
  197. if (!stage || !transaction) {
  198. throw '计算数据错误';
  199. }
  200. const stagePays = await this.getStagePays(stage);
  201. const PayCalculator = require('../lib/pay_calc');
  202. const payCalculator = new PayCalculator(this.ctx, stage, this.ctx.tender.info);
  203. await payCalculator.calculateAll(stagePays);
  204. const srUpdate = [], update = [];
  205. for (const sp of stagePays) {
  206. update.push({
  207. id: sp.id,
  208. tp: sp.tp,
  209. end_tp: sp.end_tp,
  210. });
  211. if (stage.order === 1 || sp.csorder >= stage.order) {
  212. srUpdate.push({
  213. id: sp.pid,
  214. sprice: sp.sprice,
  215. rprice: sp.rprice,
  216. });
  217. }
  218. }
  219. await transaction.updateRows(this.tableName, update);
  220. if (srUpdate.length > 0) {
  221. await transaction.updateRows(this.ctx.service.pay.tableName, srUpdate);
  222. }
  223. const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
  224. return yf;
  225. }
  226. async getYfTotalPrice(stage) {
  227. if (!stage) throw '计算数据错误';
  228. const stagePays = await this.getStagePays(stage);
  229. const PayCalculator = require('../lib/pay_calc');
  230. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(stage.tid);
  231. const payCalculator = new PayCalculator(this.ctx, stage, tenderInfo);
  232. await payCalculator.calculateAll(stagePays);
  233. const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
  234. return yf ? yf.tp : 0;
  235. }
  236. /**
  237. * 拷贝上一操作人数据 为 下一操作人数据
  238. * @param stage - 期数据
  239. * @param times - 下一操作人 该期第几次
  240. * @param order - 下一操作人顺序
  241. * @param transaction - 事务
  242. * @returns {Promise<*>}
  243. */
  244. async copyAuditStagePays(stage, times, order, transaction) {
  245. if (!stage || !transaction || !times || order === undefined) {
  246. throw '数据错误';
  247. }
  248. const sql = 'INSERT INTO ?? (`tid`, `sid`, `pid`, `stimes`, `sorder`, `name`, `tp`, `expr`, `pause`, `attachment`, `pre_tp`) ' +
  249. ' SELECT SP.`tid`, SP.`sid`, SP.`pid`, ?, ?, SP.name, SP.`tp`, SP.`expr`, SP.`pause`, SP.`attachment`, SP.`pre_tp` ' +
  250. ' FROM ?? As SP, ?? As P ' +
  251. ' WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? And SP.`pid` = P.`id` And P.`valid`';
  252. const sqlParam = [this.tableName, times, order, this.tableName, this.ctx.service.pay.tableName,
  253. stage.id, stage.curTimes, stage.curOrder];
  254. return await transaction.query(sql, sqlParam);
  255. }
  256. /**
  257. * 保存附件
  258. * @param data
  259. * @returns {Promise<void>}
  260. */
  261. async saveAtt(id, att) {
  262. // 获取attachment
  263. const info = await this.getDataById(id);
  264. let attachment = null;
  265. if (info.attachment !== null && JSON.parse(info.attachment).length !== 0) {
  266. attachment = JSON.parse(info.attachment);
  267. attachment.unshift(att);
  268. } else {
  269. attachment = new Array(att);
  270. }
  271. const result = await this.db.update(this.tableName, { id, attachment: JSON.stringify(attachment) });
  272. return result.affectedRows === 1;
  273. }
  274. /**
  275. * 删除附件
  276. * @param data
  277. * @returns {Promise<void>}
  278. */
  279. async deleteAtt(id, attachment) {
  280. const result = await this.db.update(this.tableName, { id, attachment: JSON.stringify(attachment) });
  281. return result.affectedRows === 1;
  282. }
  283. }
  284. return StagePay;
  285. };