material_list.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. in_time: new Date(),
  42. };
  43. list.push(newLists);
  44. }
  45. // 新增工料
  46. const result = await this.db.insert(this.tableName, list);
  47. if (result.affectedRows === 0) {
  48. throw '新增工料数据失败';
  49. }
  50. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  51. }
  52. /**
  53. * 删除工料清单关联
  54. * @param {int} id 工料id
  55. * @return {void}
  56. */
  57. async del(id, mb_id) {
  58. if (!this.ctx.tender || !this.ctx.material) {
  59. throw '数据错误';
  60. }
  61. const transaction = await this.db.beginTransaction();
  62. try {
  63. // 判断是否可删
  64. await transaction.delete(this.tableName, { id });
  65. await this.calcQuantityByML(transaction, mb_id);
  66. await transaction.commit();
  67. return true;
  68. } catch (err) {
  69. await transaction.rollback();
  70. throw err;
  71. }
  72. }
  73. /**
  74. * 修改工料清单关联信息
  75. * @param {Object} data 工料内容
  76. * @param {int} order 期数
  77. * @return {void}
  78. */
  79. async save(data, order) {
  80. if (!this.ctx.tender || !this.ctx.material) {
  81. throw '数据错误';
  82. }
  83. const transaction = await this.db.beginTransaction();
  84. try {
  85. const mb_id = data.mb_id;
  86. delete data.mb_id;
  87. await transaction.update(this.tableName, data);
  88. await this.calcQuantityByML(transaction, mb_id);
  89. await transaction.commit();
  90. return true;
  91. } catch (err) {
  92. await transaction.rollback();
  93. throw err;
  94. }
  95. }
  96. /**
  97. * 修改工料信息
  98. * @param {Object} data 工料内容
  99. * @return {void}
  100. */
  101. async saveDatas(datas) {
  102. if (!this.ctx.tender || !this.ctx.material) {
  103. throw '数据错误';
  104. }
  105. // 判断是否可修改
  106. // 判断t_type是否为费用
  107. const transaction = await this.db.beginTransaction();
  108. try {
  109. for (const data of datas) {
  110. const mb_id = data.mb_id;
  111. delete data.mb_id;
  112. await transaction.update(this.tableName, data);
  113. await this.calcQuantityByML(transaction, mb_id);
  114. }
  115. await transaction.commit();
  116. return true;
  117. } catch (err) {
  118. await transaction.rollback();
  119. throw err;
  120. }
  121. }
  122. /**
  123. * 应用工料清单到其它清单中
  124. * @return {void}
  125. */
  126. async addOther(data) {
  127. if (!this.ctx.tender || !this.ctx.material) {
  128. throw '数据错误';
  129. }
  130. const transaction = await this.db.beginTransaction();
  131. try {
  132. // 先删除addxmj里所有的清单工料再添加新工料,并重新计算每个工料的单价数量
  133. // 还要找出删除的工料,更新单价数量
  134. const list = [];
  135. const delList = [];
  136. const materialBills = data.materialBills;
  137. const mb_idList = [];
  138. for (const xmj of data.addXmj) {
  139. const mlList = await this.getAllDataByCondition({
  140. where: {
  141. mid: this.ctx.material.id,
  142. gcl_id: xmj.gcl_id,
  143. xmj_id: xmj.id,
  144. mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  145. },
  146. });
  147. const mbIdList = this._.map(mlList, 'mb_id');
  148. mb_idList.push(...mbIdList);
  149. delList.push(...this._.map(mlList, 'id'));
  150. for (const mb of materialBills) {
  151. const newLists = {
  152. tid: this.ctx.tender.id,
  153. order: this.ctx.material.order,
  154. mid: this.ctx.material.id,
  155. mb_id: mb.mb_id,
  156. gcl_id: xmj.gcl_id,
  157. xmj_id: xmj.id,
  158. mx_id: xmj.mx_id ? xmj.mx_id : '',
  159. gather_qty: xmj.gather_qty ? xmj.gather_qty : null,
  160. quantity: mb.quantity,
  161. in_time: new Date(),
  162. };
  163. list.push(newLists);
  164. mb_idList.push(mb.mb_id);
  165. }
  166. }
  167. // 删除工料清单关联
  168. if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  169. // 新增工料清单关联
  170. if (list.length > 0) {
  171. const result = await transaction.insert(this.tableName, list);
  172. if (result.affectedRows === 0) {
  173. throw '新增工料数据失败';
  174. }
  175. }
  176. // 重算工料和总金额
  177. const calcMBIdList = this._.uniq(mb_idList);
  178. if (calcMBIdList.length > 0) {
  179. for (const select of calcMBIdList) {
  180. await this.calcQuantityByML(transaction, select);
  181. }
  182. }
  183. // throw 'fail';
  184. // await this.calcQuantityByML(transaction, select.mb_id);
  185. await transaction.commit();
  186. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  187. } catch (err) {
  188. await transaction.rollback();
  189. throw err;
  190. }
  191. }
  192. /**
  193. * 修改material_bills的quantity值和计算本期金额
  194. * @param transaction
  195. * @param mb_id
  196. * @return {Promise<*>}
  197. */
  198. async calcQuantityByML(transaction, mb_id) {
  199. // 修改material_bills值
  200. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  201. if (!mbInfo) {
  202. throw '不存在该工料';
  203. }
  204. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=? AND `is_join`=1';
  205. const sqlParam = [this.ctx.material.id, mb_id];
  206. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  207. console.log(mb_quantity);
  208. const updateData = {
  209. id: mb_id,
  210. quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
  211. m_tp: this.ctx.helper.round(this.ctx.helper.mul(this.ctx.helper.round(mb_quantity.quantity, 3), mbInfo.m_spread), 2),
  212. };
  213. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  214. // 计算本期总金额
  215. const sql2 = 'SELECT SUM(`m_tp`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ? AND `is_summary` = 1';
  216. const sqlParam2 = [this.ctx.tender.id];
  217. const tp = await transaction.queryOne(sql2, sqlParam2);
  218. console.log(tp);
  219. const updateData2 = {
  220. id: this.ctx.material.id,
  221. m_tp: tp.total_price,
  222. };
  223. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  224. }
  225. /**
  226. * 获取工料清单关联表
  227. * @param {int} tid 标段id
  228. * @param {Object} mid 期id
  229. * @return {void}
  230. */
  231. async getMaterialData(tid, mid) {
  232. 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`' +
  233. ' FROM ' + this.tableName + ' as ml' +
  234. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  235. ' ON ml.`mb_id` = mb.`id`' +
  236. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  237. const sqlParam = [tid, mid];
  238. return await this.db.query(sql, sqlParam);
  239. }
  240. /**
  241. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  242. * @param transaction
  243. * @param preMaterial
  244. * @param newMid
  245. * @return {Promise<void>}
  246. */
  247. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  248. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  249. const copyMLArray = [];
  250. for (const ml of materialListData) {
  251. // 获取小计值
  252. let gather_qty = null;
  253. if (ml.mx_id !== null && ml.mx_id !== '') {
  254. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  255. } else {
  256. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  257. }
  258. const newMaterialList = {
  259. tid: ml.tid,
  260. order: ml.order,
  261. mid: newMaterial.id,
  262. mb_id: ml.mb_id,
  263. gcl_id: ml.gcl_id,
  264. xmj_id: ml.xmj_id,
  265. mx_id: ml.mx_id,
  266. gather_qty,
  267. quantity: ml.quantity,
  268. expr: ml.expr,
  269. is_join: ml.is_join,
  270. in_time: new Date(),
  271. };
  272. copyMLArray.push(newMaterialList);
  273. }
  274. return copyMLArray.length !== 0 ? await transaction.insert(this.tableName, copyMLArray) : true;
  275. }
  276. /**
  277. * 添加工料清单关联(多清单对应)
  278. * @return {void}
  279. */
  280. async adds(datas) {
  281. if (!this.ctx.tender || !this.ctx.material) {
  282. throw '数据错误';
  283. }
  284. const transaction = await this.db.beginTransaction();
  285. try {
  286. const list = [];
  287. // const delList = [];
  288. // const mb_idList = [];
  289. for (const xmj of datas.xmjs) {
  290. for (const mb of datas.mbIds) {
  291. // // 旧数据兼容问题,要去删除相同已存在的工料
  292. // const mlInfo = await this.getDataByCondition({
  293. // mid: this.ctx.material.id,
  294. // gcl_id: xmj.gcl_id,
  295. // xmj_id: xmj.xmj_id,
  296. // mx_id: xmj.mx_id ? xmj.mx_id : [null, ''],
  297. // mb_id: mb,
  298. // });
  299. // if (mlInfo) {
  300. // delList.push(mlInfo.id);
  301. // mb_idList.push(mb);
  302. // }
  303. const newLists = {
  304. tid: this.ctx.tender.id,
  305. order: this.ctx.material.order,
  306. mid: this.ctx.material.id,
  307. mb_id: mb,
  308. gcl_id: xmj.gcl_id,
  309. xmj_id: xmj.xmj_id,
  310. mx_id: xmj.mx_id,
  311. gather_qty: xmj.gather_qty,
  312. in_time: new Date(),
  313. is_join: xmj.is_join,
  314. };
  315. list.push(newLists);
  316. }
  317. }
  318. // 删除工料清单关联
  319. // if (delList.length > 0) await transaction.delete(this.tableName, { id: delList });
  320. // 新增工料清单关联
  321. if (list.length > 0) {
  322. const result = await transaction.insert(this.tableName, list);
  323. if (result.affectedRows === 0) {
  324. throw '新增工料数据失败';
  325. }
  326. }
  327. // 重算工料和总金额
  328. // const calcMBIdList = this._.uniq(mb_idList);
  329. // if (calcMBIdList.length > 0) {
  330. // for (const select of calcMBIdList) {
  331. // await this.calcQuantityByML(transaction, select);
  332. // }
  333. // }
  334. await transaction.commit();
  335. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  336. } catch (err) {
  337. await transaction.rollback();
  338. throw err;
  339. }
  340. }
  341. /**
  342. * 删除工料清单关联(多清单对应)
  343. * @param {int} id 工料id
  344. * @return {void}
  345. */
  346. async dels(datas) {
  347. if (!this.ctx.tender || !this.ctx.material) {
  348. throw '数据错误';
  349. }
  350. const transaction = await this.db.beginTransaction();
  351. try {
  352. // 判断是否可删
  353. for (const xmj of datas.xmjs) {
  354. 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 });
  355. }
  356. // await transaction.delete(this.tableName, { id });
  357. await this.calcQuantityByML(transaction, datas.mb_id);
  358. await transaction.commit();
  359. // console.log(datas);
  360. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  361. } catch (err) {
  362. await transaction.rollback();
  363. throw err;
  364. }
  365. }
  366. /**
  367. * 修改工料清单关联信息(多清单对应)
  368. * @param {Object} data 工料内容
  369. * @param {int} order 期数
  370. * @return {void}
  371. */
  372. async saves(datas) {
  373. if (!this.ctx.tender || !this.ctx.material) {
  374. throw '数据错误';
  375. }
  376. const transaction = await this.db.beginTransaction();
  377. try {
  378. const mb_id = datas.mb_id;
  379. const updateDatas = [];
  380. for (const xmj of datas.xmjs) {
  381. const udata = {
  382. row: {
  383. expr: datas.expr,
  384. quantity: datas.quantity,
  385. },
  386. where: {
  387. tid: this.ctx.tender.id,
  388. mid: this.ctx.material.id,
  389. mb_id,
  390. gcl_id: xmj.gcl_id,
  391. xmj_id: xmj.xmj_id,
  392. mx_id: xmj.mx_id,
  393. },
  394. };
  395. updateDatas.push(udata);
  396. }
  397. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  398. await this.calcQuantityByML(transaction, mb_id);
  399. await transaction.commit();
  400. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  401. } catch (err) {
  402. await transaction.rollback();
  403. throw err;
  404. }
  405. }
  406. /**
  407. * 复制粘贴多工料信息(多清单对应)
  408. * @param {Object} data 工料内容
  409. * @return {void}
  410. */
  411. async savePastes(datas) {
  412. if (!this.ctx.tender || !this.ctx.material) {
  413. throw '数据错误';
  414. }
  415. // 判断是否可修改
  416. // 判断t_type是否为费用
  417. const transaction = await this.db.beginTransaction();
  418. try {
  419. for (const data of datas.pasteData) {
  420. const updateDatas = [];
  421. for (const xmj of datas.xmjs) {
  422. const udata = {
  423. row: {
  424. expr: data.expr,
  425. quantity: data.quantity,
  426. },
  427. where: {
  428. tid: this.ctx.tender.id,
  429. mid: this.ctx.material.id,
  430. mb_id: data.mb_id,
  431. gcl_id: xmj.gcl_id,
  432. xmj_id: xmj.xmj_id,
  433. mx_id: xmj.mx_id,
  434. },
  435. };
  436. updateDatas.push(udata);
  437. }
  438. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  439. await this.calcQuantityByML(transaction, data.mb_id);
  440. }
  441. await transaction.commit();
  442. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  443. } catch (err) {
  444. await transaction.rollback();
  445. throw err;
  446. }
  447. }
  448. }
  449. return MaterialList;
  450. };