InboundOtherService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package com.dk.mdm.service.ivt;
  2. import com.dk.common.infrastructure.annotaiton.Pagination;
  3. import com.dk.common.infrastructure.constant.Constant;
  4. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  5. import com.dk.common.mapper.BaseMapper;
  6. import com.dk.common.model.pojo.PageList;
  7. import com.dk.common.response.ResponseResultUtil;
  8. import com.dk.common.response.ResponseResultVO;
  9. import com.dk.common.service.BaseService;
  10. import com.dk.mdm.infrastructure.convert.ivt.InboundConvert;
  11. import com.dk.mdm.infrastructure.convert.ivt.InboundItemConvert;
  12. import com.dk.mdm.mapper.ivt.InboundItemMapper;
  13. import com.dk.mdm.mapper.ivt.InboundMapper;
  14. import com.dk.mdm.model.pojo.ivt.Inbound;
  15. import com.dk.mdm.model.pojo.ivt.InboundItem;
  16. import com.dk.mdm.model.query.ivt.InboundItemQuery;
  17. import com.dk.mdm.model.query.ivt.InboundQuery;
  18. import com.dk.mdm.model.query.sale.OutReturnItemQuery;
  19. import com.dk.mdm.model.response.ivt.InboundItemResponse;
  20. import com.dk.mdm.model.response.ivt.InboundResponse;
  21. import com.dk.mdm.model.response.sale.OutReturnItemResponse;
  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 org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Service;
  27. import org.springframework.transaction.annotation.Transactional;
  28. import java.math.BigDecimal;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. /**
  33. * @desc : 其他入库业务层
  34. * @date : 2024/3/7 14:11
  35. * @author : 寇珊珊
  36. */
  37. @Service
  38. @Transactional
  39. public class InboundOtherService extends BaseService<Inbound> {
  40. @Override
  41. public BaseMapper<Inbound> getRepository() {
  42. return inboundMapper;
  43. }
  44. @Autowired
  45. private InboundMapper inboundMapper;
  46. @Autowired
  47. private InboundConvert inboundConvert;
  48. @Autowired
  49. private InboundItemMapper inboundItemMapper;
  50. @Autowired
  51. private InboundItemConvert inboundItemConvert;
  52. @Autowired
  53. private CommonService commonService;
  54. /**
  55. * @desc : 条件查询
  56. * @date : 2024/3/7 14:12
  57. * @author : 寇珊珊
  58. */
  59. @Pagination
  60. public ResponseResultVO<PageList<InboundResponse>> selectInboundAndItem(InboundQuery inboundQuery) {
  61. return super.mergeListWithCount(inboundQuery, inboundMapper.selectInboundAndItem(inboundQuery),
  62. inboundMapper.selectInboundAndItemCountByCond(inboundQuery));
  63. }
  64. /**
  65. * @desc : 查询明细
  66. * @date : 2024/3/9 15:43
  67. * @author : 寇珊珊
  68. */
  69. @Pagination
  70. public ResponseResultVO<Map<String, Object>> selectOtherInboundItemInfoById(String id) {
  71. Map<String, Object> result = new HashMap<>();
  72. // 商品明细
  73. List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(id));
  74. result.put("itemList", inboundItemResponses);
  75. // 收款
  76. // 附件
  77. return ResponseResultUtil.success(result);
  78. }
  79. /**
  80. * @desc : 其他入库新建
  81. * @date : 2024/3/7 14:13
  82. * @author : 寇珊珊
  83. */
  84. @Transactional(rollbackFor = {Exception.class})
  85. public ResponseResultVO<?> otherInboundInsert(InboundVO inboundVO) {
  86. //region 总单
  87. //获取 id/单号
  88. Map<String , Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERINBOUND.getName(),false);
  89. inboundVO.setIntoId(codeMap.get("outId").toString()).
  90. setIntoNo(codeMap.get("outNote").toString());
  91. //入库类型
  92. inboundVO.setIntoType(Constant.IntoType.OTHER.getName());
  93. //自动入库标识
  94. if (inboundVO.getAutomaticFlg()) {
  95. //已入库
  96. inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
  97. } else {
  98. //入库中
  99. inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
  100. }
  101. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  102. if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
  103. inboundVO.setIntoQty(inboundVO.getIntoingQty())
  104. .setIntoAmt(inboundVO.getIntoingAmt());
  105. }
  106. //实体转换
  107. Inbound inbound = inboundConvert.convertToPo(inboundVO);
  108. inboundMapper.insert(inbound);
  109. //endregion
  110. //region 明细
  111. //校验明细
  112. if (inboundVO.getItemList().size() == 0) {
  113. return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
  114. ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
  115. }
  116. for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
  117. Map<String , Object> map = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERINBOUNDITEM.getName(),true);
  118. //明细id
  119. inboundItemVO.setItemId(map.get("outId").toString());
  120. //总单id
  121. inboundItemVO.setIntoId(inboundVO.getIntoId());
  122. //入库类型
  123. inboundItemVO.setIntoType(inboundVO.getIntoType());
  124. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  125. if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
  126. inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty())
  127. .setIntoAmt(inboundItemVO.getIntoingAmt());
  128. }
  129. //成本单价
  130. inboundItemVO.setCostPrice(inboundItemVO.getCostPrice());
  131. //成本金额
  132. inboundItemVO.setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP));
  133. //库存数量 已入库-出库
  134. inboundItemVO.setInvQty(inboundItemVO.getReturnQty().subtract(inboundItemVO.getOutQty()));
  135. //入库状态
  136. inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
  137. //实体转换
  138. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  139. inboundItemMapper.insert(inboundItem);
  140. }
  141. //endregion
  142. //todo 如果是已入库 调用库存 后续写库存这里补上
  143. //region 库存
  144. //endregion
  145. return ResponseResultUtil.success(inboundVO);
  146. }
  147. /**
  148. * @desc : 其他入库办理
  149. * @date : 2024/3/7 15:47
  150. * @author : 寇珊珊
  151. */
  152. @Transactional(rollbackFor = {Exception.class})
  153. public ResponseResultVO<?> otherHandleInbound(InboundVO inboundVO) {
  154. //大编辑标识
  155. if (inboundVO.getAllEdit() != null && inboundVO.getAllEdit()) {
  156. //region 编辑明细
  157. //校验明细
  158. if (inboundVO.getItemList().size() == 0) {
  159. return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
  160. ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
  161. }
  162. for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
  163. if (inboundItemVO.getIntoId() != null) {
  164. //编辑明细
  165. inboundItemVO.setCostPrice(inboundItemVO.getPriceInto())
  166. .setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP))
  167. .setIntoQty(inboundItemVO.getIntoingQty())
  168. .setIntoAmt(inboundItemVO.getIntoingAmt());
  169. //入库状态
  170. String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
  171. inboundItemVO.setIntoStatus(intoStatus);
  172. //实体转换
  173. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  174. //修改
  175. inboundItemMapper.updateById(inboundItem);
  176. }
  177. //endregion
  178. //region 新建明细
  179. else {
  180. Map<String , Object> map = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERINBOUNDITEM.getName(),true);
  181. inboundItemVO
  182. .setItemId(map.get("outId").toString())
  183. .setIntoQty(inboundItemVO.getIntoingQty())
  184. .setIntoAmt(inboundItemVO.getIntoingAmt())
  185. .setIntoId(inboundVO.getIntoId())
  186. .setCostPrice(inboundItemVO.getPriceInto())
  187. .setCostAmt(inboundItemVO.getIntoingQty().multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP))
  188. .setIntoType(Constant.IntoType.OTHER.getName());
  189. //入库状态
  190. String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
  191. inboundItemVO.setIntoStatus(intoStatus);
  192. //实体转换
  193. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  194. //新建
  195. inboundItemMapper.insert(inboundItem);
  196. }
  197. }
  198. //endregion
  199. //region 删除明细
  200. if (inboundVO.getDeleteItemList() != null) {
  201. for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
  202. if (inboundItemVO.getItemId() != null) {
  203. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  204. inboundItem.setFlgValid(false);
  205. inboundItemMapper.updateById(inboundItem);
  206. }
  207. }
  208. }
  209. //endregion
  210. //region 编辑总单
  211. BigDecimal sumIntoQty = inboundVO.getItemList().stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  212. BigDecimal sumIntoAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  213. inboundVO.setIntoQty(sumIntoQty);
  214. inboundVO.setIntoAmt(sumIntoAmt);
  215. //已入库数量==入库中数量 入库完成
  216. if(sumIntoQty.compareTo(inboundVO.getIntoQty())==0){
  217. //已入库
  218. inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
  219. } else {
  220. //入库中
  221. inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
  222. }
  223. //实体转换
  224. Inbound inbound = inboundConvert.convertToPo(inboundVO);
  225. //修改
  226. super.update(inbound);
  227. //endregion
  228. //todo 如果是已入库 调用库存 后续写库存这里补上
  229. //region 修改库存
  230. //endregion
  231. }
  232. return ResponseResultUtil.success(inboundVO);
  233. }
  234. /**
  235. * @desc : 其它入库撤销
  236. * @date : 2024/3/7 17:06
  237. * @author : 寇珊珊
  238. */
  239. @Transactional(rollbackFor = {Exception.class})
  240. public ResponseResultVO<?> otherInboundCancel(InboundVO inboundVO) {
  241. //region 总单数据信息
  242. InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
  243. Inbound inbound = new Inbound();
  244. inbound.setIntoId(inboundResponse.getIntoId())
  245. .setIntoDate(null)
  246. .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName())
  247. .setIntoQty(BigDecimal.ZERO)
  248. .setIntoAmt(BigDecimal.ZERO);
  249. inboundMapper.updateById(inbound);
  250. //endregion
  251. //region 明细数据
  252. InboundItemQuery inboundItemQuery = new InboundItemQuery().setIntoId(inbound.getIntoId());
  253. //根据总单查明细
  254. List<InboundItemResponse> orderEntryItemResponsesList = inboundItemMapper.selectByCond(inboundItemQuery);
  255. for (InboundItemResponse inboundItemResponse : orderEntryItemResponsesList) {
  256. InboundItem inboundItem = new InboundItem();
  257. inboundItem.setIntoId(inbound.getIntoId())
  258. .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName())
  259. .setIntoQty(BigDecimal.ZERO)
  260. .setIntoAmt(BigDecimal.ZERO)
  261. .setItemId(inboundItemResponse.getItemId());
  262. inboundItemMapper.updateById(inboundItem);
  263. }
  264. //endregion
  265. //region 修改库存
  266. //endregion
  267. return ResponseResultUtil.success();
  268. }
  269. /**
  270. * @desc : 入库状态通用(目前本页面)
  271. * @date : 2024/3/9 8:59
  272. * @author : 寇珊珊
  273. */
  274. public String setIntoStatus(BigDecimal intoingQty, BigDecimal intoQty) {
  275. //入库状态
  276. String intoStatus = null;
  277. //已入库数量=0 入库中数量>0
  278. if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0) {
  279. //入库中
  280. intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
  281. }
  282. //已入库数量=0 入库中数量=0
  283. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  284. //待入库
  285. intoStatus = Constant.IntoStatus.DAIRUKU.getName();
  286. }
  287. //已入库数量>0 入库中数量>0
  288. else if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0) {
  289. //入库中
  290. intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
  291. }
  292. return intoStatus;
  293. }
  294. }