OutboundSaleOrderService.java 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package com.dk.mdm.service.ivt.outbound;
  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.mapper.BaseMapper;
  8. import com.dk.common.model.pojo.PageList;
  9. import com.dk.common.response.ResponseCodeEnum;
  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.OutboundConvert;
  14. import com.dk.mdm.infrastructure.convert.ivt.OutboundItemConvert;
  15. import com.dk.mdm.mapper.ivt.OutboundItemMapper;
  16. import com.dk.mdm.mapper.ivt.OutboundMapper;
  17. import com.dk.mdm.mapper.sale.OrderItemMapper;
  18. import com.dk.mdm.mapper.sale.OrderMapper;
  19. import com.dk.mdm.model.pojo.ivt.Outbound;
  20. import com.dk.mdm.model.pojo.ivt.OutboundItem;
  21. import com.dk.mdm.model.pojo.sale.Order;
  22. import com.dk.mdm.model.pojo.sale.OrderItem;
  23. import com.dk.mdm.model.query.ivt.OutboundItemQuery;
  24. import com.dk.mdm.model.query.ivt.OutboundQuery;
  25. import com.dk.mdm.model.response.ivt.InboundResponse;
  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.sale.OrderItemResponse;
  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 org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.math.BigDecimal;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.UUID;
  41. /**
  42. * @desc : 销售出库业务层
  43. * @date : 2024/3/18 15:33
  44. * @author : 寇珊珊
  45. */
  46. @Service
  47. public class OutboundSaleOrderService extends BaseService<Outbound> {
  48. @Override
  49. public BaseMapper<Outbound> getRepository() {
  50. return outboundMapper;
  51. }
  52. @Autowired
  53. private CommonService commonService;
  54. @Autowired
  55. private OutboundMapper outboundMapper;
  56. @Autowired
  57. private OutboundConvert outboundConvert;
  58. @Autowired
  59. private OutboundItemMapper outboundItemMapper;
  60. @Autowired
  61. private OutboundItemConvert outboundItemConvert;
  62. @Autowired
  63. private OrderMapper orderMapper;
  64. @Autowired
  65. private OrderItemMapper orderItemMapper;
  66. /**
  67. * @desc : 条件查询
  68. * @date : 2024/3/18 11:20
  69. * @author : 寇珊珊
  70. */
  71. @Pagination
  72. public ResponseResultVO<PageList<InboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  73. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  74. outboundMapper.countByCond(outboundQuery));
  75. }
  76. /**
  77. * @desc : 查询明细
  78. * @date : 2024/3/15 16:43
  79. * @author : 寇珊珊
  80. */
  81. @Pagination
  82. public ResponseResultVO<Map<String, Object>> selectOutBoundSaleOrderItemInfoById(String id) {
  83. Map<String, Object> result = new HashMap<>();
  84. // 商品明细
  85. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  86. result.put("itemList", outboundItemResponseList);
  87. // 收款
  88. // 附件
  89. return ResponseResultUtil.success(result);
  90. }
  91. /**
  92. * @desc : 销售出库新建
  93. * @date : 2024/3/7 14:13
  94. * @author : 寇珊珊
  95. */
  96. @Transactional(rollbackFor = {Exception.class})
  97. public ResponseResultVO<?> saleOrderOutboundInsert(OutboundVO outboundVO) {
  98. //region 总单
  99. //获取 id/单号
  100. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.SALEORDER.getName(), false);
  101. outboundVO.setOutId(codeMap.get("outId").toString()).
  102. setOutNo(codeMap.get("outNote").toString());
  103. //出库类型
  104. outboundVO.setOutType(Constant.OutType.SALE.getName());
  105. //自动入库标识
  106. if (outboundVO.getAutomaticFlg()) {
  107. //已出库
  108. outboundVO.setOutStatus(Constant.OutStatus.YICHUKU.getName());
  109. } else {
  110. //出库中
  111. outboundVO.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  112. }
  113. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  114. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  115. outboundVO
  116. .setOutQty(outboundVO.getOutingQty())
  117. .setOutAmt(outboundVO.getOutingAmt())
  118. .setOutingQty(BigDecimal.ZERO)
  119. .setOutingAmt(BigDecimal.ZERO)
  120. ;
  121. } else {
  122. outboundVO
  123. .setOutQty(BigDecimal.ZERO)
  124. .setOutAmt(BigDecimal.ZERO)
  125. ;
  126. }
  127. //实体转换
  128. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  129. outboundMapper.insert(outbound);
  130. //endregion
  131. //region 销售退货
  132. if (outboundVO.getFromId() != null) {
  133. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  134. Order order = new Order();
  135. order.setOrderId(outboundVO.getFromId());
  136. //已出库
  137. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())){
  138. order.setOutingQty(outboundVO.getOutQty().negate());
  139. order.setOutingAmt(outboundVO.getOutAmt().negate());
  140. order.setOutQty(outboundVO.getOutQty());
  141. order.setOutAmt(outboundVO.getOutAmt());
  142. }
  143. //出库中
  144. else{
  145. order.setOutingQty(outboundVO.getOutingQty());
  146. order.setOutingAmt(outboundVO.getOutingAmt());
  147. }
  148. //根据id查询
  149. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  150. //入库状态
  151. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()), orderResponse.getOutQty().add(order.getOutQty()));
  152. order.setOutStatus(outStatus);
  153. //修改
  154. int countRow = orderMapper.updateById(order);
  155. //数量超出
  156. if (countRow == 0) {
  157. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  158. }
  159. }
  160. //endregion
  161. //region 明细
  162. //校验明细
  163. if (outboundVO.getItemList().size() == 0) {
  164. return ResponseResultUtil.error(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  165. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  166. }
  167. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  168. //总单id
  169. outboundItemVO.setOutId(outboundVO.getOutId());
  170. //出库类型
  171. outboundItemVO.setOutType(outboundVO.getOutType());
  172. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  173. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  174. outboundItemVO
  175. .setOutQty(outboundItemVO.getOutingQty())
  176. .setOutAmt(outboundItemVO.getOutingAmt())
  177. .setOutingQty(BigDecimal.ZERO)
  178. .setOutingAmt(BigDecimal.ZERO)
  179. .setCostPrice(outboundItemVO.getPriceOut())
  180. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  181. ;
  182. } else {
  183. outboundItemVO
  184. .setOutQty(BigDecimal.ZERO)
  185. .setOutAmt(BigDecimal.ZERO);
  186. }
  187. //入库状态
  188. outboundItemVO.setOutStatus(outboundItemVO.getOutStatus());
  189. //实体转换
  190. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  191. outboundItemMapper.insert(outboundItem);
  192. //endregion
  193. //region 销售退货明细
  194. if (outboundItemVO.getFromItemId() != null) {
  195. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  196. OrderItem orderItem = new OrderItem();
  197. orderItem.setItemId(outboundItemVO.getFromItemId());
  198. //已出库
  199. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())){
  200. orderItem.setOutingQty(outboundVO.getOutQty().negate());
  201. orderItem.setOutingAmt(outboundVO.getOutAmt().negate());
  202. orderItem.setOutQty(outboundVO.getOutQty());
  203. orderItem.setOutAmt(outboundVO.getOutAmt());
  204. }
  205. //出库中
  206. else{
  207. orderItem.setOutingQty(outboundVO.getOutingQty());
  208. orderItem.setOutingAmt(outboundVO.getOutingAmt());
  209. }
  210. //根据id查询
  211. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  212. //入库状态
  213. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  214. orderItem.setOutStatus(outStatus);
  215. //修改
  216. int countRow = orderItemMapper.updateById(orderItem);
  217. //数量超出
  218. if (countRow == 0) {
  219. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  220. }
  221. }
  222. //endregion
  223. }
  224. //endregion
  225. //todo 如果是已出库 调用库存 后续写库存这里补上
  226. //region 库存
  227. //endregion
  228. return ResponseResultUtil.success(outboundVO);
  229. }
  230. /**
  231. * @desc : 销售出库办理
  232. * @date : 2024/3/7 15:47
  233. * @author : 寇珊珊
  234. */
  235. @Transactional(rollbackFor = {Exception.class})
  236. public ResponseResultVO<?> saleOrderHandleOutbound(OutboundVO outboundVO) {
  237. //region 编辑明细
  238. //校验明细
  239. if (outboundVO.getItemList().size() == 0) {
  240. return ResponseResultUtil.error(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  241. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  242. }
  243. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  244. if (outboundItemVO.getItemId() != null) {
  245. //查询原单
  246. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  247. //编辑明细
  248. outboundItemVO
  249. .setOutQty(outboundItemResponse.getOutQty().add(outboundItemVO.getOutingQty()))
  250. .setOutAmt(outboundItemResponse.getOutAmt().add(outboundItemVO.getOutingAmt()))
  251. .setOutingQty(outboundItemResponse.getOutingQty().subtract(outboundItemVO.getOutingQty()))
  252. .setOutAmt(outboundItemResponse.getOutingAmt().subtract(outboundItemVO.getOutingAmt()))
  253. .setCostPrice(outboundItemVO.getPriceOut())
  254. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  255. ;
  256. //出库状态
  257. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  258. outboundItemVO.setOutStatus(outStatus);
  259. //实体转换
  260. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  261. //修改
  262. outboundItemMapper.update(outboundItem,
  263. new UpdateWrapper<OutboundItem>().lambda()
  264. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  265. );
  266. }
  267. //endregion
  268. //region 新建明细
  269. else {
  270. outboundItemVO
  271. .setOutId(outboundItemVO.getOutId())
  272. .setOutQty(outboundItemVO.getOutQty())
  273. .setOutAmt(outboundItemVO.getOutAmt())
  274. .setCostPrice(outboundItemVO.getPriceOut())
  275. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  276. .setOutType(Constant.OutType.SALE.getName());
  277. //入库状态
  278. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  279. outboundItemVO.setOutStatus(outStatus);
  280. //实体转换
  281. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  282. //新建
  283. outboundItemMapper.insert(outboundItem);
  284. }
  285. //endregion
  286. //region 销售订单明细
  287. if (outboundItemVO.getFromItemId() != null) {
  288. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  289. OrderItem orderItem = new OrderItem();
  290. orderItem.setItemId(outboundItemVO.getFromItemId());
  291. orderItem.setOutingQty(outboundItemVO.getOutingQty());
  292. orderItem.setOutingAmt(outboundItemVO.getOutingAmt());
  293. orderItem.setOutQty(outboundItemVO.getOutQty());
  294. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  295. //根据id查询
  296. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  297. //出库状态
  298. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  299. orderItem.setOutStatus(outStatus);
  300. //修改
  301. int countRow = orderItemMapper.updateById(orderItem);
  302. //数量超出
  303. if (countRow == 0) {
  304. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  305. }
  306. }
  307. //endregion
  308. }
  309. //endregion
  310. //region 删除明细 todo 2024年3月20日13:17:33 这里可能用不到
  311. // BigDecimal sumDelOutQty = BigDecimal.ZERO;
  312. // BigDecimal sumDelOutAmt = BigDecimal.ZERO;
  313. // BigDecimal sumDelOutingQty = BigDecimal.ZERO;
  314. // BigDecimal sumDelOutingAmt = BigDecimal.ZERO;
  315. // if (outboundVO.getDeleteItemList() != null) {
  316. // sumDelOutQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  317. // sumDelOutAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  318. // sumDelOutingQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  319. // sumDelOutingAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  320. // for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  321. // if (outboundItemVO.getItemId() != null) {
  322. // OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  323. // outboundItem.setFlgValid(false);
  324. // //修改
  325. // outboundItemMapper.update(outboundItem,
  326. // new UpdateWrapper<OutboundItem>().lambda()
  327. // .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  328. // );
  329. // }
  330. // //region 销售退货明细
  331. // if (outboundItemVO.getFromItemId() != null) {
  332. // //region 销售退货订单明细
  333. // //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  334. // OrderItem orderItem = new OrderItem();
  335. // orderItem.setItemId(outboundItemVO.getFromItemId());
  336. // orderItem.setOutingQty(outboundItemVO.getOutingQty().negate());
  337. // orderItem.setOutingAmt(outboundItemVO.getOutingAmt().negate());
  338. // orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  339. // orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  340. // //根据id查询
  341. // OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  342. // //出库状态
  343. // String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  344. // orderItem.setOutStatus(outStatus);
  345. // //修改
  346. // int countRow = orderItemMapper.updateById(orderItem);
  347. // //数量超出
  348. // if (countRow == 0) {
  349. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  350. // }
  351. // //endregion
  352. // }
  353. // //endregion
  354. // }
  355. // }
  356. //endregion
  357. //region 编辑总单
  358. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  359. BigDecimal sumOutQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  360. BigDecimal sumOutAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  361. // BigDecimal sumOutingQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  362. // BigDecimal sumOutingAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  363. outboundVO.setOutQty(sumOutQty);
  364. outboundVO.setOutAmt(sumOutAmt);
  365. outboundVO.setOutingQty(outboundResponse.getOutingQty().subtract(sumOutQty));
  366. outboundVO.setOutingAmt(outboundResponse.getOutingAmt().subtract(sumOutAmt));
  367. //出库状态
  368. String outStatus = this.setOutStatus(outboundVO.getOutingQty(), outboundVO.getOutQty());
  369. outboundVO.setOutStatus(outStatus);
  370. //实体转换
  371. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  372. //修改
  373. outboundMapper.update(outbound,
  374. new UpdateWrapper<Outbound>().lambda()
  375. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  376. );
  377. //endregion
  378. //region 修改销售退货订单
  379. if (outboundVO.getFromId() != null) {
  380. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  381. Order order = new Order();
  382. order.setOrderId(outboundVO.getFromId());
  383. //region todo 2024年3月20日13:18:14 这里可能不用
  384. // order.setOutingQty(sumOutQty.subtract(sumDelOutQty));
  385. // order.setOutingAmt(sumOutAmt.subtract(sumDelOutAmt));
  386. // order.setOutQty((sumOutingQty.add(sumDelOutingQty)).negate());
  387. // order.setOutAmt((sumOutingAmt.add(sumDelOutingAmt)).negate());
  388. //endregion
  389. order.setOutingQty(sumOutQty);
  390. order.setOutingAmt(sumOutAmt);
  391. order.setOutQty(sumOutQty.negate());
  392. order.setOutAmt(sumOutAmt.negate());
  393. //根据id查询
  394. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  395. //出库状态
  396. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()), orderResponse.getOutQty().add(order.getOutQty()));
  397. order.setOutStatus(orderOutStatus);
  398. //修改
  399. int countRow = orderMapper.updateById(order);
  400. //数量超出
  401. if (countRow == 0) {
  402. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  403. }
  404. }
  405. //endregion
  406. //todo 如果是已出库 调用库存 后续写库存这里补上
  407. //region 修改库存
  408. //endregion
  409. return ResponseResultUtil.success(outboundVO);
  410. }
  411. /**
  412. * @desc : 销售出库撤销
  413. * @date : 2024/3/7 17:06
  414. * @author : 寇珊珊
  415. */
  416. @Transactional(rollbackFor = {Exception.class})
  417. public ResponseResultVO<?> saleOrderOutboundCancel(OutboundVO outboundVO) {
  418. //region 查询出库总单数据信息
  419. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  420. //endregion
  421. //region 修改订单数据信息
  422. if (outboundResponse.getFromId() != null) {
  423. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  424. Order order = new Order();
  425. order.setOrderId(outboundVO.getFromId());
  426. order.setOutingQty(outboundVO.getOutingQty());
  427. order.setOutingAmt(outboundVO.getOutingAmt());
  428. order.setOutQty(outboundVO.getOutQty().negate());
  429. order.setOutAmt(outboundVO.getOutAmt().negate());
  430. //根据id查询
  431. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  432. //出库状态
  433. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()), orderResponse.getOutQty().add(order.getOutQty()));
  434. order.setOutStatus(outStatus);
  435. //修改
  436. int countRow = orderMapper.updateById(order);
  437. //数量超出
  438. if (countRow == 0) {
  439. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  440. }
  441. }
  442. //endregion
  443. //region 修改总单数据信息
  444. Outbound outbound = new Outbound();
  445. outbound.setOutId(outboundVO.getOutId());
  446. outbound.setOutDate(null);
  447. outbound.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  448. outbound.setOutQty(BigDecimal.ZERO);
  449. outbound.setOutAmt(BigDecimal.ZERO);
  450. //修改
  451. outboundMapper.update(outbound,
  452. new UpdateWrapper<Outbound>().lambda()
  453. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  454. );
  455. //endregion
  456. //region 明细数据
  457. //根据总单id查明细
  458. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundVO.getOutId()));
  459. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  460. //region 修改销售退货明细数据信息
  461. if (outboundItemResponse.getFromItemId() != null) {
  462. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  463. OrderItem orderItem = new OrderItem();
  464. orderItem.setItemId(outboundItemResponse.getFromItemId());
  465. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  466. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  467. orderItem.setOutingQty(outboundItemResponse.getOutingQty());
  468. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt());
  469. //根据id查询
  470. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  471. //出库状态
  472. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  473. orderItem.setOutStatus(outStatus);
  474. //修改
  475. int countRow = orderItemMapper.updateById(orderItem);
  476. //数量超出
  477. if (countRow == 0) {
  478. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  479. }
  480. }
  481. //endregion
  482. //region修改出库明细信息
  483. OutboundItem outboundItem = new OutboundItem();
  484. outboundItem
  485. .setOutId(outboundResponse.getOutId())
  486. .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  487. .setOutingQty(outboundItemResponse.getOutingQty().add(outboundItemResponse.getOutQty()))
  488. .setOutAmt(outboundItemResponse.getOutingAmt().add(outboundItemResponse.getOutAmt()))
  489. .setOutQty(BigDecimal.ZERO)
  490. .setOutAmt(BigDecimal.ZERO)
  491. .setCostPrice(BigDecimal.ZERO)
  492. .setCostAmt(BigDecimal.ZERO)
  493. .setItemId(outboundItemResponse.getItemId());
  494. //修改
  495. outboundItemMapper.update(outboundItem,
  496. new UpdateWrapper<OutboundItem>().lambda()
  497. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  498. );
  499. //endregion
  500. }
  501. //endregion
  502. //todo 调用库存 后续写库存这里补上
  503. //region 修改库存
  504. //endregion
  505. return ResponseResultUtil.success();
  506. }
  507. /**
  508. * @desc : 入库状态通用(目前本页面)
  509. * @date : 2024/3/9 8:59
  510. * @author : 寇珊珊
  511. */
  512. public String setIntoStatus(BigDecimal intoingQty, BigDecimal intoQty) {
  513. //入库状态
  514. String intoStatus = null;
  515. //已出库数量>0 出库中数量>0
  516. if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0) {
  517. //出库中
  518. intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
  519. }
  520. //已出库数量=0 出库中数量=0
  521. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  522. //待入库
  523. intoStatus = Constant.IntoStatus.DAIRUKU.getName();
  524. }
  525. //已出库数量>0 出库中数量=0
  526. else if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  527. //已出库
  528. intoStatus = Constant.IntoStatus.YIRUKU.getName();
  529. }
  530. return intoStatus;
  531. }
  532. /**
  533. * @desc : 出库状态通用(目前本页面)
  534. * @date : 2024/3/9 8:59
  535. * @author : 寇珊珊
  536. */
  537. public String setOutStatus(BigDecimal outingQty, BigDecimal outQty) {
  538. //出库状态
  539. String outStatus = null;
  540. //已出库数量>0 出库中数量>0
  541. if (outQty.compareTo(BigDecimal.ZERO) > 0 && outingQty.compareTo(BigDecimal.ZERO) > 0) {
  542. //出库中
  543. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  544. }
  545. //出入库数量=0 出库中数量=0
  546. else if (outQty.compareTo(BigDecimal.ZERO) == 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  547. //待出库
  548. outStatus = Constant.OutStatus.DAICHUKU.getName();
  549. }
  550. //已出库数量>0 出库中数量=0
  551. else if (outQty.compareTo(BigDecimal.ZERO) > 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  552. //已出库
  553. outStatus = Constant.OutStatus.YICHUKU.getName();
  554. }
  555. return outStatus;
  556. }
  557. /**
  558. * @desc : 获取单据信息(编辑用)
  559. * @date : 2024/3/16 16:28
  560. * @author : 寇珊珊
  561. */
  562. public ResponseResultVO<?> selectByUpdate(String id) {
  563. Map<String, Object> dataInfo = new HashMap<>();
  564. //总单
  565. OutboundResponse outboundResponse = outboundMapper.selectMessageByOtherQuery(new OutboundQuery().setOutId(id).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName()));
  566. //单据不存在
  567. if (outboundResponse == null) {
  568. return ResponseResultUtil.error(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getCode(),
  569. ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getMessage());
  570. }
  571. dataInfo.put("data", outboundResponse);
  572. // 明细
  573. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  574. dataInfo.put("dataItem", outboundItemResponseList);
  575. return ResponseResultUtil.success(dataInfo);
  576. }
  577. }