material_month.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. const materialConst = require('../const/material');
  11. const MaterialCalculator = require('../lib/material_calc');
  12. module.exports = app => {
  13. class MaterialMonth extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'material_month';
  23. }
  24. /**
  25. * 返回月信息价列表
  26. * @return {void}
  27. */
  28. async getListByMid(mid) {
  29. return await this.getAllDataByCondition({ where: { mid } });
  30. }
  31. /**
  32. * 添加月信息价 并更新 工料平均单价、本期单价,调差金额等
  33. * @return {void}
  34. */
  35. async add(data, monthList, mbList) {
  36. if (!this.ctx.tender || !this.ctx.material) {
  37. throw '数据错误';
  38. }
  39. const transaction = await this.db.beginTransaction();
  40. try {
  41. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  42. material_month.push(data.yearmonth);
  43. if (mbList.length !== 0) {
  44. const insertArray = [];
  45. const updateArray = [];
  46. for (const mb of mbList) {
  47. const one_month = {
  48. tid: this.ctx.tender.id,
  49. mid: this.ctx.material.id,
  50. mb_id: mb.id,
  51. msg_tp: monthList.length !== 0 ? null : mb.msg_tp,
  52. yearmonth: data.yearmonth,
  53. };
  54. insertArray.push(one_month);
  55. if (monthList.length !== 0) {
  56. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  57. const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(this.ctx.helper.add(mb_msg_tp_sum, one_month.msg_tp), material_month.length), 3);
  58. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  59. updateArray.push({
  60. id: mb.id,
  61. msg_tp: new_msg_tp,
  62. msg_spread: newmsg_spread,
  63. m_spread: newm_spread,
  64. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  65. });
  66. }
  67. }
  68. if (insertArray.length !== 0) await transaction.insert(this.tableName, insertArray);
  69. if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
  70. }
  71. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
  72. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  73. await transaction.commit();
  74. this.ctx.material.months = material_month.join(',');
  75. return m_tp;
  76. } catch (err) {
  77. await transaction.rollback();
  78. throw err;
  79. }
  80. }
  81. /**
  82. * 删除月信息价 并更新 工料平均单价、本期单价,调差金额等
  83. * @return {void}
  84. */
  85. async del(data, monthList, mbList) {
  86. if (!this.ctx.tender || !this.ctx.material) {
  87. throw '数据错误';
  88. }
  89. const transaction = await this.db.beginTransaction();
  90. try {
  91. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  92. this._.remove(material_month, function(n) {
  93. return data.indexOf(n) !== -1;
  94. });
  95. await transaction.delete(this.tableName, { mid: this.ctx.material.id, yearmonth: data });
  96. if (mbList.length !== 0) {
  97. const updateArray = [];
  98. for (const mb of mbList) {
  99. if (monthList.length !== 0) {
  100. this._.remove(monthList, function(m) {
  101. return data.indexOf(m.yearmonth) !== -1;
  102. });
  103. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  104. const new_msg_tp = material_month.length !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, material_month.length), 3) : null;
  105. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  106. updateArray.push({
  107. id: mb.id,
  108. msg_tp: new_msg_tp,
  109. msg_spread: newmsg_spread,
  110. m_spread: newm_spread,
  111. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  112. });
  113. }
  114. }
  115. if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
  116. }
  117. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
  118. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  119. await transaction.commit();
  120. this.ctx.material.months = material_month.join(',');
  121. return m_tp;
  122. } catch (err) {
  123. await transaction.rollback();
  124. throw err;
  125. }
  126. }
  127. /**
  128. * 修改月信息价值 并更新 本期单价,调差金额等
  129. * @return {void}
  130. */
  131. async save(data) {
  132. if (!this.ctx.tender || !this.ctx.material) {
  133. throw '数据错误';
  134. }
  135. const transaction = await this.db.beginTransaction();
  136. try {
  137. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  138. await transaction.update(this.tableName, { msg_tp: data.value }, { where: { mb_id: data.mb_id, yearmonth: data.yearmonth } });
  139. const monthList = await transaction.select(this.tableName, { where: { mb_id: data.mb_id } });
  140. const mbInfo = await transaction.get(this.ctx.service.materialBills.tableName, { id: data.mb_id });
  141. if (monthList.length !== 0) {
  142. const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
  143. const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, material_month.length), 3);
  144. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp);
  145. await transaction.update(this.ctx.service.materialBills.tableName, {
  146. id: mbInfo.id,
  147. msg_tp: new_msg_tp,
  148. msg_spread: newmsg_spread,
  149. m_spread: newm_spread,
  150. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mbInfo.quantity, newm_spread), 2),
  151. });
  152. }
  153. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  154. await transaction.commit();
  155. return m_tp;
  156. } catch (err) {
  157. await transaction.rollback();
  158. throw err;
  159. }
  160. }
  161. /**
  162. * 修改多个月信息价值 并更新 本期单价,调差金额等
  163. * @return {void}
  164. */
  165. async saveDatas(datas, mbList) {
  166. if (!this.ctx.tender || !this.ctx.material) {
  167. throw '数据错误';
  168. }
  169. const transaction = await this.db.beginTransaction();
  170. try {
  171. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  172. const updateArray = [];
  173. for (const data of datas) {
  174. for (const m of material_month) {
  175. const one_update = {
  176. row: {
  177. msg_tp: data[m],
  178. },
  179. where: {
  180. mb_id: data.mb_id,
  181. yearmonth: m,
  182. },
  183. };
  184. updateArray.push(one_update);
  185. }
  186. }
  187. await transaction.updateRows(this.tableName, updateArray);
  188. const monthList = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
  189. if (mbList.length !== 0) {
  190. const mbUpdateArray = [];
  191. for (const mb of mbList) {
  192. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  193. const new_msg_tp = material_month.length !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, material_month.length), 3) : null;
  194. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  195. mbUpdateArray.push({
  196. id: mb.id,
  197. msg_tp: new_msg_tp,
  198. msg_spread: newmsg_spread,
  199. m_spread: newm_spread,
  200. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  201. });
  202. }
  203. if (mbUpdateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, mbUpdateArray);
  204. }
  205. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  206. await transaction.commit();
  207. return m_tp;
  208. } catch (err) {
  209. await transaction.rollback();
  210. throw err;
  211. }
  212. }
  213. }
  214. return MaterialMonth;
  215. };