Helius
2022-09-19 f2b9a83f75a4345cd42d008f3153c18f8f890cad
zq-erp/src/main/java/com/matrix/system/hive/service/imp/ShoppingGoodsServiceImpl.java
@@ -1,11 +1,10 @@
package com.matrix.system.hive.service.imp;
import cn.hutool.core.collection.CollUtil;
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 +17,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.SysOrderItemDto;
import com.matrix.system.hive.service.ShoppingGoodsService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -31,8 +29,11 @@
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;
import java.util.Objects;
import java.util.stream.Collectors;
/**
@@ -57,11 +58,52 @@
    @Autowired
    private SysShopInfoDao shopInfoDao;
    @Autowired
    private SysOrderItemDao orderItemDao;
    @Override
    public void checkGoodsSealLimit(GoodsSealLimitDto goodsSealLimitDto) {
        LogUtil.info("开始检测商品销售限制:{}", goodsSealLimitDto.toString());
        //校验参数不能为空
        checkGoodsSelLimtParam(goodsSealLimitDto);
        goodsSealLimitDto.getSysOrderItemDtoList().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.equals(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.getSysOrderItemDtoList());
        ParamCheckUtil.requireListElementNonNull(goodsSealLimitDto.getSysOrderItemDtoList(),
                Arrays.asList(SysOrderItemDto::getCount, SysOrderItemDto::getGoodsId));
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -70,18 +112,21 @@
        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);
        }
        shoppingGoods.setShopId(sysUsers.getShopId());
        // 校验去重
        if (serviceUtil.addCheckRepeatTowColumn("shopping_goods",
                "code", shoppingGoods.getCode(),
                "company_id", shoppingGoods.getCompanyId())) {
        //去重查询
        ShoppingGoods queryGoods=new ShoppingGoods();
        queryGoods.setCompanyId(shoppingGoods.getCompanyId());
        queryGoods.setCode(shoppingGoods.getCode());
        queryGoods.setIsDel(ShoppingGoods.NORMAL);
        List<ShoppingGoods>  oldGoods=shoppingGoodsDao.selectByModel(queryGoods);
        if(CollUtil.isNotEmpty(oldGoods)){
            throw new GlobleException("编号" + shoppingGoods.getCode() + "重复");
        }
@@ -109,7 +154,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 +164,6 @@
        } else {
            setGoodsAssembles(shoppingGoods);
        }
        return i;
@@ -207,17 +251,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());
            }
@@ -242,6 +286,17 @@
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(List<Long> list) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        SysShopInfo zbShop = shopInfoDao.selectZbShop(user.getCompanyId());
        //非总部员工只能删除自己门店的产品
        if(user.getShopId()!=zbShop.getId()){
            //校验是否可以删除
            List<ShoppingGoods> dataList = shoppingGoodsDao.selectByIds(list);
            List<ShoppingGoods> collect = dataList.stream().filter(goods -> !Objects.equals(user.getShopId(), goods.getShopId())).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(collect)){
                throw new GlobleException("非总部员工只能删除自己门店的产品");
            }
        }
        return shoppingGoodsDao.deleteByIds(list);
    }
@@ -352,9 +407,6 @@
            case Dictionary.SHOPPING_GOODS_TYPE_TC:
                shoppingGoods.setIsAssemble(Dictionary.FLAG_YES);
                break;
            case Dictionary.SHOPPING_GOODS_TYPE_ZHK:
                shoppingGoods.setIsAssemble(Dictionary.FLAG_YES);
                break;
        }
    }
@@ -373,7 +425,6 @@
            shoppingGoods.setWeiImg(Dictionary.DEFAULT_IMG);
        }
    }
    /**
@@ -428,8 +479,8 @@
     * 计算失效时间
     *
     * @param shoppingGoods
     * @param type 1 - 购买时  2 - 消耗时
     * @param buyDate 购买日期, 当计算消耗日期时,不能为空
     * @param type          1 - 购买时  2 - 消耗时
     * @param buyDate       购买日期, 当计算消耗日期时,不能为空
     * @return
     */
    @Override