OutboundService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.dk.mdm.service.ivt;
  2. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  3. import com.dk.common.exception.BaseBusinessException;
  4. import com.dk.common.infrastructure.annotaiton.Pagination;
  5. import com.dk.common.infrastructure.constant.Constant;
  6. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  7. import com.dk.common.model.pojo.PageList;
  8. import com.dk.common.response.ResponseCodeEnum;
  9. import com.dk.common.response.ResponseResultUtil;
  10. import com.dk.common.response.ResponseResultVO;
  11. import com.dk.mdm.infrastructure.convert.ivt.OutboundConvert;
  12. import com.dk.mdm.infrastructure.convert.ivt.OutboundItemConvert;
  13. import com.dk.mdm.mapper.ivt.OutboundItemMapper;
  14. import com.dk.mdm.mapper.pur.PurchaseItemMapper;
  15. import com.dk.mdm.mapper.sale.OrderItemMapper;
  16. import com.dk.mdm.mapper.sale.OrderMapper;
  17. import com.dk.mdm.model.pojo.ivt.Outbound;
  18. import com.dk.mdm.mapper.ivt.OutboundMapper;
  19. import com.dk.common.service.BaseService;
  20. import com.dk.common.mapper.BaseMapper;
  21. import com.dk.mdm.model.pojo.ivt.OutboundItem;
  22. import com.dk.mdm.model.pojo.sale.Order;
  23. import com.dk.mdm.model.pojo.sale.OrderItem;
  24. import com.dk.mdm.model.query.ivt.OutboundItemQuery;
  25. import com.dk.mdm.model.query.ivt.OutboundQuery;
  26. import com.dk.mdm.model.response.ivt.OutboundItemResponse;
  27. import com.dk.mdm.model.response.ivt.OutboundResponse;
  28. import com.dk.mdm.model.response.pur.PurchaseItemResponse;
  29. import com.dk.mdm.model.response.sale.OrderResponse;
  30. import com.dk.mdm.model.vo.ivt.OutboundItemVO;
  31. import com.dk.mdm.model.vo.ivt.OutboundVO;
  32. import com.dk.mdm.service.common.CommonService;
  33. import com.dk.mdm.service.sale.OrderItemService;
  34. import com.dk.mdm.service.sale.OrderService;
  35. import org.springframework.stereotype.Service;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.transaction.annotation.Transactional;
  38. import java.math.BigDecimal;
  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Map;
  42. import java.util.UUID;
  43. import java.util.stream.Collectors;
  44. @Service
  45. @Transactional
  46. public class OutboundService extends BaseService<Outbound> {
  47. @Override
  48. public String getPrimaryKey() {
  49. return "out_id";
  50. }
  51. @Override
  52. public BaseMapper<Outbound> getRepository() {
  53. return outboundMapper;
  54. }
  55. @Autowired
  56. private OutboundMapper outboundMapper;
  57. @Autowired
  58. private OutboundItemService outboundItemService;
  59. @Autowired
  60. private OutboundItemMapper outboundItemMapper;
  61. @Autowired
  62. private OrderService orderService;
  63. @Autowired
  64. private OrderMapper orderMapper;
  65. @Autowired
  66. private OrderItemService orderItemService;
  67. @Autowired
  68. private OrderItemMapper orderItemMapper;
  69. @Autowired
  70. private CommonService commonService;
  71. @Autowired
  72. private OutboundConvert outboundConvert;
  73. @Autowired
  74. private OutboundItemConvert outboundItemConvert;
  75. @Autowired
  76. private PurchaseItemMapper purchaseItemMapper;
  77. /**
  78. * @desc : 条件查询
  79. * @author : 付斌
  80. * @date : 2023/1/9 10:40
  81. */
  82. @Pagination
  83. public ResponseResultVO<PageList<OutboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  84. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  85. outboundMapper.countByCond(outboundQuery));
  86. }
  87. /**
  88. * @desc : 查询订单明细(货物、收款、附件)
  89. * @author : 付斌
  90. * @date : 2024-02-28 13:25
  91. */
  92. @Pagination
  93. public ResponseResultVO<Map<String, Object>> selectOutboundInfoById(String id) {
  94. Map<String, Object> result = new HashMap<>();
  95. // 商品明细
  96. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  97. result.put("outboundItem", outboundItem);
  98. // 收款
  99. // 附件
  100. return ResponseResultUtil.success(result);
  101. }
  102. /**
  103. * @desc : 新建方法
  104. * @author : 付斌
  105. * @date : 2023/1/9 10:49
  106. */
  107. @Transactional(
  108. rollbackFor = {Exception.class}
  109. )
  110. public ResponseResultVO<?> insert(OutboundVO outboundVO) {
  111. // 获取单号
  112. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.OUTBOUND.getName(), false);
  113. outboundVO.setOutId(codeMap.get("outId").toString()).setOutNo(codeMap.get("outNote").toString())
  114. .setOutType(Constant.OutType.SALE.getName());
  115. // 转化实体
  116. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  117. // 总单保存
  118. super.insert(outbound);
  119. // 明细保存
  120. if (outboundVO.getItemList() != null && outboundVO.getItemList().size() > 0) {
  121. double sumOutingQty = 0; // 合计出库中数量
  122. double sumOutingAmt = 0; // 合计出库中金额
  123. OrderItem orderItemForUpdate;
  124. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  125. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  126. outboundItem.setOutId(outbound.getOutId()).setCpId(outbound.getCpId()).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  127. .setOutType(Constant.OutType.SALE.getName());
  128. outboundItemMapper.insert(outboundItem);
  129. // 反写订单出库中数量、金额
  130. orderItemForUpdate = orderItemMapper.selectByIdForUpdate(outboundItem.getFromItemId());
  131. // 如果商品数量小于订单+本次出库单上的出库中数量
  132. if (orderItemForUpdate.getItemQty().compareTo(orderItemForUpdate.getOutingQty().add(outboundItem.getOutingQty())) == -1) {
  133. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ITEMQTY_NO_LESS_OUTQTY.getMessage());
  134. }
  135. OrderItem orderItemUpdate = new OrderItem();
  136. orderItemUpdate.setOutingQty(orderItemForUpdate.getOutingQty().add(outboundItem.getOutingQty()))
  137. .setOutingAmt(orderItemForUpdate.getOutingAmt().add(outboundItem.getOutingAmt()))
  138. .setItemId(orderItemForUpdate.getItemId());
  139. orderItemService.updateByUuid(orderItemUpdate);
  140. // 累加出库中数量,金额
  141. sumOutingQty += outboundItem.getOutingQty().doubleValue();
  142. sumOutingAmt += outboundItem.getOutingAmt().doubleValue();
  143. }
  144. // 更新订单上的出库中数量,金额,状态
  145. Order orderForUpdate = orderMapper.selectByIdForUpdate(outboundVO.getFromId());
  146. Order orderUpdate = new Order();
  147. orderUpdate.setOutingQty(orderForUpdate.getOutingQty().add(new BigDecimal(sumOutingQty)))
  148. .setOutingAmt(orderForUpdate.getOutingAmt().add(new BigDecimal(sumOutingAmt)))
  149. .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  150. .setOrderId(outboundVO.getFromId());
  151. orderService.updateByUuid(orderUpdate);
  152. }
  153. return ResponseResultUtil.success();
  154. }
  155. /**
  156. * @desc : 编辑方法
  157. * @author : 付斌
  158. * @date : 2023/1/9 10:49
  159. */
  160. @Transactional(
  161. rollbackFor = {Exception.class}
  162. )
  163. public ResponseResultVO<Boolean> update(OutboundVO outboundVO) {
  164. // 转化实体
  165. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  166. // 编辑的
  167. List<OutboundItemVO> editOutboundItemVOList = outboundVO.getItemList().stream().filter(it -> it.getItemId() != null).collect(Collectors.toList());
  168. for (OutboundItemVO outboundItemVO : editOutboundItemVOList) {
  169. // 出库中数量不能小于出库数量
  170. if (outboundItemVO.getOutingQty().compareTo(outboundItemVO.getOutQty()) == -1) {
  171. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.OUTINGQTY_NO_LESS_OUTQTY.getMessage());
  172. } else {
  173. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  174. outboundItemService.updateByUuid(outboundItem);
  175. }
  176. }
  177. return ResponseResultUtil.success(super.update(outbound, new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId,
  178. UUID.fromString(outbound.getOutId()))));
  179. }
  180. /**
  181. * @desc : 作废
  182. * @author : 付斌
  183. * @date : 2024-03-08 16:38
  184. */
  185. public ResponseResultVO<?> invalid(String id) {
  186. return ResponseResultUtil.success();
  187. }
  188. /**
  189. * @desc : 获取订单信息(编辑用)
  190. * @author : 付斌
  191. * @date : 2024-03-02 17:27
  192. */
  193. public ResponseResultVO<?> getOutboundForUpdate(String id) {
  194. Map<String, Object> outboundInfo = new HashMap<>();
  195. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  196. outboundInfo.put("outbound", outboundResponse);
  197. // 商品明细
  198. List<OutboundItemResponse> outboundItemResponse = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  199. outboundInfo.put("outboundItem", outboundItemResponse);
  200. return ResponseResultUtil.success(outboundInfo);
  201. }
  202. /**
  203. * @desc : 采购退货出库
  204. * @author : 于继渤
  205. * @date : 2023/1/9 10:49
  206. */
  207. @Transactional(
  208. rollbackFor = {Exception.class}
  209. )
  210. public ResponseResultVO<?> insertOutBound(OutboundVO outboundVO) {
  211. // 获取单号
  212. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.OUTBOUND.getName(), false);
  213. outboundVO.setOutId(codeMap.get("outId").toString()).setOutNo(codeMap.get("outNote").toString())
  214. .setOutType(Constant.OutType.PURRETURN.getName());
  215. // 转化实体
  216. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  217. // 总单保存
  218. super.insert(outbound);
  219. // 明细保存
  220. if (outboundVO.getItemList() != null && outboundVO.getItemList().size() > 0) {
  221. double sumOutingQty = 0; // 合计出库中数量
  222. double sumOutingAmt = 0; // 合计出库中金额
  223. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  224. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  225. outboundItem.setOutId(outbound.getOutId()).setCpId(outbound.getCpId()).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  226. .setOutType(Constant.OutType.PURRETURN.getName());
  227. outboundItemMapper.insert(outboundItem);
  228. // PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(outboundItem.getFromItemId());
  229. // 如果商品数量小于订单+本次出库单上的出库中数量
  230. // if(orderItem.getItemQty().compareTo(orderItem.getOutingQty().add(outboundItem.getOutingQty())) == -1){
  231. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ITEMQTY_NO_LESS_OUTQTY.getMessage());
  232. // }else{
  233. // OrderItem orderItemUpdate = new OrderItem();
  234. // orderItemUpdate.setOutingQty(orderItem.getOutingQty().add(outboundItem.getOutingQty()))
  235. // .setOutingAmt(orderItem.getOutingAmt().add(outboundItem.getOutingAmt()))
  236. // .setItemId(orderItem.getItemId());
  237. // orderItemService.updateByUuid(orderItemUpdate);
  238. // // 累加出库中数量,金额
  239. // sumOutingQty += outboundItem.getOutingQty().doubleValue();
  240. // sumOutingAmt += outboundItem.getOutingAmt().doubleValue();
  241. // }
  242. }
  243. // // 更新订单上的出库中数量,金额,状态
  244. // OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  245. // Order orderUpdate = new Order();
  246. // orderUpdate.setOutingQty(orderResponse.getOutingQty().add(new BigDecimal(sumOutingQty)))
  247. // .setOutingAmt(orderResponse.getOutingAmt().add(new BigDecimal(sumOutingAmt)))
  248. // .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  249. // .setOrderId(outboundVO.getFromId());
  250. // orderService.updateByUuid(orderUpdate);
  251. }
  252. return ResponseResultUtil.success();
  253. }
  254. }