material_list.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. module.exports = app => {
  11. class MaterialList extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'material_list';
  21. }
  22. /**
  23. * 添加工料清单关联
  24. * @return {void}
  25. */
  26. async add(data) {
  27. if (!this.ctx.tender || !this.ctx.material) {
  28. throw '数据错误';
  29. }
  30. const list = [];
  31. for (const mb of data.mb_id) {
  32. const newLists = {
  33. tid: this.ctx.tender.id,
  34. order: this.ctx.material.order,
  35. mid: this.ctx.material.id,
  36. mb_id: mb,
  37. gcl_id: data.gcl_id,
  38. xmj_id: data.xmj_id,
  39. mx_id: data.mx_id,
  40. gather_qty: data.gather_qty,
  41. in_time: new Date(),
  42. };
  43. list.push(newLists);
  44. }
  45. // 新增工料
  46. const result = await this.db.insert(this.tableName, list);
  47. if (result.affectedRows === 0) {
  48. throw '新增工料数据失败';
  49. }
  50. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  51. }
  52. /**
  53. * 删除工料清单关联
  54. * @param {int} id 工料id
  55. * @return {void}
  56. */
  57. async del(id, mb_id) {
  58. if (!this.ctx.tender || !this.ctx.material) {
  59. throw '数据错误';
  60. }
  61. const transaction = await this.db.beginTransaction();
  62. try {
  63. // 判断是否可删
  64. await transaction.delete(this.tableName, { id });
  65. await this.calcQuantityByML(transaction, mb_id);
  66. await transaction.commit();
  67. return true;
  68. } catch (err) {
  69. await transaction.rollback();
  70. throw err;
  71. }
  72. }
  73. /**
  74. * 修改工料清单关联信息
  75. * @param {Object} data 工料内容
  76. * @param {int} order 期数
  77. * @return {void}
  78. */
  79. async save(data, order) {
  80. if (!this.ctx.tender || !this.ctx.material) {
  81. throw '数据错误';
  82. }
  83. const transaction = await this.db.beginTransaction();
  84. try {
  85. const mb_id = data.mb_id;
  86. delete data.mb_id;
  87. await transaction.update(this.tableName, data);
  88. await this.calcQuantityByML(transaction, mb_id);
  89. await transaction.commit();
  90. return true;
  91. } catch (err) {
  92. await transaction.rollback();
  93. throw err;
  94. }
  95. }
  96. /**
  97. * 应用工料清单到其它清单中
  98. * @return {void}
  99. */
  100. async addOther(data) {
  101. if (!this.ctx.tender || !this.ctx.material) {
  102. throw '数据错误';
  103. }
  104. const transaction = await this.db.beginTransaction();
  105. try {
  106. const list = [];
  107. const select = data.select;
  108. for (const index in data.mx_id) {
  109. const newLists = {
  110. tid: this.ctx.tender.id,
  111. order: this.ctx.material.order,
  112. mid: this.ctx.material.id,
  113. mb_id: select.mb_id,
  114. gcl_id: select.gcl_id,
  115. xmj_id: select.xmj_id,
  116. mx_id: data.mx_id[index],
  117. gather_qty: data.gather_qty[index],
  118. quantity: select.quantity,
  119. in_time: new Date(),
  120. };
  121. list.push(newLists);
  122. }
  123. // 新增工料
  124. const result = await transaction.insert(this.tableName, list);
  125. if (result.affectedRows === 0) {
  126. throw '新增工料数据失败';
  127. }
  128. await this.calcQuantityByML(transaction, select.mb_id);
  129. await transaction.commit();
  130. return await this.getMaterialData(this.ctx.tender.id, this.ctx.material.id);
  131. } catch (err) {
  132. await transaction.rollback();
  133. throw err;
  134. }
  135. }
  136. /**
  137. * 修改material_bills的quantity值和计算本期金额
  138. * @param transaction
  139. * @param mb_id
  140. * @returns {Promise<*>}
  141. */
  142. async calcQuantityByML(transaction, mb_id) {
  143. // 修改material_bills值
  144. const mbInfo = await this.ctx.service.materialBills.getDataById(mb_id);
  145. if (!mbInfo) {
  146. throw '不存在该工料';
  147. }
  148. const sql = 'SELECT SUM(`gather_qty`*`quantity`) as quantity FROM ' + this.tableName + ' WHERE `mid`=? AND `mb_id`=?';
  149. const sqlParam = [this.ctx.material.id, mb_id];
  150. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  151. console.log(mb_quantity);
  152. mbInfo.quantity = this.ctx.helper.round(mb_quantity.quantity, 3);
  153. const updateData = {
  154. id: mb_id,
  155. quantity: mbInfo.quantity,
  156. };
  157. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  158. // 计算本期总金额
  159. const sql2 = 'SELECT SUM(`m_spread`*`quantity`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ?';
  160. const sqlParam2 = [this.ctx.tender.id];
  161. const tp = await transaction.queryOne(sql2, sqlParam2);
  162. console.log(tp);
  163. const updateData2 = {
  164. id: this.ctx.material.id,
  165. m_tp: tp.total_price,
  166. };
  167. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  168. }
  169. /**
  170. * 获取工料清单关联表
  171. * @param {int} tid 标段id
  172. * @param {Object} mid 期id
  173. * @return {void}
  174. */
  175. async getMaterialData(tid, mid) {
  176. const sql = 'SELECT ml.`id`, mb.`code`, mb.`name`, mb.`unit`, ml.`order`, ml.`quantity`, ml.`mb_id`, ml.`gcl_id`, ml.`xmj_id`, ml.`mx_id`, ml.`tid`, ml.`mid`' +
  177. ' FROM ' + this.tableName + ' as ml' +
  178. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  179. ' ON ml.`mb_id` = mb.`id`' +
  180. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  181. const sqlParam = [tid, mid];
  182. return await this.db.query(sql, sqlParam);
  183. }
  184. }
  185. return MaterialList;
  186. };