package com.ruoyi.export.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.export.domain.bo.ExportArticleCategoryBo;
import com.ruoyi.export.domain.vo.ExportArticleCategoryVo;
import com.ruoyi.export.domain.ExportArticleCategory;
import com.ruoyi.export.mapper.ExportArticleCategoryMapper;
import com.ruoyi.export.service.IExportArticleCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文章分类Service业务层处理
 *
 * @author JR
 * @date 2025-07-16
 */
@RequiredArgsConstructor
@Service
public class ExportArticleCategoryServiceImpl implements IExportArticleCategoryService {

    private final ExportArticleCategoryMapper baseMapper;

    /**
     * 查询文章分类
     */
    @Override
    public ExportArticleCategoryVo queryById(Long categoryId){
        return baseMapper.selectVoById(categoryId);
    }

    /**
     * 查询文章分类列表
     */
    @Override
    public TableDataInfo<ExportArticleCategoryVo> queryPageList(ExportArticleCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExportArticleCategory> lqw = buildQueryWrapper(bo);
        Page<ExportArticleCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文章分类列表
     */
    @Override
    public List<ExportArticleCategoryVo> queryList(ExportArticleCategoryBo bo) {
        LambdaQueryWrapper<ExportArticleCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExportArticleCategory> buildQueryWrapper(ExportArticleCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExportArticleCategory> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentId() != null, ExportArticleCategory::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getLangCode()), ExportArticleCategory::getLangCode, bo.getLangCode());
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), ExportArticleCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ExportArticleCategory::getDescription, bo.getDescription());
        lqw.eq(bo.getSortOrder() != null, ExportArticleCategory::getSortOrder, bo.getSortOrder());
        lqw.eq(bo.getIsEnabled() != null, ExportArticleCategory::getIsEnabled, bo.getIsEnabled());
        return lqw;
    }

    /**
     * 新增文章分类
     */
    @Override
    public Boolean insertByBo(ExportArticleCategoryBo bo) {
        ExportArticleCategory add = BeanUtil.toBean(bo, ExportArticleCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改文章分类
     */
    @Override
    public Boolean updateByBo(ExportArticleCategoryBo bo) {
        ExportArticleCategory update = BeanUtil.toBean(bo, ExportArticleCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExportArticleCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除文章分类
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
