package com.yongqi.xinrenli.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.yongqi.xinrenli.domain.bo.DbUserCommentBo;
import com.yongqi.xinrenli.domain.vo.DbUserCommentVo;
import com.yongqi.xinrenli.domain.DbUserComment;
import com.yongqi.xinrenli.mapper.DbUserCommentMapper;
import com.yongqi.xinrenli.service.IDbUserCommentService;

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

/**
 * 用户评论Service业务层处理
 *
 * @author zyf
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class DbUserCommentServiceImpl implements IDbUserCommentService {

    private final DbUserCommentMapper baseMapper;

    /**
     * 查询用户评论
     */
    @Override
    public DbUserCommentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户评论列表
     */
    @Override
    public TableDataInfo<DbUserCommentVo> queryPageList(DbUserCommentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DbUserComment> lqw = buildQueryWrapper(bo);
        Page<DbUserCommentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户评论列表
     */
    @Override
    public List<DbUserCommentVo> queryList(DbUserCommentBo bo) {
        LambdaQueryWrapper<DbUserComment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DbUserComment> buildQueryWrapper(DbUserCommentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DbUserComment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getArticleId() != null, DbUserComment::getArticleId, bo.getArticleId());
        lqw.eq(bo.getUserId() != null, DbUserComment::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), DbUserComment::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateUser()), DbUserComment::getCreateUser, bo.getCreateUser());
        lqw.eq(StringUtils.isNotBlank(bo.getModifyUser()), DbUserComment::getModifyUser, bo.getModifyUser());
        lqw.eq(bo.getModifyTime() != null, DbUserComment::getModifyTime, bo.getModifyTime());
        lqw.eq(bo.getDeleteStatus() != null, DbUserComment::getDeleteStatus, bo.getDeleteStatus());
        return lqw;
    }

    /**
     * 新增用户评论
     */
    @Override
    public Boolean insertByBo(DbUserCommentBo bo) {
        DbUserComment add = BeanUtil.toBean(bo, DbUserComment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户评论
     */
    @Override
    public Boolean updateByBo(DbUserCommentBo bo) {
        DbUserComment update = BeanUtil.toBean(bo, DbUserComment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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