material_list_self.js 9.4 KB

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