jiangping
2023-09-22 691cbf9350a6e98bfeb3cc2274906da75466bf1d
server/service/src/main/java/com/doumee/service/business/impl/GoodsServiceImpl.java
@@ -18,6 +18,8 @@
import com.doumee.dao.business.join.GoodsJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.model.dto.*;
import com.doumee.dao.system.model.SystemDepartment;
import com.doumee.service.CateParamSelectService;
import com.doumee.service.business.BaseDataService;
import com.doumee.service.business.GoodsService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
@@ -29,6 +31,8 @@
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
@@ -43,6 +47,7 @@
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 商品信息表Service实现
@@ -64,6 +69,8 @@
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private BaseCategoryMapper baseCategoryMapper;
    @Autowired
    private CateAttrMapper cateAttrMapper;
    @Autowired
    private CateParamMapper cateParamMapper;
@@ -79,10 +86,13 @@
    @Autowired
    private GoodsJoinMapper goodsJoinMapper;
    @Lazy
    @Autowired
    private CateParamSelectMapper cateParamSelectMapper;
    private CateParamSelectService cateParamSelectService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
@@ -130,6 +140,8 @@
        return goods.getId();
    }
    @Async
    @Override
    @Transactional( rollbackFor = {Exception.class, BusinessException.class})
    public  Integer createPlat(GoodCreatePlatRequest param){
@@ -153,6 +165,11 @@
        if(category.getPlatCateId() == null){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请前往【类别管理】先关联平台类别!");
        }
        BaseCategory baseCategory = baseCategoryMapper.selectById(category.getPlatCateId());
        if(baseCategory == null){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请前往【类别管理】先关联平台类别!");
        }
        BaseGoods bp = new BaseGoods();
        bp.setCategoryId(category.getPlatCateId());
        bp.setIsdeleted(Constants.ZERO);
@@ -175,24 +192,40 @@
            //  如果选择平台商品和查询结果对不上,提示错误
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请正确选择平台商品!");
        }
        for(BaseGoods model : goodsList){
            //遍历平台商品信息,进行商品信息同步
            Goods goods = initGoodsPlatParam(user,model,param);
            if(goods == null){
                continue;
        List<Multifile> multifileList = new ArrayList<>();
        List<GoodsParam> goodsParamList = new ArrayList<>();
        try{
            for(BaseGoods model : goodsList){
                //遍历平台商品信息,进行商品信息同步
                //分类加价系数
                model.setCateRatePrice(baseCategory.getPriceRate());
                Goods goods = initGoodsPlatParam(user,model,param);
                if(goods == null){
                    continue;
                }
                goodsMapper.insert(goods);
                //开始处理附件
                dealPlatMultifule(goods.getId(),user,model,multifileList);
                //开始处理参数配置
                dealPlatGoodsParam(model,goods,user,goodsParamList);
            }
            goodsMapper.insert(goods);
           //开始处理附件
            dealPlatMultifule(goods.getId(),user,model);
            //开始处理参数配置
            dealPlatGoodsParam(model,goods,user);
            if(multifileList.size()>0){
                multifileMapper.insertBatch(multifileList);
            }
            if(goodsParamList.size()>0){
                goodsParamMapper.insertBatch(goodsParamList);
            }
            cateParamSelectService.dealCateParamSelect();
        }catch (Exception e){
           e.printStackTrace();
        }finally {
            redisTemplate.opsForValue().set(user.getCompanyId()+Constants.OPEN_SYNC_SWITCH,false);
        }
        return 1;
    }
    private void dealPlatGoodsParam(BaseGoods model, Goods goods, LoginUserInfo user) {
    private void dealPlatGoodsParam(BaseGoods model, Goods goods, LoginUserInfo user,List<GoodsParam> paramList) {
        BaseGoodsParam baseGoodsParam = new BaseGoodsParam();
        baseGoodsParam.setGoodsId(model.getId());
        baseGoodsParam.setIsdeleted(Constants.ZERO);
@@ -220,7 +253,8 @@
                goodsParam.setVal(bgParam.getVal());
                goodsParam.setName(bgParam.getName());
                goodsParam.setPramaId(Objects.isNull(cateParam)?null:cateParam.getId());
                goodsParamMapper.insert(goodsParam);
                paramList.add(goodsParam);
//                goodsParamMapper.insert(goodsParam);
            }
        }
    }
@@ -257,7 +291,7 @@
    /**
     *  开始处理平台商品关联附件,复制一套到企业商品上
     */
    private void dealPlatMultifule(Integer id, LoginUserInfo user, BaseGoods model) {
    private void dealPlatMultifule(Integer id, LoginUserInfo user, BaseGoods model,List<Multifile> fileList ) {
        Multifile f = new Multifile();
        f.setObjId(model.getId());
        f.setIsdeleted(Constants.ZERO);
@@ -276,7 +310,8 @@
                multifile.setName(multifile.getName());
                multifile.setSortnum(multifile.getSortnum());
                multifile.setCompanyId(user.getCompanyId());
                multifileMapper.insert(multifile);
                fileList.add(multifile);
//                multifileMapper.insert(multifile);
            }
        }
    }
@@ -288,13 +323,14 @@
                    return p.getPrice().setScale(0,BigDecimal.ROUND_CEILING);
                }
            }
            throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(),"数据异常请刷新重试");
        }
        //如果没有配置入手价,安装加价系数来计算
//        if(model.getCateRatePrice()==null){
//            model.setCateRatePrice(systemDictDataBiz.getGoodsPriceRate());
//        }
        if(model.getCateRatePrice()==null){
            model.setCateRatePrice(systemDictDataBiz.getGoodsPriceRate());
        }
        //返回 .multiply(model.getCateRatePrice())
        return (Constants.formatBigdecimal(model.getPrice()).multiply(param.getRate())).setScale(0,BigDecimal.ROUND_CEILING);
        return (Constants.formatBigdecimal(model.getPrice()).multiply(model.getCateRatePrice()).multiply(param.getRate())).setScale(0,BigDecimal.ROUND_CEILING);
    }
@@ -371,7 +407,10 @@
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        goodsMapper.deleteBatchIds(ids);
        for(Integer id : ids){
            deleteById(id);
        }
//        goodsMapper.deleteBatchIds(ids);
    }
    @Override
@@ -547,7 +586,7 @@
                continue;
            }
            //拉式商品 且 上架时,需要限制 平台商品是否已下架
            if(dbGoods.getType().equals(Constants.goodsType.PULL)&&batchDisableDTO.getStatus().equals(Constants.ZERO)){
            if(dbGoods.getType().equals(Constants.goodsType.PULL.getValue())&&batchDisableDTO.getStatus().equals(Constants.ZERO)){
                BaseGoods baseGoods = baseGoodsMapper.selectById(dbGoods.getGoodsId());
                if(Objects.isNull(baseGoods)||!baseGoods.getStatus().equals(Constants.ZERO)){
                    errNum++;
@@ -613,13 +652,7 @@
                Goods::getPrice,goods.getSPrice(),goods.getEPrice());
        queryWrapper.orderByDesc(Goods::getId);
        List<Goods> goodsList = goodsJoinMapper.selectJoinList(Goods.class,queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        goodsList.forEach(i->{
            i.setPrefixUrl(prefixUrl);
            i.setMultifileList(multifileMapper.selectList(new QueryWrapper<Multifile>().eq("OBJ_ID",i.getId()).orderByAsc(" SORTNUM ")));
            i.setGoodsParamList(goodsParamMapper.selectList(new QueryWrapper<GoodsParam>().eq("GOODS_ID",i.getId()).orderByAsc(" SORTNUM ")));
        });
        this.dealGoodsMsg(goodsList);
        return goodsList;
    }
@@ -670,6 +703,7 @@
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        goodsIPage.getRecords().forEach(i->{
            i.setPrefixUrl(prefixUrl);
            i.setBasePrice(Constants.formatBigdecimal(i.getBasePrice()).multiply(Constants.formatBigdecimal(i.getBasePriceRate())));
        });
        return PageData.from(goodsIPage);
    }
@@ -751,8 +785,8 @@
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(num)+"】行商品品牌无效,请检查输入!");
            }
            if(goodsMapper.selectCount(new QueryWrapper<Goods>().eq("ISDELETED",Constants.ZERO).eq("name",m.getName()))>0){
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"【"+m.getName()+"】已存在");
            };
                throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"第【"+(num)+"】行【"+m.getName()+"】已存在");
            }
            Goods newModel = new Goods();
            newModel.setRemark(m.getId());
            newModel.setCategoryId(cate.getId());
@@ -1060,18 +1094,33 @@
                .apply(!Objects.isNull(pageWrap.getModel()) && !Objects.isNull(pageWrap.getModel().getEPrice()), "  t.PRICE <=  "+pageWrap.getModel().getEPrice()+" ");
        queryWrapper.orderByDesc(Goods::getId);
        IPage<Goods> goodsIPage =  goodsJoinMapper.selectJoinPage(page, Goods.class, queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        goodsIPage.getRecords().forEach(i->{
            i.setPrefixUrl(prefixUrl);
            i.setMultifileList(multifileMapper.selectList(new QueryWrapper<Multifile>().eq("OBJ_ID",i.getId()).orderByAsc(" SORTNUM ")));
            i.setGoodsParamList(goodsParamMapper.selectList(new QueryWrapper<GoodsParam>()
                    .eq("GOODS_ID",i.getId())
                    .orderByAsc(" SORTNUM ")));
        });
        if(!Objects.isNull(goodsIPage.getRecords())&&goodsIPage.getRecords().size()>Constants.ZERO){
            this.dealGoodsMsg(goodsIPage.getRecords());
        }
        return PageData.from(goodsIPage);
    }
    public void dealGoodsMsg(List<Goods> goodsList){
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        String prefixUrlCategory = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.CATEGORY_IMG).getCode();
        List<Integer> integerList = goodsList.stream().map(Goods::getId).collect(Collectors.toList());
        List<Multifile> mFileList = multifileMapper.selectList(new QueryWrapper<Multifile>()
                .in("OBJ_ID",integerList).eq("TYPE",Constants.ZERO));
        List<GoodsParam> goodsParamList = goodsParamMapper.selectList(new QueryWrapper<GoodsParam>()
                    .in("GOODS_ID",integerList)
                    .orderByAsc(" SORTNUM "));
        goodsList.forEach(i->{
            i.setCategoryImgurl(prefixUrlCategory + i.getCategoryImgurl());
            i.setPrefixUrl(prefixUrl);
            i.setMultifileList(mFileList.stream().filter(s -> s.getObjId().equals(i.getId())).collect(Collectors.toList()));
            i.setGoodsParamList(goodsParamList.stream().filter(s -> s.getGoodsId().equals(i.getId())).collect(Collectors.toList()));
        });
    }
    @Override
    public List<Goods> findListForH5(GoodsRequest goodsRequest) {
@@ -1104,18 +1153,21 @@
                .apply(!Objects.isNull(goodsRequest) && !Objects.isNull(goodsRequest.getEPrice()), "  t.PRICE <=  "+goodsRequest.getEPrice()+" ");
        queryWrapper.orderByDesc(Goods::getId);
        List<Goods> goodsList = goodsJoinMapper.selectJoinList(Goods.class,queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        goodsList.forEach(i->{
            i.setPrefixUrl(prefixUrl);
            i.setMultifileList(multifileMapper.selectList(new QueryWrapper<Multifile>().eq("OBJ_ID",i.getId()).orderByAsc(" SORTNUM ")));
            i.setGoodsParamList(goodsParamMapper.selectList(
                    new QueryWrapper<GoodsParam>().eq("GOODS_ID",i.getId())
                            .apply(" name in ( select cp.NAME from cate_param cp where cp.CATEGORY_ID = "+i.getCategoryId()+" and cp.ISSHOW = 1 and cp.ISDELETED = 0 and cp.STATUS = 0 ) ")
                            .orderByAsc(" SORTNUM ")));
        });
        this.dealGoodsMsg(goodsList);
        return goodsList;
    }
    @Override
    @Transactional
    public  void openSyncSwitch() {
        LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        Boolean  openSyncSwitch = (Boolean)redisTemplate.opsForValue().get(loginUserInfo.getCompanyId()+Constants.OPEN_SYNC_SWITCH);
        if(openSyncSwitch!=null && openSyncSwitch ){
            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"数据同步中,请稍后刷新重试");
        }
        redisTemplate.opsForValue().set(loginUserInfo.getCompanyId()+Constants.OPEN_SYNC_SWITCH,true);
    }
}