package org.dromara.elevator.job;

import cn.hutool.core.date.DateUtil;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.elevator.domain.EElevatorInfo;
import org.dromara.elevator.domain.bo.BehaviorMessageStatisticsBo;
import org.dromara.elevator.domain.bo.EElevatorInfoBo;
import org.dromara.elevator.domain.vo.EElevatorInfoVo;
import org.dromara.elevator.service.IEBehaviorStatisticsService;
import org.dromara.elevator.service.IEElevatorInfoService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 故障统计定时器
 *
 * @author ym
 * @version 1.0.0
 * @date 2024-09-04 09:26
 */
@Component
@RequiredArgsConstructor
public class BehaviorStatisticsJob {


    private final IEElevatorInfoService elevatorInfoService;

    private final IEBehaviorStatisticsService behaviorStatisticsService;

    /**
     * 昨天数据统计
     */
    //@Scheduled(cron = "0 10 0 * * ? ")
    public void yesterdayBehaviorStatisticsJob() {
        Date yesterday = DateUtil.yesterday();
        BehaviorMessageStatisticsBo bo = new BehaviorMessageStatisticsBo();
        bo.setStartTime(DateUtil.beginOfDay(yesterday));
        bo.setEndTime(DateUtil.endOfDay(yesterday));
        for (EElevatorInfoVo elevatorInfoVo : elevatorInfoService.queryList(new EElevatorInfoBo())) {
            bo.setElevatorNum(elevatorInfoVo.getElevatorNum());
            bo.setElevatorInfo(MapstructUtils.convert(elevatorInfoVo, EElevatorInfo.class));
            behaviorStatisticsService.behaviorStatistics(bo);
        }
    }

    /**
     * 当天数据统计
     */
    //@Scheduled(cron = "0 3/5 * * * ? ")
    public void behaviorStatisticsJob() {
        Date now = new Date();
        BehaviorMessageStatisticsBo bo = new BehaviorMessageStatisticsBo();
        bo.setStartTime(DateUtil.beginOfDay(now));
        bo.setEndTime(DateUtil.endOfDay(now));
        for (EElevatorInfoVo elevatorInfoVo : elevatorInfoService.queryList(new EElevatorInfoBo())) {
            bo.setElevatorNum(elevatorInfoVo.getElevatorNum());
            bo.setElevatorInfo(MapstructUtils.convert(elevatorInfoVo, EElevatorInfo.class));
            behaviorStatisticsService.behaviorStatistics(bo);
        }
    }
}
