material_list_notjoin.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. 'use strict';
  2. /**
  3. * 不参与调差-清单关联表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. module.exports = app => {
  10. class MaterialListNotJoin extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'material_list_notjoin';
  20. }
  21. /**
  22. * 添加不参与调差的清单
  23. * @return {void}
  24. */
  25. async add(data) {
  26. if (!this.ctx.tender || !this.ctx.material) {
  27. throw '数据错误';
  28. }
  29. const transaction = await this.db.beginTransaction();
  30. try {
  31. const newListNotJoin = {
  32. tid: this.ctx.tender.id,
  33. mid: this.ctx.material.id,
  34. gcl_id: data.gcl_id,
  35. xmj_id: data.id,
  36. mx_id: data.mx_id !== undefined ? data.mx_id : null,
  37. in_time: new Date(),
  38. };
  39. data.xmj_id = data.id;
  40. data.mx_id = data.mx_id !== undefined ? data.mx_id : null;
  41. await this.updateAllMaterials(transaction, data, 'add');
  42. // 新增不参与调差清单
  43. const result = await transaction.insert(this.tableName, newListNotJoin);
  44. if (result.affectedRows === 0) {
  45. throw '新增不参与调差清单数据失败';
  46. }
  47. await transaction.commit();
  48. return await this.getDataById(result.insertId);
  49. } catch (err) {
  50. await transaction.rollback();
  51. throw err;
  52. }
  53. }
  54. /**
  55. * 删除不参与调差的清单
  56. * @param {int} id 工料id
  57. * @return {void}
  58. */
  59. async del(id) {
  60. if (!this.ctx.tender || !this.ctx.material) {
  61. throw '数据错误';
  62. }
  63. const transaction = await this.db.beginTransaction();
  64. try {
  65. const notJoinInfo = await this.getDataById(id);
  66. await this.updateAllMaterials(transaction, notJoinInfo, 'del');
  67. // 判断是否可删
  68. const result = await transaction.delete(this.tableName, { id });
  69. await transaction.commit();
  70. return result;
  71. } catch (err) {
  72. await transaction.rollback();
  73. throw err;
  74. }
  75. }
  76. /**
  77. * 修改调差list和bill和material对应值
  78. * @param transaction
  79. * @returns {Promise<void>}
  80. */
  81. async updateAllMaterials(transaction, data, type) {
  82. // 先判断material_list是否存在值并quantity不为null
  83. const searchSql = {
  84. mid: this.ctx.material.id,
  85. gcl_id: data.gcl_id,
  86. xmj_id: data.xmj_id,
  87. };
  88. if (data.mx_id !== null) {
  89. searchSql.mx_id = data.mx_id;
  90. }
  91. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  92. where: searchSql,
  93. });
  94. console.log(data);
  95. console.log(materialListData);
  96. if (materialListData && materialListData.length !== 0) {
  97. // 对应修改更新本期应耗数量值和总的本期金额计算
  98. const mbIdList = [];
  99. for (const ml of materialListData) {
  100. const updateData = {
  101. id: ml.id,
  102. is_join: type === 'add' ? 0 : 1,
  103. };
  104. if (mbIdList.indexOf(ml.mb_id) === -1) {
  105. mbIdList.push(ml.mb_id);
  106. }
  107. await transaction.update(this.ctx.service.materialList.tableName, updateData);
  108. }
  109. // 重新计算金额
  110. for (const mb_id of mbIdList) {
  111. await this.service.materialList.calcQuantityByML(transaction, mb_id);
  112. }
  113. }
  114. }
  115. /**
  116. * 复制上一期不参与调差的清单到下一期中
  117. * @param {Object} transaction - 新增一期的事务
  118. * @param {Object} list 上期清单
  119. * @param {int} mid 工料id
  120. * @return {void}
  121. */
  122. async copyNewStageNotJoinList(transaction, list, mid) {
  123. if (!this.ctx.tender) {
  124. throw '数据错误';
  125. }
  126. const notJoinlist = [];
  127. for (const mb of list) {
  128. const newLists = {
  129. tid: mb.tid,
  130. mid,
  131. gcl_id: mb.gcl_id,
  132. xmj_id: mb.xmj_id,
  133. mx_id: mb.mx_id,
  134. in_time: new Date(),
  135. };
  136. notJoinlist.push(newLists);
  137. }
  138. // 复制上一期不参与调差的清单
  139. return notJoinlist.length > 0 ? await transaction.insert(this.tableName, notJoinlist) : true;
  140. }
  141. }
  142. return MaterialListNotJoin;
  143. };