stage_yjcl.js 14 KB

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