package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.enums.ResultCodeEnum;
import com.example.entity.ResourceCategory;
import com.example.entity.TreatmentResource;
import com.example.exception.CustomException;
import com.example.mapper.ResourceCategoryMapper;
import com.example.mapper.TreatmentResourceMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

@Service
public class ResourceCategoryService {

    @Resource
    private ResourceCategoryMapper resourceCategoryMapper;
    
    @Resource
    private TreatmentResourceMapper treatmentResourceMapper;
    
    public List<ResourceCategory> findAll() {
        return resourceCategoryMapper.selectAllCategories();
    }
    
    public List<ResourceCategory> findByParentId(Integer parentId) {
        return resourceCategoryMapper.selectByParentId(parentId);
    }
    
    public ResourceCategory findById(Integer id) {
        return resourceCategoryMapper.selectById(id);
    }
    
    public void add(ResourceCategory category) {
        // 检查是否存在同名分类
        LambdaQueryWrapper<ResourceCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResourceCategory::getName, category.getName())
               .eq(ResourceCategory::getParentId, category.getParentId());
        
        if (resourceCategoryMapper.selectCount(wrapper) > 0) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR.code, "已存在同名分类");
        }
        
        resourceCategoryMapper.insert(category);
    }
    
    public void update(ResourceCategory category) {
        // 检查是否存在同名分类（排除自身）
        LambdaQueryWrapper<ResourceCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResourceCategory::getName, category.getName())
               .eq(ResourceCategory::getParentId, category.getParentId())
               .ne(ResourceCategory::getId, category.getId());
        
        if (resourceCategoryMapper.selectCount(wrapper) > 0) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR.code, "已存在同名分类");
        }
        
        resourceCategoryMapper.updateById(category);
    }
    
    public void delete(Integer id) {
        // 检查是否有子分类
        LambdaQueryWrapper<ResourceCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ResourceCategory::getParentId, id);
        
        if (resourceCategoryMapper.selectCount(wrapper) > 0) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR.code, "该分类下存在子分类，无法删除");
        }
        
        // 检查是否有关联的资源
        List<TreatmentResource> resources = treatmentResourceMapper.selectByCategoryId(id);
        if (resources != null && !resources.isEmpty()) {
            throw new CustomException(ResultCodeEnum.PARAM_ERROR.code, "该分类下存在资源，无法删除");
        }
        
        resourceCategoryMapper.deleteById(id);
    }
    
    public PageInfo<ResourceCategory> selectPage(ResourceCategory category, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ResourceCategory> list = resourceCategoryMapper.selectPage(category.getName() != null ? category.getName() : "");
        return new PageInfo<>(list);
    }
} 