InboundCheckService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package com.dk.mdm.service.ivt.inbound;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  6. import com.dk.common.exception.BaseBusinessException;
  7. import com.dk.common.infrastructure.constant.Constant;
  8. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  9. import com.dk.common.mapper.BaseMapper;
  10. import com.dk.common.response.ResponseResultUtil;
  11. import com.dk.common.response.ResponseResultVO;
  12. import com.dk.common.service.BaseService;
  13. import com.dk.mdm.infrastructure.convert.ivt.InboundConvert;
  14. import com.dk.mdm.infrastructure.convert.ivt.InboundItemConvert;
  15. import com.dk.mdm.mapper.ivt.InboundItemMapper;
  16. import com.dk.mdm.mapper.ivt.InboundMapper;
  17. import com.dk.mdm.model.pojo.ivt.Inbound;
  18. import com.dk.mdm.model.pojo.ivt.InboundItem;
  19. import com.dk.mdm.model.query.ivt.InboundItemQuery;
  20. import com.dk.mdm.model.response.ivt.InboundItemResponse;
  21. import com.dk.mdm.model.response.ivt.InboundResponse;
  22. import com.dk.mdm.model.vo.ivt.InboundItemVO;
  23. import com.dk.mdm.model.vo.ivt.InboundVO;
  24. import com.dk.mdm.service.common.CommonService;
  25. import com.dk.mdm.service.ivt.inventory.InventoryService;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import org.springframework.transaction.annotation.Transactional;
  29. import java.math.BigDecimal;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.UUID;
  34. import java.util.stream.Collectors;
  35. /**
  36. * @author : 寇珊珊
  37. * @desc : 盘盈入库业务层
  38. * @date : 2024/3/7 14:11
  39. */
  40. @Service
  41. public class InboundCheckService extends BaseService<Inbound> {
  42. @Override
  43. public BaseMapper<Inbound> getRepository() {
  44. return inboundMapper;
  45. }
  46. @Autowired
  47. private InboundMapper inboundMapper;
  48. @Autowired
  49. private InboundConvert inboundConvert;
  50. @Autowired
  51. private InboundItemMapper inboundItemMapper;
  52. @Autowired
  53. private InboundItemConvert inboundItemConvert;
  54. @Autowired
  55. private CommonService commonService;
  56. @Autowired
  57. private InventoryService inventoryService;
  58. /**
  59. * @desc : 查询入库价
  60. * @date : 2024/7/1 11:29
  61. * @author : 寇珊珊
  62. */
  63. @Transactional(rollbackFor = {Exception.class})
  64. public InboundItemResponse selectPriceInto(InboundItemVO inboundItemVO) {
  65. List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectPriceInto(new InboundItemQuery().setSkuId(inboundItemVO.getSkuId())
  66. .setNonStdCode(inboundItemVO.getNonStdCode())
  67. .setWhId(inboundItemVO.getWhId()));
  68. if (inboundItemResponses != null && inboundItemResponses.size() > 0) {
  69. return inboundItemResponses.get(0);
  70. }
  71. return null;
  72. }
  73. /**
  74. * @desc : 盘盈入库新建、库存调整-入库 \ 新建商品档案->期初入库
  75. * @date : 2024/3/7 14:13
  76. * 入库中数量/金额 已入库数量/金额 由调用方传入
  77. * @author : 寇珊珊
  78. */
  79. @Transactional(rollbackFor = {Exception.class})
  80. public InboundVO checkInboundInsert(Map<String, Object> map) {
  81. //region map转json
  82. JSONObject total = new JSONObject();
  83. JSONArray detail = new JSONArray();
  84. //总单
  85. if (map.get("total") != null) {
  86. total = (JSONObject) JSON.toJSON(map.get("total"));
  87. }
  88. //明细
  89. if (map.get("total") != null) {
  90. detail = (JSONArray) JSON.toJSON(map.get("detail"));
  91. }
  92. InboundVO inboundVO = new InboundVO();
  93. inboundVO = total.toJavaObject(InboundVO.class);
  94. List<InboundItemVO> inboundItemVOList = detail.toJavaList(InboundItemVO.class);
  95. inboundVO.setItemList(inboundItemVOList);
  96. //endregion
  97. //region 校验明细
  98. if (inboundVO.getItemList().size() == 0) {
  99. throw new BaseBusinessException(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
  100. ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
  101. }
  102. //endregion
  103. //region 查询当前入库明细中是否存在未空或者0的入库价,如果存在去库存流水差最近一条有价格的数据赋值到当前明细
  104. Boolean priceIntoFlag = false;
  105. for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
  106. if(inboundItemVO.getCostPrice()==null || inboundItemVO.getCostPrice().compareTo(BigDecimal.ZERO)==0){
  107. priceIntoFlag = true;
  108. //查询库存批次最近一条入库价
  109. InboundItemResponse inboundItemResponse = this.selectPriceInto(inboundItemVO);
  110. inboundItemVO.setPriceInto(inboundItemResponse != null ? inboundItemResponse.getPriceInto() : BigDecimal.ZERO);
  111. inboundItemVO.setIntoingAmt(inboundItemResponse != null ? inboundItemResponse.getPriceInto().multiply(inboundItemVO.getIntoingQty()).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
  112. inboundItemVO.setCostPrice(inboundItemResponse != null ? inboundItemResponse.getCostPrice() : BigDecimal.ZERO);
  113. inboundItemVO.setCostAmt(inboundItemResponse != null ? inboundItemResponse.getCostPrice().multiply(inboundItemVO.getIntoingQty()).setScale(2, BigDecimal.ROUND_HALF_UP) : BigDecimal.ZERO);
  114. }
  115. }
  116. if(priceIntoFlag){
  117. BigDecimal intoingAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  118. inboundVO.setIntoingAmt(intoingAmt);
  119. }
  120. //endregion
  121. //region 总单
  122. //获取 id/单号
  123. Map<String, Object> codeMap = new HashMap<>();
  124. // 盘盈入库
  125. if(Constant.IntoType.PROFIT.getName().equals(inboundVO.getIntoType())){
  126. codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.INVENTORYPROFITINBOUND.getName(), false);
  127. }
  128. //仓库调整
  129. else{
  130. codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.MOVE.getName(), false);
  131. }
  132. inboundVO.setIntoId(codeMap.get("outId").toString()).
  133. setIntoNo(codeMap.get("outNote").toString());
  134. //todo 入库类型可能是盘盈可能是库位移动 调用的时候传
  135. // //入库类型
  136. // inboundVO.setIntoType(Constant.IntoType.PROFIT.getName());
  137. //已入库
  138. inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
  139. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  140. inboundVO.setIntoQty(inboundVO.getIntoingQty())
  141. .setIntoAmt(inboundVO.getIntoingAmt())
  142. .setIntoingQty(BigDecimal.ZERO)
  143. .setIntoingAmt(BigDecimal.ZERO)
  144. ;
  145. //实体转换
  146. Inbound inbound = inboundConvert.convertToPo(inboundVO);
  147. inboundMapper.insert(inbound);
  148. //endregion
  149. //region 明细
  150. for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
  151. //region 明细
  152. //总单id
  153. inboundItemVO.setIntoId(inboundVO.getIntoId());
  154. //入库类型
  155. inboundItemVO.setIntoType(inboundVO.getIntoType());
  156. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  157. inboundItemVO
  158. .setIntoQty(inboundItemVO.getIntoingQty())
  159. .setIntoAmt(inboundItemVO.getIntoingAmt())
  160. .setIntoingQty(BigDecimal.ZERO)
  161. .setIntoingAmt(BigDecimal.ZERO)
  162. ;
  163. //入库状态
  164. inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
  165. //实体转换
  166. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  167. inboundItemMapper.insert(inboundItem);
  168. inboundItemVO.setItemId(inboundItem.getItemId());
  169. //endregion
  170. //region 将库存需要的参数赋值
  171. inboundItemVO.setInventoryType(Constant.InventoryType.INBOUND.getName());
  172. //盘盈入库
  173. if(Constant.IntoType.PROFIT.getName().equals(inboundVO.getIntoType())){
  174. inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.INVENTORY_PROFIT.getValue());
  175. }
  176. //期初入库
  177. if(Constant.IntoType.BEGIN.getName().equals(inboundVO.getIntoType())){
  178. inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.INVENTORY_BEGIN.getValue());
  179. }
  180. //仓库调整
  181. else{
  182. inboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.MOVE.getValue());
  183. }
  184. inboundItemVO.setAddOrEditFlag(true);
  185. //endregion
  186. }
  187. //endregion
  188. //region 修改库存
  189. Map<String, Object> invMap = new HashMap<>();
  190. invMap.put("intoDetail", inboundVO.getItemList());
  191. inventoryService.operatingInventoryInformation(invMap);
  192. //endregion
  193. return inboundVO;
  194. }
  195. /**
  196. * @desc : 作废
  197. * @date : 2024/4/16 13:27
  198. * @author : 寇珊珊
  199. */
  200. @Transactional(rollbackFor = {Exception.class})
  201. public ResponseResultVO<?> inboundRepeal(String fromId) {
  202. //region 查询总单 查询明细
  203. //根据来源id查询 此条入库单的数据还未更改前的数据
  204. InboundResponse inboundResponse = inboundMapper.selectByFromId(fromId);
  205. //根据总单id查询
  206. List<InboundItemResponse> inboundItemResponseList = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inboundResponse.getIntoId()));
  207. //endregion
  208. //region 修改明细
  209. for (InboundItemResponse inboundItemResponse : inboundItemResponseList) {
  210. //region 将库存需要的参数赋值
  211. inboundItemResponse.setInventoryType(Constant.InventoryType.INBOUND.getName());
  212. //盘盈入库
  213. if(Constant.IntoType.PROFIT.getName().equals(inboundItemResponse.getIntoType())){
  214. inboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.INVENTORY_PROFIT.getValue());
  215. }
  216. //仓库调整
  217. else{
  218. inboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.MOVE.getValue());
  219. }
  220. inboundItemResponse.setIntoQty(inboundItemResponse.getIntoQty().negate());
  221. inboundItemResponse.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
  222. //endregion
  223. //赋值
  224. InboundItem inboundItem = new InboundItem();
  225. inboundItem.setItemId(inboundItemResponse.getItemId());
  226. inboundItem.setFlgValid(false);
  227. //修改
  228. inboundItemMapper.update(inboundItem,
  229. new UpdateWrapper<InboundItem>().lambda()
  230. .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
  231. );
  232. }
  233. //endregion
  234. //region 修改入库总单
  235. Inbound inbound = new Inbound();
  236. inbound.setIntoId(inboundResponse.getIntoId());
  237. inbound.setFlgValid(false);
  238. //修改
  239. inboundMapper.update(inbound,
  240. new UpdateWrapper<Inbound>().lambda().eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
  241. );
  242. //endregion
  243. //region 库存
  244. Map<String, Object> map = new HashMap<>();
  245. map.put("delIntoDetail", inboundItemResponseList);
  246. inventoryService.operatingInventoryInformation(map);
  247. //endregion
  248. return ResponseResultUtil.success();
  249. }
  250. }