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.atlas.CoreAtlasManagement;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.school.atlas.mapper.CoreAtlasManagementMapper;
import com.yida.data.school.atlas.service.CoreAtlasManagementService;
import com.yida.data.school.dto.atlas.AtlasSaveDTO;
import com.yida.data.school.dto.atlas.AtlasSelectPageDTO;
import com.yida.data.school.vo.atlas.AtlasDetailVO;
import com.yida.data.school.vo.atlas.AtlasSelectPageVO;

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/9/2
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CoreAtlasManagementServiceImpl
        extends ServiceImpl<CoreAtlasManagementMapper, CoreAtlasManagement>
        implements CoreAtlasManagementService {

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

    /**
     * 保存图集数据
     *
     * @param atlasSaveDTO 保存图集请求类
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/9/2 13:33
     */
    @Override
    public ResultBean<String> saveAtlas(AtlasSaveDTO atlasSaveDTO) {
        CoreAtlasManagement coreAtlasManagement = new CoreAtlasManagement();
        BeanUtils.copyProperties(atlasSaveDTO, coreAtlasManagement);
        // 新增
        if (!Optional.ofNullable(Objects.requireNonNull(coreAtlasManagement).getId()).isPresent()) {
            // 设置新增数据
            coreAtlasManagement.setCreateDate(LocalDateTime.now());
            coreAtlasManagement.setCreateId(Objects.requireNonNull(FebsUtil.getCurrentUser()).getUserId());
        } else {
            // 设置修改数据
            coreAtlasManagement.setUpdateDate(LocalDateTime.now());
            coreAtlasManagement.setUpdateId(Objects.requireNonNull(FebsUtil.getCurrentUser()).getUserId());
        }
        this.saveOrUpdate(coreAtlasManagement);
        return ResultBean.buildSuccess();
    }

    /**
     * 获取图集详情
     *
     * @param atlasId 图集id
     * @return com.yida.data.common.core.common.ResultBean<com.yida.data.atlas.vo.AtlasDetailVO>
     * @author ZYJ
     * @date 2021/9/2 13:49
     */
    @Override
    public ResultBean<AtlasDetailVO> getAtlasInfo(Long atlasId) {
        // 获取图集详情
        CoreAtlasManagement coreAtlasManagement = this.getById(atlasId);
        AtlasDetailVO atlasDetailVO = new AtlasDetailVO();
        Optional.ofNullable(coreAtlasManagement).ifPresent(atlas -> BeanUtils.copyProperties(atlas, atlasDetailVO));
        return ResultBean.buildSuccess(atlasDetailVO);
    }

    /**
     * 批量删除图集
     *
     * @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> deleteAtlasBatch(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));
        return ResultBean.buildSuccess();
    }

    /**
     * 启用或停用图集
     *
     * @param atlasId      图集id
     * @param enableStatus 需要设置的启用或停用状态
     * @return com.yida.data.common.core.common.ResultBean<java.lang.String>
     * @author ZYJ
     * @date 2021/9/2 14:35
     */
    @Override
    public ResultBean<String> updateEnableStatus(Long atlasId, Integer enableStatus) {
        CoreAtlasManagement coreAtlasManagement = new CoreAtlasManagement();
        coreAtlasManagement.setId(atlasId);
        coreAtlasManagement.setEnableStatus(enableStatus);
        coreAtlasManagement.setUpdateId(Objects.requireNonNull(FebsUtil.getCurrentUser()).getUserId());
        coreAtlasManagement.setUpdateDate(LocalDateTime.now());
        // 启用或停用图集
        this.saveOrUpdate(coreAtlasManagement);
        return ResultBean.buildSuccess();
    }
}
