package com.ruoyi.quartz.task;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.SubPreNewMapper;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.SubInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

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

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {

    @Autowired
    private ITenderService tenderService;
    @Autowired
    private ITenderLogService tenderLogService;
    @Autowired
    private ISubPreNewService subPreNewService;
    @Autowired
    private ISubFlowService subFlowService;
    @Autowired
    private ISubInfoService subInfoService;


    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params) {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams() {
        //this.syncTender();
        //this.syncTenderLog();
    }


    public void syncTenderLog() {
        //查询没有处理的招投标信息
        List<TenderLog> list = tenderLogService.listTask();
        /*        this.addTenderLog(tender);*/
        //处理招标任务
        for (TenderLog tenderLog : list) {
            this.addTenderLog(tenderLog);
       /*     switch (tenderLog.getState()) {
                case "1": //处理新增的
                    this.addTenderLog(tender);
                    break;
*//*                case "2": //处理废标的
                    this.editTender(tender);
                    break;
                case "3": //处理删除的
                    this.editTender(tender);
                    break;*//*
            }*/

        }
        //任务处理成功,没有返回异常将记录置为已处理
        System.out.println("执行无参方法");
    }

    private void addTenderLog(TenderLog tenderLog) {
        //批复文件名称
        SubInfo subInfo = subInfoService.findOne(new LambdaQueryWrapper<SubInfo>().eq(SubInfo::getSubName, tenderLog.getTitle()));
        if (subInfo == null) {
            //将需要手动处理的放入到正式库里面
            tenderLogService.saveOrUpdateSlave(tenderLog);
        } else {
   /*         SubPreNew subPreNew = null;
            subPreLog = subPreNewService.findOne(new LambdaQueryWrapper<SubPreNew>().eq(SubPreNew::getSubId, subInfo.getId()).eq(SubPreNew::getPreFlowId, "16").eq(SubPreNew::getLogicDeleteFlag, 0));
            if (subPreNew == null) {
                subPreNew = new SubPreNew();
                subPreNew.setId(UUID.randomUUID().toString());
            }
            subPreNew.setSubId(subInfo.getId());
            subPreNew.setPreFlowId("16");
            subPreNew.setCreateTime(new Date());
            subPreNew.setFileAddre(tender.getFileUrl());
            subPreNew.setDatePlan(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", tender.getSiteDate()));
            subPreNew.setDateConfirm(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", tender.getPublishDate()));
            subPreNew.setTransactors(tender.getUser());
            subPreNew.setTransactorsPhone(tender.getPhone());
            subPreNew.setLogicDeleteFlag(0);
            subPreNew.setStatusConf("1"); //设置正在办理
            if (StringUtils.isNotBlank(tender.getPublishDate())) {
                subPreNew.setStatusConf("2");//设置已经办理
                subPreNew.setNum3(Long.valueOf(DateUtil.between(subPreNew.getDateConfirm(), subPreNew.getDatePlan(), DateUnit.DAY)).intValue());
            }
            subPreNewService.saveOrUpdate(subPreNew);
            if (StringUtils.isNotBlank(tender.getPublishDate())) {
                subInfo.setStatus("7");
                subInfoService.saveOrUpdate(subInfo);
            }*/
        }
        tenderLog.setIsHandle("2");
        tenderLogService.saveOrUpdate(tenderLog);
    }


    public void syncTender() {
        //查询没有处理的招投标信息
        List<Tender> list = tenderService.listTask();
        //处理招标任务
        for (Tender tender : list) {
            switch (tender.getState()) {
                case "1": //处理新增的
                    this.addTender(tender);
                    break;
                case "2": //处理废标的
                    this.editTender(tender);
                    break;
                case "3": //处理删除的
                    this.editTender(tender);
                    break;
            }

        }
        //任务处理成功,没有返回异常将记录置为已处理
        System.out.println("执行无参方法");
    }

    private void editTender(Tender tender) {
        //批复文件名称
        SubInfo subInfo = subInfoService.findOne(new LambdaQueryWrapper<SubInfo>().eq(SubInfo::getSubName, tender.getTitle()));
        if (subInfo == null) {
            //将需要手动处理的放入到正式库里面
            tenderService.saveOrUpdateSlave(tender);
        } else {
            SubPreNew subPreNew = null;
            subPreNew = subPreNewService.findOne(new LambdaQueryWrapper<SubPreNew>().eq(SubPreNew::getSubId, subInfo.getId()).eq(SubPreNew::getPreFlowId, "16").eq(SubPreNew::getLogicDeleteFlag, 0));
            if (subPreNew == null) {
                subPreNew = new SubPreNew();
                subPreNew.setId(UUID.randomUUID().toString());
            }
            subPreNew.setSubId(subInfo.getId());
            subPreNew.setPreFlowId("16");
            subPreNew.setCreateTime(new Date());
            subPreNew.setFileAddre(tender.getFileUrl());
            subPreNew.setDatePlan(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", tender.getSiteDate()));
            subPreNew.setDateConfirm(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", tender.getPublishDate()));
            subPreNew.setTransactors(tender.getUser());
            subPreNew.setTransactorsPhone(tender.getPhone());
            subPreNew.setLogicDeleteFlag(1);
            subPreNew.setStatusConf("0"); //废标后设置未办理
            subPreNewService.saveOrUpdate(subPreNew);
            subInfo.setStatus("6"); //废标进入新建库
            subInfoService.saveOrUpdate(subInfo);
        }
        tender.setIsHandle("2");
        tenderService.saveOrUpdate(tender);
    }

    private void addTender(Tender tender) {
        //批复文件名称
        SubInfo subInfo = subInfoService.findOne(new LambdaQueryWrapper<SubInfo>().eq(SubInfo::getSubName, tender.getTitle()));
        if (subInfo == null) {
            //将需要手动处理的放入到正式库里面
            tenderService.saveOrUpdateSlave(tender);
        } else {
            SubPreNew subPreNew = null;
            subPreNew = subPreNewService.findOne(new LambdaQueryWrapper<SubPreNew>().eq(SubPreNew::getSubId, subInfo.getId()).eq(SubPreNew::getPreFlowId, "16").eq(SubPreNew::getLogicDeleteFlag, 0));
            if (subPreNew == null) {
                subPreNew = new SubPreNew();
                subPreNew.setId(UUID.randomUUID().toString());
            }
            subPreNew.setSubId(subInfo.getId());
            subPreNew.setPreFlowId("16");
            subPreNew.setCreateTime(new Date());
            subPreNew.setFileAddre(tender.getFileUrl());
            subPreNew.setDatePlan(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", tender.getSiteDate()));
            subPreNew.setDateConfirm(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", tender.getPublishDate()));
            subPreNew.setTransactors(tender.getUser());
            subPreNew.setTransactorsPhone(tender.getPhone());
            subPreNew.setLogicDeleteFlag(0);
            subPreNew.setStatusConf("1"); //设置正在办理
            if (StringUtils.isNotBlank(tender.getPublishDate())) {
                subPreNew.setStatusConf("2");//设置已经办理
                subPreNew.setNum3(Long.valueOf(DateUtil.between(subPreNew.getDateConfirm(), subPreNew.getDatePlan(), DateUnit.DAY)).intValue());
            }
            subPreNewService.saveOrUpdate(subPreNew);
            if (StringUtils.isNotBlank(tender.getPublishDate())) {
                subInfo.setStatus("7");
                subInfoService.saveOrUpdate(subInfo);
            }
        }
        tender.setIsHandle("2");
        tenderService.saveOrUpdate(tender);
    }
}
