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

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.AssemblyTorqueWrench;
import net.vtstar.zhongtong.avi.sqlserver.domain.TestLineResult;
import net.vtstar.zhongtong.avi.sqlserver.domain.TestSpeedResult;
import net.vtstar.zhongtong.avi.sqlserver.mapper.AssemblyTorqueWrenchMapper;
import net.vtstar.zhongtong.avi.sqlserver.mapper.TestLineResultMapper;
import net.vtstar.zhongtong.avi.sqlserver.mapper.TestSpeedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @Author: xiadongwei
 * @Date: 2019/9/24 16:44
 */
@Slf4j
@Service
public class TestDeliverService {

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

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

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

    @Autowired
    private TestLineResultMapper testLineResultMapper;
    @Autowired
    private TestSpeedMapper testSpeedMapper;
    @Autowired
    private AssemblyTorqueWrenchMapper assemblyTorqueWrenchMapper;

    public void tableTest(String timeStr) {
        log.error(DateUtils.parseDateToString(new Date(), "HHmmss"));
        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(timeStr);
        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"));
            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, timeStr);
        }}, TestLineResult.class);
        testLineResultMapper.insertList(query);
        log.error(DateUtils.parseDateToString(new Date(), "HHmmss"));
    }

    public void tableWrench(String timeStr) {
        log.info("--------开始同步试交车间限速值数据--------");
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT vin, MODEL_ID, testDate, SPEED_MAX, SPEED_XSBZ0, SPEED_XSBZ1 FROM CARTEST_SPEED_RESULT WHERE testDate > '");
        sql.append(timeStr);
        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.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, timeStr);
        }}, TestSpeedResult.class);
        testSpeedMapper.insertList(query);
        log.info("--------同步试交车间限速值数据结束--------");
    }

    public void tabletorque(String timeStr) {
        log.info("--------开始同步总装车间智能扭力扳手数据--------");
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT  Result.ID AS ResultID, Result.ResultDateTime, rstoverall.LanguageConstant AS OverallStatus, " +
                "rsttorque.LanguageConstant AS TorqueStatus, ResultTightening.FinalTorque, Program.Name AS ProgramName, " +
                "Program.ProgramDateTime, Unit.Identifier " +
                "FROM Result " +
                "INNER JOIN ResultStatusType AS rstoverall ON Result.ResultStatusTypeID = rstoverall.ID  " +
                "INNER JOIN ResultTightening ON Result.ID = ResultTightening.ResultID " +
                "INNER JOIN ResultStatusType AS rsttorque ON ResultTightening.FinalTorqueStatusID = rsttorque.ID " +
                "INNER JOIN Program ON Result.ProgramID = Program.ID " +
                "INNER JOIN Unit ON Result.UnitID = Unit.ID WHERE ResultDateTime > '");
        sql.append(timeStr);
        sql.append("'");
        List<AssemblyTorqueWrench> query = torqueSqlServerJdbcTemplate.query(sql.toString(), (rs, rowNum) -> {
            AssemblyTorqueWrench map = new AssemblyTorqueWrench();
            map.setIdentifier(rs.getString("Identifier"));
            map.setProgramName(rs.getString("ProgramName"));
            map.setProgramDateTime(rs.getTimestamp("ProgramDateTime"));
            map.setFinalTorque(rs.getFloat("FinalTorque"));
            map.setResultDateTime(rs.getTimestamp("ResultDateTime"));
            map.setOverallStatus(rs.getString("OverallStatus"));
            map.setTorqueStatus(rs.getString("TorqueStatus"));
            return map;
        });
        log.error("总装车间智能扭力扳手查询到数据: {} 条", query.size());
        if (CollectionUtils.isEmpty(query)) {
            //todo: ping通
            log.error("连接总装车间智能扭力扳手SQL SERVER失败!");
            return;
        }
        assemblyTorqueWrenchMapper.delByFilter(new WhereFilter() {{
            addFilter("ResultDateTime", Operation.GREATER_THAN, timeStr);
        }}, AssemblyTorqueWrench.class);
        assemblyTorqueWrenchMapper.insertList(query);
        log.info("--------同步总装车间智能扭力扳手数据结束--------");
    }
}
