package cn.meowrain.aioj.backend.blogservice.service.impl;

import cn.meowrain.aioj.backend.blogservice.dao.entity.Category;
import cn.meowrain.aioj.backend.blogservice.dao.mapper.CategoryMapper;
import cn.meowrain.aioj.backend.blogservice.dto.req.CategoryCreateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.req.CategoryUpdateRequestDTO;
import cn.meowrain.aioj.backend.blogservice.dto.resp.CategoryResponseDTO;
import cn.meowrain.aioj.backend.blogservice.service.CategoryService;
import cn.meowrain.aioj.backend.framework.core.exception.ClientException;
import cn.hutool.core.bean.BeanUtil;
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.List;
import java.util.stream.Collectors;

/**
 * 文章分类服务实现类
 *
 * @author AIOJ
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCategory(CategoryCreateRequestDTO request) {
        Category category = BeanUtil.copyProperties(request, Category.class);
        this.save(category);
        return category.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateCategory(CategoryUpdateRequestDTO request) {
        Category category = this.getById(request.getId());
        if (category == null) {
            throw new ClientException("分类不存在");
        }

        Category updateCategory = BeanUtil.copyProperties(request, Category.class);
        this.updateById(updateCategory);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteCategory(Long categoryId) {
        Category category = this.getById(categoryId);
        if (category == null) {
            throw new ClientException("分类不存在");
        }

        this.removeById(categoryId);
        return true;
    }

    @Override
    public CategoryResponseDTO getCategoryById(Long categoryId) {
        Category category = this.getById(categoryId);
        if (category == null) {
            return null;
        }
        return BeanUtil.copyProperties(category, CategoryResponseDTO.class);
    }

    @Override
    public CategoryResponseDTO getCategoryBySlug(String slug) {
        Category category = this.lambdaQuery()
                .eq(Category::getSlug, slug)
                .one();
        if (category == null) {
            return null;
        }
        return BeanUtil.copyProperties(category, CategoryResponseDTO.class);
    }

    @Override
    public List<CategoryResponseDTO> getAllCategories() {
        List<Category> categories = this.lambdaQuery()
                .orderByAsc(Category::getSortOrder)
                .list();
        return categories.stream()
                .map(category -> BeanUtil.copyProperties(category, CategoryResponseDTO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<CategoryResponseDTO> getEnabledCategories() {
        List<Category> categories = this.lambdaQuery()
                .eq(Category::getIsEnabled, 1)
                .orderByAsc(Category::getSortOrder)
                .list();
        return categories.stream()
                .map(category -> BeanUtil.copyProperties(category, CategoryResponseDTO.class))
                .collect(Collectors.toList());
    }
}
