stage_pay.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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,' +
  57. ' P.rprice, P.rexpr, P.is_yf, P.dl_type, P.dl_count, P.dl_tp_type, P.dl_tp, P.name as pName ' +
  58. ' FROM ?? As SP, ?? As P ' +
  59. ' WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? AND SP.`pid` = P.`id` AND P.`valid`' +
  60. ' ORDER BY P.`order`';
  61. const sqlParam = [this.tableName, this.ctx.service.pay.tableName, sid, times, order];
  62. return await this.db.query(sql, sqlParam);
  63. }
  64. /**
  65. * 新增期时,初始化 期--合同支付 数据
  66. * @param {Object} stage - 新增的期
  67. * @param transaction - 事务
  68. * @returns {Promise<void>}
  69. */
  70. async addInitialStageData(stage, transaction) {
  71. if (!stage) {
  72. throw '初始化期合同支付数据失败';
  73. }
  74. let pays = await this.ctx.service.pay.getAllDataByCondition({where: { tid: this.ctx.tender.id } });
  75. // 兼容旧项目,无初始化合同支付数据
  76. if (!pays || pays.length === 0) {
  77. const result = await this.ctx.service.pay.addDefaultPayData(this.ctx.tender.id, transaction);
  78. if (!result) {
  79. throw '初始化合同支付数据失败';
  80. }
  81. pays = await transaction.select(this.ctx.service.pay.tableName, {where: { tid: this.ctx.tender.id } });
  82. }
  83. const stagePays = [];
  84. // 获取截止上期数据
  85. if (stage.order > 1) {
  86. const preStage = await this.ctx.service.stage.getDataByCondition({tid: stage.tid, order: stage.order - 1});
  87. if (!preStage) {
  88. throw '标段数据有误';
  89. }
  90. const prePays = await this.getStageLastestPays(preStage.id);
  91. for (const pp of prePays) {
  92. const p = this._.find(pays, {id: pp.pid});
  93. stagePays.push({
  94. tid: p.tid, sid: stage.id, pid: p.id,
  95. stimes: stage.times, sorder: 0,
  96. name: pp.name,
  97. expr: (p.ptype === payConst.payType.normal || p.ptype === payConst.payType.wc) ? pp.expr : null,
  98. pause: pp.pause,
  99. pre_tp: pp.end_tp,
  100. pre_used: pp.pre_used || !this.ctx.helper.checkZero(pp.tp),
  101. pre_finish: pp.pre_finish || (pp.rprice ? pp.end_tp === pp.rprice : false),
  102. start_stage_order: pp.start_stage_order,
  103. });
  104. }
  105. } else {
  106. for (const p of pays) {
  107. stagePays.push({
  108. tid: p.tid, sid: stage.id, pid: p.id,
  109. stimes: stage.times, sorder: 0, expr: p.expr, name: p.name,
  110. });
  111. }
  112. }
  113. let result;
  114. if (transaction) {
  115. result = await transaction.insert(this.tableName, stagePays);
  116. } else {
  117. result = await this.db.insert(this.tableName, stagePays);
  118. }
  119. return result.affectedRows === stagePays.length;
  120. }
  121. async getStagePay(stage, pid) {
  122. return await this.getAuditorStagePay(pid, stage.id, stage.curTimes, stage.curOrder);
  123. }
  124. /**
  125. * 获取某期合同支付数据
  126. *
  127. * @param {Object} stage - 期数据(通过StageController._getStageData()获取)
  128. * @returns {Promise<*>}
  129. */
  130. async getStagePays(stage) {
  131. return await this.getAuditorStageData(stage.id, stage.curTimes, stage.curOrder);
  132. }
  133. /**
  134. * 获取某期最后数据(不限制该期是否已经审批通过)
  135. *
  136. * @param {Number} sid - 期id
  137. * @returns {Promise<*>}
  138. */
  139. async getStageLastestPays(sid) {
  140. 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 ' +
  141. ' FROM ' + this.tableName + ' As SP' +
  142. ' INNER JOIN ' + this.ctx.service.pay.tableName + ' As P ON SP.pid = P.id' +
  143. ' INNER JOIN ( ' +
  144. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `sprogress` ' +
  145. ' FROM ' + this.tableName +
  146. ' WHERE `sid` = ? ' +
  147. ' GROUP BY `sid`) As M ON (SP.`stimes` * ' + timesLen + ' + SP.`sorder`) = M.`sprogress`' +
  148. ' WHERE SP.`sid` = ? AND P.`valid` = true' +
  149. ' ORDER BY P.`order`';
  150. const sqlParam = [sid, sid];
  151. return await this.db.query(sql, sqlParam);
  152. }
  153. /**
  154. * 同步新增 (仅供合同支付新增时调用)
  155. * @param {Object} pay - 合同支付项数据
  156. * @param transaction - 新增合同支付的事务
  157. * @returns {Promise<{tid, sid: number|*, pid, stimes: number|*, sorder: number|*}>}
  158. */
  159. async syncAdd(pay, transaction) {
  160. const stagePay = {
  161. tid: pay.tid,
  162. sid: pay.csid,
  163. pid: pay.id,
  164. stimes: pay.cstimes,
  165. sorder: pay.csaorder,
  166. };
  167. const result = await transaction.insert(this.tableName, stagePay);
  168. if (result.affectedRows !== 1) {
  169. throw '新增数据失败';
  170. }
  171. stagePay.id = result.insertId;
  172. return stagePay;
  173. }
  174. /**
  175. * 保存本期金额/表达式
  176. * @param data
  177. * @returns {Promise<void>}
  178. */
  179. async save(saveData) {
  180. const datas = saveData instanceof Array ? saveData : [saveData], updateDatas = [];
  181. const stagePays = await this.getStagePay(this.ctx.stage, this._.map(datas, 'pid'));
  182. for (const data of datas) {
  183. const stagePay = stagePays.find(function (x) {
  184. return x.pid === data.pid;
  185. });
  186. const updateData = {id: stagePay.id};
  187. if (data.name !== undefined) { updateData.name = data.name }
  188. if (data.expr !== undefined) { updateData.expr = data.expr }
  189. if (data.tp !== undefined) { updateData.tp = data.tp }
  190. if (data.pause !== undefined) { updateData.pause = data.pause }
  191. updateDatas.push(updateData);
  192. }
  193. const result = await this.db.updateRows(this.tableName, updateDatas);
  194. if (result.affectedRows !== updateDatas.length) {
  195. throw '保存数据失败';
  196. }
  197. }
  198. /**
  199. * 计算
  200. * @param stage
  201. * @param transaction
  202. * @returns {Promise<boolean>}
  203. */
  204. async calcAllStagePays(stage, transaction) {
  205. if (!stage || !transaction) {
  206. throw '计算数据错误';
  207. }
  208. const stagePays = await this.getStagePays(stage);
  209. const PayCalculator = require('../lib/pay_calc');
  210. const payCalculator = new PayCalculator(this.ctx, stage, this.ctx.tender.info);
  211. await payCalculator.calculateAll(stagePays);
  212. const srUpdate = [], update = [];
  213. for (const sp of stagePays) {
  214. update.push({
  215. id: sp.id,
  216. tp: sp.tp,
  217. end_tp: sp.end_tp,
  218. start_stage_order: sp.start_stage_order && sp.start_stage_order < stage.order
  219. ? sp.start_stage_order
  220. : (this.ctx.helper.checkZero(sp.tp) ? null : stage.order),
  221. });
  222. if (stage.order === 1 || sp.csorder >= stage.order) {
  223. srUpdate.push({
  224. id: sp.pid,
  225. sprice: sp.sprice,
  226. rprice: sp.rprice,
  227. });
  228. }
  229. }
  230. await transaction.updateRows(this.tableName, update);
  231. if (srUpdate.length > 0) {
  232. await transaction.updateRows(this.ctx.service.pay.tableName, srUpdate);
  233. }
  234. const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
  235. const sf = this._.find(stagePays, {ptype: payConst.payType.sf});
  236. return [yf, sf];
  237. }
  238. async getSpecialTotalPrice(stage) {
  239. if (!stage) throw '计算数据错误';
  240. const stagePays = await this.getStagePays(stage);
  241. const PayCalculator = require('../lib/pay_calc');
  242. const tenderInfo = await this.ctx.service.tenderInfo.getTenderInfo(stage.tid);
  243. const payCalculator = new PayCalculator(this.ctx, stage, tenderInfo);
  244. await payCalculator.calculateAll(stagePays);
  245. const yf = this._.find(stagePays, {ptype: payConst.payType.yf});
  246. const sf = this._.find(stagePays, {ptype: payConst.payType.sf});
  247. return { yf: yf ? yf.tp : 0, sf: sf ? sf.tp : 0 };
  248. }
  249. async getHistorySf(stage) {
  250. if (!stage) return 0;
  251. const stagePays = await this.getStageLastestPays(stage.id);
  252. const sf = this._.find(stagePays, {ptype: payConst.payType.sf});
  253. return sf;
  254. }
  255. /**
  256. * 拷贝上一操作人数据 为 下一操作人数据
  257. * @param stage - 期数据
  258. * @param times - 下一操作人 该期第几次
  259. * @param order - 下一操作人顺序
  260. * @param transaction - 事务
  261. * @returns {Promise<*>}
  262. */
  263. async copyAuditStagePays(stage, times, order, transaction) {
  264. if (!stage || !transaction || !times || order === undefined) {
  265. throw '数据错误';
  266. }
  267. const sql = 'INSERT INTO ?? (`tid`, `sid`, `pid`, `stimes`, `sorder`, `name`, `tp`, `expr`, `pause`, `attachment`,' +
  268. ' `pre_tp`, `end_tp`, `pre_used`, `pre_finish`, `start_stage_order`) ' +
  269. ' SELECT SP.`tid`, SP.`sid`, SP.`pid`, ?, ?, SP.name, SP.`tp`, SP.`expr`, SP.`pause`, SP.`attachment`,' +
  270. ' SP.`pre_tp`, SP.`end_tp`, SP.`pre_used`, SP.`pre_finish`, SP.`start_stage_order` ' +
  271. ' FROM ?? As SP, ?? As P ' +
  272. ' WHERE SP.`sid` = ? AND SP.`stimes` = ? AND SP.`sorder` = ? And SP.`pid` = P.`id` And P.`valid`';
  273. const sqlParam = [this.tableName, times, order, this.tableName, this.ctx.service.pay.tableName,
  274. stage.id, stage.curTimes, stage.curOrder];
  275. return await transaction.query(sql, sqlParam);
  276. }
  277. /**
  278. * 保存附件
  279. * @param data
  280. * @returns {Promise<void>}
  281. */
  282. async saveAtt(id, att) {
  283. // 获取attachment
  284. const info = await this.getDataById(id);
  285. let attachment = null;
  286. if (info.attachment !== null && JSON.parse(info.attachment).length !== 0) {
  287. attachment = JSON.parse(info.attachment);
  288. attachment.unshift(att);
  289. } else {
  290. attachment = new Array(att);
  291. }
  292. const result = await this.db.update(this.tableName, { id, attachment: JSON.stringify(attachment) });
  293. return result.affectedRows === 1;
  294. }
  295. /**
  296. * 删除附件
  297. * @param data
  298. * @returns {Promise<void>}
  299. */
  300. async deleteAtt(id, attachment) {
  301. const result = await this.db.update(this.tableName, { id, attachment: JSON.stringify(attachment) });
  302. return result.affectedRows === 1;
  303. }
  304. }
  305. return StagePay;
  306. };