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.AssemblyTorqueWrench;
import net.vtstar.zhongtong.avi.sqlserver.mapper.AssemblyTorqueWrenchMapper;
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 AssemblyTorqueWrenchJob extends QuartzJobBean {

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

    @Autowired
    private AssemblyTorqueWrenchMapper assemblyTorqueWrenchMapper;

    @Value("${zt.ip.assemblytorquewrench}")
    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  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(dateString);
        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, dateString);
        }}, AssemblyTorqueWrench.class);
        assemblyTorqueWrenchMapper.insertList(query);
        log.info("--------同步总装车间智能扭力扳手数据结束--------");
    }
}
