material_list_qty.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. /**
  3. * 调差清单-本期调差数量表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. module.exports = app => {
  10. class MaterialListQty extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'material_list_qty';
  20. }
  21. /**
  22. * 增删改本期数量及同步至调差清单表
  23. * @return {void}
  24. */
  25. async save(data, ms_id = null) {
  26. if (!this.ctx.tender || !this.ctx.material) {
  27. throw '数据错误';
  28. }
  29. const transaction = await this.db.beginTransaction();
  30. try {
  31. const mx_id = data.mx_id || '';
  32. const condition = {
  33. mid: this.ctx.material.id,
  34. ms_id,
  35. gcl_id: data.gcl_id,
  36. xmj_id: data.xmj_id,
  37. mx_id,
  38. };
  39. const now = new Date();
  40. const isDelete = data.qty == null;
  41. let record = null;
  42. if (data.id > 0) {
  43. record = { id: data.id };
  44. } else {
  45. record = await this.getDataByCondition(condition);
  46. }
  47. if (record) {
  48. if (isDelete) {
  49. await transaction.delete(this.tableName, { id: record.id });
  50. record = null;
  51. } else {
  52. await transaction.update(this.tableName, {
  53. id: record.id,
  54. qty: data.qty,
  55. in_time: now,
  56. });
  57. }
  58. } else if (!isDelete) {
  59. const insertData = {
  60. tid: this.ctx.tender.id,
  61. ...condition,
  62. qty: data.qty,
  63. in_time: now,
  64. };
  65. const result = await transaction.insert(this.tableName, insertData);
  66. record = { id: result.insertId };
  67. }
  68. const newData = record ? await transaction.get(this.tableName, condition) : null;
  69. await this.updateAllMaterials(transaction, data, newData, ms_id);
  70. await transaction.commit();
  71. return {
  72. info: newData,
  73. materialListData: await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  74. };
  75. } catch (err) {
  76. await transaction.rollback();
  77. throw err;
  78. }
  79. }
  80. /**
  81. * 增删改本期数量及同步至调差清单表
  82. * @return {void}
  83. */
  84. async savePastes(datas, ms_id = null) {
  85. if (!this.ctx.tender || !this.ctx.material) {
  86. throw '数据错误';
  87. }
  88. const transaction = await this.db.beginTransaction();
  89. try {
  90. const insertDatas = [];
  91. const updateDatas = [];
  92. const delDatas = [];
  93. const newDatas = [];
  94. for (const data of datas) {
  95. const mx_id = data.mx_id || '';
  96. const condition = {
  97. mid: this.ctx.material.id,
  98. ms_id,
  99. gcl_id: data.gcl_id,
  100. xmj_id: data.xmj_id,
  101. mx_id,
  102. };
  103. const now = new Date();
  104. const isDelete = data.qty == null;
  105. let record = null;
  106. if (data.id > 0) {
  107. record = { id: data.id };
  108. } else {
  109. record = await this.getDataByCondition(condition);
  110. }
  111. if (record) {
  112. if (isDelete) {
  113. delDatas.push(record.id);
  114. } else {
  115. updateDatas.push({
  116. id: record.id,
  117. qty: data.qty,
  118. in_time: now,
  119. });
  120. }
  121. } else if (!isDelete) {
  122. const insertData = {
  123. tid: this.ctx.tender.id,
  124. ...condition,
  125. qty: data.qty,
  126. in_time: now,
  127. };
  128. insertDatas.push(insertData);
  129. }
  130. newDatas.push({...condition, qty: data.qty});
  131. }
  132. if (insertDatas.length > 0) await transaction.insert(this.tableName, insertDatas);
  133. if (updateDatas.length > 0) await transaction.updateRows(this.tableName, updateDatas);
  134. if (delDatas.length > 0) await transaction.delete(this.tableName, { id: delDatas });
  135. await this.updateAllMaterialsByPastes(transaction, datas, newDatas, ms_id);
  136. await transaction.commit();
  137. return {
  138. qtyList: await this.getAllDataByCondition({ where: { tid: this.ctx.tender.id, mid: this.ctx.material.id } }),
  139. materialListData: await this.ctx.service.materialList.getMaterialData(this.ctx.tender.id, this.ctx.material.id),
  140. };
  141. } catch (err) {
  142. await transaction.rollback();
  143. throw err;
  144. }
  145. }
  146. /**
  147. * 修改调差list和bill和material对应值
  148. * @param transaction
  149. * @returns {Promise<void>}
  150. */
  151. async updateAllMaterials(transaction, data, newData, ms_id = null) {
  152. // 先判断material_list是否存在值并quantity不为null
  153. const searchSql = {
  154. mid: this.ctx.material.id,
  155. ms_id,
  156. gcl_id: data.gcl_id,
  157. xmj_id: data.xmj_id,
  158. mx_id: data.mx_id,
  159. };
  160. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  161. where: searchSql,
  162. });
  163. if (materialListData && materialListData.length !== 0) {
  164. // 对应修改更新本期应耗数量值和总的本期金额计算
  165. const mbIdList = [];
  166. const updateList = [];
  167. for (const ml of materialListData) {
  168. const updateData = {
  169. id: ml.id,
  170. qty: newData ? newData.qty : null,
  171. };
  172. if (mbIdList.indexOf(ml.mb_id) === -1) {
  173. mbIdList.push(ml.mb_id);
  174. }
  175. updateList.push(updateData);
  176. }
  177. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.materialList.tableName, updateList);
  178. // 重新计算金额
  179. for (const mb_id of mbIdList) {
  180. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  181. }
  182. }
  183. }
  184. /**
  185. * 修改调差list和bill和material对应值
  186. * @param transaction
  187. * @returns {Promise<void>}
  188. */
  189. async updateAllMaterialsByPastes(transaction, datas, newDatas, ms_id = null) {
  190. // 先判断material_list是否存在值并quantity不为null
  191. if (datas.length > 0) {
  192. const updateList = [];
  193. const mbIdList = [];
  194. for (const data of datas) {
  195. const searchSql = {
  196. mid: this.ctx.material.id,
  197. ms_id,
  198. gcl_id: data.gcl_id,
  199. xmj_id: data.xmj_id,
  200. mx_id: data.mx_id,
  201. };
  202. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  203. where: searchSql,
  204. });
  205. if (materialListData && materialListData.length !== 0) {
  206. const newData = this._.find(newDatas, searchSql);
  207. for (const ml of materialListData) {
  208. const updateData = {
  209. id: ml.id,
  210. qty: newData ? newData.qty : null,
  211. };
  212. if (mbIdList.indexOf(ml.mb_id) === -1) {
  213. mbIdList.push(ml.mb_id);
  214. }
  215. updateList.push(updateData);
  216. }
  217. }
  218. }
  219. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.materialList.tableName, updateList);
  220. // 重新计算金额
  221. for (const mb_id of mbIdList) {
  222. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  223. }
  224. }
  225. }
  226. }
  227. return MaterialListQty;
  228. };