material_list_notjoin.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. 'use strict';
  2. /**
  3. * 不参与调差-清单关联表 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. module.exports = app => {
  10. class MaterialListNotJoin 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_notjoin';
  20. }
  21. /**
  22. * 添加不参与调差的清单
  23. * @return {void}
  24. */
  25. async add(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 newListNotJoin = {
  32. tid: this.ctx.tender.id,
  33. mid: this.ctx.material.id,
  34. gcl_id: data.gcl_id,
  35. xmj_id: data.id,
  36. mx_id: data.mx_id ? data.mx_id : '',
  37. type: 1,
  38. in_time: new Date(),
  39. };
  40. // 如果存在数量变更不参与调差,需要删除
  41. await transaction.delete(this.tableName, {
  42. tid: this.ctx.tender.id,
  43. mid: this.ctx.material.id,
  44. gcl_id: data.gcl_id,
  45. xmj_id: data.id,
  46. mx_id: data.mx_id ? data.mx_id : '',
  47. type: 2,
  48. });
  49. data.xmj_id = data.id;
  50. data.mx_id = data.mx_id ? data.mx_id : '';
  51. await this.updateAllMaterials(transaction, data, 'add', ms_id);
  52. // 新增不参与调差清单
  53. const result = await transaction.insert(this.tableName, newListNotJoin);
  54. if (result.affectedRows === 0) {
  55. throw '新增不参与调差清单数据失败';
  56. }
  57. await transaction.commit();
  58. return await this.getDataById(result.insertId);
  59. } catch (err) {
  60. await transaction.rollback();
  61. throw err;
  62. }
  63. }
  64. /**
  65. * 添加数量变更不计量的清单
  66. * @return {void}
  67. */
  68. async addChange(data, ms_id = null) {
  69. if (!this.ctx.tender || !this.ctx.material) {
  70. throw '数据错误';
  71. }
  72. const transaction = await this.db.beginTransaction();
  73. try {
  74. const newListNotChange = {
  75. tid: this.ctx.tender.id,
  76. mid: this.ctx.material.id,
  77. gcl_id: data.gcl_id,
  78. xmj_id: data.id,
  79. mx_id: data.mx_id ? data.mx_id : '',
  80. type: 2,
  81. in_time: new Date(),
  82. };
  83. data.xmj_id = data.id;
  84. data.mx_id = data.mx_id ? data.mx_id : '';
  85. await this.updateAllMaterials(transaction, data, 'change', ms_id);
  86. // 新增不参与调差清单
  87. const result = await transaction.insert(this.tableName, newListNotChange);
  88. if (result.affectedRows === 0) {
  89. throw '新增数量变更不计量不参与调差数据失败';
  90. }
  91. await transaction.commit();
  92. return await this.getDataById(result.insertId);
  93. } catch (err) {
  94. await transaction.rollback();
  95. throw err;
  96. }
  97. }
  98. /**
  99. * 删除不参与调差的清单
  100. * @param {int} id 工料id
  101. * @return {void}
  102. */
  103. async del(id, ms_id = null) {
  104. if (!this.ctx.tender || !this.ctx.material) {
  105. throw '数据错误';
  106. }
  107. const transaction = await this.db.beginTransaction();
  108. try {
  109. const notJoinInfo = await this.getDataById(id);
  110. await this.updateAllMaterials(transaction, notJoinInfo, 'del', ms_id);
  111. // 判断是否可删
  112. const result = await transaction.delete(this.tableName, { id });
  113. await transaction.commit();
  114. return result;
  115. } catch (err) {
  116. await transaction.rollback();
  117. throw err;
  118. }
  119. }
  120. /**
  121. * 修改调差list和bill和material对应值
  122. * @param transaction
  123. * @returns {Promise<void>}
  124. */
  125. async updateAllMaterials(transaction, data, type, ms_id = null) {
  126. // 先判断material_list是否存在值并quantity不为null
  127. const searchSql = {
  128. mid: this.ctx.material.id,
  129. gcl_id: data.gcl_id,
  130. xmj_id: data.xmj_id,
  131. mx_id: data.mx_id,
  132. };
  133. // if (data.mx_id !== '') {
  134. // searchSql.mx_id = data.mx_id;
  135. // }
  136. const materialListData = await this.ctx.service.materialList.getAllDataByCondition({
  137. where: searchSql,
  138. });
  139. // console.log(data);
  140. // console.log(materialListData);
  141. if (materialListData && materialListData.length !== 0) {
  142. // 对应修改更新本期应耗数量值和总的本期金额计算
  143. const mbIdList = [];
  144. const updateList = [];
  145. for (const ml of materialListData) {
  146. const updateData = {
  147. id: ml.id,
  148. is_join: type === 'add' ? 0 : type === 'change' ? 2 : 1,
  149. };
  150. if (mbIdList.indexOf(ml.mb_id) === -1) {
  151. mbIdList.push(ml.mb_id);
  152. }
  153. updateList.push(updateData);
  154. }
  155. if (updateList.length > 0) await transaction.updateRows(this.ctx.service.materialList.tableName, updateList);
  156. // 重新计算金额
  157. for (const mb_id of mbIdList) {
  158. await this.service.materialList.calcQuantityByML(transaction, mb_id, ms_id, 'all');
  159. }
  160. }
  161. }
  162. /**
  163. * 复制上一期不参与调差的清单到下一期中
  164. * @param {Object} transaction - 新增一期的事务
  165. * @param {Object} list 上期清单
  166. * @param {int} mid 工料id
  167. * @return {void}
  168. */
  169. async copyNewStageNotJoinList(transaction, list, mid) {
  170. if (!this.ctx.tender) {
  171. throw '数据错误';
  172. }
  173. const notJoinlist = [];
  174. for (const mb of list) {
  175. const newLists = {
  176. tid: mb.tid,
  177. mid,
  178. gcl_id: mb.gcl_id,
  179. xmj_id: mb.xmj_id,
  180. mx_id: mb.mx_id,
  181. type: mb.type,
  182. in_time: new Date(),
  183. };
  184. notJoinlist.push(newLists);
  185. }
  186. // 复制上一期不参与调差的清单
  187. return notJoinlist.length > 0 ? await transaction.insert(this.tableName, notJoinlist) : true;
  188. }
  189. async getJoinMsg(transaction, mid, datas) {
  190. const searchData = {
  191. tid: this.ctx.tender.id,
  192. mid,
  193. gcl_id: datas.gcl_id,
  194. xmj_id: datas.xmj_id,
  195. mx_id: datas.mx_id,
  196. };
  197. const info = await transaction.get(this.tableName, searchData);
  198. if (info) {
  199. return info.type === 1 ? 0 : info.type === 2 ? 2 : 0;
  200. }
  201. return 1;
  202. }
  203. }
  204. return MaterialListNotJoin;
  205. };