package com.yida.data.school.atlas.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.atlas.CoreAtlasManagementDetailsCommentReply;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementDetailsCommentReplyMapper;
import com.yida.data.school.atlas.service.CoreAtlasManagementDetailsCommentReplyService;
import com.yida.data.school.dto.atlas.AtlasDetailsCommentReplySelectPageDTO;
import com.yida.data.school.dto.atlas.CheckCommentReplyDTO;
import com.yida.data.school.vo.atlas.AtlasDetailsCommentReplyInfoVO;
import com.yida.data.school.vo.atlas.AtlasDetailsCommentReplySelectPageVO;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * 学校图集作品评论回复service层
 *
 * @author ZYJ
 * @date 2021/10/13
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CoreAtlasManagementDetailsCommentReplyServiceImpl
        extends ServiceImpl<CoreAtlasManagementDetailsCommentReplyMapper, CoreAtlasManagementDetailsCommentReply>
        implements CoreAtlasManagementDetailsCommentReplyService {

    /**
     * 查询评论回复列表数据
     *
     * @param atlasDetailsCommentReplySelectPageDTO 评论回复列表返回数据
     * @return com.yida.data.common.core.common.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < com.yida.data.atlas.vo.AtlasDetailsCommentReplySelectPageVO>>
     * @author ZYJ
     * @date 2021/10/13 15:48
     */
    @Override
    public ResultBean<IPage<AtlasDetailsCommentReplySelectPageVO>> listAtlasDetailsCommentReplyPage(AtlasDetailsCommentReplySelectPageDTO atlasDetailsCommentReplySelectPageDTO) {
        IPage<AtlasDetailsCommentReplySelectPageVO> iPage = this.baseMapper.listAtlasDetailsCommentReplyPage(atlasDetailsCommentReplySelectPageDTO.toPage(), atlasDetailsCommentReplySelectPageDTO);
        return ResultBean.buildSuccess(iPage);
    }

    /**
     * 获取评论回复详情
     *
     * @param commentReplyId 评论回复id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.atlas.vo.AtlasDetailsCommentReplyInfoVO>
     * @author ZYJ
     * @date 2021/10/13 15:52
     */
    @Override
    public ResultBean<AtlasDetailsCommentReplyInfoVO> getCommentReplyInfo(Long commentReplyId) {
        // 获取评论回复详情数据
        CoreAtlasManagementDetailsCommentReply coreAtlasManagementDetailsCommentReply = this.getById(commentReplyId);
        AtlasDetailsCommentReplyInfoVO atlasDetailsCommentReplyInfoVO = new AtlasDetailsCommentReplyInfoVO();
        Optional.ofNullable(coreAtlasManagementDetailsCommentReply).ifPresent(reply ->
                BeanUtils.copyProperties(reply, atlasDetailsCommentReplyInfoVO));
        return ResultBean.buildSuccess(atlasDetailsCommentReplyInfoVO);
    }

    /**
     * 作品回复审核接口
     *
     * @param checkCommentReplyDTO 作品评论审核请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/10/13 15:55
     */
    @Override
    public ResultBean<String> checkCommentReply(CheckCommentReplyDTO checkCommentReplyDTO) {
        CoreAtlasManagementDetailsCommentReply coreAtlasManagementDetailsCommentReply = new CoreAtlasManagementDetailsCommentReply();
        BeanUtils.copyProperties(checkCommentReplyDTO, coreAtlasManagementDetailsCommentReply);
        // 当前登陆用户数据
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        coreAtlasManagementDetailsCommentReply.setUpdateId(Objects.requireNonNull(currentUser).getUserId());
        coreAtlasManagementDetailsCommentReply.setUpdateDate(LocalDateTime.now());
        this.saveOrUpdate(coreAtlasManagementDetailsCommentReply);
        return ResultBean.buildSuccess();
    }

    /**
     * 批量删除评论回复数据
     *
     * @param ids 回复id集合
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/10/13 15:58
     */
    @Override
    public ResultBean<String> deleteCommentReplyBatch(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));
        return ResultBean.buildSuccess();
    }
}
