谷粒商城product模块实现类Attr和Brand实现类

2023-10-15 09:31:04 浏览数 (2)

谷粒商城product模块实现类Attr和Brand实现类 

谷粒商城product模块实现类 AttrAttrgroupRelationServiceImpl类

谷粒商城product模块实现类 AttrGroupServiceImpl类

谷粒商城product模块实现类 AttrServiceImpl类

谷粒商城product模块实现类BrandServiceImpl类

代码语言:javascript复制
AttrAttrgroupRelationServiceImpl类
代码语言:javascript复制
package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void seveBath(List<AttrGroupRelationVo> vos) {
        List<AttrAttrgroupRelationEntity> collect = vos.stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        this.saveBatch(collect);

    }

}
代码语言:javascript复制
AttrGroupServiceImpl
代码语言:javascript复制
package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupWithAttrsVo;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String)params.get("key");

        if(catelogId==0){
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),
                    new LambdaQueryWrapper<AttrGroupEntity>()
                            .like(key!=null,AttrGroupEntity::getAttrGroupName,key)
                            .or().eq(key!=null,AttrGroupEntity::getAttrGroupId,key));
            return new PageUtils(page);
        }else{
            System.out.println("1111");
            LambdaQueryWrapper<AttrGroupEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AttrGroupEntity::getCatelogId,catelogId);
            if(!StringUtils.isEmpty(key)){
                lambdaQueryWrapper.and((obj)->{
                    obj.eq(AttrGroupEntity::getAttrGroupId,key).or()
                            .like(AttrGroupEntity::getAttrGroupName,key);
                });

            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), lambdaQueryWrapper);
            System.out.println(page.getRecords());
            return  new PageUtils(page);
        }
        }

    /**
     * 根据id插叙分组和所有的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsBycatelogId(Long catelogId) {
        //获得在属性分组表中的所有属于当前分类的实体
        System.out.println("******");
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map((group) -> {
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group,attrsVo);
            //当前分组下的所有属性(没有"valueType": 0,)
            List<AttrEntity> attr = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attr);
            if ( attr!=null){
                System.out.println("******");
                return attrsVo;
            }
            return null;
        }).collect(Collectors.toList());
        collect.removeIf(Objects::isNull);
        System.out.println("******");

        return collect;
    }


}
代码语言:javascript复制
AttrServiceImpl
代码语言:javascript复制
package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.constant.ProductConstant;
import com.atguigu.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.atguigu.gulimall.product.dao.AttrDao;
import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.AttrGroupRelationVo;
import com.atguigu.gulimall.product.vo.AttrRespVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import javax.annotation.Resource;


@Service("attrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {

    @Resource
    AttrAttrgroupRelationDao relationDao;

    @Resource
    AttrGroupDao attrGroupDao;

    @Resource
    CategoryDao categoryDao;

    @Resource
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Resource
    AttrDao attrDao;

    @Resource
    CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveAttr(AttrVo attr) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attr,attrEntity);
        //1.保存基本数据
        this.save(attrEntity);
        //如果是是销售属性不需要保存
        //2.保存关联关系
        if(attr.getAttrType()==1&&attr.getAttrGroupId()!=null){
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            attrAttrgroupRelationEntity.setAttrGroupId(attr.getAttrGroupId());
            attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
            relationDao.insert(attrAttrgroupRelationEntity);
        }


    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params, Long categoryId,String attrType) {
        LambdaQueryWrapper<AttrEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(categoryId!=0,AttrEntity::getCatelogId,categoryId);
        lambdaQueryWrapper.eq(AttrEntity::getAttrType,"base".equalsIgnoreCase(attrType)?1:0);
        List<AttrEntity> entities = attrDao.selectList(lambdaQueryWrapper);
        System.out.println(entities);
        String key=(String) params.get("key");
       lambdaQueryWrapper.and((obj)->{
          obj.like(key!=null,AttrEntity::getAttrName,key).or()
                  .eq(key!=null,AttrEntity::getAttrId,key);
       });
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), lambdaQueryWrapper);
        System.out.println(page.getRecords());
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        System.out.println(records);
        List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
            System.out.println("????");
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
            //1.设置分类和分组的名字
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity.getName()!=null){
                String catelogName = categoryEntity.getName();
                attrRespVo.setCatelogName(catelogName);
            }
            LambdaQueryWrapper<AttrAttrgroupRelationEntity> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            Long attrId = attrEntity.getAttrId();
           lambdaQueryWrapper1.eq(AttrAttrgroupRelationEntity::getAttrId,attrId);
            System.out.println(attrId);
            AttrAttrgroupRelationEntity one = attrAttrgroupRelationService.getOne(lambdaQueryWrapper1);
            if(one!=null&&one.getAttrGroupId()!=null){
                Long attrGroupId = one.getAttrGroupId();
                System.out.println(one);
                AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
                if(attrGroupEntity!=null){
                    String attrGroupName = attrGroupEntity.getAttrGroupName();
                    attrRespVo.setGroupName(attrGroupName);
                }

            }
            return attrRespVo;
        }).collect(Collectors.toList());
        pageUtils.setList(respVos);
        return  pageUtils;

    }

    @Override
    public AttrRespVo getAttrInfo(Long id) {
        AttrEntity attrEntity = this.getById(id);
        AttrRespVo attrRespVo = new AttrRespVo();
        BeanUtils.copyProperties(attrEntity,attrRespVo);
        //在关系表中查出分组id
        System.out.println("****");
        AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()));
       if(relationEntity!=null){
           Long attrGroupId = relationEntity.getAttrGroupId();

           attrRespVo.setAttrGroupId(attrGroupId);
           //根据分组id查出分组名
           AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
           System.out.println("%%%%%" attrGroupEntity);
           if(attrGroupEntity!=null){
               attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
           }
       }
      //分类消息
        Long catelogId = attrEntity.getCatelogId();
        Long[] catelogIdPath = categoryService.findCatelogIdPath(catelogId);
        attrRespVo.setCatelogPath(catelogIdPath);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(categoryEntity!=null){
            attrRespVo.setCatelogName(categoryEntity.getName());
        }
        System.out.println(catelogIdPath.length);
        for(int i=0;i<catelogIdPath.length;i  ){
            System.out.println("***");
            System.out.println(catelogIdPath[i]);
        }

        return attrRespVo;

    }

    @Override
    public void updateAttr(AttrVo attrVo) {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
        this.updateById(attrEntity);

        //1.修改分组关联
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();

        relationEntity.setAttrId(attrVo.getAttrId());
        relationEntity.setAttrGroupId(attrVo.getAttrGroupId());

        Integer count = relationDao.selectCount(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, attrEntity.getAttrId()));
        if(count>0){
            LambdaUpdateWrapper<AttrAttrgroupRelationEntity> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.eq(AttrAttrgroupRelationEntity::getAttrId,attrVo.getAttrId());
            relationDao.update(relationEntity,lambdaUpdateWrapper);

        }else{
            relationDao.insert(relationEntity);
        }

    }

    /**根据分组的id查找关联的所有基本属性
     *
     * @param attrgroupId
     * @return
     */
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>()
                .eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));
        List<AttrEntity> collect = entities.stream().map((attr) -> {
            Long attrId = attr.getAttrId();
            AttrEntity attrEntity = this.getById(attrId);
            return attrEntity;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public void deleteRelation(AttrGroupRelationVo[] vos) {
        List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            System.out.println(relationEntity);
            return relationEntity;
        }).collect(Collectors.toList());
        System.out.println("***");
        relationDao.deleteBatchRelation(collect);


    }

    /**
     * 获取没有关联的属性
     * @param params
     * @param attrgroupId
     * @return
     */
    @Override
    public PageUtils getNORelation(Map<String, Object> params, Long attrgroupId) {
        //1.当前分组只能关联自己的分类的属性
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        Long catelogId = attrGroupEntity.getCatelogId();
        //2.当前分组只能关联别的分组没有引用的属性
        //2.1当前分类的所有分组
        System.out.println("12233214134234");
        List<AttrGroupEntity> attrGroupEntities = attrGroupDao.selectList(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCatelogId, catelogId).ne(AttrGroupEntity::getAttrGroupId,attrgroupId));
        List<Long> collect = attrGroupEntities.stream().map((item) -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        System.out.println("12233214134234");
        //2.2这些分类关联的属性
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities = relationDao.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().
                in(collect!=null,AttrAttrgroupRelationEntity::getAttrGroupId, collect));
        List<Long> attrIds = attrAttrgroupRelationEntities.stream().map((item) -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        System.out.println("12233214134234");
        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrEntity::getCatelogId,catelogId);
        System.out.println(catelogId);
        if(attrIds!=null&&attrIds.size()>0){
            wrapper.notIn(AttrEntity::getAttrId,attrIds);
        }
        System.out.println(attrIds);
        System.out.println("12233214134234");
        //2.3从当前分类的所有属性中移除这些属性
        String key =(String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((w)->{
                w.eq(AttrEntity::getAttrId,key).or().like(AttrEntity::getAttrName,key);
            });
        }
        System.out.println("12233214134234");
        wrapper.eq(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());

        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        System.out.println("12233214134234");
        return  pageUtils;

    }


}
代码语言:javascript复制
BrandServiceImpl类
代码语言:javascript复制
package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {


    @Autowired
    CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //1.获取key
        String key = (String)params.get("key");
        LambdaQueryWrapper<BrandEntity> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.eq(BrandEntity::getBrandId,key).or().like(BrandEntity::getName,key);
        if(!StringUtils.isEmpty(key)){
            Wrapper.eq(BrandEntity::getBrandId,key).or().like(BrandEntity::getName,key);
        }

        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                Wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //1.保证雍余字段一致
        this.updateById(brand);
        if(!StringUtils.isEmpty(brand.getName())){
            //1.同步更新其他关联表的数据一致
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());

            //TODO 更新其他关联

        }
    }

}

谷粒商城product模块实现类 AttrAttrgroupRelationServiceImpl类

谷粒商城product模块实现类 AttrGroupServiceImpl类

谷粒商城product模块实现类 AttrServiceImpl类

谷粒商城product模块实现类BrandServiceImpl类

谷粒商城product模块实现类Attr和Brand实现类 

0 人点赞