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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.atlas.CoreAtlasManagementDetails;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementDetailsMapper;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementMapper;
import com.yida.data.school.atlas.service.CoreAtlasManagementDetailsService;
import com.yida.data.school.dto.atlas.AtlasDetailsSaveDTO;
import com.yida.data.school.dto.atlas.AtlasDetailsSelectPageDTO;
import com.yida.data.school.dto.atlas.app.AppAtlasWorkListDTO;
import com.yida.data.school.vo.atlas.AtlasDetailsSelectPageVO;
import com.yida.data.school.vo.atlas.AtlasDetailsVO;
import com.yida.data.school.vo.atlas.app.AppAtlasWorkBaseVO;

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.List;
import java.util.Objects;
import java.util.Optional;

import lombok.RequiredArgsConstructor;

/**
 * 学校图集作品管理service层
 *
 * @author ZYJ
 * @date 2021/9/2
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CoreAtlasManagementDetailsServiceImpl
        extends ServiceImpl<CoreAtlasManagementDetailsMapper, CoreAtlasManagementDetails>
        implements CoreAtlasManagementDetailsService {

    private final CoreAtlasManagementMapper coreAtlasManagementMapper;

    /**
     * 查询图集作品列表数据
     *
     * @param atlasDetailsSelectPageDTO 图集作品后台列表请求类
     * @return com.yida.data.common.core.common.ResultBean<com.baomidou.mybatisplus.core.metadata.IPage < com.yida.data.atlas.vo.AtlasDetailsSelectPageVO>>
     * @author ZYJ
     * @date 2021/9/2 11:11
     */
    @Override
    public ResultBean<IPage<AtlasDetailsSelectPageVO>> listAtlasDetailsPage(AtlasDetailsSelectPageDTO atlasDetailsSelectPageDTO) {
        IPage<AtlasDetailsSelectPageVO> iPage = this.baseMapper.listAtlasDetailsPage(atlasDetailsSelectPageDTO.toPage(), atlasDetailsSelectPageDTO);
        return ResultBean.buildSuccess(iPage);
    }

    /**
     * 保存图集作品数据
     *
     * @param list 保存图集作品请求类集合
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/9/2 13:33
     */
    @Override
    public ResultBean<String> saveAtlasDetails(List<AtlasDetailsSaveDTO> list) {

        list.forEach(atlasDetailsSaveDTO -> {
            CoreAtlasManagementDetails coreAtlasManagementDetails = new CoreAtlasManagementDetails();
            BeanUtils.copyProperties(atlasDetailsSaveDTO, coreAtlasManagementDetails);
            // 新增
            if (!Optional.ofNullable(coreAtlasManagementDetails.getId()).isPresent()) {
                // 设置新增数据
                coreAtlasManagementDetails.setCreateDate(LocalDateTime.now());
                coreAtlasManagementDetails.setCreateId(Objects.requireNonNull(FebsUtil.getCurrentUser()).getUserId());
            } else {
                // 设置修改数据
                coreAtlasManagementDetails.setUpdateDate(LocalDateTime.now());
                coreAtlasManagementDetails.setUpdateId(Objects.requireNonNull(FebsUtil.getCurrentUser()).getUserId());
            }
            this.saveOrUpdate(coreAtlasManagementDetails);
        });
        return ResultBean.buildSuccess();
    }

    /**
     * 获取图集作品数据
     *
     * @param atlasDetailsId 图集作品id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.atlas.vo.AtlasDetailsVO>
     * @author ZYJ
     * @date 2021/9/2 13:49
     */
    @Override
    public ResultBean<AtlasDetailsVO> getAtlasDetailsInfo(Long atlasDetailsId) {
        // 获取图集详情数据
        CoreAtlasManagementDetails coreAtlasManagementDetails = this.getById(atlasDetailsId);
        AtlasDetailsVO atlasDetailsVO = new AtlasDetailsVO();
        Optional.ofNullable(coreAtlasManagementDetails).ifPresent(details -> BeanUtils.copyProperties(details, atlasDetailsVO));
        return ResultBean.buildSuccess(atlasDetailsVO);
    }

    /**
     * 批量删除图集作品
     *
     * @param ids 数据id集合
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/9/2 13:56
     */
    @Override
    public ResultBean<String> deleteAtlasDetailsBatch(Long[] ids) {
        // 需要删除对应的点赞数和评论数
        if (ArrayUtils.isNotEmpty(ids)) {
            int likeCount = 0;
            int commentCount = 0;
            Long atlasId = null;

            for (Long detailsId : ids) {
                CoreAtlasManagementDetails coreAtlasManagementDetails = this.getById(detailsId);
                if (Objects.nonNull(coreAtlasManagementDetails)) {
                    likeCount = likeCount - coreAtlasManagementDetails.getLikeNum();
                    commentCount = commentCount - coreAtlasManagementDetails.getCommentNum();
                    atlasId = coreAtlasManagementDetails.getCoreAtlasManagementId();
                }
            }
            // 减少图集的总评论数和总点赞数
            if (Objects.nonNull(atlasId)) {
                this.coreAtlasManagementMapper.updateCommentCount(atlasId, commentCount);
                this.coreAtlasManagementMapper.updateLikeCount(atlasId, likeCount);
            }
        }
        this.removeByIds(Arrays.asList(ids));
        return ResultBean.buildSuccess();
    }

    /**
     * 根据类型查询图集作品分页数据接口
     *
     * @param toPage              分页数据
     * @param appAtlasWorkListDTO app根据类型查询图集作品分页数据请求类
     * @return com.baomidou.mybatisplus.core.metadata.IPage<com.yida.data.atlas.vo.app.AppAtlasWorkBaseVO>
     * @author ZYJ
     * @date 2021/9/6 17:35
     */
    @Override
    public IPage<AppAtlasWorkBaseVO> listAtlasWorkByType(Page<CoreAtlasManagementDetails> toPage,
                                                         AppAtlasWorkListDTO appAtlasWorkListDTO) {
        return this.baseMapper.listAtlasWorkByType(toPage, appAtlasWorkListDTO);
    }
}
