material_checklist.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. /**
  3. * 清单设置 数据模型
  4. * @author LanJianRong
  5. * @date 2020/6/30
  6. * @version
  7. */
  8. const materialConst = require('../const/material');
  9. module.exports = app => {
  10. class MaterialChecklist 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_checklist';
  20. }
  21. async resetData(pushData, removeData, updateData) {
  22. if (!this.ctx.tender || !this.ctx.material) {
  23. throw '数据错误';
  24. }
  25. const transaction = await this.db.beginTransaction();
  26. try {
  27. if (pushData.length > 0) {
  28. const insertDatas = [];
  29. for (const p of pushData) {
  30. p.mid = this.ctx.material.id;
  31. p.tid = this.ctx.tender.id;
  32. insertDatas.push(p);
  33. }
  34. await transaction.insert(this.tableName, insertDatas);
  35. }
  36. if (removeData.length > 0) {
  37. for (const r of removeData) {
  38. await transaction.delete(this.tableName, { id: r });
  39. }
  40. }
  41. if (updateData.length > 0) {
  42. await transaction.updateRows(this.tableName, updateData);
  43. }
  44. await transaction.commit();
  45. const materialChecklistData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  46. const self = this;
  47. return await materialChecklistData.sort(function(a, b) {
  48. return self.ctx.helper.compareCode(a.b_code, b.b_code);
  49. });
  50. } catch (err) {
  51. console.log(err);
  52. await transaction.rollback();
  53. throw err;
  54. }
  55. }
  56. async updateHadBills(transaction, id, had_bills) {
  57. if (!this.ctx.tender || !this.ctx.material) {
  58. throw '数据错误';
  59. }
  60. return await transaction.update(this.tableName, { id, had_bills });
  61. }
  62. async addExportCB(addChecklist, addBillsList) {
  63. if (!this.ctx.tender || !this.ctx.material) {
  64. throw '数据错误';
  65. }
  66. const transaction = await this.db.beginTransaction();
  67. try {
  68. if (addChecklist.length > 0) {
  69. const insertDatas = [];
  70. for (const p of addChecklist) {
  71. p.mid = this.ctx.material.id;
  72. p.tid = this.ctx.tender.id;
  73. insertDatas.push(p);
  74. }
  75. await transaction.insert(this.tableName, insertDatas);
  76. }
  77. if (addBillsList.length > 0) {
  78. let order = await this.ctx.service.materialBills._getMaxOrder(this.ctx.tender.id);
  79. const pushBills = [];
  80. for (const b of addBillsList) {
  81. const newBills = {
  82. tid: this.ctx.tender.id,
  83. mid: this.ctx.material.id,
  84. code: b.code,
  85. name: b.name,
  86. unit: b.unit,
  87. order: order + 1,
  88. in_time: new Date(),
  89. };
  90. pushBills.push(newBills);
  91. ++order;
  92. }
  93. // 新增工料
  94. const result = await transaction.insert(this.ctx.service.materialBills.tableName, pushBills);
  95. // 获取刚批量添加的所有list
  96. for (let j = 0; j < pushBills.length; j++) {
  97. pushBills[j].id = result.insertId + j;
  98. }
  99. if (this.ctx.material.is_stage_self) {
  100. await this.ctx.service.materialStageBills.adds(transaction, pushBills);
  101. }
  102. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  103. if (material_month.length > 0) {
  104. const insertArray = [];
  105. for (const pb of pushBills) {
  106. for (const ym of material_month) {
  107. const one_month = {
  108. tid: this.ctx.tender.id,
  109. mid: this.ctx.material.id,
  110. mb_id: pb.id,
  111. msg_tp: null,
  112. yearmonth: ym,
  113. };
  114. insertArray.push(one_month);
  115. }
  116. }
  117. if (insertArray.length !== 0) await transaction.insert(this.ctx.service.materialMonth.tableName, insertArray);
  118. }
  119. }
  120. await transaction.commit();
  121. const materialChecklistData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id } });
  122. const searchsql = { tid: this.ctx.tender.id };
  123. let midList = [];
  124. if (this.ctx.material.highOrder !== this.ctx.material.order) {
  125. midList = await this.ctx.service.material.getPreMidList(this.ctx.tender.id, this.ctx.material.order);
  126. searchsql.mid = midList;
  127. }
  128. searchsql.t_type = materialConst.t_type[0].value;
  129. const materialBillsData = await this.ctx.service.materialBills.getAllDataByCondition({ where: searchsql, orders: [['order', 'asc']] });
  130. // 取对应期的截取上期的调差金额和应耗数量
  131. if (this.ctx.material.highOrder !== this.ctx.material.order) {
  132. for (const [mindex, mb] of materialBillsData.entries()) {
  133. const result = await this.ctx.service.materialBillsHistory.getByMbId(this.ctx.material.id, this.ctx.material.order, mb.id);
  134. this._.forEach(result, function(value, key) {
  135. if (key === 'mb_id') {
  136. materialBillsData[mindex].id = result ? result[key] : null;
  137. } else {
  138. materialBillsData[mindex][key] = result ? result[key] : null;
  139. }
  140. });
  141. }
  142. }
  143. const materialStageBillsData = this.ctx.material.is_stage_self ? await this.ctx.service.materialStageBills.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } }) : [];
  144. const self = this;
  145. return { materialChecklistData: await materialChecklistData.sort(function(a, b) {
  146. return self.ctx.helper.compareCode(a.b_code, b.b_code);
  147. }), materialBillsData, materialStageBillsData };
  148. } catch (err) {
  149. console.log(err);
  150. await transaction.rollback();
  151. throw err;
  152. }
  153. }
  154. }
  155. return MaterialChecklist;
  156. };