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.MesScanCodeZpInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesScanCodeZpMapper;
import net.vtstar.zhongtong.avi.utils.PingIpUtils;
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;

/**
 * mes底盘扫码设备同步数据
 * @Author: xiadongwei
 * @Date: 2020/04/23 18:20
 */
@Slf4j
@Component
public class MesScanCodeZpJob extends QuartzJobBean {

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

    @Autowired
    private MesScanCodeZpMapper mapper;

    @Value("${zt.ip.mes}")
    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)){
            return;
        }
        log.info("--------开始同步mes数据--------");
        Date startOfDay = DateUtils.getStartOfDay(0);
        String dateString = DateUtils.parseDateToString(startOfDay, "yyyy-MM-dd HH:mm:ss");
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT v.SaleOrderNO, v.VehicleID, v.PartTraceID, v.PartDrawingNo, v.PartDrawingName, v.SupplierNo, v.SupplierName, v.workshopID," +
                " v.workLineID, v.WPCode, v.ProductionBatch, v.OperationTime, v.OperationPerson, v.OperationPersonName, v.IsSpecified," +
                " v.Deleted, v.Memo, v.WkStation, v.DocClosedBy, v.Operation, v.DocClosed, v.CrtDate, v.UpdDate, v.Archived," +
                " v.IsSynchronize, v.MesNoticeNo, v.personFlag, v.supplierFlag, v.matFlag, b.WSName, c.MLineNo, d.WPName" +
                " FROM VehicleQualityTraceDocument v " +
                " left join WS_WorkShop b on v.workshopID = b.WSCode " +
                " left join WS_MaufactoryLine c on v.workLineID = c.MLineNo" +
                " left join WS_Point d on v.WPCode = d.WPCode" +
                "WHERE v.workshopID = 'ZP' AND v.Deleted = 'F' AND v.OperationTime > '" + dateString + "'");
        List<MesScanCodeZpInfo> query = mesSqlServerJdbcTemplate.query(sql.toString(), (rs, rowNum) -> {
            MesScanCodeZpInfo map = new MesScanCodeZpInfo();
            map.setSaleOrderNO(rs.getString("SaleOrderNO"));
            map.setVehicleID(rs.getString("VehicleID"));
            map.setPartTraceID(rs.getString("PartTraceID"));
            map.setPartDrawingNo(rs.getString("PartDrawingNo"));
            map.setPartDrawingName(rs.getString("PartDrawingName"));
            map.setSupplierNo(rs.getString("SupplierNo"));
            map.setSupplierName(rs.getString("SupplierName"));
            map.setWorkshopID(rs.getString("workshopID"));
            map.setWorkLineID(rs.getString("workshopID"));
            map.setWorkLineName(rs.getString("MLineNo"));
            map.setWPCode(rs.getString("WPCode"));
            map.setWPName(rs.getString("WPName"));
            map.setProductionBatch(rs.getString("ProductionBatch"));
            map.setOperationTime(rs.getTimestamp("OperationTime"));
            map.setOperationPerson(rs.getString("OperationPerson"));
            map.setOperationPersonName(rs.getString("OperationPersonName"));
            map.setStatus("1");
            map.setCreateTime(rs.getTimestamp("OperationTime"));
            return map;
        });
        log.error("mes查询到数据: {} 条", query.size());
        if (CollectionUtils.isEmpty(query)) {
            log.error("连接mesSQL SERVER失败!");
            return;
        }
        mapper.delByFilter(new WhereFilter(){{
            addFilter("OperationTime", Operation.GREATER_THAN, dateString);
        }}, MesScanCodeZpInfo.class);
        mapper.insertList(query);
        log.info("--------同步mess装配扫码数据结束--------");
    }
}
