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.mapper.PostCollectionsMapper;
import com.ruoyi.system.mapper.PostCollectionsSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

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

@Configuration
@EnableScheduling
public class SpringTaskConfig {
    @Autowired
    private PostCollectionsSystemMapper postCollectionsSystemMapper;
    @Autowired
    private PostCollectionsMapper postCollectionsMapper;

    /**
     * 每10秒查询数据库修改type的状态
     */
    @Scheduled(cron = "0/10 * * * * ?")
    private void cron() {
        Date nowDate = DateUtils.getNowDate();
        PostCollectionsSystem postCollectionsSystem = new PostCollectionsSystem();
        List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemMapper.selectPostCollectionsSystemList(postCollectionsSystem);
        postCollectionsSystemList = postCollectionsSystemList.stream().map((item) -> {
            Date startTime = item.getStartTime();
            Date endTime = item.getEndTime();
            if (null != startTime && null != endTime) {
                if (nowDate.before(startTime)) {
                    item.setType((long) 1);
                } else if (nowDate.after(endTime)) {
                    item.setType((long) 2);
                } else {
                    item.setType((long) 0);
                }
            }
            return item;
        }).collect(Collectors.toList());
        postCollectionsSystemMapper.updateBatchById(postCollectionsSystemList);
    }
    /**
     * 每10秒查询数据库藏品套系的时间，根据套系时间自动修改藏品状态
     */
    @Scheduled(cron = "0/10 * * * * ?")
    private void cronA() {
        Date nowDate = DateUtils.getNowDate();
        PostCollectionsSystem postCollectionsSystem = new PostCollectionsSystem();
        List<PostCollectionsSystem> postCollectionsSystemList = postCollectionsSystemMapper.selectPostCollectionsSystemList(postCollectionsSystem);
        postCollectionsSystemList = postCollectionsSystemList.stream().map((item) -> {
            Long systemId = item.getId();
            Date startTime = item.getStartTime();
            Date endTime = item.getEndTime();
            return item;
        }).collect(Collectors.toList());
        for (int i = 0; i < postCollectionsSystemList.size(); i++) {
            Date startTime = postCollectionsSystemList.get(i).getStartTime();
            Date endTime = postCollectionsSystemList.get(i).getEndTime();
            List<PostCollections> postCollectionsList = postCollectionsMapper.selectPostCollectionsList(postCollectionsSystemList.get(i).getId());
            for (int j = 0; j < postCollectionsList.size(); j++) {
                PostCollections postCollections = new PostCollections();
                postCollections.setId(postCollectionsList.get(j).getId());
                postCollections.setSystemId(postCollectionsSystemList.get(i).getId());
                postCollections.setUpdateTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, DateUtils.getTime()));

                if (null!=startTime&&null!=endTime){
                    if (nowDate.before(startTime)){
                        postCollections.setStatus("0");
                    }
                    if (nowDate.after(endTime)){
                        postCollections.setStatus("3");
                    }
                    if(startTime.before(nowDate)&&endTime.after(nowDate)&&
                            null!=postCollectionsList.get(j).getCollectionsNumber()&&
                            postCollectionsList.get(j).getCollectionsNumber() > 0 ) {
                        postCollections.setStatus("2");
                    }
                    if(startTime.before(nowDate)&&endTime.after(nowDate)&&
                            null!=postCollectionsList.get(j).getCollectionsNumber()&&
                            postCollectionsList.get(j).getCollectionsNumber() == 0 ) {
                        postCollections.setStatus("1");
                    }
                }
                postCollectionsMapper.updatePostCollectionsBySysetmIdAndId(postCollections);
            }

        }

    }
}