935090232@qq.com
2022-03-08 f7d7da8cf5c3f310d2aac143523dae3000d2b395
refactor:重构商品限制销售检测
3 files added
3 files modified
279 ■■■■ changed files
zq-erp/src/main/java/com/matrix/core/tools/ParamCheckUtil.java 57 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/hive/dto/GoodsSealLimitDto.java 25 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/hive/dto/OrderItemDto.java 25 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/hive/service/ShoppingGoodsService.java 42 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java 76 ●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/system/hive/service/imp/SysOrderServiceImpl.java 54 ●●●●● patch | view | raw | blame | history
zq-erp/src/main/java/com/matrix/core/tools/ParamCheckUtil.java
New file
@@ -0,0 +1,57 @@
package com.matrix.core.tools;
import com.matrix.system.hive.dto.OrderItemDto;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class ParamCheckUtil {
    /**
     * 校验列表参数不能为空
     *
     * @param args
     */
    public static void requireNonNulls(Object... args) {
        if (args == null || args.length < 0) {
            throw new NullPointerException("参数校验列表为空");
        }
        for (int i = 0; i < args.length; i++) {
            if (args[i] == null) {
                throw new NullPointerException(String.format("参数校验列表第%s个参数为空", i + 1));
            }
        }
    }
    /**
     * 校验集合中的参数不能为空
     * requireListElementNonNull(objects, Arrays.asList(OrderItemDto::getCount, OrderItemDto::getGoodsId));
     * @param
     */
    public static <T> void requireListElementNonNull(List<T> list, List<Function<? super T, ? extends Object>> getterFunctions) {
        requireNonNulls(list,getterFunctions);
        for (int i = 0; i < list.size(); i++) {
            for (int j = 0; j < getterFunctions.size(); j++) {
                if(getterFunctions.get(j).apply(list.get(i))==null){
                    throw new NullPointerException(String.format("集合中第%s个对象第%s参数为空",i+1,j+1));
                }
            }
        }
    }
    public static void main(String[] args) {
        List<OrderItemDto> objects = new ArrayList<>();
        OrderItemDto o1 = new OrderItemDto();
        o1.setCount(1);
        objects.add(o1);
        requireListElementNonNull(objects, null);
    }
}
zq-erp/src/main/java/com/matrix/system/hive/dto/GoodsSealLimitDto.java
New file
@@ -0,0 +1,25 @@
package com.matrix.system.hive.dto;
import lombok.Data;
import lombok.ToString;
import java.util.List;
/**
 * 商品限消检测dto
 */
@Data
@ToString
public class GoodsSealLimitDto {
    /**
     * 会员id
     */
    private Long vipId;
    /**
     * 销售明细
     */
    private List<OrderItemDto> orderItemDtoList;
}
zq-erp/src/main/java/com/matrix/system/hive/dto/OrderItemDto.java
New file
@@ -0,0 +1,25 @@
package com.matrix.system.hive.dto;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
/**
 * 订单明细DTO
 */
@Data
@ToString
@Builder
public class OrderItemDto {
    /**
     * 商城 商品id
     */
    private Long  goodsId;
    /**
     * 购买数量
     */
    private Integer count;
}
zq-erp/src/main/java/com/matrix/system/hive/service/ShoppingGoodsService.java
@@ -5,6 +5,7 @@
import com.matrix.system.app.vo.ShoppingGoodsDetailVo;
import com.matrix.system.app.vo.ShoppingGoodsListVo;
import com.matrix.system.hive.bean.ShoppingGoods;
import com.matrix.system.hive.dto.GoodsSealLimitDto;
import com.matrix.system.hive.plugin.util.BaseServices;
import java.util.Date;
@@ -14,61 +15,72 @@
 * @date 2016-07-30 09:54
 */
public interface ShoppingGoodsService  extends BaseServices<ShoppingGoods>{
    /**
     * 检测商品销售限制
     * 1、商品是否超出最大销售量
     * 2、商品是否限购
     */
    void checkGoodsSealLimit(GoodsSealLimitDto goodsSealLimitDto);
    
    /**
     * 新增ShoppingGoods
     * 
     */
    public int add(ShoppingGoods shoppingGoods);
    int add(ShoppingGoods shoppingGoods);
       
       /**
     * 更新ShoppingGoods
     * 
     */
    public int modify(ShoppingGoods shoppingGoods);
    int modify(ShoppingGoods shoppingGoods);
    
    /**
     * 批量删除ShoppingGoods
     * 
     */
    public int remove(List<Long> list);
    int remove(List<Long> list);
    /**
     * 根据id删除ShoppingGoods
     * 
     */
    public int removeById(Long id);
    int removeById(Long id);
    
    /**
     * 分页查询ShoppingGoods
     * 
     */
    public List<ShoppingGoods> findInPage(ShoppingGoods shoppingGoods, PaginationVO pageVo);
    List<ShoppingGoods> findInPage(ShoppingGoods shoppingGoods, PaginationVO pageVo);
    /**
     * 根据对象查询ShoppingGoods
     * 
     */
    public List<ShoppingGoods> findByModel(ShoppingGoods shoppingGoods);
    List<ShoppingGoods> findByModel(ShoppingGoods shoppingGoods);
    /**
     * 数据迁移专用
     * @param shoppingGoods
     * @return
     */
    public List<ShoppingGoods> findByModelData(ShoppingGoods shoppingGoods);
    List<ShoppingGoods> findByModelData(ShoppingGoods shoppingGoods);
    /**
     * 统计记录数ShoppingGoods
     * 
     */
    public int  findTotal(ShoppingGoods shoppingGoods);
    int  findTotal(ShoppingGoods shoppingGoods);
    
    /**
     * 根据id查询ShoppingGoods
     * 
     */
    public ShoppingGoods  findById(Long id);
    ShoppingGoods  findById(Long id);
    
    public int update(Long id);
    int update(Long id);
    
    
    
@@ -77,20 +89,20 @@
     * 数据迁移用
     * 分页查询查询项目充值卡,去除套餐
     */
    public List<ShoppingGoods> findInPageNoTaocan(ShoppingGoods shoppingGoods, PaginationVO pageVo);
    List<ShoppingGoods> findInPageNoTaocan(ShoppingGoods shoppingGoods, PaginationVO pageVo);
    
    /**
     * 数据迁移用
     * 查询查询项目充值卡,去除套餐
     * 统计记录数ShoppingGoods
     */
    public int  findTotalNoTaocan(ShoppingGoods shoppingGoods);
    int  findTotalNoTaocan(ShoppingGoods shoppingGoods);
    /**
     * 检测是否可以修改
     * @param id
     * @return
     */
    public boolean checkIsUpdate(Long id);
    boolean checkIsUpdate(Long id);
    /**
     * 查询所有
     * @author xiaochonggao
@@ -99,9 +111,9 @@
     * @param shoppingGoods
     * @return
     */
    public List<ShoppingGoods> findAll(ShoppingGoods shoppingGoods);
    List<ShoppingGoods> findAll(ShoppingGoods shoppingGoods);
    public  ShoppingGoods findByCode(String goodsCode);
    ShoppingGoods findByCode(String goodsCode);
    Date calInvalidTime(ShoppingGoods shoppingGoods, Integer type, Date buyDate);
zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java
@@ -3,9 +3,7 @@
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.DateUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.core.tools.*;
import com.matrix.system.app.dto.ShoppingGoodsListDto;
import com.matrix.system.app.vo.ShoppingGoodsDetailVo;
import com.matrix.system.app.vo.ShoppingGoodsListVo;
@@ -18,10 +16,9 @@
import com.matrix.system.hive.bean.ShoppingGoodsAssemble;
import com.matrix.system.hive.bean.ShoppingGoodsCategory;
import com.matrix.system.hive.bean.SysShopInfo;
import com.matrix.system.hive.dao.MoneyCardAssembleDao;
import com.matrix.system.hive.dao.ShoppingGoodsAssembleDao;
import com.matrix.system.hive.dao.ShoppingGoodsDao;
import com.matrix.system.hive.dao.SysShopInfoDao;
import com.matrix.system.hive.dao.*;
import com.matrix.system.hive.dto.GoodsSealLimitDto;
import com.matrix.system.hive.dto.OrderItemDto;
import com.matrix.system.hive.service.ShoppingGoodsService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,6 +28,7 @@
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@@ -57,10 +55,55 @@
    @Autowired
    private SysShopInfoDao shopInfoDao;
    @Autowired
    private SysOrderItemDao orderItemDao;
    @Override
    public void checkGoodsSealLimit(GoodsSealLimitDto goodsSealLimitDto) {
        LogUtil.info("开始检测商品销售限制:{}", goodsSealLimitDto.toString());
        //校验参数不能为空
        checkGoodsSelLimtParam(goodsSealLimitDto);
        goodsSealLimitDto.getOrderItemDtoList().forEach(item -> {
            ShoppingGoods shopGoods = shoppingGoodsDao.selectById(item.getGoodsId());
            //最大销售次数检测
            Integer maxNum = shopGoods.getCarMaxSaleCount();
            if (maxNum != null && maxNum != 0) {
                Integer buyNum = orderItemDao.selectByGoodsId(shopGoods.getId(), null);
                if ((buyNum + item.getCount()) == maxNum) {
                    if (!shopGoods.getStaus().equals(Dictionary.BUSINESS_STATE_DOWN)) {
                        shopGoods.setStaus(Dictionary.BUSINESS_STATE_DOWN);
                        shoppingGoodsDao.update(shopGoods);
                        LogUtil.info("{}商品达到最大销售数量自动下架",shopGoods.getName());
                    }
                } else if ((buyNum + item.getCount()) > maxNum) {
                    throw new GlobleException(shopGoods.getName() + "已超过最大销售数量");
                }
            }
            //每人限购次数检测
            Integer onceCount = shopGoods.getIsOnce();
            if (onceCount != null && onceCount != 0) {
                Integer buyOnceCount = orderItemDao.selectByGoodsId(shopGoods.getId(), goodsSealLimitDto.getVipId());
                if ((buyOnceCount + item.getCount()) > onceCount) {
                    throw new GlobleException(shopGoods.getName() + "每人限购" + onceCount + "次");
                }
            }
        });
    }
    private void checkGoodsSelLimtParam(GoodsSealLimitDto goodsSealLimitDto) {
        ParamCheckUtil.requireNonNulls(goodsSealLimitDto,
                goodsSealLimitDto.getVipId(),
                goodsSealLimitDto.getOrderItemDtoList());
        ParamCheckUtil.requireListElementNonNull(goodsSealLimitDto.getOrderItemDtoList(),
                Arrays.asList(OrderItemDto::getCount, OrderItemDto::getGoodsId));
    }
    @Override
@@ -70,10 +113,9 @@
        SysShopInfo shopInfo = shopInfoDao.selectById(sysUsers.getShopId());
        if(shopInfo.getShopType()==SysShopInfo.SHOP_TYPE_ZONGBU){
        if (shopInfo.getShopType() == SysShopInfo.SHOP_TYPE_ZONGBU) {
            shoppingGoods.setHeadquarters(1);
        }else{
        } else {
            shoppingGoods.setHeadquarters(2);
        }
@@ -109,7 +151,7 @@
        if (shoppingGoods.getReferencePice() == null) {
            shoppingGoods.setReferencePice(0d);
        }
        shoppingGoods.setZjm(StringUtils.toHanyuPinyin(shoppingGoods.getName())+","+StringUtils.toHeadWordHanyuPinyin(shoppingGoods.getName()));
        shoppingGoods.setZjm(StringUtils.toHanyuPinyin(shoppingGoods.getName()) + "," + StringUtils.toHeadWordHanyuPinyin(shoppingGoods.getName()));
        shoppingGoods.setIsDel(ShoppingGoods.NORMAL);
        //新增销售产品
        int i = shoppingGoodsDao.insert(shoppingGoods);
@@ -119,7 +161,6 @@
        } else {
            setGoodsAssembles(shoppingGoods);
        }
        return i;
@@ -207,17 +248,17 @@
        if (ShoppingGoods.SHOPPING_GOODS_TYPE_CZK.equals(shoppingGoods.getGoodType())) {
            setCardAssemble(shoppingGoods);
        }else{
        } else {
            //清除原有绑定关系
            shoppingGoodsAssembleDao.deleteByGoodsId(shoppingGoods.getId());
            //合并绑定的产品和项目
            List<ShoppingGoodsAssemble> assembles = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(shoppingGoods.getAssembleGoods())){
            if (CollectionUtils.isNotEmpty(shoppingGoods.getAssembleGoods())) {
                assembles.addAll(shoppingGoods.getAssembleGoods());
            }
            //如果是套餐叠加套餐绑定的项目部分
            if (Dictionary.SHOPPING_GOODS_TYPE_TC.equals(shoppingGoods.getGoodType())){
            if (Dictionary.SHOPPING_GOODS_TYPE_TC.equals(shoppingGoods.getGoodType())) {
                assembles.addAll(shoppingGoods.getAssembleProj());
            }
@@ -375,7 +416,6 @@
    }
    /**
     * 检测是否能更新 @Title: checkIsUpdate  @author:jyy @return boolean
     * 返回类型 @date 2016年9月18日 上午10:06:23 @throws
@@ -428,8 +468,8 @@
     * 计算失效时间
     *
     * @param shoppingGoods
     * @param type 1 - 购买时  2 - 消耗时
     * @param buyDate 购买日期, 当计算消耗日期时,不能为空
     * @param type          1 - 购买时  2 - 消耗时
     * @param buyDate       购买日期, 当计算消耗日期时,不能为空
     * @return
     */
    @Override
zq-erp/src/main/java/com/matrix/system/hive/service/imp/SysOrderServiceImpl.java
@@ -1,6 +1,8 @@
package com.matrix.system.hive.service.imp;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.matrix.component.asyncmessage.AsyncMessageManager;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
@@ -26,6 +28,8 @@
import com.matrix.system.enums.OperationFunctionEnum;
import com.matrix.system.hive.bean.*;
import com.matrix.system.hive.dao.*;
import com.matrix.system.hive.dto.GoodsSealLimitDto;
import com.matrix.system.hive.dto.OrderItemDto;
import com.matrix.system.hive.plugin.util.CollectionUtils;
import com.matrix.system.hive.plugin.util.MoneyUtil;
import com.matrix.system.hive.pojo.CzXkVo;
@@ -553,6 +557,8 @@
        addVipScore(pageOrder);
    }
    /**
@@ -601,11 +607,22 @@
    }
    private void checkOrder(SysOrder pageOrder) {
        //检查交易限制调整
        checkSealLimit(pageOrder);
        //检查交易限制调整 jyytodo 测试一下
        GoodsSealLimitDto goodsSealLimitDto = new GoodsSealLimitDto();
        goodsSealLimitDto.setVipId(pageOrder.getVipId());
        goodsSealLimitDto.setOrderItemDtoList(Lists.newArrayList());
        pageOrder.getItems().forEach(e->{
            goodsSealLimitDto.getOrderItemDtoList().add(BeanUtil.copyProperties(e, OrderItemDto.class));
        });
        shoppingGoodsService.checkGoodsSealLimit(goodsSealLimitDto);
        //检查业绩设置
        checkOrderAchieve(pageOrder);
    }
    /**
     * 检查业绩设置是否合理
@@ -638,40 +655,7 @@
    }
    /**
     * 检查产品销售次数
     */
    private void checkSealLimit(SysOrder pageOrder) {
        pageOrder.getItems().forEach(item -> {
            ShoppingGoods shopGoods = shoppingGoodsDao.selectById(item.getGoodsId());
            //最大销售次数检测
            Integer maxNum = shopGoods.getCarMaxSaleCount();
            if (maxNum != null && maxNum != 0) {
                Integer buyNum = orderItemDao.selectByGoodsId(shopGoods.getId(), null);
                if ((buyNum + item.getCount()) > maxNum) {
                    throw new GlobleException(shopGoods.getName() + "已超过最大销售数量");
                }
                if ((buyNum + item.getCount()) == maxNum) {
                    if (!shopGoods.getStaus().equals(Dictionary.BUSINESS_STATE_DOWN)) {
                        shopGoods.setStaus(Dictionary.BUSINESS_STATE_DOWN);
                        shoppingGoodsDao.update(shopGoods);
                    }
                }
            }
            //每人限购次数检测
            Integer onceCount = shopGoods.getIsOnce();
            if (onceCount != null && onceCount != 0) {
                Integer buyOnceCount = orderItemDao.selectByGoodsId(shopGoods.getId(), pageOrder.getVipId());
                if ((buyOnceCount + item.getCount()) > onceCount) {
                    throw new GlobleException(shopGoods.getName() + "每人限购" + onceCount + "次");
                }
            }
        });
    }
    /**