package net.vtstar.zhongtong.avi.gateway.assembly.job;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.domain.EquipInfo;
import net.vtstar.scada.base.equipmgt.mapper.EquipInfoMapper;
import net.vtstar.scada.base.equipmgt.service.EquipDataService;
import net.vtstar.scada.base.equipmgt.service.EquipInfoService;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.equipment.mapper.EquipmentDataMapper;
import net.vtstar.zhongtong.avi.gateway.assembly.domain.PlateChainMachine;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

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

/**
 * 总装板链设备数据采集JOB
 */
@Slf4j
@Component
@DisallowConcurrentExecution
public class PlateChainTimeMachineJob extends QuartzJobBean {

    @Autowired
    EquipInfoMapper equipInfoMapper;

    @Autowired
    private EquipInfoService equipInfoService;

    @Autowired
    private EquipDataService equipDataService;

    @Autowired
    private EquipmentDataMapper dataMapper;

    private static final String NAME = "ASSEMBLY_PLATE_CHAIN";

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<EquipInfo> equipInfoList = equipInfoService.getEquipInfoList(NAME);
        if (CollectionUtils.isEmpty(equipInfoList)) {
            return;
        }
        for (EquipInfo equipInfo : equipInfoList){
            String startOfDay = DateUtils.parseDateToString(DateUtils.getStartOfDay(0, new Date()), "yyyy-MM-dd HH:mm:ss");
            String endOfDay = DateUtils.parseDateToString(DateUtils.getEndOfDay(0, new Date()), "yyyy-MM-dd HH:mm:ss");
            List<EquipmentData> runByCodeAndDate = equipDataService.findRunByCodeAndDate2(equipInfo.getEquipCode(), startOfDay, endOfDay);
            if (runByCodeAndDate.size() < 2){
                continue;
            }
            Date startTime = null;
            //总间隔时间
            long totalTime = 0;
            for (EquipmentData equipmentData : runByCodeAndDate){
                if (null == startTime){
                    startTime = equipmentData.getEndTime();
                    continue;
                }
                //如果记录的开始时间大于上一条记录的结束时间
                Date beginTime = equipmentData.getBeginTime();
                if (null == beginTime || null == equipmentData.getEndTime()){
                    continue;
                }
                if (beginTime.after(startTime)){
                    totalTime = totalTime + beginTime.getTime() - startTime.getTime();
                    startTime = equipmentData.getEndTime();
                }
            }
            if (totalTime != 0){
                PlateChainMachine plateChainMachine = new PlateChainMachine();
                plateChainMachine.setEquipCode(equipInfo.getEquipCode());
                plateChainMachine.setAlias(equipInfo.getAlias());
                plateChainMachine.setCreateTime(new Date());
                String averageTime = (totalTime / runByCodeAndDate.size() / 1000 / 60) + "min" + (totalTime / runByCodeAndDate.size() / 1000 % 60) + "s";
                plateChainMachine.setAverageTime(averageTime);
                dataMapper.insert(plateChainMachine);
            }
        }

    }
}
