package com.ruoyi.export.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
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.ExportCategoryBo;
import com.ruoyi.export.domain.vo.ExportCategoryVo;
import com.ruoyi.export.domain.ExportCategory;
import com.ruoyi.export.mapper.ExportCategoryMapper;
import com.ruoyi.export.service.IExportCategoryService;

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

/**
 * 分类Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-17
 */
@RequiredArgsConstructor
@Service
public class ExportCategoryServiceImpl implements IExportCategoryService {

    private final ExportCategoryMapper baseMapper;

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


    /**
     * 查询分类列表
     */
    @Override
    public List<ExportCategoryVo> queryList(ExportCategoryBo bo) {
        LambdaQueryWrapper<ExportCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

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

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

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

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