material_list_self.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, gather_qty = 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. selfInfo.gather_qty = gather_qty;
  77. await this.updateAllMaterials(transaction, selfInfo, ms_id);
  78. // 判断是否可删
  79. const result = await transaction.delete(this.tableName, { id });
  80. await transaction.commit();
  81. return await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  82. } catch (err) {
  83. await transaction.rollback();
  84. throw err;
  85. }
  86. }
  87. /**
  88. * 修改调差list和bill和material对应值
  89. * @param transaction
  90. * @returns {Promise<void>}
  91. */
  92. async updateAllMaterials(transaction, data, ms_id = null) {
  93. // 先判断material_list是否存在值并quantity不为null
  94. const searchSql = {
  95. mid: this.ctx.material.id,
  96. gcl_id: data.gcl_id,
  97. xmj_id: data.xmj_id,
  98. mx_id: data.mx_id,
  99. };
  100. // if (data.mx_id !== null) {
  101. // searchSql.mx_id = data.mx_id;
  102. // }
  103. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  104. where: searchSql,
  105. });
  106. const mbIdList = [];
  107. if (materialListData && materialListData.length > 0) {
  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 : '',
  119. });
  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. const is_join = await this.ctx.service.materialListNotjoin.getJoinMsg(transaction, this.ctx.material.id, data);
  126. if (this.ctx.material.is_stage_self) {
  127. const materialStageList = await transaction.select(this.ctx.service.materialStage.tableName, { where: { mid: this.ctx.material.id } });
  128. for (const ms of materialStageList) {
  129. // const oneML = this._.filter(materialListData, { ms_id: ms.id });
  130. // const gather_qty = oneML[0].gather_qty;
  131. // const is_join = oneML[0].is_join;
  132. for (const m of materialListGclData) {
  133. insertList.push({
  134. tid: this.ctx.tender.id,
  135. order: m.order,
  136. mid: this.ctx.material.id,
  137. mb_id: m.mb_id,
  138. gcl_id: data.gcl_id,
  139. xmj_id: data.xmj_id,
  140. mx_id: data.mx_id,
  141. ms_id: ms.id,
  142. gather_qty: data.gather_qty[ms.id],
  143. quantity: m.quantity,
  144. expr: m.expr,
  145. is_join,
  146. is_self: 0,
  147. in_time: new Date(),
  148. });
  149. }
  150. }
  151. } else {
  152. // const gather_qty = materialListData[0].gather_qty;
  153. // const is_join = materialListData[0].is_join;
  154. for (const m of materialListGclData) {
  155. insertList.push({
  156. tid: this.ctx.tender.id,
  157. order: m.order,
  158. mid: this.ctx.material.id,
  159. mb_id: m.mb_id,
  160. gcl_id: data.gcl_id,
  161. xmj_id: data.xmj_id,
  162. mx_id: data.mx_id,
  163. gather_qty: data.gather_qty,
  164. quantity: m.quantity,
  165. expr: m.expr,
  166. is_join,
  167. is_self: 0,
  168. in_time: new Date(),
  169. });
  170. }
  171. }
  172. if (insertList.length > 0) await transaction.insert(this.ctx.service.materialList.tableName, insertList);
  173. // 重新计算金额
  174. for (const mb_id of mbIdList) {
  175. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  176. }
  177. }
  178. /**
  179. * 复制上一期不参与调差的清单到下一期中
  180. * @param {Object} transaction - 新增一期的事务
  181. * @param {Object} list 上期清单
  182. * @param {int} mid 工料id
  183. * @return {void}
  184. */
  185. async copyNewStageSelfList(transaction, list, mid) {
  186. if (!this.ctx.tender) {
  187. throw '数据错误';
  188. }
  189. const selfList = [];
  190. for (const mb of list) {
  191. const newLists = {
  192. tid: mb.tid,
  193. mid,
  194. gcl_id: mb.gcl_id,
  195. xmj_id: mb.xmj_id,
  196. mx_id: mb.mx_id,
  197. in_time: new Date(),
  198. };
  199. selfList.push(newLists);
  200. }
  201. // 复制上一期不参与调差的清单
  202. return selfList.length > 0 ? await transaction.insert(this.tableName, selfList) : true;
  203. }
  204. }
  205. return MaterialListSelf;
  206. };