package cn.iocoder.yudao.module.tkdata.service.comment;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.CommentPageReqVO;
import cn.iocoder.yudao.module.tkdata.controller.admin.configController.vo.CommentSaveReqVO;
import cn.iocoder.yudao.module.tkdata.dal.dataobject.comment.CommentDO;
import cn.iocoder.yudao.module.tkdata.dal.mysql.comment.CommentMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.tkdata.enums.ErrorCodeConstants.COMMENT_NOT_EXISTS;


/**
 * AI 评论内容
 Service 实现类
 *
 * @author 总后台
 */
@Service
@Validated
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public Integer createComment(CommentSaveReqVO createReqVO) {
        // 插入
        CommentDO comment = BeanUtils.toBean(createReqVO, CommentDO.class);
        commentMapper.insert(comment);
        // 返回
        return comment.getId();
    }

    @Override
    public void updateComment(CommentSaveReqVO updateReqVO) {
        // 校验存在
        validateCommentExists(updateReqVO.getId());
        // 更新
        CommentDO updateObj = BeanUtils.toBean(updateReqVO, CommentDO.class);
        commentMapper.updateById(updateObj);
    }

    @Override
    public void deleteComment(Integer id) {
        // 校验存在
        validateCommentExists(id);
        // 删除
        commentMapper.deleteById(id);
    }

    @Override
        public void deleteCommentListByIds(List<Integer> ids) {
        // 校验存在
        validateCommentExists(ids);
        // 删除
        commentMapper.deleteByIds(ids);
        }

    private void validateCommentExists(List<Integer> ids) {
        List<CommentDO> list = commentMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(COMMENT_NOT_EXISTS);
        }
    }

    private void validateCommentExists(Integer id) {
        if (commentMapper.selectById(id) == null) {
            throw exception(COMMENT_NOT_EXISTS);
        }
    }

    @Override
    public CommentDO getComment(Integer id) {
        return commentMapper.selectById(id);
    }

    @Override
    public PageResult<CommentDO> getCommentPage(CommentPageReqVO pageReqVO) {
        return commentMapper.selectPage(pageReqVO);
    }

}