material_list_self.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 !== undefined ? data.mx_id : null,
  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 : null,
  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. };
  98. if (data.mx_id !== null) {
  99. searchSql.mx_id = data.mx_id;
  100. }
  101. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  102. where: searchSql,
  103. });
  104. if (materialListData && materialListData.length > 0) {
  105. const gather_qty = materialListData[0].gather_qty;
  106. const is_join = materialListData[0].is_join;
  107. const mbIdList = [];
  108. for (const ml of materialListData) {
  109. if (mbIdList.indexOf(ml.mb_id) === -1) {
  110. mbIdList.push(ml.mb_id);
  111. }
  112. }
  113. await transaction.delete(this.ctx.service.materialList.tableName, {
  114. tid: this.ctx.tender.id,
  115. mid: this.ctx.material.id,
  116. gcl_id: data.gcl_id,
  117. xmj_id: data.xmj_id,
  118. mx_id: data.mx_id ? data.mx_id : null,
  119. });
  120. const materialListGclData = await this.ctx.service.materialListGcl.getAllDataByCondition({
  121. where: { tid: this.ctx.tender.id, gcl_id: data.gcl_id },
  122. });
  123. const insertList = [];
  124. for (const m of materialListGclData) {
  125. insertList.push({
  126. tid: this.ctx.tender.id,
  127. order: m.order,
  128. mid: this.ctx.material.id,
  129. mb_id: m.mb_id,
  130. gcl_id: data.gcl_id,
  131. xmj_id: data.xmj_id,
  132. mx_id: data.mx_id,
  133. gather_qty,
  134. quantity: m.quantity,
  135. expr: m.expr,
  136. is_join,
  137. is_self: 0,
  138. in_time: new Date(),
  139. });
  140. }
  141. if (insertList.length > 0) await transaction.insert(this.ctx.service.materialList.tableName, insertList);
  142. // 重新计算金额
  143. for (const mb_id of mbIdList) {
  144. await this.service.materialList.calcQuantityByML(transaction, mb_id);
  145. }
  146. }
  147. }
  148. /**
  149. * 复制上一期不参与调差的清单到下一期中
  150. * @param {Object} transaction - 新增一期的事务
  151. * @param {Object} list 上期清单
  152. * @param {int} mid 工料id
  153. * @return {void}
  154. */
  155. async copyNewStageSelfList(transaction, list, mid) {
  156. if (!this.ctx.tender) {
  157. throw '数据错误';
  158. }
  159. const selfList = [];
  160. for (const mb of list) {
  161. const newLists = {
  162. tid: mb.tid,
  163. mid,
  164. gcl_id: mb.gcl_id,
  165. xmj_id: mb.xmj_id,
  166. mx_id: mb.mx_id,
  167. in_time: new Date(),
  168. };
  169. selfList.push(newLists);
  170. }
  171. // 复制上一期不参与调差的清单
  172. return selfList.length > 0 ? await transaction.insert(this.tableName, selfList) : true;
  173. }
  174. }
  175. return MaterialListSelf;
  176. };