material_list_self.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. 'use strict';
  2. /**
  3. * 单独计量单元设置-清单关联表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. module.exports = app => {
  10. class MaterialListSelf 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_self';
  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 newListSelf = {
  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 ? data.mx_id : '',
  37. in_time: new Date(),
  38. };
  39. // 更新list表为is_self为1
  40. await transaction.update(this.ctx.service.materialList.tableName, { is_self: 1 }, {
  41. where: {
  42. tid: this.ctx.tender.id,
  43. mid: this.ctx.material.id,
  44. gcl_id: data.gcl_id,
  45. xmj_id: data.id,
  46. mx_id: data.mx_id ? data.mx_id : '',
  47. },
  48. });
  49. // data.xmj_id = data.id;
  50. // data.mx_id = data.mx_id !== undefined ? data.mx_id : null;
  51. // await this.updateAllMaterials(transaction, data, 'add');
  52. // 新增不参与调差清单
  53. const result = await transaction.insert(this.tableName, newListSelf);
  54. if (result.affectedRows === 0) {
  55. throw '新增不参与调差清单数据失败';
  56. }
  57. await transaction.commit();
  58. return await this.getDataById(result.insertId);
  59. } catch (err) {
  60. await transaction.rollback();
  61. throw err;
  62. }
  63. }
  64. /**
  65. * 删除单独计量的清单
  66. * @param {int} id 工料id
  67. * @return {void}
  68. */
  69. async del(id) {
  70. if (!this.ctx.tender || !this.ctx.material) {
  71. throw '数据错误';
  72. }
  73. const transaction = await this.db.beginTransaction();
  74. try {
  75. const selfInfo = await this.getDataById(id);
  76. await this.updateAllMaterials(transaction, selfInfo);
  77. // 判断是否可删
  78. const result = await transaction.delete(this.tableName, { id });
  79. await transaction.commit();
  80. return result;
  81. } catch (err) {
  82. await transaction.rollback();
  83. throw err;
  84. }
  85. }
  86. /**
  87. * 修改调差list和bill和material对应值
  88. * @param transaction
  89. * @returns {Promise<void>}
  90. */
  91. async updateAllMaterials(transaction, data, type) {
  92. // 先判断material_list是否存在值并quantity不为null
  93. const searchSql = {
  94. mid: this.ctx.material.id,
  95. gcl_id: data.gcl_id,
  96. xmj_id: data.xmj_id,
  97. mx_id: data.mx_id,
  98. };
  99. // if (data.mx_id !== null) {
  100. // searchSql.mx_id = data.mx_id;
  101. // }
  102. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  103. where: searchSql,
  104. });
  105. if (materialListData && materialListData.length > 0) {
  106. const gather_qty = materialListData[0].gather_qty;
  107. const is_join = materialListData[0].is_join;
  108. const mbIdList = [];
  109. for (const ml of materialListData) {
  110. if (mbIdList.indexOf(ml.mb_id) === -1) {
  111. mbIdList.push(ml.mb_id);
  112. }
  113. }
  114. await transaction.delete(this.ctx.service.materialList.tableName, {
  115. tid: this.ctx.tender.id,
  116. mid: this.ctx.material.id,
  117. gcl_id: data.gcl_id,
  118. xmj_id: data.xmj_id,
  119. mx_id: data.mx_id ? data.mx_id : '',
  120. });
  121. const materialListGclData = await this.ctx.service.materialListGcl.getAllDataByCondition({
  122. where: { tid: this.ctx.tender.id, gcl_id: data.gcl_id },
  123. });
  124. const insertList = [];
  125. for (const m of materialListGclData) {
  126. insertList.push({
  127. tid: this.ctx.tender.id,
  128. order: m.order,
  129. mid: this.ctx.material.id,
  130. mb_id: m.mb_id,
  131. gcl_id: data.gcl_id,
  132. xmj_id: data.xmj_id,
  133. mx_id: data.mx_id,
  134. gather_qty,
  135. quantity: m.quantity,
  136. expr: m.expr,
  137. is_join,
  138. is_self: 0,
  139. in_time: new Date(),
  140. });
  141. }
  142. if (insertList.length > 0) await transaction.insert(this.ctx.service.materialList.tableName, insertList);
  143. // 重新计算金额
  144. for (const mb_id of mbIdList) {
  145. await this.service.materialList.calcQuantityByML(transaction, mb_id);
  146. }
  147. }
  148. }
  149. /**
  150. * 复制上一期不参与调差的清单到下一期中
  151. * @param {Object} transaction - 新增一期的事务
  152. * @param {Object} list 上期清单
  153. * @param {int} mid 工料id
  154. * @return {void}
  155. */
  156. async copyNewStageSelfList(transaction, list, mid) {
  157. if (!this.ctx.tender) {
  158. throw '数据错误';
  159. }
  160. const selfList = [];
  161. for (const mb of list) {
  162. const newLists = {
  163. tid: mb.tid,
  164. mid,
  165. gcl_id: mb.gcl_id,
  166. xmj_id: mb.xmj_id,
  167. mx_id: mb.mx_id,
  168. in_time: new Date(),
  169. };
  170. selfList.push(newLists);
  171. }
  172. // 复制上一期不参与调差的清单
  173. return selfList.length > 0 ? await transaction.insert(this.tableName, selfList) : true;
  174. }
  175. }
  176. return MaterialListSelf;
  177. };