material_month.js 11 KB

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