package com.ruoyi.system.utils;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.PostCollections;
import com.ruoyi.system.domain.PostCollectionsSystem;
import com.ruoyi.system.domain.vo.PostCollectionsVo;
import com.ruoyi.system.mapper.PostCollectionsMapper;
import com.ruoyi.system.mapper.PostCollectionsSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Objects;

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

@Component
public class CollectionStatusJudgment {

    private static PostCollectionsSystemMapper postCollectionsSystemMapper;

    private static PostCollectionsMapper postCollectionsMapper;

    @Autowired
    public void setPostCollectionsSystemMapper(PostCollectionsSystemMapper postCollectionsSystemMapper) {
        CollectionStatusJudgment.postCollectionsSystemMapper = postCollectionsSystemMapper;
    }

    @Autowired
    public void setPostCollectionsMapper(PostCollectionsMapper postCollectionsMapper) {
        CollectionStatusJudgment.postCollectionsMapper = postCollectionsMapper;
    }

    /**
     * 如果已经上架，判断藏品状态时间
     *
     * @param postCollectionsVo
     * @return 结果
     */
    public static void JudgmentTime(PostCollectionsVo postCollectionsVo) {

        Date nowDate = DateUtils.getNowDate();
        Date startTime = postCollectionsVo.getStartTime();
        Date endTime = postCollectionsVo.getEndTime();
        //获取此藏品套系status
        Long systemId = postCollectionsVo.getSystemId();
        String postCollectionsSystemStatus = postCollectionsSystemMapper.selectPostCollectionsSystemByStatus(systemId);

        if (Objects.equals(postCollectionsSystemStatus, "1")) {
            //判空
            if (null != startTime && null != endTime) {

                /**
                 * 判断时间 （0预售 /1已售尽 /2正在售卖 /3已过期）
                 * 根据当前时间，对比售卖时间，
                 * 当前时间小于售卖时间，该套系藏品为预售，
                 * 当前时间大于售卖时间为已过期，
                 * 当前时间在售卖时间之间并且数量大于0为正在售卖
                 * 其余情况已售尽
                 */
                if (nowDate.before(startTime)) {
                    postCollectionsVo.setStatus("0");//预售
                } else if (nowDate.after(endTime)) {
                    postCollectionsVo.setStatus("3");//已过期
                } else if (startTime.before(nowDate) &&
                        endTime.after(nowDate) &&
                        postCollectionsVo.getCollectionsNumber() > 0) {
                    postCollectionsVo.setStatus("2");//正在售卖
                } else if (postCollectionsVo.getCollectionsNumber() == 0) {
                    postCollectionsVo.setStatus("1");//售尽
                }
            }
        }

    }

    /**
     * 如果未上架，令藏品状态为未上架
     * 如果已上架，根据时间线判断藏品状态
     * @param systemId
     * @return 结果
     */
    public static void JudgmentStatus(List<Long> systemId,String status) {
        PostCollections postCollections = new PostCollections();
        List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemMapper.selectPostCollectionsSystemByIdList(systemId);
        //判断藏品套系是否上架，如果没上架，令藏品status全部置为未上架
        for (int i = 0; i < postCollectionsSystemList.size(); i++) {
            //判断套系是否status=0
            if (Objects.equals(status, "0")) {
                postCollections.setUpdateBy(getUsername());
                postCollections.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getTime()));
                postCollections.setSystemId(postCollectionsSystemList.get(i).getId());
                //批量修改status,动态查询藏品列表
                postCollectionsMapper.updatePostCollectionsStatus(postCollections);
            } else {
                //上架
                Long id = postCollectionsSystemList.get(i).getId();
                List<PostCollections> postCollectionsList = postCollectionsMapper.selectPostCollectionsList(id);//获取藏品列表根据套系id
                for (int j = 0; j < postCollectionsList.size(); j++) {
                    postCollectionsList.get(j).setUpdateBy(getUsername());
                    postCollectionsList.get(j).setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getTime()));
                    postCollectionsList.get(j).setSystemId(postCollectionsSystemList.get(i).getId());
                    //获取时间线
                    Date nowDate = DateUtils.getNowDate();
                    Date startTime = postCollectionsList.get(j).getStartTime();
                    Date endTime = postCollectionsList.get(j).getEndTime();
                    //判空
                    if (null != startTime && null != endTime) {
                        if (nowDate.before(startTime)) {
                            postCollectionsList.get(j).setStatus("0");//预售
                        }else if (nowDate.after(endTime)) {
                            postCollectionsList.get(j).setStatus("3");//已过期
                        }else if (startTime.before(nowDate) &&
                                endTime.after(nowDate) &&
                                null!=postCollectionsList.get(j).getCollectionsNumber()&&
                                postCollectionsList.get(j).getCollectionsNumber() > 0) {
                            postCollectionsList.get(j).setStatus("2");//正在售卖
                        } else if (startTime.before(nowDate) &&
                                endTime.after(nowDate) &&
                                null!=postCollectionsList.get(j).getCollectionsNumber()&&
                                postCollectionsList.get(j).getCollectionsNumber() == 0) {
                            postCollectionsList.get(j).setStatus("1");//售尽
                        }
                    }
                    postCollectionsMapper.updatePostCollectionsStatus1(postCollectionsList.get(j));
                }
            }
        }
    }
}