stage_jgcl.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 StageJgcl 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_jgcl';
  22. }
  23. async getStageData(sid) {
  24. const data = await this.getAllDataByCondition({where: { sid: sid }});
  25. return data;
  26. }
  27. async getPreStageData(sorder) {
  28. const sql = 'SELECT c.uuid, Sum(c.arrive_qty) as arrive_qty, Sum(c.arrive_tp) as arrive_tp, Sum(c.deduct_qty) as deduct_qty, Sum(c.deduct_tp) as deduct_tp From ' + this.tableName + ' c' +
  29. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = c.sid' +
  30. ' WHERE s.`order` < ? And s.`tid` = ?' +
  31. ' GROUP By uuid';
  32. const sqlParam = [sorder, this.ctx.tender.id];
  33. const data = await this.db.query(sql, sqlParam);
  34. return data;
  35. }
  36. async getEndStageData(sorder) {
  37. const sql = 'SELECT c.uuid, Sum(c.arrive_qty) as arrive_qty, Sum(c.arrive_tp) as arrive_tp, Sum(c.deduct_qty) as deduct_qty, Sum(c.deduct_tp) as deduct_tp From ' + this.tableName + ' c' +
  38. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = c.sid' +
  39. ' WHERE s.`order` <= ? And s.`tid` = ?' +
  40. ' GROUP By uuid';
  41. const sqlParam = [sorder, this.ctx.tender.id];
  42. const data = await this.db.query(sql, sqlParam);
  43. return data;
  44. }
  45. async _addDatas(data) {
  46. const datas = data instanceof Array ? data : [data];
  47. const insertData = [];
  48. for (const d of datas) {
  49. if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
  50. const nd = {
  51. uuid: this.uuid.v4(),
  52. add_sid: this.ctx.stage.id,
  53. add_uid: this.ctx.session.sessionUser.accountId,
  54. sid: this.ctx.stage.id,
  55. };
  56. nd.name = d.name;
  57. nd.order = d.order;
  58. if (d.unit) nd.unit = d.unit;
  59. if (d.unit_price) nd.unit_price = d.unit_price;
  60. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit_price);
  61. if (d.arrive_qty) {
  62. nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
  63. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, this.ctx.tender.info.decimal.tp);
  64. }
  65. if (d.deduct_qty) {
  66. nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
  67. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, this.ctx.tender.info.decimal.tp);
  68. }
  69. if (d.source) nd.source = d.source;
  70. if (d.bills_code) nd.bills_code = d.bills_code;
  71. if (d.check_code) nd.check_code = d.check_code;
  72. if (d.memo) nd.memo = d.memo;
  73. insertData.push(nd);
  74. }
  75. await this.db.insert(this.tableName, insertData);
  76. return await this.getAllDataByCondition({
  77. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  78. });
  79. }
  80. async _delDatas (data) {
  81. const datas = data instanceof Array ? data : [data];
  82. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  83. for (const od of orgDatas) {
  84. if (od.pre_used) throw '甲供材料往期已经计量,不可删除';
  85. }
  86. await this.db.delete(this.tableName, {id: datas});
  87. return datas;
  88. }
  89. async _updateDatas (data) {
  90. const datas = data instanceof Array ? data : [data];
  91. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  92. const info = this.ctx.tender.info;
  93. const uDatas = [];
  94. for (const d of datas) {
  95. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  96. if (!od) continue;
  97. const nd = {id: od.id};
  98. if (d.name) nd.name = d.name;
  99. if (od.sid === od.add_sid) {
  100. if (d.unit) nd.unit = d.unit;
  101. nd.unit_price = d.unit_price ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
  102. }
  103. const precision = this.ctx.helper.findPrecision(info.precision, d.unit_price);
  104. if (d.arrive_qty) {
  105. nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
  106. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, info.decimal.tp);
  107. } else if (d.unit_price) {
  108. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, od.arrive_qty, info.decimal.tp);
  109. }
  110. if (d.deduct_qty) {
  111. nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
  112. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, info.decimal.tp);
  113. } else if (d.unit_price) {
  114. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, od.deduct_qty, info.decimal.tp);
  115. }
  116. if (d.source) nd.source = d.source;
  117. if (d.bills_code) nd.bills_code = d.bills_code;
  118. if (d.check_code) nd.check_code = d.check_code;
  119. if (d.memo) nd.memo = d.memo;
  120. if (d.order) nd.order = d.order;
  121. uDatas.push(nd);
  122. }
  123. if (uDatas.length > 0) {
  124. await this.db.updateRows(this.tableName, uDatas);
  125. return uDatas;
  126. } else {
  127. return [];
  128. }
  129. }
  130. async updateDatas(data) {
  131. const result = {add: [], del: [], update: []};
  132. try {
  133. if (data.add) {
  134. result.add = await this._addDatas(data.add);
  135. }
  136. if (data.update) {
  137. result.update = await this._updateDatas(data.update);
  138. }
  139. if (data.del) {
  140. result.del = await this._delDatas(data.del);
  141. }
  142. return result;
  143. } catch (err) {
  144. if (err) result.err = err;
  145. return result;
  146. }
  147. }
  148. async updateHistory(stage, transaction) {
  149. const datas = await this.getStageData(stage.id);
  150. if (datas.length === 0) return;
  151. const filter = {stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder};
  152. const updateDatas = [];
  153. for (const d of datas) {
  154. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  155. const his = this.ctx.helper._.find(datas, filter);
  156. if (his) {
  157. his.arrive_qty = d.arrive_qty;
  158. his.arrive_tp = d.arrive_tp;
  159. his.deduct_qty = d.deduct_qty;
  160. his.deduct_tp = d.deduct_tp;
  161. } else {
  162. history.push({
  163. stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder,
  164. arraive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
  165. deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
  166. });
  167. }
  168. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  169. }
  170. await transaction.updateRows(this.tableName, updateDatas);
  171. }
  172. async addInitialStageData(stage, preStage, transaction) {
  173. if (!stage || !preStage) {
  174. throw '标段数据有误';
  175. }
  176. const preDatas = await this.getStageData(preStage.id);
  177. if (preDatas.length > 0) {
  178. for (const pd of preDatas) {
  179. delete pd.id;
  180. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.arrive_qty) || !this.ctx.helper.checkZero(pd.deduct_qty);
  181. delete pd.arrive_qty;
  182. delete pd.arrive_tp;
  183. delete pd.deduct_qty;
  184. delete pd.deduct_tp;
  185. pd.sid = stage.id;
  186. }
  187. const result = await transaction.insert(this.tableName, preDatas);
  188. return result.affectedRows === preDatas.length;
  189. } else {
  190. return true;
  191. }
  192. }
  193. }
  194. return StageJgcl;
  195. };