stage_jgcl.js 10 KB

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