src/main/java/cc/mrbird/febs/ai/service/impl/AiProductCategoryServiceImpl.java
@@ -1,15 +1,28 @@
package cc.mrbird.febs.ai.service.impl;
import cc.mrbird.febs.ai.entity.AiMemberRole;
import cc.mrbird.febs.ai.entity.AiProductCategory;
import cc.mrbird.febs.ai.enumerates.ProductCategoryLevelEnum;
import cc.mrbird.febs.ai.mapper.AiProductCategoryMapper;
import cc.mrbird.febs.ai.req.productCategory.ApiProductCategoryAllDto;
import cc.mrbird.febs.ai.req.productCategory.ApiProductCategoryHotDto;
import cc.mrbird.febs.ai.req.productCategory.ApiProductCategoryPageDto;
import cc.mrbird.febs.ai.res.productCategory.ApiProductCategoryVo;
import cc.mrbird.febs.ai.service.AiProductCategoryService;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.mall.vo.ApiActivityInfoVo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
/**
@@ -25,137 +38,159 @@
    private final AiProductCategoryMapper aiProductCategoryMapper;
    @Override
    public AiProductCategory getById(String id) {
        return this.getById(id);
    public List<AiProductCategory> getListByQuery(LambdaQueryWrapper<AiProductCategory> query) {
        return aiProductCategoryMapper.selectList(query);
    }
    @Override
    public List<AiProductCategory> getByCompanyId(String companyId) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getCompanyId, companyId);
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    public Page<ApiProductCategoryVo> getPageListByQuery(Page<ApiProductCategoryVo> page , ApiProductCategoryPageDto dto) {
        return aiProductCategoryMapper.selectPageListByQuery(page,dto);
    }
    @Override
    public AiProductCategory getByCode(String code) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getCode, code);
        return this.getOne(queryWrapper);
    }
    public FebsResponse hot() {
    @Override
    public List<AiProductCategory> getByName(String name) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(AiProductCategory::getName, name);
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    }
        List<ApiProductCategoryVo> list = new ArrayList<>();
    @Override
    public List<AiProductCategory> getByState(Integer state) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getState, state);
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    }
    @Override
    public List<AiProductCategory> getByHotState(Integer hotState) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getHotState, hotState);
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    }
    @Override
    public List<AiProductCategory> getByCompanyIdAndState(String companyId, Integer state) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getCompanyId, companyId);
        queryWrapper.eq(AiProductCategory::getState, state);
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    }
    @Override
    public List<AiProductCategory> getByCompanyIdAndHotState(String companyId, Integer hotState) {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getCompanyId, companyId);
        queryWrapper.eq(AiProductCategory::getHotState, hotState);
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    }
    @Override
    public List<AiProductCategory> getEnabledCategories() {
        LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AiProductCategory::getState, 1); // 1-启用
        queryWrapper.orderByAsc(AiProductCategory::getSort);
        queryWrapper.orderByDesc(AiProductCategory::getCreatedTime);
        return this.list(queryWrapper);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveProductCategory(AiProductCategory aiProductCategory) {
        try {
            return this.save(aiProductCategory);
        } catch (Exception e) {
            log.error("保存AI产品类别失败: ", e);
            return false;
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        query.eq(AiProductCategory::getLevel, ProductCategoryLevelEnum.LEVEL_TWO.getLevel());
        query.eq(AiProductCategory::getState, 1);
        query.eq(AiProductCategory::getHotState, 1);
        query.orderByAsc(AiProductCategory::getSort);
        List<AiProductCategory> listByQuery = this.getListByQuery(query);
        if (CollUtil.isNotEmpty(listByQuery)){
            for (AiProductCategory aiProductCategory : listByQuery){
                ApiProductCategoryVo apiProductCategoryVo = new ApiProductCategoryVo();
                apiProductCategoryVo.setId(aiProductCategory.getId());
                apiProductCategoryVo.setName(aiProductCategory.getName());
                apiProductCategoryVo.setBackImg(aiProductCategory.getBackImg());
                apiProductCategoryVo.setIconImg(aiProductCategory.getIconImg());
                list.add(apiProductCategoryVo);
            }
        }
        return new FebsResponse().success().data(list);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatchProductCategories(List<AiProductCategory> productCategories) {
        try {
            return this.saveBatch(productCategories);
        } catch (Exception e) {
            log.error("批量保存AI产品类别失败: ", e);
            return false;
    public FebsResponse hotV1(ApiProductCategoryHotDto dto) {
        List<ApiProductCategoryVo> list = new ArrayList<>();
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        if (StrUtil.isNotEmpty(dto.getCompanyId())){
            query.eq(AiProductCategory::getCompanyId, dto.getCompanyId());
        }else{
            query.isNull(AiProductCategory::getCompanyId);
        }
        query.eq(AiProductCategory::getLevel, ProductCategoryLevelEnum.LEVEL_TWO.getLevel());
        query.eq(AiProductCategory::getState, 1);
        query.eq(AiProductCategory::getHotState, 1);
        query.orderByAsc(AiProductCategory::getSort);
        List<AiProductCategory> listByQuery = this.getListByQuery(query);
        if (CollUtil.isNotEmpty(listByQuery)){
            for (AiProductCategory aiProductCategory : listByQuery){
                ApiProductCategoryVo apiProductCategoryVo = new ApiProductCategoryVo();
                apiProductCategoryVo.setId(aiProductCategory.getId());
                apiProductCategoryVo.setName(aiProductCategory.getName());
                apiProductCategoryVo.setBackImg(aiProductCategory.getBackImg());
                apiProductCategoryVo.setIconImg(aiProductCategory.getIconImg());
                list.add(apiProductCategoryVo);
            }
        }
        return new FebsResponse().success().data(list);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProductCategory(AiProductCategory aiProductCategory) {
        try {
            return this.updateById(aiProductCategory);
        } catch (Exception e) {
            log.error("更新AI产品类别失败: ", e);
            return false;
    public FebsResponse hotV2(ApiProductCategoryHotDto dto) {
        List<ApiProductCategoryVo> list = new ArrayList<>();
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        if (StrUtil.isNotEmpty(dto.getCompanyId())){
            query.eq(AiProductCategory::getCompanyId, dto.getCompanyId());
        }else{
            query.isNull(AiProductCategory::getCompanyId);
        }
        query.eq(AiProductCategory::getLevel, ProductCategoryLevelEnum.LEVEL_ONE.getLevel());
        query.eq(AiProductCategory::getState, 1);
        query.eq(AiProductCategory::getHotState, 1);
        query.orderByAsc(AiProductCategory::getSort);
        List<AiProductCategory> listByQuery = this.getListByQuery(query);
        if (CollUtil.isNotEmpty(listByQuery)){
            for (AiProductCategory aiProductCategory : listByQuery){
                ApiProductCategoryVo apiProductCategoryVo = new ApiProductCategoryVo();
                apiProductCategoryVo.setId(aiProductCategory.getId());
                apiProductCategoryVo.setName(aiProductCategory.getName());
                apiProductCategoryVo.setBackImg(aiProductCategory.getBackImg());
                apiProductCategoryVo.setIconImg(aiProductCategory.getIconImg());
                list.add(apiProductCategoryVo);
            }
        }
        return new FebsResponse().success().data(list);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(String id) {
        try {
            return this.removeById(id);
        } catch (Exception e) {
            log.error("删除AI产品类别失败: ", e);
            return false;
    public FebsResponse categoryChildList(ApiProductCategoryPageDto dto) {
        List<ApiProductCategoryVo> vos = new ArrayList<>();
        List<AiProductCategory> aiProductCategories = aiProductCategoryMapper.selectList(
                Wrappers.lambdaQuery(AiProductCategory.class)
                        .eq(AiProductCategory::getParentId, dto.getParentId())
                        .eq(AiProductCategory::getState, 1)
                .orderByAsc(AiProductCategory::getSort)
        );
        if (CollUtil.isNotEmpty(aiProductCategories)){
            for (AiProductCategory aiProductCategory : aiProductCategories) {
                ApiProductCategoryVo vo = new ApiProductCategoryVo();
                vo.setId(aiProductCategory.getId());
                vo.setName(aiProductCategory.getName());
                vos.add(vo);
            }
        }
        return new FebsResponse().success().data(vos);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByCompanyId(String companyId) {
        try {
            LambdaQueryWrapper<AiProductCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AiProductCategory::getCompanyId, companyId);
            return this.remove(queryWrapper);
        } catch (Exception e) {
            log.error("根据公司ID删除AI产品类别失败: ", e);
            return false;
    public String getDefaultProductCategoryId() {
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        query.eq(AiProductCategory::getState, 1);
        query.isNull(AiProductCategory::getCompanyId);
        query.eq(AiProductCategory::getHotState, 1);
        query.orderByAsc(AiProductCategory::getSort);
        query.last("limit 1");
        List<AiProductCategory> listByQuery = this.getListByQuery(query);
        if (CollUtil.isNotEmpty(listByQuery)){
            return listByQuery.get(0).getId();
        }
        return null;
    }
    @Override
    public FebsResponse allList(ApiProductCategoryAllDto dto) {
        List<ApiProductCategoryVo> list = new ArrayList<>();
        LambdaQueryWrapper<AiProductCategory> query = Wrappers.lambdaQuery(AiProductCategory.class);
        if (StrUtil.isNotEmpty(dto.getCompanyId())){
            query.eq(AiProductCategory::getCompanyId, dto.getCompanyId());
        }else{
            query.isNull(AiProductCategory::getCompanyId);
        }
        query.eq(AiProductCategory::getState, 1);
        query.eq(AiProductCategory::getLevel, ProductCategoryLevelEnum.LEVEL_ONE.getLevel());
        query.orderByAsc(AiProductCategory::getSort);
        List<AiProductCategory> listByQuery = this.getListByQuery(query);
        if (CollUtil.isNotEmpty(listByQuery)){
            for (AiProductCategory aiProductCategory : listByQuery){
                ApiProductCategoryVo apiProductCategoryVo = new ApiProductCategoryVo();
                apiProductCategoryVo.setId(aiProductCategory.getId());
                apiProductCategoryVo.setName(aiProductCategory.getName());
                apiProductCategoryVo.setBackImg(aiProductCategory.getBackImg());
                apiProductCategoryVo.setIconImg(aiProductCategory.getIconImg());
                list.add(apiProductCategoryVo);
            }
        }
        return new FebsResponse().success().data(list);
    }
}