|
@@ -1,18 +1,41 @@
|
|
|
package com.fjhx.victoriatourist.service.jd.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fjhx.common.utils.Assert;
|
|
|
+import com.fjhx.item.entity.product.po.ProductInfo;
|
|
|
+import com.fjhx.item.service.product.ProductInfoService;
|
|
|
+import com.fjhx.victoriatourist.entity.jd.dto.JdRefundDto;
|
|
|
import com.fjhx.victoriatourist.entity.jd.dto.JdRefundSelectDto;
|
|
|
import com.fjhx.victoriatourist.entity.jd.po.JdRefund;
|
|
|
import com.fjhx.victoriatourist.entity.jd.po.JdRefundDetail;
|
|
|
+import com.fjhx.victoriatourist.entity.jd.po.JdRefundNotQualityCheck;
|
|
|
import com.fjhx.victoriatourist.entity.jd.vo.JdRefundVo;
|
|
|
import com.fjhx.victoriatourist.mapper.jd.JdRefundMapper;
|
|
|
+import com.fjhx.victoriatourist.service.jd.JdRefundDetailService;
|
|
|
+import com.fjhx.victoriatourist.service.jd.JdRefundNotQualityCheckService;
|
|
|
import com.fjhx.victoriatourist.service.jd.JdRefundService;
|
|
|
+import com.fjhx.wms.entity.stock.po.Stock;
|
|
|
+import com.fjhx.wms.service.stock.StockService;
|
|
|
+import com.ruoyi.common.constant.StatusConstant;
|
|
|
+import com.ruoyi.common.core.domain.BaseIdPo;
|
|
|
import com.ruoyi.common.utils.wrapper.IWrapper;
|
|
|
import com.ruoyi.common.utils.wrapper.SqlField;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -26,6 +49,37 @@ import java.util.Date;
|
|
|
@Service
|
|
|
public class JdRefundServiceImpl extends ServiceImpl<JdRefundMapper, JdRefund> implements JdRefundService {
|
|
|
|
|
|
+ // 仓库id
|
|
|
+ public final static Long WAREHOUSE_ID = 1774616929466654722L;
|
|
|
+ private static final String sql = "victoriatourist_json=JSON_SET(" +
|
|
|
+ "victoriatourist_json,'$.{}',(json_extract(victoriatourist_json,'$.{}'){}{}))";
|
|
|
+ private static final Map<Integer, String[]> typeMappings = new HashMap<Integer, String[]>() {{
|
|
|
+ put(1, new String[]{"frozenQuantity", "+"});
|
|
|
+ put(2, new String[]{"frozenQuantity", "-"});
|
|
|
+ put(3, new String[]{"defectiveQuantity", "+"});
|
|
|
+ put(4, new String[]{"defectiveQuantity", "-"});
|
|
|
+ }};
|
|
|
+ @Autowired
|
|
|
+ private JdRefundDetailService jdRefundDetailService;
|
|
|
+ @Autowired
|
|
|
+ private ProductInfoService productInfoService;
|
|
|
+ @Autowired
|
|
|
+ private StockService stockService;
|
|
|
+ @Autowired
|
|
|
+ private JdRefundNotQualityCheckService jdRefundNotQualityCheckService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param type 1冻结新增 2冻结减少 3次品新增 4次品减少
|
|
|
+ * @return sql
|
|
|
+ */
|
|
|
+ public static String getUpdateStockSql(Integer type, Number quantity) {
|
|
|
+ String[] mapping = typeMappings.get(type);
|
|
|
+ if (mapping == null) {
|
|
|
+ throw new IllegalArgumentException("type参数错误");
|
|
|
+ }
|
|
|
+ return StrUtil.format(sql, mapping[0], mapping[0], mapping[1], quantity);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Page<JdRefundVo> getPage(JdRefundSelectDto dto) {
|
|
|
|
|
@@ -49,4 +103,164 @@ public class JdRefundServiceImpl extends ServiceImpl<JdRefundMapper, JdRefund> i
|
|
|
return baseMapper.getPage(dto.getPage(), wrapper);
|
|
|
}
|
|
|
|
|
|
+ @DSTransactional
|
|
|
+ @Override
|
|
|
+ public void confirm(JdRefundDto dto) {
|
|
|
+ JdRefundDetail jdRefundDetail = jdRefundDetailService.getById(dto.getId());
|
|
|
+ Assert.notEmpty(jdRefundDetail, "京东售后明细不存在");
|
|
|
+
|
|
|
+ Integer status = jdRefundDetail.getStatus();
|
|
|
+ Assert.eqZero(status, "京东售后单已确认");
|
|
|
+
|
|
|
+ // 获取产品编号
|
|
|
+ String wareId = jdRefundDetail.getWareId();
|
|
|
+ ProductInfo productInfo = productInfoService.getOne(q -> q.eq(ProductInfo::getCustomCode, wareId));
|
|
|
+ Assert.notEmpty(productInfo, "未知产品编码:" + wareId);
|
|
|
+
|
|
|
+ // key: product_id value:产品数量
|
|
|
+ Map<Long, Integer> map = new HashMap<>();
|
|
|
+
|
|
|
+ // 添加质检信息
|
|
|
+ setJdRefundQualityCheck(map, productInfo, jdRefundDetail, 1);
|
|
|
+
|
|
|
+ // 修改为已确认
|
|
|
+ jdRefundDetail.setStatus(StatusConstant.YES);
|
|
|
+ jdRefundDetailService.updateById(jdRefundDetail);
|
|
|
+
|
|
|
+ // 新增锁定库存
|
|
|
+ Set<Long> productIdSet = map.keySet();
|
|
|
+
|
|
|
+ Map<Long, JdRefundNotQualityCheck> notQualityCheckMap = jdRefundNotQualityCheckService
|
|
|
+ .list(q -> q.in(JdRefundNotQualityCheck::getProductId, productIdSet))
|
|
|
+ .stream().collect(Collectors.toMap(JdRefundNotQualityCheck::getProductId, Function.identity()));
|
|
|
+
|
|
|
+ Map<Long, Stock> stockMap = stockService
|
|
|
+ .list(q -> q.in(Stock::getProductId, productIdSet).eq(Stock::getWarehouseId, WAREHOUSE_ID))
|
|
|
+ .stream().collect(Collectors.toMap(Stock::getProductId, Function.identity()));
|
|
|
+
|
|
|
+ map.forEach((k, v) -> {
|
|
|
+
|
|
|
+ // 添加未质检库存
|
|
|
+ JdRefundNotQualityCheck jdRefundNotQualityCheck = notQualityCheckMap.get(k);
|
|
|
+ if (jdRefundNotQualityCheck == null) {
|
|
|
+ JdRefundNotQualityCheck saveJdRefundNotQualityCheck = new JdRefundNotQualityCheck();
|
|
|
+ saveJdRefundNotQualityCheck.setProductId(k);
|
|
|
+ saveJdRefundNotQualityCheck.setQuantity(v);
|
|
|
+ jdRefundNotQualityCheckService.save(saveJdRefundNotQualityCheck);
|
|
|
+ } else {
|
|
|
+ jdRefundNotQualityCheckService.update(q -> q
|
|
|
+ .eq(BaseIdPo::getId, jdRefundNotQualityCheck.getId())
|
|
|
+ .setSql("quantity = quantity + " + v)
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // 修改库存
|
|
|
+ Stock stock = stockMap.get(k);
|
|
|
+ if (stock == null) {
|
|
|
+ JSONObject victoriatouristJson = new JSONObject();
|
|
|
+ victoriatouristJson.put("frozenQuantity", v);
|
|
|
+ victoriatouristJson.put("defectiveQuantity", 0);
|
|
|
+
|
|
|
+ Stock saveStock = new Stock();
|
|
|
+ saveStock.setWarehouseId(WAREHOUSE_ID);
|
|
|
+ saveStock.setProductId(k);
|
|
|
+ saveStock.setQuantity(BigDecimal.ZERO);
|
|
|
+ saveStock.setVictoriatouristJson(victoriatouristJson.toJSONString());
|
|
|
+ stockService.save(saveStock);
|
|
|
+ } else {
|
|
|
+ stockService.update(q -> q
|
|
|
+ .eq(BaseIdPo::getId, stock.getId())
|
|
|
+ .setSql(getUpdateStockSql(1, v))
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @DSTransactional
|
|
|
+ @Override
|
|
|
+ public void cancelConfirm(JdRefundDto dto) {
|
|
|
+ JdRefundDetail jdRefundDetail = jdRefundDetailService.getById(dto.getId());
|
|
|
+ Assert.notEmpty(jdRefundDetail, "京东售后明细不存在");
|
|
|
+
|
|
|
+ Integer status = jdRefundDetail.getStatus();
|
|
|
+ Assert.eqOne(status, "京东售后单未确认");
|
|
|
+
|
|
|
+ // 获取产品编号
|
|
|
+ String wareId = jdRefundDetail.getWareId();
|
|
|
+ ProductInfo productInfo = productInfoService.getOne(q -> q.eq(ProductInfo::getCustomCode, wareId));
|
|
|
+ Assert.notEmpty(productInfo, "未知产品编码:" + wareId);
|
|
|
+
|
|
|
+ // key: product_id value:产品数量
|
|
|
+ Map<Long, Integer> map = new HashMap<>();
|
|
|
+
|
|
|
+ // 添加质检信息
|
|
|
+ setJdRefundQualityCheck(map, productInfo, jdRefundDetail, 1);
|
|
|
+
|
|
|
+ // 修改为待确认
|
|
|
+ jdRefundDetail.setStatus(StatusConstant.NO);
|
|
|
+ jdRefundDetailService.updateById(jdRefundDetail);
|
|
|
+
|
|
|
+ // 新增锁定库存
|
|
|
+ Set<Long> productIdSet = map.keySet();
|
|
|
+
|
|
|
+ Map<Long, JdRefundNotQualityCheck> notQualityCheckMap = jdRefundNotQualityCheckService
|
|
|
+ .list(q -> q.in(JdRefundNotQualityCheck::getProductId, productIdSet))
|
|
|
+ .stream().collect(Collectors.toMap(JdRefundNotQualityCheck::getProductId, Function.identity()));
|
|
|
+
|
|
|
+ Map<Long, Stock> stockMap = stockService
|
|
|
+ .list(q -> q.in(Stock::getProductId, productIdSet).eq(Stock::getWarehouseId, WAREHOUSE_ID))
|
|
|
+ .stream().collect(Collectors.toMap(Stock::getProductId, Function.identity()));
|
|
|
+
|
|
|
+ map.forEach((k, v) -> {
|
|
|
+
|
|
|
+ // 修改未质检库存
|
|
|
+ JdRefundNotQualityCheck jdRefundNotQualityCheck = notQualityCheckMap.get(k);
|
|
|
+ Assert.notEmpty(jdRefundNotQualityCheck, "未质检库存少于撤回售后退货数量,撤回失败");
|
|
|
+ boolean update = jdRefundNotQualityCheckService.update(q -> q
|
|
|
+ .eq(BaseIdPo::getId, jdRefundNotQualityCheck.getId())
|
|
|
+ .setSql("quantity = quantity - " + v)
|
|
|
+ .ge(JdRefundNotQualityCheck::getQuantity, v)
|
|
|
+ );
|
|
|
+ Assert.eqTrue(update, "未质检库存少于撤回售后退货数量,撤回失败");
|
|
|
+
|
|
|
+ // 修改库存
|
|
|
+ Stock stock = stockMap.get(k);
|
|
|
+ Assert.notEmpty(stock, "冻结少于撤回售后退货数量,撤回失败");
|
|
|
+ boolean stockUpdate = stockService.update(Wrappers.<Stock>update()
|
|
|
+ .eq("id", stock.getId())
|
|
|
+ .setSql(getUpdateStockSql(2, v))
|
|
|
+ .geSql("json_extract(victoriatourist_json,'$.frozenQuantity')", v.toString())
|
|
|
+ );
|
|
|
+ Assert.eqTrue(stockUpdate, "冻结库存少于撤回售后退货数量,撤回失败");
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 赋值需要质检的商品
|
|
|
+ */
|
|
|
+ private void setJdRefundQualityCheck(Map<Long, Integer> map, ProductInfo productInfo, JdRefundDetail jdRefundDetail, Integer number) {
|
|
|
+ String victoriatouristJson = productInfo.getVictoriatouristJson();
|
|
|
+ JSONObject victoriatourist = JSONObject.parseObject(victoriatouristJson);
|
|
|
+
|
|
|
+ // 如果是组合商品,拆分组合
|
|
|
+ if (StatusConstant.YES.equals(victoriatourist.getInteger("combination"))) {
|
|
|
+ List<JSONObject> list = victoriatourist.getJSONArray("productCombinationList").toJavaList(JSONObject.class);
|
|
|
+ list.forEach(item -> {
|
|
|
+ Long linkProductId = item.getLong("linkProductId");
|
|
|
+ ProductInfo linkProductInfo = productInfoService.getById(linkProductId);
|
|
|
+ Assert.notEmpty(linkProductInfo, "未知组合商品id:" + linkProductId);
|
|
|
+ setJdRefundQualityCheck(map, linkProductInfo, jdRefundDetail, item.getInteger("linkQuantity") * number);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 不是组合
|
|
|
+ else {
|
|
|
+ Long productInfoId = productInfo.getId();
|
|
|
+ map.put(productInfoId, map.getOrDefault(productInfoId, 0) + number);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|