package com.ruoyi.web.runner;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.buyerapi.domain.BuyerCompanyInfo;
import com.ruoyi.buyerapi.domain.BuyerOrderRecord;
import com.ruoyi.buyerapi.domain.BuyerProject;
import com.ruoyi.buyerapi.mapper.BuyerCompanyInfoMapper;
import com.ruoyi.buyerapi.mapper.BuyerProjectMapper;
import com.ruoyi.buyerapi.service.IBuyerCompanyInfoService;
import com.ruoyi.buyerapi.service.IBuyerOrderRecordService;
import com.ruoyi.buyerapi.service.IBuyerProjectService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

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

/**
 * 项目启动完毕，就回调的方法
 *
 * @author: zhangyu
 * @date: 2022/9/24
 */
@Component
@EnableScheduling
public class MyCommandLineRunner implements CommandLineRunner {
    @Autowired
    private IBuyerOrderRecordService buyerOrderRecordService;

    @Autowired
    private BuyerCompanyInfoMapper buyerCompanyInfoMapper;

    @Autowired
    private BuyerProjectMapper buyerProjectMapper;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("项目启动完毕，执行回调函数");
        refreshOrder();
//        refreshProject();

    }

    /**
     * [秒] [分] [小时] [日] [月] [周] [年]
     */
    @Scheduled(cron = "0 0/1 * * * ?")
    public void refreshOrder() {
        BuyerOrderRecord buyerOrderRecord = new BuyerOrderRecord();
        // 查询所有待支付的订单
        buyerOrderRecord.setOrderStatus("0");
        List<BuyerOrderRecord> buyerOrderRecords = buyerOrderRecordService.selectBuyerOrderRecordList(buyerOrderRecord);
        buyerOrderRecords.forEach(buyerOrderRecordEntiy -> {
            // 设置订单15分钟之后过期
            Date closeDate = DateUtil.offset(buyerOrderRecordEntiy.getCreateTime(), DateField.MINUTE, 15);
            Date nowDate = DateUtils.getNowDate();
            int compare = DateUtil.compare(closeDate, nowDate);
            // 订单已超时，更新订单状态
            if (compare < 0) {
                buyerOrderRecord.setId(buyerOrderRecordEntiy.getId());
                buyerOrderRecord.setOrderStatus("2");
                buyerOrderRecordService.updateBuyerOrderRecord(buyerOrderRecord);
            }
        });

        // 自动检查企业会员是否过期
        List<BuyerCompanyInfo> companyInfos = buyerCompanyInfoMapper.selectBuyerCompanyInfoList(new BuyerCompanyInfo());
        companyInfos.forEach(buyerCompanyInfo -> {
            Date expireTime = buyerCompanyInfo.getExpireTime();
            if (expireTime == null) {
                expireTime = DateUtils.getNowDate();
            }
            Date nowDate = DateUtils.getNowDate();
            // 过期时间和当日时间比较
            int compare = DateUtil.compare(nowDate, expireTime);
            if (compare >= 0) {
                buyerCompanyInfo.setIsExpire("1");
                // 会员已过期，修改会员状态
                buyerCompanyInfoMapper.updateBuyerCompanyInfo(buyerCompanyInfo);
            }
        });

        // 更新项目状态
        List<BuyerProject> buyerProjectList = buyerProjectMapper.selectBuyerProjectList(new BuyerProject());
        buyerProjectList.forEach(buyerProject -> {
            try{
                Date nowDate = DateUtil.date();
                Date projectStartDate = buyerProject.getStartDate();
                Date projectendDate = buyerProject.getEndDate();
                if (projectStartDate != null && projectendDate != null) {
                    // 当前时间大于项目开始时间小于项目结束时间并且项目状态不是已完成或终止
                    if (nowDate.getTime() > projectStartDate.getTime() && nowDate.getTime() < projectendDate.getTime() && !buyerProject.getStatus().equals("1") && !buyerProject.getStatus().equals("3")) {
                        buyerProject.setStatus("0");
                        buyerProjectMapper.updateBuyerProject(buyerProject);
                    }
                    // 当前时间大于项目结束时间并且项目状态不是已完成或终止
                    else if (nowDate.getTime() > projectendDate.getTime() && !buyerProject.getStatus().equals("1") && !buyerProject.getStatus().equals("3")) {
                        buyerProject.setStatus("2");
                        buyerProjectMapper.updateBuyerProject(buyerProject);
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("项目时间有误！！");
            }
        });

        // 每天凌晨更新项目信息
        String nowTime = DateUtil.now().substring(11);
        if (nowTime.equals("23:59:59")) {
            BuyerProject buyerProject = new BuyerProject();
            // 查询所有项目
            List<BuyerProject> buyerProjects = buyerProjectMapper.selectBuyerProjectList(buyerProject);
            buyerProjects.forEach(buyerProjectEntity -> {
                // 更新过程文件状态
                if (buyerProjectEntity.getCloseStatu().equals("0")) {
                    buyerProjectEntity.setTodayStatu("0");
                    buyerProjectMapper.updateBuyerProject(buyerProjectEntity);
                }
                // 更新验收资料状态
                if (buyerProjectEntity.getCloseStatus().equals("0")) {
                    buyerProjectEntity.setTodayStatus("0");
                    buyerProjectMapper.updateBuyerProject(buyerProjectEntity);
                }
            });
        }
    }

    /**
     * [秒] [分] [小时] [日] [月] [周] [年]
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void refreshProject() {
        BuyerProject buyerProject = new BuyerProject();
        // 查询所有项目
        List<BuyerProject> buyerProjects = buyerProjectMapper.selectBuyerProjectList(buyerProject);
        buyerProjects.forEach(buyerProjectEntity -> {
            // 更新过程文件状态
            if (buyerProjectEntity.getCloseStatu().equals("0")) {
                buyerProjectEntity.setTodayStatu("0");
                buyerProjectMapper.updateBuyerProject(buyerProjectEntity);
            }
            // 更新验收资料状态
            if (buyerProjectEntity.getCloseStatus().equals("0")) {
                buyerProjectEntity.setTodayStatus("0");
                buyerProjectMapper.updateBuyerProject(buyerProjectEntity);
            }
        });
    }

}
