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

import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.sqlserver.domain.TestLineResult;
import net.vtstar.zhongtong.avi.sqlserver.mapper.TestLineResultMapper;
import net.vtstar.zhongtong.avi.utils.PingIpUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.Date;
import java.util.List;

/**
 * 试交车间--检测线同步数据
 * @Author: xiadongwei
 * @Date: 2019/10/16 18:20
 */
@Slf4j
@Component
@DisallowConcurrentExecution
public class TestLineResultJob extends QuartzJobBean {

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

    @Autowired
    private TestLineResultMapper testLineResultMapper;

    @Value("${zt.ip.testlineresult}")
    private String ipAddress;
    @Value("${zt.ip.pingtimes}")
    private int pingTimes;
    @Value("${zt.ip.timeout}")
    private int timeOut;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        if (!PingIpUtils.ping(ipAddress, pingTimes, timeOut)){
            log.error("ping: {}失败, 此次同步试交车间检测线数据操作结束!", ipAddress);
            return;
        }
        log.info("--------开始同步试交车间检测线数据--------");
        Date startOfDay = DateUtils.getStartOfDay(0);
        String dateString = DateUtils.parseDateToString(startOfDay, "yyyy-MM-dd HH:mm:ss");
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT PID, VIN, TEST_TIME, FST_WEIGHT, SEC_WEIGHT, TOTAL_WEIGHT, FST_LBRAKE_FORCE, FST_RBRAKE_FORCE, " +
                "SEC_LBRAKE_FORCE, SEC_RBRAKE_FORCE, SPEAKER, LAMP_LEFT_LUM, LAMP_RIGHT_LUM, GAS_HICO, GAS_HIHC, " +
                "GAS_LOWCO, GAS_LOWHC, SMOKE_AVER, SPEED, SIDESLIP, TEST_RESULT FROM CARTEST_RECORD_RESULT WHERE TEST_TIME > '");
        sql.append(dateString);
        sql.append("'");
        List<TestLineResult> query = testSqlServerJdbcTemplate.query(sql.toString(), (rs, rowNum) -> {
            TestLineResult map = new TestLineResult();
            map.setPID(rs.getString("PID"));
            map.setVIN(rs.getString("VIN"));
            map.setTEST_TIME(rs.getTimestamp("TEST_TIME"));
            map.setFST_WEIGHT(rs.getFloat("FST_WEIGHT"));
            map.setSEC_WEIGHT(rs.getFloat("SEC_WEIGHT"));
            map.setTOTAL_WEIGHT(rs.getFloat("TOTAL_WEIGHT"));
            map.setFST_LBRAKE_FORCE(rs.getFloat("FST_LBRAKE_FORCE"));
            map.setFST_RBRAKE_FORCE(rs.getFloat("FST_RBRAKE_FORCE"));
            map.setSEC_LBRAKE_FORCE(rs.getFloat("SEC_LBRAKE_FORCE"));
            map.setSEC_RBRAKE_FORCE(rs.getFloat("SEC_RBRAKE_FORCE"));
            map.setSPEAKER(rs.getFloat("SPEAKER"));
            map.setLAMP_LEFT_LUM(rs.getFloat("LAMP_LEFT_LUM"));
            map.setLAMP_RIGHT_LUM(rs.getFloat("LAMP_RIGHT_LUM"));
            map.setGAS_HICO(rs.getFloat("GAS_HICO"));
            map.setGAS_HIHC(rs.getFloat("GAS_HIHC"));
            map.setGAS_LOWCO(rs.getFloat("GAS_LOWCO"));
            map.setGAS_LOWHC(rs.getFloat("GAS_LOWHC"));
            map.setSMOKE_AVER(rs.getFloat("SMOKE_AVER"));
            map.setSPEED(rs.getFloat("SPEED"));
            map.setSIDESLIP(rs.getFloat("SIDESLIP"));
            map.setTEST_RESULT(rs.getString("TEST_RESULT"));
            map.setCreateTime(rs.getTimestamp("TEST_TIME"));
            return map;
        });
        log.error("检测线查询到数据: {} 条", query.size());
        if (CollectionUtils.isEmpty(query)) {
            //todo: ping通
            log.error("连接检测线SQL SERVER失败!");
            return;
        }
        testLineResultMapper.delByFilter(new WhereFilter() {{
            addFilter("TEST_TIME", Operation.GREATER_THAN, dateString);
        }}, TestLineResult.class);
        testLineResultMapper.insertList(query);
        log.info("--------同步试交车间检测线数据结束--------");
    }
}
