stage_change.js 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. 'use strict';
  2. /**
  3. * 期 - 变更数据
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const defaultPid = -1; // 非pid
  10. const audit = require('../const/audit');
  11. const timesLen = audit.stage.timesLen;
  12. const changeConst = require('../const/change');
  13. class autoUseChange {
  14. constructor(helper, info, settleStatus) {
  15. this.helper = helper;
  16. this.precision = info.precision;
  17. this.decimal = info.decimal;
  18. this.settleStatus = settleStatus;
  19. this.insertBills = [];
  20. this.insertPos = [];
  21. this.updateBills = [];
  22. this.updatePos = [];
  23. this.insertChange = [];
  24. this.updateChange = [];
  25. this.changeBills = {};
  26. this.changePos = {};
  27. this.changeDetail = [];
  28. }
  29. init(source) {
  30. this.default = source.default;
  31. const LedgerModel = require('../lib/ledger');
  32. this.ledgerTree = new LedgerModel.billsTree(this.ctx, {
  33. id: 'ledger_id', pid: 'ledger_pid', order: 'order', level: 'level', rootId: -1, calcFields: [],
  34. });
  35. this.pos = new LedgerModel.pos({ id: 'id', ledgerId: 'lid' });
  36. this.ledgerTree.loadDatas(source.ledgerData);
  37. this.pos.loadDatas(source.posData);
  38. this.stageBills = source.stageBills;
  39. this.stagePos = source.stagePos;
  40. this.stageChange = source.stageChange || [];
  41. }
  42. findBillsPos(changeBills){
  43. if (changeBills.gcl_id) {
  44. const node = this.ledgerTree.datas.find(x => {return x.id === changeBills.gcl_id});
  45. if (node.settle_status === this.settleStatus.finish) return null;
  46. const posData = this.pos.getLedgerPos(node.id) || [];
  47. const changePos = posData.find(x => { return x.name === changeBills.bwmx; });
  48. if (changePos.settle_status) return null;
  49. let defaultPos;
  50. if (posData.length > 0) {
  51. defaultPos = posData.length > 0 ? posData.find(x => { return !x.settle_status}) : null;
  52. if (!defaultPos) return null;
  53. } else {
  54. defaultPos = { id: '-1' };
  55. }
  56. return { bills: node, lid: node.id, pid: changePos ? changePos.id : defaultPos.id };
  57. } else {
  58. const cb = {
  59. b_code: changeBills.code || '',
  60. name: changeBills.name || '',
  61. unit: changeBills.unit || '',
  62. unit_price: changeBills.unit_price || 0,
  63. is_tp: false,
  64. };
  65. for (const node of this.ledgerTree.nodes) {
  66. if (node.children && node.children.length > 0) continue;
  67. if (node.settle_status === this.settleStatus.finish) return null;
  68. const b = {
  69. b_code: node.b_code || '',
  70. name: node.name || '',
  71. unit: node.unit || '',
  72. unit_price: node.unit_price || 0,
  73. is_tp: !!node.is_tp,
  74. };
  75. if (this.helper._.isMatch(cb, b)) {
  76. const posData = this.pos.getLedgerPos(node.id) || [];
  77. let defaultPos;
  78. if (posData.length > 0) {
  79. defaultPos = posData.length > 0 ? posData.find(x => { return !x.settle_status}) : null;
  80. if (!defaultPos) return null;
  81. } else {
  82. defaultPos = { id: '-1' };
  83. }
  84. return { bills: node, lid: node.id, pid: defaultPos.id };
  85. }
  86. }
  87. return null;
  88. }
  89. }
  90. useBills(bills) {
  91. if (bills.billsPos) bills.billsPos.bills = this.ledgerTree.datas.find(x => {return x.id === bills.billsPos.lid; });
  92. const billsPos = bills.billsPos || this.findBillsPos(bills);
  93. if (!billsPos) return;
  94. const bamount = parseFloat(bills.samount);
  95. const minus = bamount < 0;
  96. this.changeDetail.push({
  97. tid: this.default.tid, sid: this.default.sid,
  98. lid: billsPos.lid, pid: billsPos.pid + '', cid: bills.cid, cbid: bills.id,
  99. qty: bills.valid_qty, stimes: 1, sorder: 0, unit_price: bills.unit_price, minus, no_value: !bills.is_valuation,
  100. });
  101. if (!bills.is_valuation) return;
  102. if (billsPos.pid !== '-1') {
  103. let cp = this.changePos[billsPos.pid];
  104. if (!cp) {
  105. cp = { lid: billsPos.lid, pid: billsPos.pid, qty: 0, bills: billsPos.bills };
  106. this.changePos[billsPos.pid] = cp
  107. }
  108. cp.qty = this.helper.add(cp.qty, bills.valid_qty);
  109. if (minus) {
  110. cp.negative_qc_qty = cp.qty;
  111. } else {
  112. cp.positive_qc_qty = cp.qty;
  113. }
  114. } else {
  115. let cb = this.changeBills[billsPos.lid];
  116. if (!cb) {
  117. cb = { lid: billsPos.lid, qty: 0, bills: billsPos.bills };
  118. this.changeBills[billsPos.lid] = cb;
  119. }
  120. cb.qty = this.helper.add(cb.qty, bills.valid_qty);
  121. if (minus) {
  122. cb.negative_qc_qty = this.helper.add(cb.negative_qc_qty, bills.valid_qty);
  123. } else {
  124. cb.positive_qc_qty = this.helper.add(cb.positive_qc_qty, bills.valid_qty);
  125. }
  126. }
  127. }
  128. calculateAll() {
  129. for (const cd of this.changeDetail) {
  130. const sci = this.stageChange.findIndex(x => {
  131. return x.lid === cd.lid && x.pid === cd.pid && x.cbid === cd.cbid;
  132. });
  133. const sc = this.stageChange[sci];
  134. if (sc) {
  135. this.updateChange.push({ id: sc.id, qty: cd.qty });
  136. this.stageChange.splice(sci, 1);
  137. } else {
  138. this.insertChange.push(cd);
  139. }
  140. }
  141. for (const sc of this.stageChange) {
  142. if (sc.no_value) continue;
  143. const cp = this.changePos[sc.pid];
  144. if (cp) {
  145. cp.qty = this.helper.add(cp.qty, sc.qty);
  146. if (sc.minus) {
  147. cp.negative_qc_qty = this.helper.add(cp.negative_qc_qty, sc.qty);
  148. } else {
  149. cp.positive_qc_qty = this.helper.add(cp.positive_qc_qty, sc.qty);
  150. }
  151. }
  152. }
  153. for (const pid in this.changePos) {
  154. const cp = this.changePos[pid];
  155. if (!cp) continue;
  156. const precision = this.helper.findPrecision(this.precision, cp.bills.unit);
  157. const qc_qty = this.helper.round(cp.qty, precision.value);
  158. const positive_qc_qty = this.helper.round(cp.positive_qc_qty || 0, precision.value);
  159. const negative_qc_qty = this.helper.round(cp.negative_qc_qty || 0, precision.value);
  160. const sp = this.stagePos.find(x => {return x.pid === pid});
  161. if (sp) {
  162. this.updatePos.push({ id: sp.id, qc_qty, positive_qc_qty, negative_qc_qty });
  163. } else {
  164. this.insertPos.push({
  165. tid: this.default.tid, sid: this.default.sid, said: this.default.said,
  166. lid: cp.lid, pid, qc_qty, positive_qc_qty, negative_qc_qty, times: 1, order: 0
  167. });
  168. }
  169. const cb = this.changeBills[cp.lid];
  170. if (!cb) {
  171. this.changeBills[cp.lid] = { lid: cp.lid, qty: qc_qty, positive_qc_qty, negative_qc_qty, bills: cp.bills };
  172. } else {
  173. cb.qty = this.helper.add(cb.qty, qc_qty);
  174. cb.positive_qc_qty = this.helper.add(cb.positive_qc_qty, positive_qc_qty);
  175. cb.negative_qc_qty = this.helper.add(cb.negative_qc_qty, negative_qc_qty);
  176. }
  177. }
  178. for (const sc of this.stageChange) {
  179. if (sc.no_value) continue;
  180. const cp = this.changePos[sc.pid];
  181. if (cp) continue;
  182. const cb = this.changeBills[sc.lid];
  183. if (cb) {
  184. cb.qty = this.helper.add(cb.qty, sc.qty);
  185. if (sc.minus) {
  186. cb.negative_qc_qty = this.helper.add(cb.negative_qc_qty, sc.qty);
  187. } else {
  188. cb.positive_qc_qty = this.helper.add(cb.positive_qc_qty, sc.qty);
  189. }
  190. }
  191. }
  192. for (const lid in this.changeBills) {
  193. const cb = this.changeBills[lid];
  194. if (!cb) continue;
  195. const precision = this.helper.findPrecision(this.precision, cb.bills.unit);
  196. const qc_qty = this.helper.round(cb.qty, precision.value);
  197. const qc_tp = this.helper.mul(cb.qty, cb.bills.unit_price, this.decimal.tp);
  198. const positive_qc_qty = this.helper.round(cb.positive_qc_qty || 0, precision.value);
  199. const positive_qc_tp = this.helper.mul(positive_qc_qty, cb.bills.unit_price, this.decimal.tp);
  200. const negative_qc_qty = this.helper.round(cb.negative_qc_qty || 0, precision.value);
  201. const negative_qc_tp = this.helper.mul(negative_qc_qty, cb.bills.unit_price, this.decimal.tp);
  202. const sb = this.stageBills.find(x => {return x.lid === lid});
  203. if (sb) {
  204. this.updateBills.push({ id: sb.id, qc_qty, qc_tp, positive_qc_qty, positive_qc_tp, negative_qc_qty, negative_qc_tp });
  205. } else {
  206. this.insertBills.push({
  207. tid: this.default.tid, sid: this.default.sid, said: this.default.said,
  208. lid, qc_qty, qc_tp, positive_qc_qty, positive_qc_tp, negative_qc_qty, negative_qc_tp, times: 1, order: 0
  209. });
  210. }
  211. }
  212. }
  213. use(source, validChangeBills) {
  214. this.init(source);
  215. for (const bills of validChangeBills) {
  216. this.useBills(bills);
  217. }
  218. this.calculateAll();
  219. }
  220. }
  221. module.exports = app => {
  222. class StageChange extends app.BaseService {
  223. /**
  224. * 构造函数
  225. *
  226. * @param {Object} ctx - egg全局变量
  227. * @return {void}
  228. */
  229. constructor(ctx) {
  230. super(ctx);
  231. this.tableName = 'stage_change';
  232. }
  233. /**
  234. * 查询 调用的变更令 最新数据
  235. * @param {Number} tid - 标段id
  236. * @param {Number} sid - 期id
  237. * @param {Number} lid - 台账节点id
  238. * @param {Number} pid - 部位明细id
  239. * @return {Promise<*>}
  240. */
  241. async getLastestStageData(tid, sid, lid, pid, noValue) {
  242. const filter = noValue !== undefined ? ' And no_value = ?' : '';
  243. const sql = 'SELECT c.*,' +
  244. ' oc.p_code As c_code, oc.new_code As c_new_code' +
  245. ' FROM ' + this.tableName + ' As c ' +
  246. ' INNER JOIN ( ' +
  247. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  248. ' WHERE tid = ? And sid = ? And lid = ? And pid = ?' + filter +
  249. ' GROUP By `lid`, `pid`, `cbid`, `no_value`' +
  250. ' ) As m ' +
  251. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  252. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  253. ' ON c.cid = oc.cid' +
  254. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  255. ' ON c.cbid = ocb.id' +
  256. ' WHERE not ISNULL(ocb.id)';
  257. const sqlParam = [tid, sid, lid, pid ? pid : -1];
  258. if (noValue !== undefined) sqlParam.push(noValue);
  259. return await this.db.query(sql, sqlParam);
  260. }
  261. /**
  262. * 查询 调用的变更令 某轮 某人的数据
  263. * @param {Number} tid - 标段id
  264. * @param {Number} sid - 期id
  265. * @param {Number} times - 第几轮
  266. * @param {Number} order - 第几人
  267. * @param {Number} lid - 台账节点id
  268. * @param {Number} pid - 部位明细id
  269. * @return {Promise<*>}
  270. */
  271. async getAuditorStageData(tid, sid, times, order, lid, pid, noValue) {
  272. const filter = noValue !== undefined ? ' And no_value = ?' : '';
  273. const sql = 'SELECT c.*,' +
  274. ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
  275. ' FROM ' + this.tableName + ' As c ' +
  276. ' INNER JOIN ( ' +
  277. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  278. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?)) And lid = ? And pid = ?' + filter +
  279. ' GROUP By `lid`, `pid`, cbid, no_value' +
  280. ' ) As m ' +
  281. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  282. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  283. ' ON c.cid = oc.cid' +
  284. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  285. ' ON c.cbid = ocb.id' +
  286. ' WHERE not ISNULL(ocb.id)';
  287. const sqlParam = [tid, sid, times, times, order, lid, pid ? pid : -1];
  288. if (noValue !== undefined) sqlParam.push(noValue);
  289. return await this.db.query(sql, sqlParam);
  290. }
  291. async getLastestAllStageData(tid, sid) {
  292. const sql = 'SELECT c.*,' +
  293. ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
  294. ' FROM ' + this.tableName + ' As c ' +
  295. ' INNER JOIN ( ' +
  296. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  297. ' WHERE tid = ? And sid = ?' +
  298. ' GROUP By `lid`, `pid`, cbid, no_value' +
  299. ' ) As m ' +
  300. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  301. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  302. ' ON c.cid = oc.cid' +
  303. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  304. ' ON c.cbid = ocb.id' +
  305. ' WHERE not ISNULL(ocb.id)';
  306. const sqlParam = [tid, sid];
  307. return await this.db.query(sql, sqlParam);
  308. }
  309. async getAuditorAllStageData(tid, sid, times, order) {
  310. const sql = 'SELECT c.*, ' +
  311. ' oc.p_code As c_code, oc.new_code As c_new_code, oc.quality, ocb.code as b_code, ocb.name, ocb.unit' +
  312. ' FROM ' + this.tableName + ' As c ' +
  313. ' INNER JOIN ( ' +
  314. ' SELECT MAX(`stimes` * ' + timesLen + ' + `sorder`) As `progress`, `lid`, `pid`, `sid`, `cid`, `cbid`, `no_value` From ' + this.tableName +
  315. ' WHERE tid = ? And sid = ? And (`stimes` < ? OR (`stimes` = ? AND `sorder` <= ?))' +
  316. ' GROUP By `lid`, `pid`, cbid, no_value' +
  317. ' ) As m ' +
  318. ' ON (c.stimes * ' + timesLen + ' + c.sorder) = m.progress And c.lid = m.lid And c.pid = m.pid And c.`sid` = m.`sid` And c.`cbid` = m.`cbid` And c.`no_value` = m.`no_value`' +
  319. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' As oc' +
  320. ' ON c.cid = oc.cid' +
  321. ' LEFT JOIN ' + this.ctx.service.changeAuditList.tableName + ' As ocb' +
  322. ' ON c.cbid = ocb.id' +
  323. ' WHERE not ISNULL(ocb.id)';
  324. const sqlParam = [tid, sid, times, times, order];
  325. return await this.db.query(sql, sqlParam);
  326. }
  327. /**
  328. * 台账,调用变更令
  329. *
  330. * @param {Object} bills - 台账节点数据
  331. * @param {Array} changes - 调用的变更令
  332. * @return {Promise<void>}
  333. */
  334. async billsChange(bills, noValue, changes) {
  335. const self = this;
  336. function getNewChange(cid, cbid, times, order, qty, minus, no_value) {
  337. return {
  338. tid: self.ctx.tender.id, sid: self.ctx.stage.id,
  339. lid: bills.id, pid: -1,
  340. cid, cbid,
  341. stimes: times, sorder: order,
  342. qty, minus, no_value,
  343. };
  344. }
  345. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(bills.id);
  346. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  347. throw '提交数据错误';
  348. }
  349. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  350. // 获取原变更令
  351. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, bills.id, -1, noValue);
  352. // 获取更新数据
  353. const updateChanges = [],
  354. newChanges = [];
  355. let billsQty = 0, billsPositiveQty = 0, billsNegativeQty = 0;
  356. for (const oc of oldChanges) {
  357. const nc = this._.find(changes, { cid: oc.cid, cbid: oc.cbid });
  358. if (!nc || nc.qty !== oc.qty) {
  359. const qty = nc ? this.round(nc.qty, precision.value) : null;
  360. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty, nc ? nc.minus : oc.minus, noValue);
  361. change.unit_price = ledgerBills.unit_price;
  362. billsQty = this.ctx.helper.add(billsQty, change.qty);
  363. if (change.minus) {
  364. billsNegativeQty = this.ctx.helper.add(billsNegativeQty, change.qty);
  365. } else {
  366. billsPositiveQty = this.ctx.helper.add(billsPositiveQty, change.qty);
  367. }
  368. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  369. change.id = oc.id;
  370. updateChanges.push(change);
  371. } else {
  372. newChanges.push(change);
  373. }
  374. } else {
  375. billsQty = this.ctx.helper.add(billsQty, oc.qty);
  376. if (oc.minus) {
  377. billsNegativeQty = this.ctx.helper.add(billsNegativeQty, oc.qty);
  378. } else {
  379. billsPositiveQty = this.ctx.helper.add(billsPositiveQty, oc.qty);
  380. }
  381. }
  382. }
  383. for (const c of changes) {
  384. const nc = this._.find(oldChanges, { cid: c.cid, cbid: c.cbid });
  385. if (!nc) {
  386. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value), c.minus, noValue);
  387. change.unit_price = ledgerBills.unit_price;
  388. billsQty = this.ctx.helper.add(billsQty, change.qty);
  389. if (change.minus) {
  390. billsNegativeQty = this.ctx.helper.add(billsNegativeQty, change.qty);
  391. } else {
  392. billsPositiveQty = this.ctx.helper.add(billsPositiveQty, change.qty);
  393. }
  394. newChanges.push(change);
  395. }
  396. }
  397. // 更新数据
  398. const transaction = await this.db.beginTransaction();
  399. try {
  400. if (newChanges.length > 0) await transaction.insert(this.tableName, newChanges);
  401. if (updateChanges.length > 0) await transaction.updateRows(this.tableName, updateChanges);
  402. const stageBills = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, bills.id);
  403. const sbUpdate = noValue
  404. ? { qc_minus_qty: billsQty }
  405. : { qc_qty: billsQty, positive_qc_qty: billsPositiveQty, negative_qc_qty: billsNegativeQty };
  406. await this.ctx.service.stageBills.updateStageBillsQty(transaction, ledgerBills, stageBills, sbUpdate);
  407. await transaction.commit();
  408. } catch (err) {
  409. await transaction.rollback();
  410. throw err;
  411. }
  412. const result = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, [bills.id]);
  413. return { bills: { curStageData: result } };
  414. }
  415. /**
  416. * 部位明细,调用变更令
  417. *
  418. * @param {Object} pos - 部位明细数据
  419. * @param {Array} changes - 调用的变更令
  420. * @return {Promise<{}>}
  421. */
  422. async posChange(pos, noValue, changes) {
  423. const self = this;
  424. function getNewChange(cid, cbid, times, order, qty, minus, no_value) {
  425. return {
  426. tid: self.ctx.tender.id, sid: self.ctx.stage.id,
  427. lid: pos.lid, pid: pos.id,
  428. cid, cbid,
  429. stimes: times, sorder: order,
  430. qty, minus, no_value,
  431. };
  432. }
  433. const ledgerBills = await this.ctx.service.ledger.getCompleteDataById(pos.lid);
  434. if (!ledgerBills || ledgerBills.tender_id !== this.ctx.tender.id) {
  435. throw '提交数据错误';
  436. }
  437. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, ledgerBills.unit);
  438. // 获取原变更令
  439. const oldChanges = await this.getLastestStageData(this.ctx.tender.id, this.ctx.stage.id, pos.lid, pos.id, noValue);
  440. const updateChanges = [],
  441. newChanges = [];
  442. let posQty = 0, posPositiveQty = 0, posNegativeQty = 0;
  443. for (const oc of oldChanges) {
  444. const nc = this._.find(changes, { cid: oc.cid, cbid: oc.cbid });
  445. if (!nc || nc.qty !== oc.qty) {
  446. const qty = nc ? this.round(nc.qty, precision.value) : null;
  447. const change = getNewChange(oc.cid, oc.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, qty, nc ? nc.minus : oc.minus, noValue);
  448. change.unit_price = ledgerBills.unit_price;
  449. posQty = this.ctx.helper.add(posQty, change.qty);
  450. if (change.minus) {
  451. posNegativeQty = this.ctx.helper.add(posNegativeQty, change.qty);
  452. } else {
  453. posPositiveQty = this.ctx.helper.add(posPositiveQty, change.qty);
  454. }
  455. if (oc.stimes === this.ctx.stage.curTimes && oc.sorder === this.ctx.stage.curOrder) {
  456. change.id = oc.id;
  457. updateChanges.push(change);
  458. } else {
  459. newChanges.push(change);
  460. }
  461. } else {
  462. posQty = this.ctx.helper.add(posQty, oc.qty);
  463. if (oc.minus) {
  464. posNegativeQty = this.ctx.helper.add(posNegativeQty, oc.qty);
  465. } else {
  466. posPositiveQty = this.ctx.helper.add(posPositiveQty, oc.qty);
  467. }
  468. }
  469. }
  470. for (const c of changes) {
  471. const nc = this._.find(oldChanges, { cid: c.cid, cbid: c.cbid });
  472. if (!nc) {
  473. const change = getNewChange(c.cid, c.cbid, this.ctx.stage.curTimes, this.ctx.stage.curOrder, this.round(c.qty, precision.value), c.minus, noValue);
  474. change.unit_price = ledgerBills.unit_price;
  475. posQty = this.ctx.helper.add(posQty, change.qty);
  476. if (change.minus) {
  477. posNegativeQty = this.ctx.helper.add(posNegativeQty, change.qty);
  478. } else {
  479. posPositiveQty = this.ctx.helper.add(posPositiveQty, change.qty);
  480. }
  481. newChanges.push(change);
  482. }
  483. }
  484. // 更新数据
  485. const transaction = await this.db.beginTransaction();
  486. try {
  487. if (newChanges.length > 0) await transaction.insert(this.tableName, newChanges);
  488. if (updateChanges.length > 0) await transaction.updateRows(this.tableName, updateChanges);
  489. await this.ctx.service.stagePos.updateChangeQuantity(transaction, pos, posQty, noValue, posPositiveQty, posNegativeQty);
  490. await transaction.commit();
  491. } catch (err) {
  492. await transaction.rollback();
  493. throw err;
  494. }
  495. // 获取返回数据
  496. try {
  497. const data = { bills: {}, pos: {} };
  498. data.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, [pos.lid]);
  499. data.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, { pid: pos.id });
  500. return data;
  501. } catch (err) {
  502. throw '获取数据错误,请刷新页面';
  503. }
  504. }
  505. /**
  506. * 获取 变更令 - 变更清单 使用情况
  507. * @param {Number} sid - 查询期id
  508. * @param {uuid} cid - 变更令id
  509. * @return {Promise<void>}
  510. */
  511. async getUsedData(tid, cid) {
  512. if (this.ctx.stage.status === audit.stage.status.checked) {
  513. const sql = 'SELECT scf.* ' +
  514. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  515. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  516. ' WHERE scf.tid = ? And scf.cid = ? And s.order <= ?';
  517. const result = await this.db.query(sql, [tid, cid, this.ctx.stage.order]);
  518. return result;
  519. } else {
  520. const preSql = 'SELECT scf.* ' +
  521. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  522. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  523. ' WHERE scf.tid = ? And scf.cid = ? And s.order < ?';
  524. const pre = await this.db.query(preSql, [tid, cid, this.ctx.stage.order]);
  525. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ?';
  526. const curAll = await this.db.query(sql, [this.ctx.stage.id]);
  527. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  528. return [...pre, ...cur];
  529. }
  530. }
  531. async getFinalUsedData(tid, cid) {
  532. const stage = await this.ctx.service.stage.getFlowLatestStage(tid, true);
  533. if (!stage) { // 防止未创建期时调用
  534. return [];
  535. }
  536. if (stage.status === audit.stage.status.checked) {
  537. const sql = 'SELECT scf.* ' +
  538. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  539. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  540. this.ctx.helper.whereSql({ tid, cid }, 'scf') + ' And s.order <= ?';
  541. const result = await this.db.query(sql, [stage.order]);
  542. return result;
  543. } else {
  544. const preSql = 'SELECT scf.* ' +
  545. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  546. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  547. this.ctx.helper.whereSql({ tid, cid }, 'scf') + ' And s.order < ?';
  548. const pre = await this.db.query(preSql, [stage.order]);
  549. const sql = 'SELECT * FROM ' + this.tableName + this.ctx.helper.whereSql({ sid: stage.id, cid });
  550. const curAll = await this.db.query(sql);
  551. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  552. return [...pre, ...cur];
  553. }
  554. }
  555. async getAllFinalUsedData(tid, cid) {
  556. const result = [];
  557. const stages = await this.ctx.service.stage.getAllDataByCondition({ where: { tid: tid }, orders: [['order', 'desc']] });
  558. for (const stage of stages) {
  559. if (stage.status === audit.stage.status.checked) {
  560. const sql = 'SELECT scf.* ' +
  561. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  562. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  563. this.ctx.helper.whereSql({ tid, cid }, 'scf') + ' And s.order = ?';
  564. const pre = await this.db.query(sql, [stage.order]);
  565. result.push(...pre);
  566. } else {
  567. const sql = 'SELECT * FROM ' + this.tableName + this.ctx.helper.whereSql({ sid: stage.id, cid });
  568. const curAll = await this.db.query(sql);
  569. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  570. result.push(...cur);
  571. }
  572. }
  573. return result;
  574. }
  575. /**
  576. * 获取 变更令 - 变更清单 当期使用情况
  577. * @param {Number} sid - 查询期id
  578. * @param {uuid} cid - 变更令id
  579. * @return {Promise<*>}
  580. */
  581. async getStageUsedData(sid, cid) {
  582. const data = await this.getAllDataByCondition({ where: { sid, cid } });
  583. const _ = this.ctx.helper._;
  584. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  585. if (filter.length === 0) return filter;
  586. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  587. where: { id: _.uniq(_.map(filter, 'lid')) }
  588. });
  589. const pos = await this.ctx.service.pos.getAllDataByCondition({
  590. where: { id: _.uniq(_.map(filter, 'pid')) }
  591. });
  592. return filter.map(x => {
  593. const b = bills.find(y => { return y.id === x.lid });
  594. const rela_b = b
  595. ? {ledger_id: b ? b.ledger_id : -1, l_code: b.b_code, l_name: b.name, l_unit: b.unit, l_up: b.unit_price,
  596. l_deal_qty: b.deal_qty, l_deal_tp: b.deal_tp, l_qty: b.quantity, l_tp: b.total_price, l_drawing_code: b.drawing_code}
  597. : { ledger_id: -1, l_code: '', l_name: '', l_unit: '', l_up: 0, l_deal_qty: 0, l_deal_tp: 0, l_qty: 0, l_tp: 0, l_drawing_code: '' };
  598. const p = pos.find(y => { return y.id === x.pid });
  599. const rela_p = p ? { p_name: p.name, p_drawing_code: p.drawing_code, p_qty: p.quantity } : { p_name: '', p_drawing_code: '', p_qty: 0 };
  600. return { ...x, ...rela_b, ...rela_p };
  601. });
  602. }
  603. /**
  604. * 获取 本期 使用的变更令
  605. * @param sid
  606. * @return {Promise<void>}
  607. */
  608. async getStageUsedChangeId(sid) {
  609. const sql = 'SELECT lid, pid, cid, cbid, qty, stimes, sorder FROM ' + this.tableName + ' WHERE sid = ?';
  610. const curAll = await this.db.query(sql, [sid]);
  611. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  612. return this._.map(this._.filter(cur, 'qty'), 'cid');
  613. }
  614. async getFinalStageData(tid, sid) {
  615. const data = await this.getAllDataByCondition({ where: { tid, sid } });
  616. return this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  617. }
  618. async getSumLoadFinalData(sid) {
  619. const sql = 'Select cf.tid, cf.sid, cf.lid, cf.pid, cf.cid, cf.cbid, cf.qty, cf.stimes, cf.sorder, cf.minus, cf.no_value, c.code As c_code' +
  620. ' FROM ' + this.tableName + ' cf' +
  621. ' Left Join ' + this.ctx.service.change.tableName + ' c ON cf.cid = c.cid' +
  622. ' Where cf.sid = ?';
  623. const result = await this.db.query(sql, [sid]);
  624. return this.ctx.helper.filterLastestData(result, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  625. }
  626. async _getTender(stage) {
  627. if (this.ctx.tender) return this.ctx.tender;
  628. const tender = { id: stage.tid };
  629. tender.data = await this.ctx.service.tender.getTender(stage.tid);
  630. tender.info = await this.service.tenderInfo.getTenderInfo(tender.id);
  631. return tender;
  632. }
  633. async getSubtotal(stage) {
  634. const helper = this.ctx.helper;
  635. const tender = await this._getTender(stage);
  636. const sql = 'SELECT sc.*, c.quality FROM ' + this.tableName + ' sc' +
  637. ' LEFT JOIN ' + this.ctx.service.change.tableName + ' c ON sc.cid = c.cid' +
  638. ' WHERE sid = ? ' + (stage.readOnly ? ` and (stimes < ${stage.curTimes} or (stimes = ${stage.curTimes} and sorder <= ${stage.curOrder}))` : '');
  639. let data = await this.db.query(sql, [stage.id]);
  640. data = helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  641. const bqData = [];
  642. for (const d of data) {
  643. if (!d.qty || d.no_value) continue;
  644. let bd = bqData.find(x => { return x.lid === d.lid && x.quality === d.quality; });
  645. if (!bd) {
  646. bd = { lid: d.lid, quality: d.quality, unit_price: d.unit_price };
  647. bqData.push(bd);
  648. }
  649. const tp = this.ctx.helper.mul(d.qty, bd.unit_price, tender.info.decimal.tp);
  650. bd.tp = this.ctx.helper.add(bd.tp, tp);
  651. }
  652. const result = {};
  653. result.common = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.common.value; }), 'tp'));
  654. result.more = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.more.value; }), 'tp'));
  655. result.great = helper.sum(helper._.map(bqData.filter(x => {return x.quality === changeConst.quality.great.value; }), 'tp'));
  656. return result;
  657. }
  658. async getChangeBillsWithUsedInfo(stage) {
  659. if (stage.status === audit.stage.status.checked) {
  660. const sql = 'SELECT scf.* ' +
  661. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  662. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  663. ' WHERE scf.tid = ? And s.order <= ?';
  664. const result = await this.db.query(sql, [stage.tid, stage.order]);
  665. return result;
  666. } else {
  667. const preSql = 'SELECT scf.* ' +
  668. ' FROM ' + this.ctx.service.stageChangeFinal.tableName + ' scf ' +
  669. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s ON scf.sid = s.id' +
  670. ' WHERE scf.tid = ? And s.order < ?';
  671. const pre = await this.db.query(preSql, [stage.tid, stage.order]);
  672. const sql = 'SELECT * FROM ' + this.tableName + ' WHERE sid = ? AND (stimes * 100 + sorder) <= (? * 100 + ?)';
  673. const curAll = await this.db.query(sql, [stage.id, stage.curTimes, stage.curOrder]);
  674. const cur = this.ctx.helper.filterLastestData(curAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  675. return [...pre, ...cur];
  676. }
  677. }
  678. async getChangeWithUsedInfo(stage) {
  679. const change = await this.ctx.service.change.getAllDataByCondition({
  680. where: { tid: stage.tid, status: audit.flow.status.checked },
  681. orders: [['code', 'asc']],
  682. });
  683. if (change.length === 0) return [];
  684. const changeBills = await this.ctx.service.changeAuditList.getAllDataByCondition({
  685. where: { cid: change.map(x => { return x.cid; }) }
  686. });
  687. const changeBillsIndex = {}, changeBillsPart = {};
  688. for (const cb of changeBills) {
  689. changeBillsIndex[cb.id] = cb;
  690. if (!changeBillsPart[cb.cid]) changeBillsPart[cb.cid] = [];
  691. changeBillsPart[cb.cid].push(cb);
  692. }
  693. const stageChangeBills = await this.getChangeBillsWithUsedInfo(stage);
  694. for (const scb of stageChangeBills) {
  695. if (!scb.qty) continue;
  696. const cb = changeBillsIndex[scb.cbid];
  697. if (cb) cb.used_qty = this.ctx.helper.add(cb.used_qty, scb.qty);
  698. }
  699. for (const cid in changeBillsPart) {
  700. const c = change.find(x => { return x.cid === cid });
  701. if (!c) continue;
  702. for (const cb of changeBillsPart[cid]) {
  703. cb.tp = this.ctx.helper.mul(cb.spamount, cb.unit_price, c.tp_decimal || this.ctx.tender.info.decimal.tp);
  704. cb.used_tp = this.ctx.helper.mul(cb.used_qty, cb.unit_price, this.ctx.tender.info.decimal.tp);
  705. c.used_tp = this.ctx.helper.add(c.used_tp, cb.used_tp);
  706. if (cb.spamount > 0) {
  707. c.p_tp = this.ctx.helper.add(c.p_tp, cb.tp);
  708. c.p_used_tp = this.ctx.helper.add(c.p_used_tp, cb.used_tp);
  709. } else if (cb.spamount < 0){
  710. c.n_tp = this.ctx.helper.add(c.n_tp, cb.tp);
  711. c.n_used_tp = this.ctx.helper.add(c.n_used_tp, cb.used_tp);
  712. }
  713. }
  714. c.used_pt = c.total_price ? this.ctx.helper.mul(this.ctx.helper.div(c.used_tp, c.total_price, 4), 100) : 0;
  715. c.p_used_pt = c.p_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.p_used_tp, c.p_tp, 4), 100) : 0;
  716. c.n_used_pt = c.n_tp ? this.ctx.helper.mul(this.ctx.helper.div(c.n_used_tp, c.n_tp, 4), 100) : 0;
  717. }
  718. return change;
  719. }
  720. async getStageMinusChange(stage) {
  721. const data = await this.getAllDataByCondition({ where: { sid: stage.id, minus: 1 } });
  722. return this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  723. };
  724. async getBillsMinusQty(stage, lid) {
  725. const data = await this.getAllDataByCondition({ where: { sid: stage.id, lid, minus: 1 } });
  726. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  727. return { lid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
  728. };
  729. async getPosMinusQty(stage, pid) {
  730. const data = await this.getAllDataByCondition({ where: { sid: stage.id, pid, minus: 1 } });
  731. const filter = this.ctx.helper.filterLastestData(data, ['lid', 'pid', 'cbid'], 'stimes', 'sorder');
  732. return { pid, qty: this.ctx.helper.sum(filter.map(x => { return x.qty; })) };
  733. };
  734. async autoUseChangeBills(tender, stage, data) {
  735. const lid = [], cbid = [];
  736. data.forEach(x => {
  737. lid.push(x.lid);
  738. cbid.push(x.cbid);
  739. });
  740. const validChangeBills = await this.ctx.service.stageChangeFinal.getListChangeBillsValidQty(tender.id, cbid);
  741. validChangeBills.forEach(x => {
  742. x.billsPos = data.find(y => { return x.id === y.cbid; });
  743. });
  744. const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({
  745. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price'],
  746. where: { id: lid },
  747. });
  748. const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
  749. this.ctx.helper.assignRelaData(ledgerData, [
  750. { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  751. ]);
  752. const posData = await this.ctx.service.pos.getAllDataByCondition({
  753. columns: ['id', 'lid', 'name', 'porder'],
  754. where: { lid },
  755. });
  756. const stageBills = await this.ctx.service.stageBills.getAllDataByCondition({ where: { sid: stage.id } });
  757. const stagePos = await this.ctx.service.stagePos.getAllDataByCondition({ where: { sid: stage.id } });
  758. const stageChange = await this.ctx.service.stageChange.getAllDataByCondition({ where: { sid: stage.id, lid }});
  759. const useModal = new autoUseChange(this.ctx.helper, tender.info);
  760. useModal.use({ledgerData, posData, stageBills, stagePos, stageChange, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } }, validChangeBills);
  761. const conn = await this.db.beginTransaction();
  762. try {
  763. if (useModal.insertBills.length > 0) await conn.insert(this.ctx.service.stageBills.tableName, useModal.insertBills);
  764. if (useModal.updateBills.length > 0) await conn.updateRows(this.ctx.service.stageBills.tableName, useModal.updateBills);
  765. if (useModal.insertPos.length > 0) await conn.insert(this.ctx.service.stagePos.tableName, useModal.insertPos);
  766. if (useModal.updatePos.length > 0) await conn.updateRows(this.ctx.service.stagePos.tableName, useModal.updatePos);
  767. if (useModal.insertChange.length > 0) await conn.insert(this.tableName, useModal.insertChange);
  768. if (useModal.updateChange.length > 0) await conn.updateRows(this.tableName, useModal.updateChange);
  769. await conn.commit();
  770. } catch (err) {
  771. await conn.rollback();
  772. this.ctx.log(err);
  773. throw '保存导入数据失败';
  774. }
  775. const rst = { bills: {}, pos: {} };
  776. rst.bills.curStageData = await this.ctx.service.stageBills.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, lid);
  777. rst.pos.curStageData = await this.ctx.service.stagePos.getLastestStageData2(this.ctx.tender.id, this.ctx.stage.id, { lid });
  778. return rst;
  779. };
  780. async autoUseAllChange(tender, stage) {
  781. const validChangeBills = await this.ctx.service.stageChangeFinal.getAllChangeBillsValidQty(tender.id);
  782. const ledgerData = await this.ctx.service.ledger.getAllDataByCondition({
  783. columns: ['id', 'ledger_id', 'ledger_pid', 'level', 'order', 'full_path', 'is_leaf', 'code', 'b_code', 'name', 'unit', 'unit_price'],
  784. where: { tender_id: stage.tid },
  785. });
  786. const extraData = await this.ctx.service.ledgerExtra.getData(this.ctx.tender.id, ['is_tp']);
  787. const settleStatusBills = stage.readySettle ? await this.ctx.service.settleBills.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  788. this.ctx.helper.assignRelaData(ledgerData, [
  789. { data: extraData, fields: ['is_tp'], prefix: '', relaId: 'id' },
  790. { data: settleStatusBills, fields: ['settle_status'], prefix: '', relaId: 'lid' },
  791. ]);
  792. const posData = await this.ctx.service.pos.getAllDataByCondition({
  793. columns: ['id', 'lid', 'name', 'porder'],
  794. where: { tid: stage.tid },
  795. });
  796. const settleStatusPos = stage.readySettle ? await this.ctx.service.settlePos.getAllDataByCondition({ where: { settle_id: stage.readySettle.id }}) : [];
  797. this.ctx.helper.assignRelaData(posData, [
  798. { data: settleStatusPos, fields: ['settle_status'], prefix: '', relaId: 'pid' },
  799. ]);
  800. const stageBills = await this.ctx.service.stageBills.getAllDataByCondition({ where: { sid: stage.id } });
  801. const stagePos = await this.ctx.service.stagePos.getAllDataByCondition({ where: { sid: stage.id } });
  802. const useModal = new autoUseChange(this.ctx.helper, tender.info, this.ctx.service.settle.settleStatus);
  803. useModal.use({ledgerData, posData, stageBills, stagePos, default: { tid: stage.tid, sid: stage.id, said: this.ctx.session.sessionUser.accountId } }, validChangeBills);
  804. // if (useModal.insertChange.length === 0) return '无可调用的清单或计量单元';
  805. const conn = await this.db.beginTransaction();
  806. try {
  807. if (useModal.insertBills.length > 0) await conn.insert(this.ctx.service.stageBills.tableName, useModal.insertBills);
  808. if (useModal.updateBills.length > 0) await conn.updateRows(this.ctx.service.stageBills.tableName, useModal.updateBills);
  809. if (useModal.insertPos.length > 0) await conn.insert(this.ctx.service.stagePos.tableName, useModal.insertPos);
  810. if (useModal.updatePos.length > 0) await conn.updateRows(this.ctx.service.stagePos.tableName, useModal.updatePos);
  811. await conn.delete(this.tableName, { sid: stage.id });
  812. if (useModal.insertChange.length > 0) await conn.insert(this.tableName, useModal.insertChange);
  813. await conn.commit();
  814. } catch (err) {
  815. await conn.rollback();
  816. this.ctx.log(err);
  817. throw '保存导入数据失败';
  818. }
  819. }
  820. async updateStageChange4CheckCancel(sid, newTimes, newOrder, oldTimes, oldOrder, transaction) {
  821. const oldSBLists = await this.getAllDataByCondition({ where: { sid, stimes: oldTimes, sorder: oldOrder } });
  822. const newSBLists = await this.getAllDataByCondition({ where: { sid, stimes: newTimes, sorder: newOrder } });
  823. const delidList = [];
  824. for (const o of oldSBLists) {
  825. const newSBInfo = this._.find(newSBLists, { lid: o.lid });
  826. if (newSBInfo) {
  827. // 删除
  828. delidList.push(newSBInfo.id);
  829. }
  830. }
  831. if (delidList.length > 0) await transaction.delete(this.tableName, { id: delidList });
  832. if (oldSBLists.length > 0) {
  833. await transaction.update(this.tableName, {
  834. stimes: newTimes,
  835. sorder: newOrder,
  836. }, { where: { id: this._.map(oldSBLists, 'id') } });
  837. }
  838. }
  839. }
  840. return StageChange;
  841. };