material_list.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. 'use strict';
  2. /**
  3. * 调差清单关联工料表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. module.exports = app => {
  11. class MaterialList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_list';
  21. }
  22. /**
  23. * 添加工料清单关联
  24. * @return {void}
  25. */
  26. async add(data) {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const list = [];
  31. for (const mb of data.mb_id) {
  32. const newLists = {
  33. tid: this.ctx.tender.id,
  34. order: this.ctx.material.order,
  35. mid: this.ctx.material.id,
  36. mb_id: mb,
  37. gcl_id: data.gcl_id,
  38. xmj_id: data.xmj_id,
  39. mx_id: data.mx_id,
  40. gather_qty: data.gather_qty,
  41. is_join: data.is_join,
  42. is_self: 1,
  43. in_time: new Date(),
  44. };
  45. list.push(newLists);
  46. }
  47. // 新增工料
  48. const result = await this.db.insert(this.tableName, list);
  49. if (result.affectedRows === 0) {
  50. throw '新增工料数据失败';
  51. }
  52. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  53. }
  54. /**
  55. * 删除工料清单关联
  56. * @param {int} id 工料id
  57. * @return {void}
  58. */
  59. async del(id, mb_id) {
  60. if (!this.ctx.tender || !this.ctx.material) {
  61. throw '数据错误';
  62. }
  63. const transaction = await this.db.beginTransaction();
  64. try {
  65. // 判断是否可删
  66. await transaction.delete(this.tableName, { id });
  67. await this.calcQuantityByML(transaction, mb_id);
  68. await transaction.commit();
  69. return true;
  70. } catch (err) {
  71. await transaction.rollback();
  72. throw err;
  73. }
  74. }
  75. /**
  76. * 修改工料清单关联信息
  77. * @param {Object} data 工料内容
  78. * @param {int} order 期数
  79. * @return {void}
  80. */
  81. async save(data, order) {
  82. if (!this.ctx.tender || !this.ctx.material) {
  83. throw '数据错误';
  84. }
  85. const transaction = await this.db.beginTransaction();
  86. try {
  87. const mb_id = data.mb_id;
  88. delete data.mb_id;
  89. await transaction.update(this.tableName, data);
  90. await this.calcQuantityByML(transaction, mb_id);
  91. await transaction.commit();
  92. return true;
  93. } catch (err) {
  94. await transaction.rollback();
  95. throw err;
  96. }
  97. }
  98. /**
  99. * 修改工料信息
  100. * @param {Object} data 工料内容
  101. * @return {void}
  102. */
  103. async saveDatas(datas) {
  104. if (!this.ctx.tender || !this.ctx.material) {
  105. throw '数据错误';
  106. }
  107. // 判断是否可修改
  108. // 判断t_type是否为费用
  109. const transaction = await this.db.beginTransaction();
  110. try {
  111. for (const data of datas) {
  112. const mb_id = data.mb_id;
  113. delete data.mb_id;
  114. await transaction.update(this.tableName, data);
  115. await this.calcQuantityByML(transaction, mb_id);
  116. }
  117. await transaction.commit();
  118. return true;
  119. } catch (err) {
  120. await transaction.rollback();
  121. throw err;
  122. }
  123. }
  124. /**
  125. * 应用工料清单到其它清单中
  126. * @return {void}
  127. */
  128. async addOther(data) {
  129. if (!this.ctx.tender || !this.ctx.material) {
  130. throw '数据错误';
  131. }
  132. const transaction = await this.db.beginTransaction();
  133. try {
  134. // 先删除addxmj里所有的清单工料再添加新工料,并重新计算每个工料的单价数量
  135. // 还要找出删除的工料,更新单价数量
  136. const list = [];
  137. const delList = [];
  138. const materialBills = data.materialBills;
  139. const mb_idList = [];
  140. for (const xmj of data.addXmj) {
  141. const mlList = await this.getAllDataByCondition({
  142. where: {
  143. mid: this.ctx.material.id,
  144. gcl_id: xmj.gcl_id,
  145. xmj_id: xmj.id,
  146. mx_id: xmj.mx_id,
  147. },
  148. });
  149. const mbIdList = this._.map(mlList, 'mb_id');
  150. mb_idList.push(...mbIdList);
  151. delList.push(...this._.map(mlList, 'id'));
  152. for (const mb of materialBills) {
  153. const newLists = {
  154. tid: this.ctx.tender.id,
  155. order: mb.order,
  156. mid: this.ctx.material.id,
  157. mb_id: mb.mb_id,
  158. gcl_id: xmj.gcl_id,
  159. xmj_id: xmj.id,
  160. mx_id: xmj.mx_id,
  161. gather_qty: xmj.gather_qty ? xmj.gather_qty : null,
  162. quantity: mb.quantity,
  163. expr: mb.expr,
  164. in_time: new Date(),
  165. };
  166. list.push(newLists);
  167. mb_idList.push(mb.mb_id);
  168. }
  169. }
  170. // 删除工料清单关联
  171. if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  172. // 新增工料清单关联
  173. if (list.length > 0) {
  174. const result = await transaction.insert(this.tableName, list);
  175. if (result.affectedRows === 0) {
  176. throw '新增工料数据失败';
  177. }
  178. }
  179. // 重算工料和总金额
  180. const calcMBIdList = this._.uniq(mb_idList);
  181. if (calcMBIdList.length > 0) {
  182. for (const select of calcMBIdList) {
  183. await this.calcQuantityByML(transaction, select);
  184. }
  185. }
  186. // throw 'fail';
  187. // await this.calcQuantityByML(transaction, select.mb_id);
  188. await transaction.commit();
  189. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  190. } catch (err) {
  191. await transaction.rollback();
  192. throw err;
  193. }
  194. }
  195. /**
  196. * 修改material_bills的quantity值和计算本期金额
  197. * @param transaction
  198. * @param mb_id
  199. * @return {Promise<*>}
  200. */
  201. async calcQuantityByML(transaction, mb_id) {
  202. // 修改material_bills值
  203. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  204. if (!mbInfo) {
  205. throw '不存在该工料';
  206. }
  207. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  208. const sqlParam = [this.ctx.material.id, mb_id];
  209. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  210. console.log(mb_quantity);
  211. const newQuantity = this.ctx.helper.round(mb_quantity.quantity, this.ctx.material.decimal.qty);
  212. const newTp = this.ctx.helper.round(this.ctx.helper.mul(newQuantity, mbInfo.m_spread), this.ctx.material.decimal.tp);
  213. const updateData = {
  214. id: mb_id,
  215. quantity: newQuantity,
  216. m_tp: newTp,
  217. m_tax_tp: this.ctx.helper.round(this.ctx.helper.mul(newTp, (1 + this.ctx.helper.div(mbInfo.m_tax, 100))), this.ctx.material.decimal.tp),
  218. };
  219. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  220. // 计算本期总金额
  221. const sql2 = 'SELECT SUM(`m_tp`) as total_price, SUM(IF(`m_tax_tp` is null, `m_tp`, `m_tax_tp`)) as tax_total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  222. const sqlParam2 = [this.ctx.tender.id];
  223. const tp = await transaction.queryOne(sql2, sqlParam2);
  224. console.log(tp);
  225. const updateData2 = {
  226. id: this.ctx.material.id,
  227. m_tp: tp.total_price,
  228. m_tax_tp: tp.tax_total_price,
  229. };
  230. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  231. }
  232. /**
  233. * 获取工料清单关联表
  234. * @param {int} tid 标段id
  235. * @param {Object} mid 期id
  236. * @return {void}
  237. */
  238. async getMaterialData(tid, mid) {
  239. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`, mb.m_spread' +
  240. ' FROM ' + this.tableName + ' as ml' +
  241. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  242. ' ON ml.`mb_id` = mb.`id`' +
  243. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  244. const sqlParam = [tid, mid];
  245. return await this.db.query(sql, sqlParam);
  246. }
  247. async getPreMaterialData(tid, mid) {
  248. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`expr`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`, mbh.m_spread' +
  249. ' FROM ' + this.tableName + ' as ml' +
  250. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb ON ml.`mb_id` = mb.`id`' +
  251. ' LEFT JOIN ' + this.ctx.service.materialBillsHistory.tableName + ' as mbh ON ml.`mb_id` = mbh.`mb_id` and mbh.mid = ?' +
  252. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  253. const sqlParam = [mid, tid, mid];
  254. return await this.db.query(sql, sqlParam);
  255. }
  256. /**
  257. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  258. * @param transaction
  259. * @param preMaterial
  260. * @param newMid
  261. * @return {Promise<void>}
  262. */
  263. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  264. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  265. const copyMLArray = [];
  266. for (const ml of materialListData) {
  267. // 获取小计值
  268. let gather_qty = null;
  269. if (ml.mx_id !== null && ml.mx_id !== '') {
  270. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  271. } else {
  272. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  273. }
  274. const newMaterialList = {
  275. tid: ml.tid,
  276. order: ml.order,
  277. mid: newMaterial.id,
  278. mb_id: ml.mb_id,
  279. gcl_id: ml.gcl_id,
  280. xmj_id: ml.xmj_id,
  281. mx_id: ml.mx_id,
  282. gather_qty,
  283. quantity: ml.quantity,
  284. expr: ml.expr,
  285. is_join: ml.is_join,
  286. in_time: new Date(),
  287. };
  288. copyMLArray.push(newMaterialList);
  289. }
  290. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  291. }
  292. /**
  293. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  294. * @param transaction
  295. * @param preMaterial
  296. * @param newMid
  297. * @return {Promise<void>}
  298. */
  299. async copyPreMaterialList2(transaction, materialListData, materialSelfListData, notJoinList, newMaterial) {
  300. if (materialListData && materialListData.length > 0) {
  301. const copyMLArray = [];
  302. for (const ml of materialListData) {
  303. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id });
  304. const newMaterialList = {
  305. tid: newMaterial.tid,
  306. order: ml.order,
  307. mid: newMaterial.id,
  308. mb_id: ml.mb_id,
  309. gcl_id: ml.gcl_id,
  310. xmj_id: ml.xmj_id,
  311. mx_id: ml.mx_id,
  312. gather_qty: ml.gather_qty,
  313. quantity: ml.quantity ? ml.quantity : 0,
  314. expr: ml.expr ? ml.expr : '',
  315. is_join: is_join ? 0 : 1,
  316. in_time: new Date(),
  317. };
  318. copyMLArray.push(newMaterialList);
  319. }
  320. if (copyMLArray.length !== 0) await transaction.insert(this.tableName, copyMLArray);
  321. }
  322. if (materialSelfListData && materialSelfListData.length > 0) {
  323. const copyMLArray2 = [];
  324. for (const ml of materialSelfListData) {
  325. const is_join = this._.find(notJoinList, { gcl_id: ml.gcl_id, xmj_id: ml.xmj_id, mx_id: ml.mx_id });
  326. const newMaterialList = {
  327. tid: newMaterial.tid,
  328. order: ml.order,
  329. mid: newMaterial.id,
  330. mb_id: ml.mb_id,
  331. gcl_id: ml.gcl_id,
  332. xmj_id: ml.xmj_id,
  333. mx_id: ml.mx_id,
  334. gather_qty: ml.gather_qty,
  335. quantity: ml.quantity ? ml.quantity : 0,
  336. expr: ml.expr ? ml.expr : '',
  337. is_join: is_join ? 0 : 1,
  338. is_self: 1,
  339. in_time: new Date(),
  340. };
  341. copyMLArray2.push(newMaterialList);
  342. }
  343. if (copyMLArray2.length !== 0) await transaction.insert(this.tableName, copyMLArray2);
  344. }
  345. }
  346. /**
  347. * 添加工料清单关联(多清单对应)
  348. * @return {void}
  349. */
  350. async adds(datas, checklist = false) {
  351. if (!this.ctx.tender || !this.ctx.material) {
  352. throw '数据错误';
  353. }
  354. const transaction = await this.db.beginTransaction();
  355. try {
  356. const list = [];
  357. const listGcl = [];
  358. // const delList = [];
  359. // const mb_idList = [];
  360. for (const xmj of datas.xmjs) {
  361. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  362. for (const mb of datas.mbIds) {
  363. // // 旧数据兼容问题,要去删除相同已存在的工料
  364. // const mlInfo = await this.getDataByCondition({
  365. // mid: this.ctx.material.id,
  366. // gcl_id: xmj.gcl_id,
  367. // xmj_id: xmj.xmj_id,
  368. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  369. // mb_id: mb,
  370. // });
  371. // if (mlInfo) {
  372. // delList.push(mlInfo.id);
  373. // mb_idList.push(mb);
  374. // }
  375. if (xmj.gather_qty && this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  376. const newLists = {
  377. tid: this.ctx.tender.id,
  378. order: this.ctx.material.order,
  379. mid: this.ctx.material.id,
  380. mb_id: mb,
  381. gcl_id: xmj.gcl_id,
  382. xmj_id: xmj.xmj_id,
  383. mx_id: xmj.mx_id,
  384. gather_qty: xmj.gather_qty,
  385. in_time: new Date(),
  386. is_join: xmj.is_join,
  387. };
  388. list.push(newLists);
  389. }
  390. if (this._.findIndex(listGcl, { gcl_id: xmj.gcl_id, mb_id: mb }) === -1) {
  391. const newListGcl = {
  392. tid: this.ctx.tender.id,
  393. order: this.ctx.material.order,
  394. mid: this.ctx.material.id,
  395. mb_id: mb,
  396. gcl_id: xmj.gcl_id,
  397. quantity: 0,
  398. expr: '',
  399. };
  400. listGcl.push(newListGcl);
  401. }
  402. }
  403. }
  404. // 维护list_gcl表
  405. // 删除工料清单关联
  406. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  407. // 新增工料清单关联
  408. if (list.length > 0) {
  409. const result = await transaction.insert(this.tableName, list);
  410. if (result.affectedRows === 0) {
  411. throw '新增工料数据失败';
  412. }
  413. }
  414. if (listGcl.length > 0) {
  415. const result2 = await transaction.insert(this.ctx.service.materialListGcl.tableName, listGcl);
  416. if (result2.affectedRows === 0) {
  417. throw '新增工料关联数据失败';
  418. }
  419. }
  420. if (checklist) {
  421. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  422. }
  423. // 重算工料和总金额
  424. // const calcMBIdList = this._.uniq(mb_idList);
  425. // if (calcMBIdList.length > 0) {
  426. // for (const select of calcMBIdList) {
  427. // await this.calcQuantityByML(transaction, select);
  428. // }
  429. // }
  430. await transaction.commit();
  431. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  432. return checklist ? gclList : {
  433. gclList,
  434. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  435. };
  436. } catch (err) {
  437. await transaction.rollback();
  438. throw err;
  439. }
  440. }
  441. /**
  442. * 删除工料清单关联(多清单对应)
  443. * @param {int} id 工料id
  444. * @return {void}
  445. */
  446. async dels(datas, checklist = false) {
  447. if (!this.ctx.tender || !this.ctx.material) {
  448. throw '数据错误';
  449. }
  450. const transaction = await this.db.beginTransaction();
  451. try {
  452. // 判断是否可删
  453. const listGcl = [];
  454. for (const xmj of datas.xmjs) {
  455. await transaction.delete(this.tableName, { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: datas.mb_id, gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id, is_self: 0 });
  456. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  457. await transaction.delete(this.service.materialListGcl.tableName, { tid: this.ctx.tender.id, mid: this.ctx.material.id, mb_id: datas.mb_id, gcl_id: xmj.gcl_id });
  458. listGcl.push(xmj.gcl_id);
  459. }
  460. }
  461. // await transaction.delete(this.tableName, { id });
  462. await this.calcQuantityByML(transaction, datas.mb_id);
  463. if (checklist) {
  464. await this.ctx.service.materialChecklist.updateHadBills(transaction, checklist.id, checklist.had_bills);
  465. }
  466. await transaction.commit();
  467. // console.log(datas);
  468. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  469. return checklist ? gclList : {
  470. gclList,
  471. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  472. };
  473. } catch (err) {
  474. await transaction.rollback();
  475. throw err;
  476. }
  477. }
  478. /**
  479. * 修改工料清单关联信息(多清单对应)
  480. * @param {Object} data 工料内容
  481. * @param {int} order 期数
  482. * @return {void}
  483. */
  484. async saves(datas, checklist = false) {
  485. if (!this.ctx.tender || !this.ctx.material) {
  486. throw '数据错误';
  487. }
  488. const transaction = await this.db.beginTransaction();
  489. try {
  490. const mb_id = datas.mb_id;
  491. const updateDatas = [];
  492. const updateListGcl = [];
  493. const listGcl = [];
  494. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  495. for (const xmj of datas.xmjs) {
  496. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  497. const udata = {
  498. row: {
  499. expr: datas.expr,
  500. quantity: datas.quantity,
  501. },
  502. where: {
  503. tid: this.ctx.tender.id,
  504. mid: this.ctx.material.id,
  505. mb_id,
  506. gcl_id: xmj.gcl_id,
  507. xmj_id: xmj.xmj_id,
  508. mx_id: xmj.mx_id,
  509. },
  510. };
  511. updateDatas.push(udata);
  512. }
  513. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  514. listGcl.push(xmj.gcl_id);
  515. updateListGcl.push({
  516. row: {
  517. expr: datas.expr,
  518. quantity: datas.quantity,
  519. },
  520. where: {
  521. tid: this.ctx.tender.id,
  522. // mid: this.ctx.material.id,
  523. mb_id,
  524. gcl_id: xmj.gcl_id,
  525. },
  526. });
  527. }
  528. }
  529. console.log(updateDatas);
  530. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  531. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  532. await this.calcQuantityByML(transaction, mb_id);
  533. await transaction.commit();
  534. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  535. return checklist ? gclList : {
  536. gclList,
  537. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  538. };
  539. } catch (err) {
  540. await transaction.rollback();
  541. throw err;
  542. }
  543. }
  544. /**
  545. * 复制粘贴多工料信息(多清单对应)
  546. * @param {Object} data 工料内容
  547. * @return {void}
  548. */
  549. async savePastes(datas, checklist = false) {
  550. if (!this.ctx.tender || !this.ctx.material) {
  551. throw '数据错误';
  552. }
  553. // 判断是否可修改
  554. // 判断t_type是否为费用
  555. const transaction = await this.db.beginTransaction();
  556. try {
  557. const selfList = await transaction.select(this.ctx.service.materialListSelf.tableName, { where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } });
  558. for (const data of datas.pasteData) {
  559. const updateDatas = [];
  560. const updateListGcl = [];
  561. const listGcl = [];
  562. for (const xmj of datas.xmjs) {
  563. if (this._.findIndex(selfList, { gcl_id: xmj.gcl_id, xmj_id: xmj.xmj_id, mx_id: xmj.mx_id }) === -1) {
  564. const udata = {
  565. row: {
  566. expr: data.expr,
  567. quantity: data.quantity,
  568. },
  569. where: {
  570. tid: this.ctx.tender.id,
  571. mid: this.ctx.material.id,
  572. mb_id: data.mb_id,
  573. gcl_id: xmj.gcl_id,
  574. xmj_id: xmj.xmj_id,
  575. mx_id: xmj.mx_id,
  576. },
  577. };
  578. updateDatas.push(udata);
  579. }
  580. if (this._.indexOf(listGcl, xmj.gcl_id) === -1) {
  581. listGcl.push(xmj.gcl_id);
  582. updateListGcl.push({
  583. row: {
  584. expr: data.expr,
  585. quantity: data.quantity,
  586. },
  587. where: {
  588. tid: this.ctx.tender.id,
  589. // mid: this.ctx.material.id,
  590. mb_id: data.mb_id,
  591. gcl_id: xmj.gcl_id,
  592. },
  593. });
  594. }
  595. }
  596. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  597. if (updateListGcl.length > 0) await transaction.updateRows(this.service.materialListGcl.tableName, updateListGcl);
  598. await this.calcQuantityByML(transaction, data.mb_id);
  599. }
  600. await transaction.commit();
  601. const gclList = await this.ctx.service.materialListGcl.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  602. return checklist ? gclList : {
  603. gclList,
  604. materialListData: await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  605. };
  606. } catch (err) {
  607. await transaction.rollback();
  608. throw err;
  609. }
  610. }
  611. }
  612. return MaterialList;
  613. };