stage_jgcl.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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, cancel = false) {
  23. const data = await this.getAllDataByCondition({where: { sid: stage.id }});
  24. if (!cancel && stage && stage.readOnly && !this.ctx.tender.isTourist && 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(tid, 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, tid];
  53. const data = await this.db.query(sql, sqlParam);
  54. return data;
  55. }
  56. async getEndStageData(tid, 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, tid];
  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. tid: this.ctx.tender.id,
  80. sid: this.ctx.stage.id,
  81. };
  82. nd.name = d.name;
  83. nd.order = d.order;
  84. if (d.unit) nd.unit = d.unit;
  85. if (d.unit_price) nd.unit_price = d.unit_price;
  86. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit);
  87. if (d.arrive_qty) {
  88. nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
  89. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, tpDecimal);
  90. }
  91. if (d.deduct_qty) {
  92. nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
  93. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, tpDecimal);
  94. }
  95. if (d.source) nd.source = d.source;
  96. if (d.bills_code) nd.bills_code = d.bills_code;
  97. if (d.check_code) nd.check_code = d.check_code;
  98. if (d.memo) nd.memo = d.memo;
  99. insertData.push(nd);
  100. }
  101. await this.db.insert(this.tableName, insertData);
  102. return await this.getAllDataByCondition({
  103. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  104. });
  105. }
  106. async _delDatas (data) {
  107. const datas = data instanceof Array ? data : [data];
  108. const orgDatas = await this.getAllDataByCondition({where: {sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id')} });
  109. for (const od of orgDatas) {
  110. if (od.pre_used) throw '甲供材料往期已经计量,不可删除';
  111. }
  112. await this.db.delete(this.tableName, {id: datas});
  113. return datas;
  114. }
  115. async _updateDatas (data) {
  116. const tpDecimal = this.ctx.tender.info.decimal.extra
  117. ? this.ctx.tender.info.decimal.extraTp
  118. : this.ctx.tender.info.decimal.tp;
  119. const datas = data instanceof Array ? data : [data];
  120. const orgDatas = await this.getAllDataByCondition({
  121. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') }
  122. });
  123. const info = this.ctx.tender.info;
  124. const uDatas = [];
  125. for (const d of datas) {
  126. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  127. if (!od) continue;
  128. const nd = {id: od.id};
  129. if (d.name) nd.name = d.name;
  130. if (d.unit !== undefined) nd.unit = d.unit;
  131. nd.unit_price = d.unit_price !== undefined ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
  132. // if (od.pre_used === null || od.pre_used === undefined || od.pre_used === 0) {
  133. // if (d.unit !== undefined) nd.unit = d.unit;
  134. // nd.unit_price = d.unit_price !== undefined ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
  135. // } else {
  136. // nd.unit_price = od.unit_price;
  137. // }
  138. const precision = this.ctx.helper.findPrecision(info.precision, d.unit || od.unit);
  139. if (d.arrive_qty !== undefined) {
  140. nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, precision.value);
  141. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, nd.arrive_qty, tpDecimal);
  142. } else if (d.unit_price !== undefined) {
  143. nd.arrive_tp = this.ctx.helper.mul(nd.unit_price, od.arrive_qty, tpDecimal);
  144. }
  145. if (d.deduct_qty !== undefined) {
  146. nd.deduct_qty = this.ctx.helper.round(d.deduct_qty, precision.value);
  147. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, nd.deduct_qty, tpDecimal);
  148. } else if (d.unit_price !== undefined) {
  149. nd.deduct_tp = this.ctx.helper.mul(nd.unit_price, od.deduct_qty, tpDecimal);
  150. }
  151. if (d.source !== undefined) nd.source = d.source;
  152. if (d.bills_code !== undefined) nd.bills_code = d.bills_code;
  153. if (d.check_code !== undefined) nd.check_code = d.check_code;
  154. if (d.memo !== undefined) nd.memo = d.memo;
  155. if (d.order !== undefined) nd.order = d.order;
  156. uDatas.push(nd);
  157. }
  158. if (uDatas.length > 0) {
  159. await this.db.updateRows(this.tableName, uDatas);
  160. return uDatas;
  161. } else {
  162. return [];
  163. }
  164. }
  165. async updateDatas(data) {
  166. const result = {add: [], del: [], update: []};
  167. try {
  168. if (data.add) {
  169. result.add = await this._addDatas(data.add);
  170. }
  171. if (data.update) {
  172. result.update = await this._updateDatas(data.update);
  173. }
  174. if (data.del) {
  175. result.del = await this._delDatas(data.del);
  176. }
  177. return result;
  178. } catch (err) {
  179. if (err) result.err = err;
  180. return result;
  181. }
  182. }
  183. async updateHistory(stage, transaction) {
  184. const datas = await this.getStageData(stage);
  185. if (datas.length === 0) return;
  186. const updateDatas = [];
  187. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder;
  188. for (const d of datas) {
  189. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  190. const his = history.find(function (x) {
  191. return x.stimes && x.stimes === times
  192. && x.sorder !== undefined && x.sorder !== null && x.sorder === order;
  193. });
  194. if (his) {
  195. his.arrive_qty = d.arrive_qty;
  196. his.arrive_tp = d.arrive_tp;
  197. his.deduct_qty = d.deduct_qty;
  198. his.deduct_tp = d.deduct_tp;
  199. } else {
  200. history.push({
  201. stimes: this.ctx.stage.curTimes, sorder: this.ctx.stage.curOrder,
  202. arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
  203. deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
  204. });
  205. }
  206. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  207. }
  208. await transaction.updateRows(this.tableName, updateDatas);
  209. }
  210. async updateHistory4CheckCancel(stage, newTimes, newOrder, transaction) {
  211. const datas = await this.getStageData(stage, true);
  212. if (datas.length === 0) return;
  213. const updateDatas = [];
  214. for (const d of datas) {
  215. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  216. const his = history.find(function (x) {
  217. return x.stimes === newTimes && x.sorder === newOrder;
  218. });
  219. if (his) {
  220. his.arrive_qty = d.arrive_qty;
  221. his.arrive_tp = d.arrive_tp;
  222. his.deduct_qty = d.deduct_qty;
  223. his.deduct_tp = d.deduct_tp;
  224. } else {
  225. history.push({
  226. stimes: newTimes, sorder: newOrder,
  227. arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
  228. deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
  229. });
  230. }
  231. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  232. }
  233. await transaction.updateRows(this.tableName, updateDatas);
  234. }
  235. async updateHistory4CheckAgain(stage, transaction) {
  236. const datas = await this.getStageData(stage);
  237. if (datas.length === 0) return;
  238. const updateDatas = [];
  239. const times = this.ctx.stage.curTimes, order = this.ctx.stage.curOrder + 1;
  240. for (const d of datas) {
  241. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  242. const his = history.find(function (x) {
  243. return x.stimes && x.stimes === times
  244. && x.sorder && x.sorder === order;
  245. });
  246. if (his) {
  247. his.unit_price = d.unit_price;
  248. his.arrive_qty = d.arrive_qty;
  249. his.arrive_tp = d.arrive_tp;
  250. his.deduct_qty = d.deduct_qty;
  251. his.deduct_tp = d.deduct_tp;
  252. } else {
  253. history.push({
  254. stimes: times, sorder: order, unit_price: d.unit_price,
  255. arrive_qty: d.arrive_qty, arrive_tp: d.arrive_tp,
  256. deduct_qty: d.deduct_qty, deduct_tp: d.deduct_tp
  257. });
  258. }
  259. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  260. }
  261. await transaction.updateRows(this.tableName, updateDatas);
  262. }
  263. async addInitialStageData(stage, preStage, transaction) {
  264. if (!stage || !preStage) {
  265. throw '标段数据有误';
  266. }
  267. const preDatas = await this.getAllDataByCondition({
  268. columns: ['uuid', 'name', 'unit', 'unit_price', 'source', 'bills_code', 'check_code', 'memo', 'add_uid', 'add_sid', 'tid', 'order', 'arrive_qty', 'deduct_qty', 'pre_used'],
  269. where: { sid: preStage.id },
  270. });
  271. if (preDatas.length > 0) {
  272. for (const pd of preDatas) {
  273. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.arrive_qty) || !this.ctx.helper.checkZero(pd.deduct_qty);
  274. delete pd.arrive_qty;
  275. delete pd.deduct_qty;
  276. pd.sid = stage.id;
  277. }
  278. const result = await transaction.insert(this.tableName, preDatas);
  279. return result.affectedRows === preDatas.length;
  280. } else {
  281. return true;
  282. }
  283. }
  284. async deleteStageTimesData(sid, times, transaction) {
  285. const datas = await this.getAllDataByCondition({where: { sid: sid }});
  286. if (datas.length === 0) return;
  287. const updateDatas = [];
  288. for (const d of datas) {
  289. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  290. const his = history.filter(function (x) {
  291. return x.stimes && x.stimes < times;
  292. });
  293. his.sort(function (x, y) {
  294. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  295. });
  296. const ud = {
  297. id: d.id,
  298. shistory: JSON.stringify(his),
  299. arrive_qty: his.length > 0 ? his[his.length - 1].arrive_qty : null,
  300. deduct_qty: his.length > 0 ? his[his.length - 1].deduct_qty : null,
  301. arrive_tp: his.length > 0 ? this.ctx.helper.mul(his[his.length - 1].arrive_qty, d.unit_price, this.ctx.tender.info.decimal.tp) : null,
  302. deduct_tp: his.length > 0 ? this.ctx.helper.mul(his[his.length - 1].deduct_qty, d.unit_price, this.ctx.tender.info.decimal.tp) : null,
  303. };
  304. updateDatas.push(ud);
  305. }
  306. await transaction.updateRows(this.tableName, updateDatas);
  307. }
  308. }
  309. return StageJgcl;
  310. };