material_month.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 month_num = material_month.length - this.ctx.helper.arrayCount(this._.concat(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), one_month.msg_tp), [null, '', 0]);
  58. const new_msg_tp = this.ctx.helper.round(this.ctx.helper.div(this.ctx.helper.add(mb_msg_tp_sum, one_month.msg_tp), month_num), 3);
  59. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  60. updateArray.push({
  61. id: mb.id,
  62. msg_tp: new_msg_tp,
  63. msg_spread: newmsg_spread,
  64. m_spread: newm_spread,
  65. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  66. });
  67. }
  68. }
  69. if (insertArray.length !== 0) await transaction.insert(this.tableName, insertArray);
  70. if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
  71. }
  72. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
  73. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  74. await transaction.commit();
  75. this.ctx.material.months = material_month.join(',');
  76. return m_tp;
  77. } catch (err) {
  78. await transaction.rollback();
  79. throw err;
  80. }
  81. }
  82. /**
  83. * 删除月信息价 并更新 工料平均单价、本期单价,调差金额等
  84. * @return {void}
  85. */
  86. async del(data, monthList, mbList) {
  87. if (!this.ctx.tender || !this.ctx.material) {
  88. throw '数据错误';
  89. }
  90. const transaction = await this.db.beginTransaction();
  91. try {
  92. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  93. this._.remove(material_month, function(n) {
  94. return data.indexOf(n) !== -1;
  95. });
  96. await transaction.delete(this.tableName, { mid: this.ctx.material.id, yearmonth: data });
  97. if (mbList.length !== 0) {
  98. const updateArray = [];
  99. for (const mb of mbList) {
  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 month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]);
  105. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
  106. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  107. updateArray.push({
  108. id: mb.id,
  109. msg_tp: new_msg_tp,
  110. msg_spread: newmsg_spread,
  111. m_spread: newm_spread,
  112. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  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, mid: this.ctx.material.id } });
  139. const monthList = await transaction.select(this.tableName, { where: { mb_id: data.mb_id, mid: this.ctx.material.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 month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), [null, '', 0]);
  144. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
  145. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp);
  146. await transaction.update(this.ctx.service.materialBills.tableName, {
  147. id: mbInfo.id,
  148. msg_tp: new_msg_tp,
  149. msg_spread: newmsg_spread,
  150. m_spread: newm_spread,
  151. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mbInfo.quantity, newm_spread), 2),
  152. });
  153. }
  154. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  155. await transaction.commit();
  156. return m_tp;
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. }
  162. /**
  163. * 修改多个月信息价值 并更新 本期单价,调差金额等
  164. * @return {void}
  165. */
  166. async saveDatas(datas, mbList) {
  167. if (!this.ctx.tender || !this.ctx.material) {
  168. throw '数据错误';
  169. }
  170. const transaction = await this.db.beginTransaction();
  171. try {
  172. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  173. const updateArray = [];
  174. for (const data of datas) {
  175. for (const m of material_month) {
  176. const one_update = {
  177. row: {
  178. msg_tp: data[m],
  179. },
  180. where: {
  181. mb_id: data.mb_id,
  182. yearmonth: m,
  183. },
  184. };
  185. updateArray.push(one_update);
  186. }
  187. }
  188. await transaction.updateRows(this.tableName, updateArray);
  189. const monthList = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
  190. if (mbList.length !== 0) {
  191. const mbUpdateArray = [];
  192. for (const mb of mbList) {
  193. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  194. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), [null, '', 0]);
  195. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
  196. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  197. mbUpdateArray.push({
  198. id: mb.id,
  199. msg_tp: new_msg_tp,
  200. msg_spread: newmsg_spread,
  201. m_spread: newm_spread,
  202. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  203. });
  204. }
  205. if (mbUpdateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, mbUpdateArray);
  206. }
  207. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  208. await transaction.commit();
  209. return m_tp;
  210. } catch (err) {
  211. await transaction.rollback();
  212. throw err;
  213. }
  214. }
  215. }
  216. return MaterialMonth;
  217. };