商城项目通过CompletableFuture异步编排优化业务处理-----商城项目

package com.alatus.mall.product.service.impl;

import com.alatus.common.constant.ProductConstant;
import com.alatus.mall.product.dao.AttrAttrgroupRelationDao;
import com.alatus.mall.product.dao.AttrDao;
import com.alatus.mall.product.dao.AttrGroupDao;
import com.alatus.mall.product.dao.CategoryDao;
import com.alatus.mall.product.entity.AttrAttrgroupRelationEntity;
import com.alatus.mall.product.entity.AttrEntity;
import com.alatus.mall.product.entity.AttrGroupEntity;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.AttrGroupService;
import com.alatus.mall.product.service.AttrService;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.mall.product.vo.AttrGroupRelationVo;
import com.alatus.mall.product.vo.AttrRespVo;
import com.alatus.mall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
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.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("AttrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @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);
    }

    @Transactional
    @Override
    public void saveAttr(AttrVo attrVo) throws ExecutionException, InterruptedException {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
//        保存基本数据
        CompletableFuture<Void> saveAttr = CompletableFuture.runAsync(() -> {
            this.save(attrEntity);
        }, threadPoolExecutor);
//        保存关联关系
        CompletableFuture<Void> saveAttrRelation = CompletableFuture.runAsync(() -> {
            if (attrVo.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attrVo.getAttrGroupId() != null) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
                attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
                relationDao.insert(attrAttrgroupRelationEntity);
            }
        }, threadPoolExecutor);
        CompletableFuture.allOf(saveAttrRelation,saveAttr).get();
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params,Long cateLogId,String type) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equalsIgnoreCase(type)?ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        if(cateLogId != 0){
            queryWrapper.eq("catelog_id",cateLogId);
        }
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper) -> {
                wrapper.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
//            设置分类和分组名字
            if("base".equalsIgnoreCase(type)){
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null && attrAttrgroupRelationEntity.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());
        pageUtils.setList(respVos);
        return pageUtils;
    }

    @Cacheable(value = "attr",key = "'attrinfo:'+#root.args[0]",sync = true)
    @Override
    public AttrRespVo getAttrInfo(Long attrId) throws ExecutionException, InterruptedException {
        AttrRespVo respVo = new AttrRespVo();
        CompletableFuture<AttrEntity> attrTask = CompletableFuture.supplyAsync(() -> {
            AttrEntity attr = this.getById(attrId);
            return attr;
        }, threadPoolExecutor);
//        设置分组的相关信息
        CompletableFuture<Void> attrGroupTask = attrTask.thenAcceptAsync((resp) -> {
            BeanUtils.copyProperties(resp, respVo);
//        全都要做非空判断
            if (resp.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
                AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
                if (relationEntity != null) {
                    respVo.setAttrGroupId(relationEntity.getAttrGroupId());
                    AttrGroupEntity attrGroupEntity = attrGroupService.getById(relationEntity.getAttrGroupId());
                    if (attrGroupEntity != null) {
                        respVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                }
            }
        }, threadPoolExecutor);
//        设置分类信息
        CompletableFuture<Void> groupTask = attrTask.thenAcceptAsync((resp) -> {
            Long catelogId = resp.getCatelogId();
            Long[] catelogPath = categoryService.findCatelogPath(catelogId);
            respVo.setCatelogPath(catelogPath);
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            if(categoryEntity!=null){
                respVo.setCatelogName(categoryEntity.getName());
            }
        }, threadPoolExecutor);
        CompletableFuture.allOf(attrTask,attrGroupTask,groupTask).get();
        return respVo;
    }

    @Override
    @Transactional
    public void updateAttr(AttrVo attrVo) throws ExecutionException, InterruptedException {
        AttrEntity attrEntity = new AttrEntity();
//        修改自己
        CompletableFuture<Void> updateTask = CompletableFuture.runAsync(() -> {
            BeanUtils.copyProperties(attrVo,attrEntity);
            this.updateById(attrEntity);
        }, threadPoolExecutor);
//            修改分组关联
        CompletableFuture<Void> groupTask = CompletableFuture.runAsync(() -> {
            if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
                AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
                relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
                relationEntity.setAttrId(attrEntity.getAttrId());
//            看一下它有没有对应的
                Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrVo.getAttrId()));
                if (count > 0) {
                    relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrVo.getAttrId()));
                } else {
                    relationDao.insert(relationEntity);
                }
            }
        }, threadPoolExecutor);
        CompletableFuture.allOf(updateTask,groupTask).get();
    }

//    根据分组ID查找关联的所有基本属性
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> attrIds = entities.stream().map((entity) -> {
            return entity.getAttrId();
        }).collect(Collectors.toList());
        if(attrIds == null || attrIds.size() == 0){
            return null;
        }
        List<AttrEntity> attrEntities = this.listByIds(attrIds);
        return attrEntities;
    }

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

//    获取当前分组没有关联的所有属性
    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
        //1、当前分组只能关联自己所属的分类里面的所有属性
        AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrgroupId);
        //获取当前分类的id
        Long catelogId = attrGroupEntity.getCatelogId();
        //2、当前分组只能关联别的分组没有引用的属性
        //2.1)、当前分类下的其它分组
        List<AttrGroupEntity> groupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));
        //获取到所有的attrGroupId
        List<Long> collect = groupEntities.stream().map((item) -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        //2.2)、从当前分类的所有属性移除这些属性
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>()
                .eq("catelog_id", catelogId).eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        //2.3)、这些分组关联的属性
        if(collect != null && collect.size() > 0){
            List<AttrAttrgroupRelationEntity> groupId = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
            List<Long> attrIds = groupId.stream().map((item) -> {
                return item.getAttrId();
            }).collect(Collectors.toList());
            if (attrIds != null && attrIds.size() > 0) {
                queryWrapper.notIn("attr_id", attrIds);
            }
        }
        //判断是否有参数进行模糊查询
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((w) -> {
                w.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {
//        在所有属性集合中挑出检索属性
        return baseMapper.selectSearchAttrs(attrIds);
    }
}
package com.alatus.mall.product.service.impl;

import com.alatus.common.constant.ProductConstant;
import com.alatus.mall.product.dao.AttrAttrgroupRelationDao;
import com.alatus.mall.product.dao.AttrDao;
import com.alatus.mall.product.dao.AttrGroupDao;
import com.alatus.mall.product.dao.CategoryDao;
import com.alatus.mall.product.entity.AttrAttrgroupRelationEntity;
import com.alatus.mall.product.entity.AttrEntity;
import com.alatus.mall.product.entity.AttrGroupEntity;
import com.alatus.mall.product.entity.CategoryEntity;
import com.alatus.mall.product.service.AttrGroupService;
import com.alatus.mall.product.service.AttrService;
import com.alatus.mall.product.service.CategoryService;
import com.alatus.mall.product.vo.AttrGroupRelationVo;
import com.alatus.mall.product.vo.AttrRespVo;
import com.alatus.mall.product.vo.AttrVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
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.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("AttrService")
public class AttrServiceImpl extends ServiceImpl<AttrDao, AttrEntity> implements AttrService {
    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @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);
    }

    @Transactional
    @Override
    public void saveAttr(AttrVo attrVo) throws ExecutionException, InterruptedException {
        AttrEntity attrEntity = new AttrEntity();
        BeanUtils.copyProperties(attrVo,attrEntity);
//        保存基本数据
        CompletableFuture<Void> saveAttr = CompletableFuture.runAsync(() -> {
            this.save(attrEntity);
        }, threadPoolExecutor);
//        保存关联关系
        CompletableFuture<Void> saveAttrRelation = CompletableFuture.runAsync(() -> {
            if (attrVo.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode() && attrVo.getAttrGroupId() != null) {
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
                attrAttrgroupRelationEntity.setAttrGroupId(attrVo.getAttrGroupId());
                attrAttrgroupRelationEntity.setAttrId(attrEntity.getAttrId());
                relationDao.insert(attrAttrgroupRelationEntity);
            }
        }, threadPoolExecutor);
        CompletableFuture.allOf(saveAttrRelation,saveAttr).get();
    }

    @Override
    public PageUtils queryBaseAttrPage(Map<String, Object> params,Long cateLogId,String type) {
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type","base".equalsIgnoreCase(type)?ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode():ProductConstant.AttrEnum.ATTR_TYPE_SALE.getCode());
        if(cateLogId != 0){
            queryWrapper.eq("catelog_id",cateLogId);
        }
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            queryWrapper.and((wrapper) -> {
                wrapper.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(
                new Query<AttrEntity>().getPage(params),
                queryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<AttrEntity> records = page.getRecords();
        List<AttrRespVo> respVos = records.stream().map((attrEntity) -> {
            AttrRespVo attrRespVo = new AttrRespVo();
            BeanUtils.copyProperties(attrEntity, attrRespVo);
//            设置分类和分组名字
            if("base".equalsIgnoreCase(type)){
                AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
                if (attrAttrgroupRelationEntity != null && attrAttrgroupRelationEntity.getAttrGroupId() != null) {
                    AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrAttrgroupRelationEntity.getAttrGroupId());
                    attrRespVo.setGroupName(attrGroupEntity.getAttrGroupName());
                }
            }
            CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId());
            if (categoryEntity != null) {
                attrRespVo.setCatelogName(categoryEntity.getName());
            }
            return attrRespVo;
        }).collect(Collectors.toList());
        pageUtils.setList(respVos);
        return pageUtils;
    }

    @Cacheable(value = "attr",key = "'attrinfo:'+#root.args[0]",sync = true)
    @Override
    public AttrRespVo getAttrInfo(Long attrId) throws ExecutionException, InterruptedException {
        AttrRespVo respVo = new AttrRespVo();
        CompletableFuture<AttrEntity> attrTask = CompletableFuture.supplyAsync(() -> {
            AttrEntity attr = this.getById(attrId);
            return attr;
        }, threadPoolExecutor);
//        设置分组的相关信息
        CompletableFuture<Void> attrGroupTask = attrTask.thenAcceptAsync((resp) -> {
            BeanUtils.copyProperties(resp, respVo);
//        全都要做非空判断
            if (resp.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
                AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrId));
                if (relationEntity != null) {
                    respVo.setAttrGroupId(relationEntity.getAttrGroupId());
                    AttrGroupEntity attrGroupEntity = attrGroupService.getById(relationEntity.getAttrGroupId());
                    if (attrGroupEntity != null) {
                        respVo.setGroupName(attrGroupEntity.getAttrGroupName());
                    }
                }
            }
        }, threadPoolExecutor);
//        设置分类信息
        CompletableFuture<Void> groupTask = attrTask.thenAcceptAsync((resp) -> {
            Long catelogId = resp.getCatelogId();
            Long[] catelogPath = categoryService.findCatelogPath(catelogId);
            respVo.setCatelogPath(catelogPath);
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
            if(categoryEntity!=null){
                respVo.setCatelogName(categoryEntity.getName());
            }
        }, threadPoolExecutor);
        CompletableFuture.allOf(attrTask,attrGroupTask,groupTask).get();
        return respVo;
    }

    @Override
    @Transactional
    public void updateAttr(AttrVo attrVo) throws ExecutionException, InterruptedException {
        AttrEntity attrEntity = new AttrEntity();
//        修改自己
        CompletableFuture<Void> updateTask = CompletableFuture.runAsync(() -> {
            BeanUtils.copyProperties(attrVo,attrEntity);
            this.updateById(attrEntity);
        }, threadPoolExecutor);
//            修改分组关联
        CompletableFuture<Void> groupTask = CompletableFuture.runAsync(() -> {
            if (attrEntity.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) {
                AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
                relationEntity.setAttrGroupId(attrVo.getAttrGroupId());
                relationEntity.setAttrId(attrEntity.getAttrId());
//            看一下它有没有对应的
                Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrVo.getAttrId()));
                if (count > 0) {
                    relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrVo.getAttrId()));
                } else {
                    relationDao.insert(relationEntity);
                }
            }
        }, threadPoolExecutor);
        CompletableFuture.allOf(updateTask,groupTask).get();
    }

//    根据分组ID查找关联的所有基本属性
    @Override
    public List<AttrEntity> getRelationAttr(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> entities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<Long> attrIds = entities.stream().map((entity) -> {
            return entity.getAttrId();
        }).collect(Collectors.toList());
        if(attrIds == null || attrIds.size() == 0){
            return null;
        }
        List<AttrEntity> attrEntities = this.listByIds(attrIds);
        return attrEntities;
    }

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

//    获取当前分组没有关联的所有属性
    @Override
    public PageUtils getNoRelationAttr(Map<String, Object> params, Long attrgroupId) {
        //1、当前分组只能关联自己所属的分类里面的所有属性
        AttrGroupEntity attrGroupEntity = attrGroupService.getById(attrgroupId);
        //获取当前分类的id
        Long catelogId = attrGroupEntity.getCatelogId();
        //2、当前分组只能关联别的分组没有引用的属性
        //2.1)、当前分类下的其它分组
        List<AttrGroupEntity> groupEntities = attrGroupService.list(new QueryWrapper<AttrGroupEntity>()
                .eq("catelog_id", catelogId));
        //获取到所有的attrGroupId
        List<Long> collect = groupEntities.stream().map((item) -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());
        //2.2)、从当前分类的所有属性移除这些属性
        QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>()
                .eq("catelog_id", catelogId).eq("attr_type",ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());
        //2.3)、这些分组关联的属性
        if(collect != null && collect.size() > 0){
            List<AttrAttrgroupRelationEntity> groupId = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id", collect));
            List<Long> attrIds = groupId.stream().map((item) -> {
                return item.getAttrId();
            }).collect(Collectors.toList());
            if (attrIds != null && attrIds.size() > 0) {
                queryWrapper.notIn("attr_id", attrIds);
            }
        }
        //判断是否有参数进行模糊查询
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.and((w) -> {
                w.eq("attr_id",key).or().like("attr_name",key);
            });
        }
        IPage<AttrEntity> page = this.page(new Query<AttrEntity>().getPage(params), queryWrapper);
        PageUtils pageUtils = new PageUtils(page);
        return pageUtils;
    }

    @Override
    public List<Long> selectSearchAttrs(List<Long> attrIds) {
//        在所有属性集合中挑出检索属性
        return baseMapper.selectSearchAttrs(attrIds);
    }
}

 

package com.alatus.mall.product.service.impl;

import com.alatus.mall.product.dao.SkuInfoDao;
import com.alatus.mall.product.entity.SkuImagesEntity;
import com.alatus.mall.product.entity.SkuInfoEntity;
import com.alatus.mall.product.entity.SpuInfoDescEntity;
import com.alatus.mall.product.service.*;
import com.alatus.mall.product.vo.SkuItemSaleAttrVo;
import com.alatus.mall.product.vo.SkuItemVo;
import com.alatus.mall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

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.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;

@Service("pmsSkuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key) && !"0".equalsIgnoreCase(key)){
            queryWrapper.and((wrapper) -> {
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min) && !"0".equalsIgnoreCase(min)){
            queryWrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if(!StringUtils.isEmpty(max) && !"0".equalsIgnoreCase(max)){
            try{
                BigDecimal maxPrice = new BigDecimal(max);
                if(maxPrice.compareTo(new BigDecimal("0")) == 1){
                    queryWrapper.le("price",max);
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }
    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }
    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
    }
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
//        设置基本信息
        CompletableFuture<SkuInfoEntity> skuInfo = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);
        CompletableFuture<Void> skuImages = CompletableFuture.runAsync(() -> {
            //        设置sku的图片信息
            List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(images);
        }, threadPoolExecutor);
        CompletableFuture<Void> saleAttr = skuInfo.thenAcceptAsync((res) -> {
            //        设置spu的销售属性组合
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(skuItemSaleAttrVos);
        }, threadPoolExecutor);
        CompletableFuture<Void> spuDesc = skuInfo.thenAcceptAsync((res) -> {
            //        获取spu的介绍
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDesc);
        }, threadPoolExecutor);
        CompletableFuture<Void> spuAttrGroup = skuInfo.thenAcceptAsync((res) -> {
            //        获取SPU的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, threadPoolExecutor);
//        等待所有任务完成后会返回数据
        CompletableFuture.allOf(skuInfo,skuImages,saleAttr,spuDesc,spuAttrGroup).get();
        return skuItemVo;
    }
}
package com.alatus.mall.product.service.impl;

import com.alatus.mall.product.dao.SkuInfoDao;
import com.alatus.mall.product.entity.SkuImagesEntity;
import com.alatus.mall.product.entity.SkuInfoEntity;
import com.alatus.mall.product.entity.SpuInfoDescEntity;
import com.alatus.mall.product.service.*;
import com.alatus.mall.product.vo.SkuItemSaleAttrVo;
import com.alatus.mall.product.vo.SkuItemVo;
import com.alatus.mall.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

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.alatus.common.utils.PageUtils;
import com.alatus.common.utils.Query;

@Service("pmsSkuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SpuInfoDescService spuInfoDescService;
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key) && !"0".equalsIgnoreCase(key)){
            queryWrapper.and((wrapper) -> {
                wrapper.eq("sku_id",key).or().like("sku_name",key);
            });
        }
        String catelogId = (String) params.get("catelogId");
        if(!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)){
            queryWrapper.eq("catalog_id",catelogId);
        }
        String brandId = (String) params.get("brandId");
        if(!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)){
            queryWrapper.eq("brand_id",brandId);
        }
        String min = (String) params.get("min");
        if(!StringUtils.isEmpty(min) && !"0".equalsIgnoreCase(min)){
            queryWrapper.ge("price",min);
        }
        String max = (String) params.get("max");
        if(!StringUtils.isEmpty(max) && !"0".equalsIgnoreCase(max)){
            try{
                BigDecimal maxPrice = new BigDecimal(max);
                if(maxPrice.compareTo(new BigDecimal("0")) == 1){
                    queryWrapper.le("price",max);
                }
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }
    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }
    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id",spuId));
    }
    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
//        设置基本信息
        CompletableFuture<SkuInfoEntity> skuInfo = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, threadPoolExecutor);
        CompletableFuture<Void> skuImages = CompletableFuture.runAsync(() -> {
            //        设置sku的图片信息
            List<SkuImagesEntity> images = skuImagesService.getBySkuId(skuId);
            skuItemVo.setImages(images);
        }, threadPoolExecutor);
        CompletableFuture<Void> saleAttr = skuInfo.thenAcceptAsync((res) -> {
            //        设置spu的销售属性组合
            List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(skuItemSaleAttrVos);
        }, threadPoolExecutor);
        CompletableFuture<Void> spuDesc = skuInfo.thenAcceptAsync((res) -> {
            //        获取spu的介绍
            SpuInfoDescEntity spuInfoDesc = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDesc);
        }, threadPoolExecutor);
        CompletableFuture<Void> spuAttrGroup = skuInfo.thenAcceptAsync((res) -> {
            //        获取SPU的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(attrGroupVos);
        }, threadPoolExecutor);
//        等待所有任务完成后会返回数据
        CompletableFuture.allOf(skuInfo,skuImages,saleAttr,spuDesc,spuAttrGroup).get();
        return skuItemVo;
    }
}
package com.alatus.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.alatus.mall.product.entity.ProductAttrValueEntity;
import com.alatus.mall.product.service.AttrService;
import com.alatus.mall.product.service.ProductAttrValueService;
import com.alatus.mall.product.vo.AttrRespVo;
import com.alatus.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;



/**
 * 商品属性
 *
 * @author Alatus
 * @email 1571345941@qq.com
 * @date 2024-06-08 16:06:24
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data",productAttrValueEntityList);
    }

    @GetMapping("/{attrType}/list/{cateLogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("cateLogId") Long cateLogId,@PathVariable("attrType")String type){
        PageUtils page = attrService.queryBaseAttrPage(params,cateLogId,type);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) throws ExecutionException, InterruptedException {
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo) throws ExecutionException, InterruptedException {
        attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo) throws ExecutionException, InterruptedException {
        attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改SPU信息
     */
    @RequestMapping("/update/{spuId}")
    public R updateSpu(@PathVariable("spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
package com.alatus.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.alatus.mall.product.entity.ProductAttrValueEntity;
import com.alatus.mall.product.service.AttrService;
import com.alatus.mall.product.service.ProductAttrValueService;
import com.alatus.mall.product.vo.AttrRespVo;
import com.alatus.mall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alatus.common.utils.PageUtils;
import com.alatus.common.utils.R;



/**
 * 商品属性
 *
 * @author Alatus
 * @email 1571345941@qq.com
 * @date 2024-06-08 16:06:24
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId")Long spuId){
        List<ProductAttrValueEntity> productAttrValueEntityList = productAttrValueService.baseAttrListForSpu(spuId);
        return R.ok().put("data",productAttrValueEntityList);
    }

    @GetMapping("/{attrType}/list/{cateLogId}")
    public R baseAttrList(@RequestParam Map<String,Object> params,@PathVariable("cateLogId") Long cateLogId,@PathVariable("attrType")String type){
        PageUtils page = attrService.queryBaseAttrPage(params,cateLogId,type);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId) throws ExecutionException, InterruptedException {
        AttrRespVo attrRespVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrRespVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrVo attrVo) throws ExecutionException, InterruptedException {
        attrService.saveAttr(attrVo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrVo attrVo) throws ExecutionException, InterruptedException {
        attrService.updateAttr(attrVo);

        return R.ok();
    }

    /**
     * 修改SPU信息
     */
    @RequestMapping("/update/{spuId}")
    public R updateSpu(@PathVariable("spuId")Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值