stage_yjcl.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const decimal = { up: 6, qty: 3 };
  11. module.exports = app => {
  12. class StageYjcl 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_yjcl';
  22. }
  23. async getStageData(stage) {
  24. const data = await this.getAllDataByCondition({ where: { sid: stage.id } });
  25. if (!stage.readOnly || stage.status === auditConst.status.checked) return data;
  26. for (const d of data) {
  27. const his = d.shistory ? JSON.parse(d.shistory) : [];
  28. const h = this.ctx.helper._.find(his, {
  29. stimes: stage.curTimes, sorder: stage.curOrder,
  30. });
  31. d.qty = h ? h.qty : 0;
  32. d.tp = h ? h.tp : 0;
  33. }
  34. return data;
  35. }
  36. async _addDatas(data) {
  37. const tpDecimal = this.ctx.tender.info.decimal.extra
  38. ? this.ctx.tender.info.decimal.extraTp
  39. : this.ctx.tender.info.decimal.tp;
  40. const datas = data instanceof Array ? data : [data];
  41. const insertData = [];
  42. for (const d of datas) {
  43. if (!d.name || !d.m_order) throw '新增永久材料,提交的数据错误';
  44. const nd = {
  45. uuid: this.uuid.v4(),
  46. add_sid: this.ctx.stage.id,
  47. add_sorder: this.ctx.stage.order,
  48. add_uid: this.ctx.session.sessionUser.accountId,
  49. tid: this.ctx.tender.id,
  50. sid: this.ctx.stage.id,
  51. sorder: this.ctx.stage.order,
  52. };
  53. nd.name = d.name;
  54. nd.m_order = d.m_order;
  55. nd.spec = d.spec || '';
  56. nd.unit = d.unit || '';
  57. nd.tax = d.tax ? Math.min(Math.max(0, this.ctx.helper.round(d.tax, 0)), 100) : 0;
  58. nd.arrive_time = d.arrive_time || '';
  59. nd.source = d.source || '';
  60. nd.bills_code = d.bills_code || '';
  61. nd.location = d.location || '';
  62. nd.prepare_pos = d.prepare_pos || '';
  63. nd.memo = d.memo || '';
  64. const extraPre = 1 + this.ctx.helper.div(nd.tax, 100, 2);
  65. nd.arrive_qty = d.arrive_qty ? this.ctx.helper.round(d.arrive_qty, decimal.qty) : 0;
  66. nd.arrive_tp = d.arrive_tp ? this.ctx.helper.round(d.arrive_tp, tpDecimal) : 0;
  67. nd.unit_price = d.arrive_qty ? this.ctx.helper.div(d.arrive_tp, d.arrive_qty, decimal.up) : 0;
  68. nd.ex_tax_up = d.arrive_qty ? this.ctx.helper.div(this.ctx.helper.div(d.arrive_tp, d.arrive_qty), extraPre, decimal.up) : 0;
  69. // nd.unit_price = d.unit_price ? this.ctx.helper.round(d.unit_price, decimal.up) : 0;
  70. // const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, d.unit);
  71. // if (d.arrive_qty) {
  72. // nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, decimal.qty);
  73. // nd.arrive_tp = this.ctx.helper.mul(this.ctx.helper.mul(nd.unit_price, nd.arrive_qty), extraPre, tpDecimal);
  74. // }
  75. nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
  76. // nd.tp = this.ctx.helper.mul(this.ctx.helper.mul(nd.unit_price, nd.qty), extraPre, tpDecimal);
  77. nd.tp = this.ctx.helper.mul(nd.unit_price, nd.qty, tpDecimal);
  78. insertData.push(nd);
  79. }
  80. await this.db.insert(this.tableName, insertData);
  81. return await this.getAllDataByCondition({
  82. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') },
  83. });
  84. }
  85. async _insertDatas(data, result) {
  86. const select = await this.getDataById(data.select);
  87. if (!select) throw '选中的数据不存在';
  88. const insertData = [];
  89. for (let i = 0; i < data.count; i++) {
  90. insertData.push({
  91. uuid: this.uuid.v4(),
  92. add_sid: this.ctx.stage.id,
  93. add_uid: this.ctx.session.sessionUser.accountId,
  94. tid: this.ctx.tender.id,
  95. sid: this.ctx.stage.id,
  96. sorder: this.ctx.stage.order,
  97. name: '',
  98. m_order: select.m_order + i,
  99. });
  100. }
  101. const transaction = await this.db.beginTransaction();
  102. try {
  103. await transaction.query('Update ?? SET `m_order` = `m_order` + ? WHERE sid = ? and `m_order` >= ?', [this.tableName, data.count, this.ctx.stage.id, select.m_order]);
  104. await transaction.insert(this.tableName, insertData);
  105. await transaction.commit();
  106. } catch(err) {
  107. this.ctx.log(err);
  108. await transaction.rollback();
  109. throw '插入数据错误';
  110. }
  111. result.add = await this.getAllDataByCondition({
  112. where: { sid: this.ctx.stage.id, uuid: this.ctx.helper._.map(insertData, 'uuid') }
  113. });
  114. result.update = await this.db.query('SELECT * FROM ?? where sid = ? and `m_order` >= ?', [this.tableName, this.ctx.stage.id, select.m_order + data.count]);
  115. }
  116. async _delDatas(data) {
  117. const datas = data instanceof Array ? data : [data];
  118. const orgDatas = await this.getAllDataByCondition({
  119. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') },
  120. });
  121. for (const od of orgDatas) {
  122. if (od.pre_used) throw '永久材料往期已经计量,不可删除';
  123. }
  124. await this.db.delete(this.tableName, { id: datas });
  125. return datas;
  126. }
  127. async _updateDatas(data) {
  128. const tpDecimal = this.ctx.tender.info.decimal.extra
  129. ? this.ctx.tender.info.decimal.extraTp
  130. : this.ctx.tender.info.decimal.tp;
  131. const datas = data instanceof Array ? data : [data];
  132. const orgDatas = await this.getAllDataByCondition({
  133. where: { sid: this.ctx.stage.id, id: this.ctx.helper._.map(datas, 'id') },
  134. });
  135. const uDatas = [];
  136. for (const d of datas) {
  137. const od = this.ctx.helper._.find(orgDatas, { id: d.id });
  138. if (!od) continue;
  139. const nd = { id: od.id };
  140. if (d.name !== undefined) nd.name = d.name;
  141. if (d.m_order !== undefined) nd.m_order = d.m_order;
  142. if (d.spec !== undefined) nd.spec = d.spec || '';
  143. if (d.unit !== undefined) nd.unit = d.unit || '';
  144. if (d.tax !== undefined) nd.tax = d.tax ? Math.min(Math.max(0, this.ctx.helper.round(d.tax, 0)), 100) : 0;
  145. if (d.arrive_time !== undefined) nd.arrive_time = d.arrive_time || '';
  146. if (d.source !== undefined) nd.source = d.source || '';
  147. if (d.bills_code !== undefined) nd.bills_code = d.bills_code || '';
  148. if (d.location !== undefined) nd.location = d.location || '';
  149. if (d.prepare_pos !== undefined) nd.prepare_pos = d.prepare_pos || '';
  150. if (d.memo !== undefined)nd.memo = d.memo || '';
  151. if (d.arrive_qty !== undefined) nd.arrive_qty = this.ctx.helper.round(d.arrive_qty, decimal.qty);
  152. if (d.arrive_tp !== undefined) nd.arrive_tp = this.ctx.helper.round(d.arrive_tp, tpDecimal);
  153. let unit_price = od.unit_price, ex_tax_up = od.ex_tax_up;
  154. if (nd.arrive_qty !== undefined || nd.arrive_tp !== undefined || nd.tax !== undefined) {
  155. const tp = nd.arrive_tp !== undefined ? nd.arrive_tp : od.arrive_tp;
  156. const qty = nd.arrive_qty !== undefined ? nd.arrive_qty : od.arrive_qty;
  157. const extraPre = nd.tax !== undefined ? 1 + this.ctx.helper.div(nd.tax, 100, 2) : 1 + this.ctx.helper.div(od.tax, 100, 2);
  158. nd.unit_price = qty ? this.ctx.helper.div(tp, qty, decimal.up) : 0;
  159. nd.ex_tax_up = qty ? this.ctx.helper.div(this.ctx.helper.div(tp, qty), extraPre, decimal.up) : 0;
  160. unit_price = nd.unit_price;
  161. ex_tax_up = nd.ex_tax_up;
  162. }
  163. if (d.qty !== undefined) {
  164. nd.qty = d.qty ? this.ctx.helper.round(d.qty, decimal.qty) : 0;
  165. } else if (nd.unit_price) {
  166. nd.qty = od.qty;
  167. }
  168. if (nd.qty !== undefined) nd.tp = this.ctx.helper.mul(unit_price, nd.qty, tpDecimal);
  169. uDatas.push(nd);
  170. }
  171. if (uDatas.length > 0) {
  172. await this.db.updateRows(this.tableName, uDatas);
  173. return uDatas;
  174. } else {
  175. return [];
  176. }
  177. }
  178. async updateDatas(data) {
  179. const result = { add: [], del: [], update: [] };
  180. try {
  181. if (data.add) {
  182. result.add = await this._addDatas(data.add);
  183. }
  184. if (data.update) {
  185. result.update = await this._updateDatas(data.update);
  186. }
  187. if (data.del) {
  188. result.del = await this._delDatas(data.del);
  189. }
  190. if (data.insert) await this._insertDatas(data.insert, result);
  191. return result;
  192. } catch (err) {
  193. if (err) result.err = err;
  194. return result;
  195. }
  196. }
  197. async updateHistory4TimesOrder(stage, times, order, transaction) {
  198. const datas = await this.getStageData(stage);
  199. if (datas.length === 0) return;
  200. const updateDatas = [];
  201. for (const d of datas) {
  202. for (const curOrder of order) {
  203. const history = d.shistory ? JSON.parse(d.shistory) : [];
  204. const his = history.find(function (x) {
  205. return x.stimes === times && x.sorder === curOrder;
  206. });
  207. if (his) {
  208. his.qty = d.qty;
  209. his.tp = d.tp;
  210. } else {
  211. history.push({ stimes: times || 1, sorder: curOrder || 0, qty: d.qty, tp: d.tp });
  212. }
  213. updateDatas.push({ id: d.id, shistory: JSON.stringify(history) });
  214. }
  215. }
  216. await transaction.updateRows(this.tableName, updateDatas);
  217. }
  218. async updateHistory(stage, transaction) {
  219. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder], transaction);
  220. }
  221. async updateHistory4CheckAgain(stage, transaction) {
  222. await this.updateHistory4TimesOrder(stage, stage.curTimes, [stage.curOrder + 1], transaction);
  223. }
  224. async addInitialStageData(stage, preStage, transaction) {
  225. if (!stage || !preStage) {
  226. throw '标段数据有误';
  227. }
  228. const preDatas = await this.getAllDataByCondition({
  229. columns: ['uuid', 'add_sid', 'add_sorder', 'add_uid', 'tid', 'name', 'spec', 'm_order', 'unit', 'tax', 'arrive_time', 'source', 'bills_code', 'location', 'prepare_pos', 'memo',
  230. 'arrive_qty', 'arrive_tp', 'unit_price', 'ex_tax_up', 'qty', 'tp', 'pre_qty', 'pre_tp', 'pre_used'],
  231. where: { sid: preStage.id },
  232. });
  233. if (preDatas.length > 0) {
  234. for (const pd of preDatas) {
  235. pd.pre_used = pd.pre_used || !this.ctx.helper.checkZero(pd.qty);
  236. pd.pre_qty = this.ctx.helper.add(pd.qty, pd.pre_qty);
  237. pd.pre_tp = this.ctx.helper.add(pd.tp, pd.pre_tp);
  238. delete pd.qty;
  239. delete pd.tp;
  240. pd.sid = stage.id;
  241. pd.sorder = stage.order;
  242. }
  243. await transaction.delete(this.tableName, { sid: stage.id });
  244. const result = await transaction.insert(this.tableName, preDatas);
  245. return result.affectedRows === preDatas.length;
  246. } else {
  247. return true;
  248. }
  249. }
  250. async deleteStageTimesData(sid, times, transaction) {
  251. const datas = await this.getAllDataByCondition({ where: { sid } });
  252. if (datas.length === 0) return;
  253. const updateDatas = [];
  254. for (const d of datas) {
  255. const history = d.shistory && d.shistory !== '' ? JSON.parse(d.shistory) : [];
  256. const his = history.filter(x => { return x.stimes && x.stimes < times; });
  257. his.sort(function(x, y) {
  258. return (x.stimes * 1000 + x.sorder) - (y.stimes * 1000 + y.sorder);
  259. });
  260. const ud = {
  261. id: d.id,
  262. shistory: JSON.stringify(his),
  263. qty: his.length > 0 ? his[his.length - 1].qty : 0,
  264. tp: his.length > 0 ? his[his.length - 1].tp : 0,
  265. };
  266. updateDatas.push(ud);
  267. }
  268. await transaction.updateRows(this.tableName, updateDatas);
  269. }
  270. }
  271. return StageYjcl;
  272. };