stage_jgcl.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(stage) {
  23. const data = await this.getAllDataByCondition({where: { sid: stage.id }});
  24. if (stage && stage.readOnly && 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: stage.curTimes, sorder: stage.curOrder
  29. });
  30. if (h) {
  31. d.arrive_qty = h.arrive_qty ? h.arrive_qty : h.arraive_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,' +
  47. ' Sum(c.deduct_qty) as deduct_qty, Sum(c.deduct_tp) as deduct_tp' +
  48. ' From ' + this.tableName + ' c' +
  49. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = c.sid' +
  50. ' WHERE s.`order` < ? And s.`tid` = ?' +
  51. ' GROUP By uuid';
  52. const sqlParam = [sorder, this.ctx.tender.id];
  53. const data = await this.db.query(sql, sqlParam);
  54. return data;
  55. }
  56. async getEndStageData(sorder) {
  57. const sql = 'SELECT c.uuid, Sum(c.arrive_qty) as arrive_qty, Sum(c.arrive_tp) as arrive_tp,' +
  58. ' Sum(c.deduct_qty) as deduct_qty, Sum(c.deduct_tp) as deduct_tp' +
  59. ' From ' + this.tableName + ' c' +
  60. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON s.id = c.sid' +
  61. ' WHERE s.`order` <= ? And s.`tid` = ?' +
  62. ' GROUP By uuid';
  63. const sqlParam = [sorder, this.ctx.tender.id];
  64. const data = await this.db.query(sql, sqlParam);
  65. return data;
  66. }
  67. async _addDatas(data) {
  68. const tpDecimal = this.ctx.tender.info.decimal.extra
  69. ? this.ctx.tender.info.decimal.extraTp
  70. : this.ctx.tender.info.decimal.tp;
  71. const datas = data instanceof Array ? data : [data];
  72. const insertData = [];
  73. for (const d of datas) {
  74. if (!d.name || !d.order) throw '新增甲供材料,提交的数据错误';
  75. const nd = {
  76. uuid: this.uuid.v4(),
  77. add_sid: this.ctx.stage.id,
  78. add_uid: this.ctx.session.sessionUser.accountId,
  79. sid: this.ctx.stage.id,
  80. };
  81. nd.name = d.name;
  82. nd.order = d.order;
  83. if (d.unit) nd.unit = d.unit;
  84. if (d.unit_price) nd.unit_price = d.unit_price;
  85. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit_price);
  86. if (d.arrive_qty) {
  87. nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
  88. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, tpDecimal);
  89. }
  90. if (d.deduct_qty) {
  91. nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
  92. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, tpDecimal);
  93. }
  94. if (d.source) nd.source = d.source;
  95. if (d.bills_code) nd.bills_code = d.bills_code;
  96. if (d.check_code) nd.check_code = d.check_code;
  97. if (d.memo) nd.memo = d.memo;
  98. insertData.push(nd);
  99. }
  100. await this.db.insert(this.tableName, insertData);
  101. return await this.getAllDataByCondition({
  102. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  103. });
  104. }
  105. async _delDatas (data) {
  106. const datas = data instanceof Array ? data : [data];
  107. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
  108. for (const od of orgDatas) {
  109. if (od.pre_used) throw '甲供材料往期已经计量,不可删除';
  110. }
  111. await this.db.delete(this.tableName, {id: datas});
  112. return datas;
  113. }
  114. async _updateDatas (data) {
  115. const tpDecimal = this.ctx.tender.info.decimal.extra
  116. ? this.ctx.tender.info.decimal.extraTp
  117. : this.ctx.tender.info.decimal.tp;
  118. const datas = data instanceof Array ? data : [data];
  119. const orgDatas = await this.getAllDataByCondition({sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')});
  120. const info = this.ctx.tender.info;
  121. const uDatas = [];
  122. for (const d of datas) {
  123. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  124. if (!od) continue;
  125. const nd = {id: od.id};
  126. if (d.name) nd.name = d.name;
  127. if (od.pre_used === null || od.pre_used === undefined || od.pre_used === 0) {
  128. if (d.unit !== undefined) nd.unit = d.unit;
  129. nd.unit_price = d.unit_price !== undefined ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
  130. } else {
  131. nd.unit_price = od.unit_price;
  132. }
  133. const precision = this.ctx.helper.findPrecision(info.precision, d.unit_price);
  134. if (d.arrive_qty !== undefined) {
  135. nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
  136. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, tpDecimal);
  137. } else if (d.unit_price !== undefined) {
  138. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, od.arrive_qty, tpDecimal);
  139. }
  140. if (d.deduct_qty !== undefined) {
  141. nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
  142. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, tpDecimal);
  143. } else if (d.unit_price !== undefined) {
  144. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, od.deduct_qty, tpDecimal);
  145. }
  146. if (d.source !== undefined) nd.source = d.source;
  147. if (d.bills_code !== undefined) nd.bills_code = d.bills_code;
  148. if (d.check_code !== undefined) nd.check_code = d.check_code;
  149. if (d.memo !== undefined) nd.memo = d.memo;
  150. if (d.order !== undefined) nd.order = d.order;
  151. uDatas.push(nd);
  152. }
  153. if (uDatas.length > 0) {
  154. await this.db.updateRows(this.tableName, uDatas);
  155. return uDatas;
  156. } else {
  157. return [];
  158. }
  159. }
  160. async updateDatas(data) {
  161. const result = {add: [], del: [], update: []};
  162. try {
  163. if (data.add) {
  164. result.add = await this._addDatas(data.add);
  165. }
  166. if (data.update) {
  167. result.update = await this._updateDatas(data.update);
  168. }
  169. if (data.del) {
  170. result.del = await this._delDatas(data.del);
  171. }
  172. return result;
  173. } catch (err) {
  174. if (err) result.err = err;
  175. return result;
  176. }
  177. }
  178. async updateHistory(stage, transaction) {
  179. const datas = await this.getStageData(stage);
  180. if (datas.length === 0) return;
  181. const updateDatas = [];
  182. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  183. for (const d of datas) {
  184. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  185. const his = history.find(function (x) {
  186. return x.stimes && x.stimes === times
  187. && x.sorder && x.sorder === order;
  188. });
  189. if (his) {
  190. his.arrive_qty = d.arrive_qty;
  191. his.arrive_tp = d.arrive_tp;
  192. his.deduct_qty = d.deduct_qty;
  193. his.deduct_tp = d.deduct_tp;
  194. } else {
  195. history.push({
  196. stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder,
  197. arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
  198. deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
  199. });
  200. }
  201. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  202. }
  203. await transaction.updateRows(this.tableName, updateDatas);
  204. }
  205. async addInitialStageData(stage, preStage, transaction) {
  206. if (!stage || !preStage) {
  207. throw '标段数据有误';
  208. }
  209. const preDatas = await this.getAllDataByCondition({
  210. columns: ['uuid', 'name', 'unit', 'unit_price', 'source', 'bills_code', 'check_code', 'memo', 'add_uid', 'add_sid', 'order'],
  211. where: { sid: preStage.id },
  212. });
  213. if (preDatas.length > 0) {
  214. for (const pd of preDatas) {
  215. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.arrive_qty) || !this.ctx.helper.checkZero(pd.deduct_qty);
  216. pd.sid = stage.id;
  217. }
  218. const result = await transaction.insert(this.tableName, preDatas);
  219. return result.affectedRows === preDatas.length;
  220. } else {
  221. return true;
  222. }
  223. }
  224. async deleteStageTimesData(sid, times, transaction) {
  225. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  226. if (datas.length === 0) return;
  227. const updateDatas = [];
  228. for (const d of datas) {
  229. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  230. const his = history.filter(function (x) {
  231. return x.stimes && x.stimes < times;
  232. });
  233. his.sort(function (x, y) {
  234. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  235. });
  236. updateDatas.push({
  237. id: d.id,
  238. shistory: JSON.stringify(his),
  239. arrive_qty: his.length > 0 ? his[his.length - 1].arrive_qty : null,
  240. arrive_tp: his.length > 0 ? his[his.length - 1].arrive_tp : null,
  241. deduct_qty: his.length > 0 ? his[his.length - 1].deduct_qty : null,
  242. deduct_tp: his.length > 0 ? his[his.length - 1].deduct_tp : null,
  243. });
  244. }
  245. await transaction.updateRows(this.tableName, updateDatas);
  246. }
  247. }
  248. return StageJgcl;
  249. };