jiangping
2023-09-18 74d80f67e70666dbd38b1f0b5e4c0c7772fa5b8c
server/service/src/main/java/com/doumee/service/business/impl/GoodsServiceImpl.java
@@ -17,10 +17,8 @@
import com.doumee.dao.business.*;
import com.doumee.dao.business.join.GoodsJoinMapper;
import com.doumee.dao.business.model.*;
import com.doumee.dao.business.model.dto.BatchDisableDTO;
import com.doumee.dao.business.model.dto.GoodCreatePlatRequest;
import com.doumee.dao.business.model.dto.GoodsRequest;
import com.doumee.dao.business.model.dto.PlatGoodsParam;
import com.doumee.dao.business.model.dto.*;
import com.doumee.service.business.BaseDataService;
import com.doumee.service.business.GoodsService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.apache.commons.lang3.StringUtils;
@@ -54,6 +52,9 @@
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    @Lazy
    private BaseDataService baseDataService;
    @Autowired
    private BaseGoodsMapper baseGoodsMapper;
    @Autowired
@@ -130,7 +131,7 @@
    @Override
    public  Integer createPlat(GoodCreatePlatRequest param){
        LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
        if(Constants.equalsInteger(user.getType(), Constants.UserType.COMPANY.getKey())){
        if(!Constants.equalsInteger(user.getType(), Constants.UserType.COMPANY.getKey())){
            //非企业用户不能操作
            throw new BusinessException(ResponseStatus.NOT_ALLOWED);
        }
@@ -143,7 +144,7 @@
            throw  new BusinessException(ResponseStatus.BAD_REQUEST);
        }
        Category category = categoryMapper.selectById(param.getCategoryId());
        if(category ==null ||Constants.equalsInteger(category.getIsdeleted(), Constants.ONE) ||Constants.equalsInteger(category.getCompanyId(), user.getCompanyId())){
        if(category ==null ||Constants.equalsInteger(category.getIsdeleted(), Constants.ONE) ||!Constants.equalsInteger(category.getCompanyId(), user.getCompanyId())){
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,类别信息不正确,请刷新重试!");
        }
        if(category.getPlatCateId() == null){
@@ -160,11 +161,14 @@
                if(p.getGoodsId() == null || p.getPrice() == null){
                    throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请正确选择平台商品并配置入手价!");
                }
                if(ids == null){
                    ids = new ArrayList<>();
                }
                ids.add(p.getGoodsId());
            }
        }
        goodsList = baseGoodsMapper.selectList(new QueryWrapper<>(bp).lambda().in((ids!=null && ids.size()>0),BaseGoods::getId,ids));
        if(Constants.equalsInteger(Constants.ONE, param.getType()) || goodsList==null || ids.size()!=goodsList.size()){
        if(Constants.equalsInteger(Constants.ONE, param.getType()) &&( goodsList==null || ids.size()!=goodsList.size())){
            //  如果选择平台商品和查询结果对不上,提示错误
            throw  new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,请正确选择平台商品!");
        }
@@ -219,6 +223,7 @@
        goods.setStatus(Constants.ZERO);
        goods.setCreateDate(new Date());
        goods.setCreator(user.getId());
        goods.setCompanyId(user.getCompanyId());
        goods.setIsdeleted(Constants.ZERO);
        //处理拼音问题
        goods.setPinyin(PinYinUtil.getFullSpell(goods.getName()));
@@ -559,8 +564,8 @@
    @Override
    public Goods findById(Integer id) {
        Goods goods = goodsMapper.selectById(id);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.FILE_DIR).getCode()
                + systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.OSS, Constants.RESOURCE_PATH).getCode()
                + systemDictDataBiz.queryByCode(Constants.OSS, Constants.GOODS_IMG).getCode();
        goods.setPrefixUrl(prefixUrl);
        Brand brand = brandMapper.selectById(goods.getBrandId());
        if(!Objects.isNull(brand)){
@@ -603,8 +608,8 @@
                Goods::getPrice,goods.getSPrice(),goods.getEPrice());
        queryWrapper.orderByDesc(Goods::getId);
        List<Goods> goodsList = goodsJoinMapper.selectJoinList(Goods.class,queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.FILE_DIR).getCode()
                + systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
        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 ")));
@@ -621,11 +626,16 @@
        queryWrapper.selectAll(Goods.class);
        queryWrapper.selectAs(Category::getName, Goods::getCategoryName);
        queryWrapper.selectAs(BaseGoods::getZdPrice, Goods::getBaseZdPrice);
        queryWrapper.selectAs(BaseGoods::getPrice, Goods::getBasePrice);
        queryWrapper.selectAs(Brand::getName, Goods::getBrandName);
        queryWrapper.selectAs(Category::getAttrFirst, Goods::getAttrFirst);
        queryWrapper.selectAs(Category::getAttrSecond, Goods::getAttrSecond);
        queryWrapper.selectAs(BaseCategory::getPriceRate,Goods::getBasePriceRate);
        queryWrapper.leftJoin(Category.class, Category::getId, Goods::getCategoryId);
        queryWrapper.leftJoin(Brand.class, Brand::getId, Goods::getBrandId);
        queryWrapper.leftJoin(BaseGoods.class,BaseGoods::getId,Goods::getGoodsId);
        queryWrapper.leftJoin(BaseCategory.class,BaseCategory::getId,BaseGoods::getCategoryId);
        queryWrapper.eq(Goods::getIsdeleted, Constants.ZERO);
        if (pageWrap.getModel().getId() != null) {
@@ -649,10 +659,14 @@
        }
        queryWrapper.orderByDesc(Goods::getId);
        IPage<Goods> goodsIPage =  goodsJoinMapper.selectJoinPage(page, Goods.class, queryWrapper);
        String prefixUrl = systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.FILE_DIR).getCode()
                + systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.PROJECTS).getCode();
        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);
            if(i.getType().equals(Constants.ONE)){
                i.setBasePrice(i.getBasePrice().multiply(i.getBasePriceRate()));
            }
        });
        return PageData.from(goodsIPage);
    }
@@ -708,6 +722,7 @@
            Goods g = new Goods();
            g.setIsdeleted(Constants.ZERO);
            g.setRemark(m.getId());
            g.setCompanyId(user.getCompanyId());
            //品类数据
            g = goodsMapper.selectOne(new QueryWrapper<>(g).last(" limit 1"));
            if(g != null){
@@ -716,6 +731,7 @@
            Category cate = new Category();
            cate.setIsdeleted(Constants.ZERO);
            cate.setName(m.getCategory());
            cate.setCompanyId(user.getCompanyId());
            //品类数据
            cate = categoryMapper.selectOne(new QueryWrapper<>(cate).last(" limit 1"));
            if(cate == null){
@@ -725,7 +741,9 @@
            brand.setIsdeleted(Constants.ZERO);
            brand.setName(m.getBrand());
            //品类数据
            brand = brandMapper.selectOne(new QueryWrapper<>(brand).last(" limit 1"));
            QueryWrapper<Brand> qwrapper = new QueryWrapper<>(brand);
            qwrapper.lambda().and(wapper-> wapper.eq(Brand::getCompanyId ,user.getCompanyId()).or().eq(Brand::getType,Constants.ONE));
            brand = brandMapper.selectOne(qwrapper.last(" limit 1"));
            if(brand == null){
                throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(num)+"】行商品品牌无效,请检查输入!");
            }
@@ -738,6 +756,7 @@
            newModel.setBrandId(brand.getId());
            newModel.setIsdeleted(Constants.ZERO);
            newModel.setCreator(user.getId());
            newModel.setCompanyId(user.getCompanyId());
            newModel.setCreateDate(new Date());
            newModel.setName(m.getName());
            newModel.setStatus(Constants.ZERO);
@@ -756,67 +775,14 @@
            newModel.setPinyin(PinYinUtil.getFullSpell(m.getName()));
            newModel.setShortPinyin(PinYinUtil.getFirstSpell(m.getName()));
            CateAttr ca = new CateAttr();
            ca.setCategoryId(cate.getId());
            ca.setIsdeleted(Constants.ZERO);
            //查询全部属性1和属性2规格值
            List<CateAttr> allAttr = cateAttrMapper.selectList(new QueryWrapper<>(ca));
            //属性1集合
//            String[] attrs = StringUtils.defaultString(m.getAttrFirstNames(),"").split("\n");
            //属性2集合
//            String[] attrs2 = StringUtils.defaultString(m.getAttrSecodNames(),"").split("\n");
            //产品参数集合
            String[] params =  StringUtils.defaultString(m.getParamStr(),"").split("\n");
//            if(attrs.length > 0){
//                String attrids="" ,attrNames="";
//                for(String s : attrs){
//                    if(StringUtils.isBlank(s)){
//                        continue;
//                    }
//                    //查询属性规格对象
//                    CateAttr ta = getCateAttrByName(s,Constants.ZERO,allAttr);
//                    if(ta == null){
//                        throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(num)+"】行属性1中【"+s+"】无效,请检查输入!");
//                    }
//                    if(StringUtils.isNotBlank(attrids)){
//                        attrids += ",";
//                        attrNames += ",";
//                    }
//                    attrids+= ta.getId();
//                    attrNames+= ta.getName();
//                }
//                newModel.setAttrFirstIds(attrids);
//                newModel.setAttrFirstNames(attrNames);
//            }
//            if(attrs2.length > 0){
//                String attrids="" ,attrNames="";
//                for(String s : attrs2){
//                    if(StringUtils.isBlank(s)){
//                        continue;
//                    }
//                    //查询属性规格对象
//                    CateAttr ta = getCateAttrByName(s,Constants.ONE,allAttr);
//                    if(ta == null){
//                        throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"对不起,第【"+(num)+"】行属性1中【"+s+"】无效,请检查输入!");
//                    }
//                    if(StringUtils.isNotBlank(attrids)){
//                        attrids += ",";
//                        attrNames += ",";
//                    }
//                    attrids+= ta.getId();
//                    attrNames+= ta.getName();
//                }
//                newModel.setAttrSecodIds(attrids);
//                newModel.setAttrSecodNames(attrNames);
//
//            }
            List<String> mulFiles = null;
            String proDir =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.PROJECTS).getCode();
            String tf = proDir.substring(0,proDir.length()-1);
            String path = systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.GOODS_IMG_DIR).getCode()
                    + proDir;
            if(StringUtils.isNotBlank(path)){
                OssModel ossModel = baseDataService.initOssModel();
                File dir =new File(path+File.separator+newModel.getRemark()+File.separator);
                if(dir!=null && dir.isDirectory()){
                    File[]  files = dir.listFiles();
@@ -824,16 +790,8 @@
                        for(File f:files){
                            if(StringUtils.isBlank(newModel.getImgurl()) && isImgFile(f)){
                                //取第一张图片作为列表图
                                String imgdir =f.getPath().substring(f.getPath().indexOf(tf)+tf.length()+1);
                                newModel.setImgurl(imgdir.replace(File.separator,"/"));
//                                 newModel.setImgurl(f.getPath().replace(path.replace(File.separator,"/"),""));
                                newModel.setImgurl(baseDataService.getOssImgurl(ossModel,ossModel.getGoodsFolder(),f));
                            }
//                             else{
//                                 if(mulFiles==null){
//                                     mulFiles = new ArrayList<>();
//                                 }
//                                 mulFiles.add(f.getPath().replace(path.replace("/",File.separator),""));
//                             }
                            if((mulFiles ==null || mulFiles.size() == 0) && f.isDirectory()){
                                //如果是文件夹
                                File[] fs = f.listFiles();
@@ -843,12 +801,7 @@
                                            if(mulFiles==null){
                                                mulFiles = new ArrayList<>();
                                            }
                                            String imgdir =mf.getPath().substring(mf.getPath().indexOf(tf)+tf.length()+1);
                                            mulFiles.add(imgdir.replace(File.separator,"/"));
//                                             newModel.setImgurl(imgdir);
////                                             mulFiles.add(mf.getName());
//                                             mulFiles.add(mf.getPath().replace(path.replace(File.separator,"/"),""));
                                            mulFiles.add(baseDataService.getOssImgurl(ossModel,ossModel.getGoodsFolder(),f));
                                        }
                                    }
                                }
@@ -929,6 +882,9 @@
    }
    private boolean isImgFile(File f) {
        if(f!=null && f.isFile() ){
            String name = f.getName();