material_month.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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);
  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. if (monthList.length !== 0) {
  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);
  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. }
  117. if (updateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, updateArray);
  118. }
  119. await transaction.update(this.ctx.service.material.tableName, { id: this.ctx.material.id, months: material_month.join(',') });
  120. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  121. await transaction.commit();
  122. this.ctx.material.months = material_month.join(',');
  123. return m_tp;
  124. } catch (err) {
  125. await transaction.rollback();
  126. throw err;
  127. }
  128. }
  129. /**
  130. * 修改月信息价值 并更新 本期单价,调差金额等
  131. * @return {void}
  132. */
  133. async save(data) {
  134. if (!this.ctx.tender || !this.ctx.material) {
  135. throw '数据错误';
  136. }
  137. const transaction = await this.db.beginTransaction();
  138. try {
  139. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  140. await transaction.update(this.tableName, { msg_tp: data.value }, { where: { mb_id: data.mb_id, yearmonth: data.yearmonth, mid: this.ctx.material.id } });
  141. const monthList = await transaction.select(this.tableName, { where: { mb_id: data.mb_id, mid: this.ctx.material.id } });
  142. const mbInfo = await transaction.get(this.ctx.service.materialBills.tableName, { id: data.mb_id });
  143. if (monthList.length !== 0) {
  144. console.log(monthList);
  145. const mb_msg_tp_sum = this._.sumBy(monthList, 'msg_tp');
  146. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(monthList, 'msg_tp'), null);
  147. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
  148. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mbInfo, new_msg_tp);
  149. await transaction.update(this.ctx.service.materialBills.tableName, {
  150. id: mbInfo.id,
  151. msg_tp: new_msg_tp,
  152. msg_spread: newmsg_spread,
  153. m_spread: newm_spread,
  154. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mbInfo.quantity, newm_spread), 2),
  155. });
  156. }
  157. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  158. await transaction.commit();
  159. return m_tp;
  160. } catch (err) {
  161. await transaction.rollback();
  162. throw err;
  163. }
  164. }
  165. /**
  166. * 修改多个月信息价值 并更新 本期单价,调差金额等
  167. * @return {void}
  168. */
  169. async saveDatas(datas, mbList) {
  170. if (!this.ctx.tender || !this.ctx.material) {
  171. throw '数据错误';
  172. }
  173. const transaction = await this.db.beginTransaction();
  174. try {
  175. const material_month = this.ctx.material.months ? this.ctx.material.months.split(',') : [];
  176. const updateArray = [];
  177. for (const data of datas) {
  178. for (const m of material_month) {
  179. const one_update = {
  180. row: {
  181. msg_tp: data[m],
  182. },
  183. where: {
  184. mb_id: data.mb_id,
  185. yearmonth: m,
  186. },
  187. };
  188. updateArray.push(one_update);
  189. }
  190. }
  191. await transaction.updateRows(this.tableName, updateArray);
  192. const monthList = await transaction.select(this.tableName, { where: { mid: this.ctx.material.id } });
  193. if (mbList.length !== 0) {
  194. const mbUpdateArray = [];
  195. for (const mb of mbList) {
  196. const mb_msg_tp_sum = this._.sumBy(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp');
  197. const month_num = material_month.length - this.ctx.helper.arrayCount(this._.map(this._.filter(monthList, { mb_id: mb.id }), 'msg_tp'), null);
  198. const new_msg_tp = month_num !== 0 ? this.ctx.helper.round(this.ctx.helper.div(mb_msg_tp_sum, month_num), 3) : null;
  199. const [newmsg_spread, newm_spread] = await this.ctx.service.materialBills.getSpread(mb, new_msg_tp);
  200. mbUpdateArray.push({
  201. id: mb.id,
  202. msg_tp: new_msg_tp,
  203. msg_spread: newmsg_spread,
  204. m_spread: newm_spread,
  205. m_tp: this.ctx.helper.round(this.ctx.helper.mul(mb.quantity, newm_spread), 2),
  206. });
  207. }
  208. if (mbUpdateArray.length !== 0) await transaction.updateRows(this.ctx.service.materialBills.tableName, mbUpdateArray);
  209. }
  210. const m_tp = await this.ctx.service.materialBills.calcMaterialMTp(transaction);
  211. await transaction.commit();
  212. return m_tp;
  213. } catch (err) {
  214. await transaction.rollback();
  215. throw err;
  216. }
  217. }
  218. }
  219. return MaterialMonth;
  220. };