package net.vtstar.zhongtong.avi.equipment.job;

import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.service.EquipDataService;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.equipment.domain.EquipInfo;
import net.vtstar.zhongtong.avi.equipment.domain.RuntimeRecord;
import net.vtstar.zhongtong.avi.equipment.mapper.BasicInfoMapper;
import net.vtstar.zhongtong.avi.equipment.mapper.RuntimeRecordMapper;
import net.vtstar.zhongtong.avi.equipment.service.RunRecordService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 统计运行时间job
 */
@Slf4j
public class StatisticsTimeJob extends QuartzJobBean {

    @Autowired
    private EquipDataService equipDataService;
    @Autowired
    private RunRecordService runRecordService;
    @Autowired
    private BasicInfoMapper basicInfoMapper;
    @Autowired
    private RuntimeRecordMapper runtimeRecordMapper;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        String startOfDay = DateUtils.parseDateToString(DateUtils.getStartOfDay(0), "yyyy-MM-dd HH:mm:ss");
        String endOfDay = DateUtils.parseDateToString(DateUtils.getEndOfDay(0), "yyyy-MM-dd HH:mm:ss");

        //拿到所有设备
        List<EquipInfo> equipInfo = basicInfoMapper.findList(new WhereFilter(),EquipInfo.class);

        for (EquipInfo info : equipInfo) {
            String equipCode = info.getEquipCode();
            long totalRuntime = 0;
            //查询今日设备所有运行状态数据
            List<EquipmentData> runData = equipDataService.findRunByCodeAndDate2(equipCode, startOfDay, endOfDay);
            if (CollectionUtils.isEmpty(runData)) {
                continue;
            }
            //计算运行时间
            for (EquipmentData data : runData) {
                long runtime = data.getEndTime().getTime() - data.getBeginTime().getTime();
                totalRuntime += runtime;
            }
            BigDecimal hours = new BigDecimal(totalRuntime).divide(new BigDecimal(3600000), 2, BigDecimal.ROUND_HALF_UP);
            //生成运行时间记录
            RuntimeRecord lastRecord = runRecordService.getLastByCode(equipCode);
            if (null != lastRecord && lastRecord.getCreateTime().before(DateUtils.getEndOfDay(0)) && lastRecord.getCreateTime().after(DateUtils.getStartOfDay(0))){
                //如果上一条记录是今天创建的, 则更新当班累计运行时间
                lastRecord.setCreateTime(new Date());
                lastRecord.setTotalRuntime(lastRecord.getTotalRuntime().subtract(lastRecord.getCurrentRuntime()).add(hours));
                lastRecord.setCurrentRuntime(hours);
                runtimeRecordMapper.update(lastRecord);
            }else{
                RuntimeRecord runtimeRecord = new RuntimeRecord();
                runtimeRecord.setCreateTime(new Date());
                runtimeRecord.setCurrentRuntime(hours);
                BigDecimal total = BigDecimal.ZERO;
                if (null != lastRecord) {
                    total = lastRecord.getTotalRuntime();
                }
                runtimeRecord.setTotalRuntime(total.add(hours));
                runtimeRecord.setMachineId(info.getId());
                runtimeRecord.setWorkshopCode(info.getWorkshopCode());
                if("POLISHING_ROOM".equals(info.getEquipType())){
                    runtimeRecord.setGroupCode("1");
                }
                runtimeRecordMapper.insert(runtimeRecord);
            }
        }
    }
}
