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.RotaryRollerBedMachine;
import net.vtstar.zhongtong.avi.gateway.painting.mapper.RotaryRollerBedMapper;
import net.vtstar.zhongtong.avi.utils.EquipDataUtil;
import org.quartz.DisallowConcurrentExecution;
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.util.Date;
import java.util.List;

/**
 * 旋转辊床数据采集JOB
 */
@Slf4j
@Component
@DisallowConcurrentExecution
public class RotaryRollerBedMachineJob extends QuartzJobBean {

    private static final String NAME = "ROTARY_ROLLER_BED";

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

    @Autowired
    private RotaryRollerBedMapper mapper;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        String keyW = EquipDataUtil.getTestValueKey(NAME + "_W");
        String keyP1 = EquipDataUtil.getTestValueKey(NAME + "_P1");
        String keyP2 = EquipDataUtil.getTestValueKey(NAME + "_P2");
        String keyC = EquipDataUtil.getTestValueKey(NAME + "_C");
        if (testValueOperations.get(keyW) == null || "".equals(testValueOperations.get(keyW).toString())){
            testValueOperations.set(keyW, 0);
        }
        if (testValueOperations.get(keyP1) == null || "".equals(testValueOperations.get(keyP1).toString())){
            testValueOperations.set(keyP1, 0);
        }
        if (testValueOperations.get(keyP2) == null || "".equals(testValueOperations.get(keyP2).toString())){
            testValueOperations.set(keyP2, 0);
        }
        if (testValueOperations.get(keyC) == null || "".equals(testValueOperations.get(keyC).toString())){
            testValueOperations.set(keyC, 0);
        }
        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<RotaryRollerBedMachine> list = mapper.findList(new WhereFilter() {{
            addFilter("create_time", Operation.EQUAL_GREATER_THAN, startOfDay);
            addFilter("create_time", Operation.EQUAL_LESS_THAN, endOfDay);
        }}, RotaryRollerBedMachine.class);
        if (CollecUtils.isEmpty(list)){
            RotaryRollerBedMachine machine = new RotaryRollerBedMachine();
            machine.setRunNumWelding(Integer.valueOf(testValueOperations.get(keyW).toString()));
            machine.setRunNumPainting1(Integer.valueOf(testValueOperations.get(keyP1).toString()));
            machine.setRunNumPainting2(Integer.valueOf(testValueOperations.get(keyP2).toString()));
            machine.setRunNumChassis(Integer.valueOf(testValueOperations.get(keyC).toString()) - Integer.valueOf(testValueOperations.get(keyW).toString()));
            machine.setCreateTime(new Date());
            mapper.insert(machine);
        }else {
            RotaryRollerBedMachine machine = list.get(0);
            machine.setRunNumWelding(Integer.valueOf(testValueOperations.get(keyW).toString()));
            machine.setRunNumPainting1(Integer.valueOf(testValueOperations.get(keyP1).toString()));
            machine.setRunNumPainting2(Integer.valueOf(testValueOperations.get(keyP2).toString()));
            machine.setRunNumChassis(Integer.valueOf(testValueOperations.get(keyC).toString()));
            mapper.update(machine);
        }

    }
}
