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.TestSpeedResult;
import net.vtstar.zhongtong.avi.sqlserver.mapper.TestSpeedMapper;
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 TestSpeedResultJob extends QuartzJobBean {

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

    @Autowired
    private TestSpeedMapper testSpeedMapper;

    @Value("${zt.ip.testspeedresult}")
    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 vin, MODEL_ID, testDate, SPEED_MAX, SPEED_XSDG, SPEED_XSBZ0, SPEED_XSBZ1 FROM CARTEST_SPEED_RESULT WHERE testDate > '");
        sql.append(dateString);
        sql.append("'");
        List<TestSpeedResult> query = speedSqlServerJdbcTemplate.query(sql.toString(), (rs, rowNum) -> {
            TestSpeedResult map = new TestSpeedResult();
            map.setVin(rs.getString("vin"));
            map.setMODEL_ID(rs.getString("MODEL_ID"));
            map.setTestDate(rs.getTimestamp("testDate"));
            map.setSPEED_MAX(rs.getFloat("SPEED_MAX"));
            map.setSPEEDXSDG(rs.getString("SPEED_XSDG"));
            map.setSPEED_XSBZ0(rs.getFloat("SPEED_XSBZ0"));
            map.setSPEED_XSBZ1(rs.getFloat("SPEED_XSBZ1"));
            return map;
        });
        log.error("限速值查询到数据: {} 条", query.size());
        if (CollectionUtils.isEmpty(query)) {
            //todo: ping通
            log.error("连接限速值SQL SERVER失败!");
            return;
        }
        testSpeedMapper.delByFilter(new WhereFilter() {{
            addFilter("test_date", Operation.GREATER_THAN, dateString);
        }}, TestSpeedResult.class);
        testSpeedMapper.insertList(query);
        log.info("--------同步试交车间限速值数据结束--------");
    }
}
