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.EquipInfoService;
import net.vtstar.scada.base.gather.domain.EquipmentData;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

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

    @Autowired
    EquipInfoMapper equipInfoMapper;

    @Resource(name = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private EquipInfoService equipInfoService;

    private static final String NAME = "ASSEMBLY_PLATE_CHAIN";

    /**
     * 定时读取板链测试量, 与Redis中保存的测试量比较, 若变量发生变化, 则状态为运行
     */
    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        List<EquipInfo> equipInfoList = equipInfoService.getEquipInfoList(NAME);
        if (CollectionUtils.isEmpty(equipInfoList)) {
            return;
        }
        for (EquipInfo equipInfo : equipInfoList){
            List<EquipmentData> data = jdbcTemplate.queryForList("select  id,equip_type,equip_code,equip_status,begin_time,end_time " +
                    "from equip_status_record " +
                    "where equip_code = '" + equipInfo.getEquipCode() + "' and DATE(currentDate) = DATE(end_time) and equip_status = 'RUN'", EquipmentData.class);
        }

    }
}
