package com.ruoyi.system.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.system.domain.bo.ZsArticleCategoryBo;
import com.ruoyi.system.domain.vo.ZsArticleCategoryVo;
import com.ruoyi.system.domain.ZsArticleCategory;
import com.ruoyi.system.mapper.ZsArticleCategoryMapper;
import com.ruoyi.system.service.IZsArticleCategoryService;

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

/**
 * 站点文章分类管理Service业务层处理
 *
 * @author 网球
 * @date 2025-04-29
 */
@RequiredArgsConstructor
@Service
public class ZsArticleCategoryServiceImpl implements IZsArticleCategoryService {

    private final ZsArticleCategoryMapper baseMapper;

    /**
     * 查询站点文章分类管理
     */
    @Override
    public ZsArticleCategoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }


    /**
     * 查询站点文章分类管理列表
     */
    @Override
    public List<ZsArticleCategoryVo> queryList(ZsArticleCategoryBo bo) {
        LambdaQueryWrapper<ZsArticleCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZsArticleCategory> buildQueryWrapper(ZsArticleCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsArticleCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getWebsiteName()), ZsArticleCategory::getWebsiteName, bo.getWebsiteName());
        lqw.eq(StringUtils.isNotBlank(bo.getLanguageCode()), ZsArticleCategory::getLanguageCode, bo.getLanguageCode());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ZsArticleCategory::getName, bo.getName());
        return lqw;
    }

    /**
     * 新增站点文章分类管理
     */
    @Override
    public Boolean insertByBo(ZsArticleCategoryBo bo) {
        ZsArticleCategory add = BeanUtil.toBean(bo, ZsArticleCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

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

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

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