package com.kexue.skills.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kexue.skills.entity.CmsCategory;
import com.kexue.skills.entity.dto.CmsCategoryDto;
import com.kexue.skills.mapper.CmsCategoryMapper;
import com.kexue.skills.service.CmsCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (CmsCategory)表服务实现类
 *
 * @author 王志维
 * @since 2025-02-21 23:01:48
 */
@Service("cmsCategoryService")
@Transactional(rollbackFor = Exception.class)
public class CmsCategoryServiceImpl implements CmsCategoryService {
    @Resource
    private CmsCategoryMapper cmsCategoryMapper;

    /**
     * 分页查询
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo<CmsCategory> getPageList(CmsCategoryDto queryDto) {
        PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        List<CmsCategory> list = this.cmsCategoryMapper.getPageList(queryDto);
        return new PageInfo<>(list);
    }

    /**
     * 查询列表
     *
     * @param queryDto 筛选条件
     * @return 查询结果
     */
    @Override
    public List<CmsCategory> getList(CmsCategoryDto queryDto) {
        return this.cmsCategoryMapper.getList(queryDto);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param categoryId 主键
     * @return 实例对象
     */
    @Override
    public CmsCategory queryById(Long categoryId) {
        return this.cmsCategoryMapper.queryById(categoryId);
    }

    /**
     * 新增数据
     *
     * @param cmsCategory 实例对象
     * @return 实例对象
     */
    @Override
    public CmsCategory insert(CmsCategory cmsCategory) {
        // 设置创建时间和更新时间
        Date now = new Date();
        cmsCategory.setCreateTime(now);
        cmsCategory.setUpdateTime(now);
        // 设置默认值
        cmsCategory.setDeleteFlag(0);
        if (cmsCategory.getStatus() == null) {
            cmsCategory.setStatus(1);
        }
        if (cmsCategory.getSort() == null) {
            cmsCategory.setSort(0);
        }
        // 保存数据
        this.cmsCategoryMapper.insert(cmsCategory);
        return cmsCategory;
    }

    /**
     * 更新数据
     *
     * @param cmsCategory 实例对象
     * @return 实例对象
     */
    @Override
    public CmsCategory update(CmsCategory cmsCategory) {
        // 设置更新时间
        cmsCategory.setUpdateTime(new Date());
        // 更新数据
        this.cmsCategoryMapper.update(cmsCategory);
        return this.queryById(cmsCategory.getCategoryId());
    }

    /**
     * 通过主键逻辑删除
     *
     * @param categoryId 主键
     * @param updateBy 更新人
     * @return 影响行数
     */
    @Override
    public int logicDeleteById(Long categoryId, String updateBy) {
        return this.cmsCategoryMapper.logicDeleteById(categoryId, updateBy);
    }

    /**
     * 通过主键物理删除
     *
     * @param categoryId 主键
     * @return 影响行数
     */
    @Override
    public int deleteById(Long categoryId) {
        return this.cmsCategoryMapper.deleteById(categoryId);
    }
    
    /**
     * 查询分类树列表
     *
     * @return 分类树列表
     */
    @Override
    public List<CmsCategory> getCategoryTreeList() {
        // 查询所有分类
        CmsCategoryDto queryDto = new CmsCategoryDto();
        queryDto.setDeleteFlag(0); // 只查询未删除的分类
        List<CmsCategory> allCategories = this.cmsCategoryMapper.getList(queryDto);
        
        // 构建分类树
        return buildCategoryTree(allCategories, null);
    }
    
    /**
     * 递归构建分类树
     *
     * @param allCategories 所有分类列表
     * @param parentId 父分类ID
     * @return 分类树列表
     */
    private List<CmsCategory> buildCategoryTree(List<CmsCategory> allCategories, Long parentId) {
        List<CmsCategory> tree = new java.util.ArrayList<>();
        
        for (CmsCategory category : allCategories) {
            // 找出当前父分类下的子分类
            if ((parentId == null && category.getParentId() == null) || 
                (parentId != null && parentId.equals(category.getParentId()))) {
                // 递归查询子分类
                List<CmsCategory> subCategories = buildCategoryTree(allCategories, category.getCategoryId());
                category.setSubCategoryList(subCategories);
                tree.add(category);
            }
        }
        
        return tree;
    }
    
    /**
     * 获取分类字典
     *
     * @return 分类字典，key为分类ID，value为分类名称
     */
    @Override
    public java.util.Map<Long, String> getCategoryDict() {
        // 查询所有分类
        CmsCategoryDto queryDto = new CmsCategoryDto();
        queryDto.setDeleteFlag(0); // 只查询未删除的分类
        List<CmsCategory> allCategories = this.cmsCategoryMapper.getList(queryDto);
        
        // 构建分类字典
        java.util.Map<Long, String> categoryDict = new java.util.HashMap<>();
        for (CmsCategory category : allCategories) {
            categoryDict.put(category.getCategoryId(), category.getCategoryName());
        }
        
        return categoryDict;
    }
}