material_list_self.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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, order: this.ctx.material.order, in_time: new Date() }, {
  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 { info: await this.getDataById(result.insertId), materialListData: await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id) };
  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. if (data.gather_qty['ms_id_' + ms.id] !== null) {
  133. for (const m of materialListGclData) {
  134. insertList.push({
  135. tid: this.ctx.tender.id,
  136. order: m.order,
  137. mid: this.ctx.material.id,
  138. mb_id: m.mb_id,
  139. gcl_id: data.gcl_id,
  140. xmj_id: data.xmj_id,
  141. mx_id: data.mx_id,
  142. ms_id: ms.id,
  143. gather_qty: data.gather_qty['ms_id_' + ms.id],
  144. quantity: m.quantity,
  145. expr: m.expr,
  146. is_join,
  147. is_self: 0,
  148. in_time: new Date(),
  149. });
  150. }
  151. }
  152. }
  153. } else {
  154. // const gather_qty = materialListData[0].gather_qty;
  155. // const is_join = materialListData[0].is_join;
  156. for (const m of materialListGclData) {
  157. insertList.push({
  158. tid: this.ctx.tender.id,
  159. order: m.order,
  160. mid: this.ctx.material.id,
  161. mb_id: m.mb_id,
  162. gcl_id: data.gcl_id,
  163. xmj_id: data.xmj_id,
  164. mx_id: data.mx_id,
  165. gather_qty: data.gather_qty,
  166. quantity: m.quantity,
  167. expr: m.expr,
  168. is_join,
  169. is_self: 0,
  170. in_time: new Date(),
  171. });
  172. }
  173. }
  174. if (insertList.length > 0) await transaction.insert(this.ctx.service.materialList.tableName, insertList);
  175. // 重新计算金额
  176. for (const mb_id of mbIdList) {
  177. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  178. }
  179. }
  180. /**
  181. * 复制上一期不参与调差的清单到下一期中
  182. * @param {Object} transaction - 新增一期的事务
  183. * @param {Object} list 上期清单
  184. * @param {int} mid 工料id
  185. * @return {void}
  186. */
  187. async copyNewStageSelfList(transaction, list, mid) {
  188. if (!this.ctx.tender) {
  189. throw '数据错误';
  190. }
  191. const selfList = [];
  192. for (const mb of list) {
  193. const newLists = {
  194. tid: mb.tid,
  195. mid,
  196. gcl_id: mb.gcl_id,
  197. xmj_id: mb.xmj_id,
  198. mx_id: mb.mx_id,
  199. in_time: new Date(),
  200. };
  201. selfList.push(newLists);
  202. }
  203. // 复制上一期不参与调差的清单
  204. return selfList.length > 0 ? await transaction.insert(this.tableName, selfList) : true;
  205. }
  206. }
  207. return MaterialListSelf;
  208. };