material_list_notjoin.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, ms_id = null) {
  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 ? data.mx_id : '',
  37. in_time: new Date(),
  38. };
  39. data.xmj_id = data.id;
  40. data.mx_id = data.mx_id ? data.mx_id : '';
  41. await this.updateAllMaterials(transaction, data, 'add', ms_id);
  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, ms_id = null) {
  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', ms_id);
  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, ms_id = null) {
  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. mx_id: data.mx_id,
  88. };
  89. // if (data.mx_id !== '') {
  90. // searchSql.mx_id = data.mx_id;
  91. // }
  92. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  93. where: searchSql,
  94. });
  95. // console.log(data);
  96. // console.log(materialListData);
  97. if (materialListData && materialListData.length !== 0) {
  98. // 对应修改更新本期应耗数量值和总的本期金额计算
  99. const mbIdList = [];
  100. for (const ml of materialListData) {
  101. const updateData = {
  102. id: ml.id,
  103. is_join: type === 'add' ? 0 : 1,
  104. };
  105. if (mbIdList.indexOf(ml.mb_id) === -1) {
  106. mbIdList.push(ml.mb_id);
  107. }
  108. await transaction.update(this.ctx.service.materialList.tableName, updateData);
  109. }
  110. // 重新计算金额
  111. for (const mb_id of mbIdList) {
  112. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  113. }
  114. }
  115. }
  116. /**
  117. * 复制上一期不参与调差的清单到下一期中
  118. * @param {Object} transaction - 新增一期的事务
  119. * @param {Object} list 上期清单
  120. * @param {int} mid 工料id
  121. * @return {void}
  122. */
  123. async copyNewStageNotJoinList(transaction, list, mid) {
  124. if (!this.ctx.tender) {
  125. throw '数据错误';
  126. }
  127. const notJoinlist = [];
  128. for (const mb of list) {
  129. const newLists = {
  130. tid: mb.tid,
  131. mid,
  132. gcl_id: mb.gcl_id,
  133. xmj_id: mb.xmj_id,
  134. mx_id: mb.mx_id,
  135. in_time: new Date(),
  136. };
  137. notJoinlist.push(newLists);
  138. }
  139. // 复制上一期不参与调差的清单
  140. return notJoinlist.length > 0 ? await transaction.insert(this.tableName, notJoinlist) : true;
  141. }
  142. async getJoinMsg(transaction, mid, datas) {
  143. const searchData = {
  144. tid: this.ctx.tender.id,
  145. mid,
  146. gcl_id: datas.gcl_id,
  147. xmj_id: datas.xmj_id,
  148. mx_id: datas.mx_id,
  149. };
  150. const info = await transaction.get(this.tableName, searchData);
  151. if (info) {
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. }
  157. return MaterialListNotJoin;
  158. };