package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PostCollectionsSystem;
import com.ruoyi.system.domain.vo.CollectionsVo;
import com.ruoyi.system.mapper.PostCollectionsMapper;
import com.ruoyi.system.mapper.PostCollectionsSystemMapper;
import com.ruoyi.system.service.IPostCollectionsSystemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 藏品套系Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-15
 */
@Transactional//涉及两张表要加事务
@Service
public class PostCollectionsSystemServiceImpl implements IPostCollectionsSystemService
{

    @Autowired
    private PostCollectionsSystemMapper postCollectionsSystemMapper;

    @Autowired
    private PostCollectionsMapper postCollectionsMapper;

    /**
     * 查询藏品套系
     *
     * @param id 藏品套系主键
     * @return 藏品套系
     */
    @Override
    public PostCollectionsSystem selectPostCollectionsSystemById(Long id)
    {
        return postCollectionsSystemMapper.selectPostCollectionsSystemById(id);
    }

    /**
     * 查询藏品套系列表
     *
     * @param postCollectionsSystem 藏品套系
     * @return 藏品套系
     */
    @Override
    public List<PostCollectionsSystem> selectPostCollectionsSystemList(PostCollectionsSystem postCollectionsSystem)
    {
        return postCollectionsSystemMapper.selectPostCollectionsSystemList(postCollectionsSystem);
    }

    /**
     * 新增藏品套系
     *
     * @param postCollectionsSystem 藏品套系
     * @return 结果
     */
    @Override
    public int insertPostCollectionsSystem(PostCollectionsSystem postCollectionsSystem)
    {
        /**
         * 判断时间 （在售0/预售1/已过期2）
         * 根据当前时间，对比套系时间，当前小于套系时间，该套系藏品为“以过期”，
         * 当前时间大于套系时间为“预售”
         */
        Date nowDate = DateUtils.getNowDate();
        Date startTime = postCollectionsSystem.getStartTime();
        Date endTime = postCollectionsSystem.getEndTime();
        //当前时间早于套系时间 ->预售
        if (null!=startTime && null!=endTime){
            if (nowDate.before(startTime)){
                postCollectionsSystem.setType((long) 1);
            }
            else if (nowDate.after(endTime)){
                postCollectionsSystem.setType((long) 2);
            }
            else {
                postCollectionsSystem.setType((long) 0);
            }
        }
        //补充字段
        postCollectionsSystem.setCreateBy(getUsername());
        postCollectionsSystem.setCreateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        postCollectionsSystem.setUpdateBy(getUsername());
        postCollectionsSystem.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        return postCollectionsSystemMapper.insertPostCollectionsSystem(postCollectionsSystem);
    }

    /**
     * 修改藏品套系
     *
     * @param postCollectionsSystem 藏品套系
     * @return 结果
     */
    @Override
    public int updatePostCollectionsSystem(PostCollectionsSystem postCollectionsSystem)
    {
        //补充字段
        postCollectionsSystem.setUpdateBy(getUsername());
        postCollectionsSystem.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,DateUtils.getTime()));
        /**
         * 判断时间 （在售0/预售1/已过期2）
         * 根据当前时间，对比套系时间，当前小于套系时间，该套系藏品为“以过期”，
         * 当前时间大于套系时间为“预售”
         */
        String status = postCollectionsSystemMapper.selectPostCollectionsSystemByStatus(postCollectionsSystem.getId());
        //未上架什么都可以改
        int row=0;
        if(status.equals("0")) {
            Date nowDate = DateUtils.getNowDate();
            Date startTime = postCollectionsSystem.getStartTime();
            Date endTime = postCollectionsSystem.getEndTime();
            //当前时间早于套系时间 ->预售
            if (null != startTime && null != endTime) {
                if (nowDate.before(startTime)) {
                    postCollectionsSystem.setType((long) 1);
                } else if (nowDate.after(endTime)) {
                    postCollectionsSystem.setType((long) 2);
                } else {
                    postCollectionsSystem.setType((long) 0);
                }
            }
            row=postCollectionsSystemMapper.updatePostCollectionsSystem(postCollectionsSystem);
        }
        //已上架“时间”只允许修改截止时间，且只能可以向当前截止时间之后设置
        else {
            Date originEndTime=postCollectionsSystemMapper.getEndTime(postCollectionsSystem.getId());
            Date endTime = postCollectionsSystem.getEndTime();
            if(endTime.after(originEndTime)) {
                row = postCollectionsSystemMapper.shelvesUpdatePostCollectionsSystem(postCollectionsSystem);
            }
        }
        return row;
    }

    /**
     * 批量删除藏品套系
     *
     * @param ids 需要删除的藏品套系主键
     * @return 结果
     */
    @Override
    public int deletePostCollectionsSystemByIds(Long[] ids) {


        List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemMapper.selectListEqStatus(ids);
        if (postCollectionsSystemList.size()!= 0){
            throw new BaseException("套系已上架");
        }

        postCollectionsMapper.deletePostCollectionsBySystemIds(ids);
        return postCollectionsSystemMapper.deletePostCollectionsSystemByIds(ids);
    }

    /**
     * 删除藏品套系信息
     *
     * @param id 藏品套系主键
     * @return 结果
     */
    @Override
    public int deletePostCollectionsSystemById(Long id)
    {
        return postCollectionsSystemMapper.deletePostCollectionsSystemById(id);
    }

    /**
     * 套系搜索功能
     * @return
     */
    @Override
    public List<PostCollectionsSystem> selectByTitleAndTime(String title, Date TimeLeft, Date TimeRight) {
        if (title != null) {
            //标题不为空则判断时间
            if (TimeLeft != null && TimeRight != null) {
                if( TimeLeft.before(TimeRight)) {
                    return postCollectionsSystemMapper.selectPostListByTitleOrTime(title, TimeLeft, TimeRight);
                }else {
                    return null;
                }
            }else {
                //时间为空
                return postCollectionsSystemMapper.selectPostListByTitleOrTime(title, TimeLeft, TimeRight);

            }
        } else {
            //标题为空
            //确保查询到的公告为已发布的公告
            if (TimeLeft != null && TimeRight != null) {
                //时间不为空 ，且左时间在右时间之前
                if( TimeLeft.before(TimeRight)) {
                    return postCollectionsSystemMapper.selectPostListByTitleOrTime(title, TimeLeft, TimeRight);
                }else {
                    return null;
                }
            }
        }
        PostCollectionsSystem post = new PostCollectionsSystem();
        return postCollectionsSystemMapper.selectPostCollectionsSystemList(post);
    }

    /**
     *  获取该套系下 藏品数量
     * @param id
     * @return
     */
    @Override
    public int getCopies(Long id) {
        return postCollectionsSystemMapper.getCopiesById(id);
    }

    @Override
    public Date getEndTime(Long id) {
        return postCollectionsSystemMapper.getEndTime(id);
    }


    @Override
    public List<CollectionsVo> selectPostCollectionsSystemListPage(PostCollectionsSystem postCollectionsSystem) {
        List<PostCollectionsSystem> list = postCollectionsSystemMapper.selectPostCollectionsSystemList(postCollectionsSystem);
        List<CollectionsVo> collectionsVos=list.stream().map((item)->{
            // 创建 vo实体
            CollectionsVo collectionsVo=new CollectionsVo();
            BeanUtils.copyProperties(item,collectionsVo);
            //  获取套系id
            Long id = item.getId();
            //根据id 查询该套系藏品数量
            Integer copiesById = postCollectionsSystemMapper.getCopiesById(id);
            collectionsVo.setCopies(copiesById);
            return collectionsVo;
        }).collect(Collectors.toList());
        return collectionsVos;
    }

    @Override
    public int selectPostCollectionsSystemByName(PostCollectionsSystem postCollectionsSystem) {
        return postCollectionsSystemMapper.selectPostCollectionsSystemByName(postCollectionsSystem);
    }

    @Override
    public List<PostCollectionsSystem> listByIds(List<Long> ids) {
        return postCollectionsSystemMapper.selectPostCollectionsSystemByIds(ids);
    }

    @Override
    public int updateBatchById(List<PostCollectionsSystem> postCollectionsSystemList) {
        return postCollectionsSystemMapper.updateBatchById(postCollectionsSystemList);
    }
}
