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.CoreAtlasManagementDetails;
import com.yida.data.common.core.entity.atlas.CoreAtlasManagementDetailsComment;
import com.yida.data.common.core.enums.CheckStatusEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementDetailsCommentMapper;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementDetailsMapper;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementMapper;
import com.yida.data.school.atlas.service.CoreAtlasManagementDetailsCommentService;
import com.yida.data.school.dto.atlas.AtlasDetailsCommentSelectPageDTO;
import com.yida.data.school.dto.atlas.CheckCommentDTO;
import com.yida.data.school.vo.atlas.AtlasDetailsCommentInfoVO;
import com.yida.data.school.vo.atlas.AtlasDetailsCommentSelectPageVO;

import org.apache.commons.lang3.ArrayUtils;
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;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 学校图集作品评论管理service层
 *
 * @author ZYJ
 * @date 2021/10/13
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CoreAtlasManagementDetailsCommentServiceImpl
        extends ServiceImpl<CoreAtlasManagementDetailsCommentMapper, CoreAtlasManagementDetailsComment>
        implements CoreAtlasManagementDetailsCommentService {

    private final CoreAtlasManagementMapper coreAtlasManagementMapper;

    private final CoreAtlasManagementDetailsMapper coreAtlasManagementDetailsMapper;

    /**
     * 查询作品评论列表数据
     *
     * @param atlasDetailsCommentSelectPageDTO 图集作品评论列表请求类
     * @return com.yida.data.common.core.common.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < com.yida.data.atlas.vo.AtlasDetailsCommentSelectPageVO>>
     * @author ZYJ
     * @date 2021/10/13 11:08
     */
    @Override
    public ResultBean<IPage<AtlasDetailsCommentSelectPageVO>> listAtlasDetailsCommentPage(AtlasDetailsCommentSelectPageDTO atlasDetailsCommentSelectPageDTO) {
        IPage<AtlasDetailsCommentSelectPageVO> iPage = this.baseMapper.listAtlasDetailsCommentPage(atlasDetailsCommentSelectPageDTO.toPage(), atlasDetailsCommentSelectPageDTO);
        return ResultBean.buildSuccess(iPage);
    }

    /**
     * 获取图集作品评论详情
     *
     * @param commentId 评论id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.atlas.vo.AtlasDetailsCommentInfoVO>
     * @author ZYJ
     * @date 2021/10/13 14:43
     */
    @Override
    public ResultBean<AtlasDetailsCommentInfoVO> getAtlasDetailsCommentInfo(Long commentId) {
        // 获取图集评论详情数据
        CoreAtlasManagementDetailsComment coreAtlasManagementDetailsComment = this.getById(commentId);
        AtlasDetailsCommentInfoVO atlasDetailsCommentInfoVO = new AtlasDetailsCommentInfoVO();
        Optional.ofNullable(coreAtlasManagementDetailsComment).ifPresent(comment ->
                BeanUtils.copyProperties(comment, atlasDetailsCommentInfoVO));
        return ResultBean.buildSuccess(atlasDetailsCommentInfoVO);
    }

    /**
     * 批量删除作品评论
     *
     * @param ids 评论id集合
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/10/13 15:06
     */
    @Override
    public ResultBean<String> deleteCommentBatch(Long[] ids) {
        // 删除对应图集和图集作品的评论数
        if (ArrayUtils.isNotEmpty(ids)) {
            Integer changeCount = 0;
            Long detailsId = null;

            for (Long commentId : ids) {
                // 图集作品id
                CoreAtlasManagementDetailsComment comment = this.getById(commentId);
                if (Objects.nonNull(comment) && CheckStatusEnum.CHECK_PASS_STATUS.getStatus().equals(comment.getCheckStatus())) {
                    detailsId = comment.getCoreAtlasManagementDetailsId();
                    changeCount--;
                }
            }
            if (Objects.nonNull(detailsId)) {
                // 减少图集图片或视频的总评论数
                this.coreAtlasManagementDetailsMapper.updateCommentCount(detailsId, changeCount);
                // 减少图集的总评论数
                CoreAtlasManagementDetails coreAtlasManagementDetails = this.coreAtlasManagementDetailsMapper.selectById(detailsId);
                if (Objects.nonNull(coreAtlasManagementDetails)) {
                    this.coreAtlasManagementMapper.updateCommentCount(coreAtlasManagementDetails.getCoreAtlasManagementId(), changeCount);
                }
            }
        }
        // 批量删除作品评论
        this.removeByIds(Arrays.asList(ids));
        return ResultBean.buildSuccess();
    }

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

        // 审核通过添加评论数
        if (CheckStatusEnum.CHECK_PASS_STATUS.getStatus().equals(checkCommentDTO.getCheckStatus())) {
            // 获取图集作品id
            Long detailsId = this.getById(checkCommentDTO.getId()).getCoreAtlasManagementDetailsId();
            // 增加图集图片或视频的总评论数
            this.coreAtlasManagementDetailsMapper.updateCommentCount(detailsId, 1);
            // 增加图集的总评论数
            CoreAtlasManagementDetails coreAtlasManagementDetails = this.coreAtlasManagementDetailsMapper.selectById(detailsId);
            if (Objects.nonNull(coreAtlasManagementDetails)) {
                this.coreAtlasManagementMapper.updateCommentCount(coreAtlasManagementDetails.getCoreAtlasManagementId(), 1);
            }
        }
        return ResultBean.buildSuccess();
    }
}
