material_list.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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`=? AND `is_join`=1';
  149. const sqlParam = [this.ctx.material.id, mb_id];
  150. const mb_quantity = await transaction.queryOne(sql, sqlParam);
  151. console.log(mb_quantity);
  152. const updateData = {
  153. id: mb_id,
  154. quantity: this.ctx.helper.round(mb_quantity.quantity, 3),
  155. };
  156. await transaction.update(this.ctx.service.materialBills.tableName, updateData);
  157. // 计算本期总金额
  158. const sql2 = 'SELECT SUM(`m_spread`*`quantity`) as total_price FROM ' + this.ctx.service.materialBills.tableName + ' WHERE `tid` = ?';
  159. const sqlParam2 = [this.ctx.tender.id];
  160. const tp = await transaction.queryOne(sql2, sqlParam2);
  161. console.log(tp);
  162. const updateData2 = {
  163. id: this.ctx.material.id,
  164. m_tp: tp.total_price,
  165. };
  166. return await transaction.update(this.ctx.service.material.tableName, updateData2);
  167. }
  168. /**
  169. * 获取工料清单关联表
  170. * @param {int} tid 标段id
  171. * @param {Object} mid 期id
  172. * @return {void}
  173. */
  174. async getMaterialData(tid, mid) {
  175. 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`' +
  176. ' FROM ' + this.tableName + ' as ml' +
  177. ' LEFT JOIN ' + this.ctx.service.materialBills.tableName + ' as mb' +
  178. ' ON ml.`mb_id` = mb.`id`' +
  179. ' WHERE ml.`tid` = ? AND ml.`mid` = ?';
  180. const sqlParam = [tid, mid];
  181. return await this.db.query(sql, sqlParam);
  182. }
  183. /**
  184. * 复制上一期并生成新一期清单工料关联,计算新一期小计值
  185. * @param transaction
  186. * @param preMaterial
  187. * @param newMid
  188. * @returns {Promise<void>}
  189. */
  190. async copyPreMaterialList(transaction, preMaterial, newMaterial) {
  191. const materialListData = await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: preMaterial.id } });
  192. const copyMLArray = [];
  193. for (const ml of materialListData) {
  194. // 获取小计值
  195. let gather_qty = null;
  196. if (ml.mx_id !== null) {
  197. gather_qty = await this.ctx.service.stagePos.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id, ml.mx_id);
  198. } else {
  199. gather_qty = await this.ctx.service.stageBills.getGatherQtyByMaterial(ml.tid, newMaterial.stage_id, ml.gcl_id);
  200. }
  201. const newMaterialList = {
  202. tid: ml.tid,
  203. order: ml.order,
  204. mid: newMaterial.id,
  205. mb_id: ml.mb_id,
  206. gcl_id: ml.gcl_id,
  207. xmj_id: ml.xmj_id,
  208. mx_id: ml.mx_id,
  209. gather_qty,
  210. quantity: ml.quantity,
  211. is_join: ml.is_join,
  212. in_time: new Date(),
  213. };
  214. copyMLArray.push(newMaterialList);
  215. }
  216. return await transaction.insert(this.tableName, copyMLArray);
  217. }
  218. }
  219. return MaterialList;
  220. };