package cn.wisenergy.web.config.auto;

import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.mapper.WorkTimeOrderMapper;
import cn.wisenergy.model.app.WorkSubmitAdopt;
import cn.wisenergy.model.app.WorkTimeOrder;
import cn.wisenergy.model.enums.StatusEnum;
import cn.wisenergy.service.WorkCollectService;
import cn.wisenergy.service.WorkSubmitAdoptService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Component
@Configuration
@EnableScheduling
public class Trigger implements SchedulingConfigurer {




    @Autowired
    private WorkTimeOrderMapper workTimeOrderMapper;


    @Autowired
    private WorkCollectService workCollectService;

    @Autowired
    private WorkSubmitAdoptService workSubmitAdoptService;

    private final static Integer COMPLETED = StatusEnum.COMPLETED.getCode();
    private final static Integer RE_SUBMIT = StatusEnum.RE_SUBMIT.getCode();
    private final static Integer AUTOMATIC_AUDIT = StatusEnum.AUTOMATIC_AUDIT.getCode();

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        TriggerTask trigerTask = new TriggerTask(
                () -> {
                    //这里写业务方法
                    WorkSubmitAdopt workSubmitAdopt = workSubmitAdoptService.getById(1);
                    Integer autoAdopt = workSubmitAdopt.getSubmitTime();
                    //  更新work_time_order表
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    Calendar c = Calendar.getInstance();
                    c.setTime(new Date());
                    c.add(Calendar.DATE, -autoAdopt);
                    Date start = c.getTime();
                    String qyt= format.format(start);
                    Date yesterdayDate =  DateUtil.convertStrToDate(qyt, "yyyy-MM-dd");
                    Date now = DateUtil.getToday();
                    QueryWrapper<WorkTimeOrder> wrapper = new QueryWrapper<>();
                    wrapper.in("status", COMPLETED,RE_SUBMIT);
                    wrapper.le("work_day", yesterdayDate);
                    List<WorkTimeOrder> workTimeOrders = workTimeOrderMapper.selectList(wrapper);
                    UpdateWrapper<WorkTimeOrder> wtoWrapper = new UpdateWrapper<>();
                    wtoWrapper.in("status", COMPLETED,RE_SUBMIT);
                    wtoWrapper.le("work_day", yesterdayDate);
                    WorkTimeOrder workTimeOrder1 = new WorkTimeOrder();
                    workTimeOrder1.setStatus(AUTOMATIC_AUDIT);
                    workTimeOrder1.setModifyTime(now);
                    int wtoRow = workTimeOrderMapper.update(workTimeOrder1, wtoWrapper);
                    System.out.println("一共审核：" + wtoRow + "条工单");
                    // 更新work_collect表
                    workCollectService.updateByWorkTimeOrder(workTimeOrders);
                }
                , triggerContext -> {

            Integer autoAdopt = workSubmitAdoptService.getById(1).getAutoAdopt();
            String time = "0 0 0 */" + autoAdopt + " * ?";
            //返回执行的周期
            return new CronTrigger(time).nextExecutionTime(triggerContext);
        });
        scheduledTaskRegistrar.addTriggerTask(trigerTask);
    }
}
