material_list_self.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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, ms_id = null,) {
  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, ms_id);
  77. // 判断是否可删
  78. const result = await transaction.delete(this.tableName, { id });
  79. await transaction.commit();
  80. return await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  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, ms_id = null) {
  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 mbIdList = [];
  107. for (const ml of materialListData) {
  108. if (mbIdList.indexOf(ml.mb_id) === -1) {
  109. mbIdList.push(ml.mb_id);
  110. }
  111. }
  112. await transaction.delete(this.ctx.service.materialList.tableName, {
  113. tid: this.ctx.tender.id,
  114. mid: this.ctx.material.id,
  115. gcl_id: data.gcl_id,
  116. xmj_id: data.xmj_id,
  117. mx_id: data.mx_id ? data.mx_id : '',
  118. });
  119. const materialListGclData = await this.ctx.service.materialListGcl.getAllDataByCondition({
  120. where: { tid: this.ctx.tender.id, gcl_id: data.gcl_id },
  121. });
  122. const insertList = [];
  123. if (this.ctx.material.is_stage_self) {
  124. const materialStageList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  125. for (const ms of materialStageList) {
  126. const oneML = this._.filter(materialListData, { ms_id: ms.id });
  127. const gather_qty = oneML[0].gather_qty;
  128. const is_join = oneML[0].is_join;
  129. for (const m of materialListGclData) {
  130. insertList.push({
  131. tid: this.ctx.tender.id,
  132. order: m.order,
  133. mid: this.ctx.material.id,
  134. mb_id: m.mb_id,
  135. gcl_id: data.gcl_id,
  136. xmj_id: data.xmj_id,
  137. mx_id: data.mx_id,
  138. ms_id: ms.id,
  139. gather_qty,
  140. quantity: m.quantity,
  141. expr: m.expr,
  142. is_join,
  143. is_self: 0,
  144. in_time: new Date(),
  145. });
  146. }
  147. }
  148. } else {
  149. const gather_qty = materialListData[0].gather_qty;
  150. const is_join = materialListData[0].is_join;
  151. for (const m of materialListGclData) {
  152. insertList.push({
  153. tid: this.ctx.tender.id,
  154. order: m.order,
  155. mid: this.ctx.material.id,
  156. mb_id: m.mb_id,
  157. gcl_id: data.gcl_id,
  158. xmj_id: data.xmj_id,
  159. mx_id: data.mx_id,
  160. gather_qty,
  161. quantity: m.quantity,
  162. expr: m.expr,
  163. is_join,
  164. is_self: 0,
  165. in_time: new Date(),
  166. });
  167. }
  168. }
  169. if (insertList.length > 0) await transaction.insert(this.ctx.service.materialList.tableName, insertList);
  170. // 重新计算金额
  171. for (const mb_id of mbIdList) {
  172. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  173. }
  174. }
  175. }
  176. /**
  177. * 复制上一期不参与调差的清单到下一期中
  178. * @param {Object} transaction - 新增一期的事务
  179. * @param {Object} list 上期清单
  180. * @param {int} mid 工料id
  181. * @return {void}
  182. */
  183. async copyNewStageSelfList(transaction, list, mid) {
  184. if (!this.ctx.tender) {
  185. throw '数据错误';
  186. }
  187. const selfList = [];
  188. for (const mb of list) {
  189. const newLists = {
  190. tid: mb.tid,
  191. mid,
  192. gcl_id: mb.gcl_id,
  193. xmj_id: mb.xmj_id,
  194. mx_id: mb.mx_id,
  195. in_time: new Date(),
  196. };
  197. selfList.push(newLists);
  198. }
  199. // 复制上一期不参与调差的清单
  200. return selfList.length > 0 ? await transaction.insert(this.tableName, selfList) : true;
  201. }
  202. }
  203. return MaterialListSelf;
  204. };