package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.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 org.dromara.system.domain.bo.TblArticleBo;
import org.dromara.system.domain.vo.TblArticleVo;
import org.dromara.system.domain.TblArticle;
import org.dromara.system.mapper.TblArticleMapper;
import org.dromara.system.service.ITblArticleService;

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

/**
 * 文章Service业务层处理
 *
 * @author Lion Li
 * @date 2024-07-18
 */
@RequiredArgsConstructor
@Service
public class TblArticleServiceImpl implements ITblArticleService {

    private final TblArticleMapper baseMapper;

    /**
     * 查询文章
     */
    @Override
    public TblArticleVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

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

    /**
     * 查询文章列表
     */
    @Override
    public List<TblArticleVo> queryList(TblArticleBo bo) {
        LambdaQueryWrapper<TblArticle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblArticle> buildQueryWrapper(TblArticleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblArticle> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), TblArticle::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getSubTitle()), TblArticle::getSubTitle, bo.getSubTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), TblArticle::getContent, bo.getContent());
        lqw.eq(bo.getStatus() != null, TblArticle::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblArticle::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblArticle::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增文章
     */
    @Override
    public Boolean insertByBo(TblArticleBo bo) {
        TblArticle add = MapstructUtils.convert(bo, TblArticle.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改文章
     */
    @Override
    public Boolean updateByBo(TblArticleBo bo) {
        TblArticle update = MapstructUtils.convert(bo, TblArticle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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