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.ElectrophoresisMachine;
import net.vtstar.zhongtong.avi.gateway.painting.mapper.ElectrophoresisMapper;
import net.vtstar.zhongtong.avi.global.constant.Constant;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 电泳空中行车时间数据采集JOB
 */
@Slf4j
@Component
public class ElectrophoresisMachineJob extends QuartzJobBean {

    private static final String NAME = "VEHICLE_ELECTROPHORESIS_OUTPUT";
    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd");
    public static final List<String> eqpNoList = Arrays.asList("预脱脂", "脱脂", "水洗1", "水洗2", "表调", "磷化", "水洗3", "水洗4", "纯水洗1",
            "电泳转移槽", "电泳槽", "UF1", "UF2", "纯水洗2");

    @Resource(name = "redisTemplate")
    protected ValueOperations valueOperations;

    @Autowired
    private ElectrophoresisMapper mapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        String today = dateFormat.format(new Date());
        String areaKey = Constant.BUS_SIZE_OUT + "2" + ":" + today;
        Integer size = (Integer) valueOperations.get(areaKey);
        if (null == size) {
            size = 1;
        }
        for (int i = 0; i < 4; i++){
            String eqpNo = eqpNoList.get((int)(Math.random() * 14));
            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<ElectrophoresisMachine> list = mapper.findList(new WhereFilter() {{
                addFilter("eqp_code", Operation.EQUAL, eqpNo);
                addFilter("create_time", Operation.EQUAL_GREATER_THAN, startOfDay);
                addFilter("create_time", Operation.EQUAL_LESS_THAN, endOfDay);
            }}, ElectrophoresisMachine.class);
            if (CollecUtils.isEmpty(list)){
                ElectrophoresisMachine machine = new ElectrophoresisMachine();
                machine.setEquipCode(eqpNo);
                machine.setUpTime(new BigDecimal(size * 2));
                machine.setDownTime(new BigDecimal(size * 2.5));
                machine.setEntryTime(new BigDecimal(size * 1.5));
                machine.setTranslationTime(new BigDecimal(size * 2));
                machine.setCreateTime(new Date());
                mapper.insert(machine);
            }else {
                ElectrophoresisMachine machine = list.get(0);
                machine.setUpTime(machine.getUpTime().add(new BigDecimal((int)(Math.random() * 5))));
                machine.setDownTime(machine.getDownTime().add(new BigDecimal((int)(Math.random() * 4))));
                machine.setEntryTime(machine.getEntryTime().add(new BigDecimal((int)(Math.random() * 3))));
                machine.setTranslationTime(machine.getTranslationTime().add(new BigDecimal((int)(Math.random() * 6))));
                machine.setCreateTime(new Date());
                mapper.update(machine);
            }
        }


    }
}
