stage_jgcl.js 8.8 KB

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