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

import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.CollecUtils;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.gateway.painting.domain.PaintingRobotRecordMachine;
import net.vtstar.zhongtong.avi.gateway.painting.mapper.PaintingRobotRecordMapper;
import net.vtstar.zhongtong.avi.monitoring.domain.StationPassInfo;
import net.vtstar.zhongtong.avi.monitoring.mapper.StationPassInfoMapper;
import net.vtstar.zhongtong.avi.sqlserver.domain.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesWorkOrderMapper;
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 java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 喷涂机器人运行次数统计JOB
 */
@Slf4j
@Component
public class PaintingRobotRecordMachineJob extends QuartzJobBean {

    @Autowired
    private PaintingRobotRecordMapper recordMapper;

    @Autowired
    private MesWorkOrderMapper mesWorkOrderMapper;

    @Autowired
    private StationPassInfoMapper mapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        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<StationPassInfo> list = mapper.findList(new WhereFilter() {{
            addFilter(StationPassInfo.class, "station_no", Operation.EQUAL, "HDG1-125");
            addFilter(StationPassInfo.class, "in_time", Operation.IS_NOT, "null");
            addFilter(StationPassInfo.class, "create_time", Operation.EQUAL_GREATER_THAN, startOfDay);
            addFilter(StationPassInfo.class, "create_time", Operation.EQUAL_LESS_THAN, endOfDay);
        }}, StationPassInfo.class);
        list.stream().filter(s -> s.getVIN() != null).forEach(stationPassInfo -> {
            List<MesWorkOrderInfo> orderNo = mesWorkOrderMapper.findList(new WhereFilter() {{
                addFilter("work_order_no", Operation.EQUAL, stationPassInfo.getVIN());
                addFilter("status", Operation.EQUAL, "4");
            }}, MesWorkOrderInfo.class);
            if (!CollecUtils.isEmpty(orderNo)){
                stationPassInfo.setBusType(orderNo.get(0).getVehicleModel());
            }
        });
        Map<String, List<StationPassInfo>> collect = list.stream().filter(s -> s.getBusType() != null).collect(Collectors.groupingBy(StationPassInfo::getBusType));
        for (Map.Entry<String, List<StationPassInfo>> entry : collect.entrySet()){
            List<PaintingRobotRecordMachine> recordMachines = recordMapper.findList(new WhereFilter() {{
                addFilter("bus_type", Operation.EQUAL, entry.getKey());
                addFilter("create_time", Operation.EQUAL_GREATER_THAN, startOfDay);
                addFilter("create_time", Operation.EQUAL_LESS_THAN, endOfDay);
            }}, PaintingRobotRecordMachine.class);
            if (CollecUtils.isEmpty(recordMachines)){
                PaintingRobotRecordMachine machine = new PaintingRobotRecordMachine();
                machine.setEquipCode("喷涂机器人");
                machine.setBusType(entry.getKey());
                machine.setNum(entry.getValue().size());
                machine.setCreateTime(new Date());
                recordMapper.insert(machine);
            }else {
                PaintingRobotRecordMachine machine = recordMachines.get(0);
                machine.setBusType(entry.getKey());
                machine.setNum(entry.getValue().size());
                machine.setCreateTime(new Date());
                recordMapper.update(machine);
            }
        }
    }
}
