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.CollecUtils;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.sqlserver.domain.MesHZWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.domain.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.sqlserver.mapper.MesHZWorkOrderMapper;
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: 2019/11/4 18:20
 */
@Slf4j
@Component
public class MesHZWorkOrderJob extends QuartzJobBean {

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

    @Autowired
    private MesHZWorkOrderMapper 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 WONo, SaleOrderNo, PlannedOrderNo, RealStartTime, RealEndTime FROM APS_WeldingWorkOrder " +
                "WHERE RealEndTime > '" + dateString + "'");
        List<MesHZWorkOrderInfo> query = mesSqlServerJdbcTemplate.query(sql.toString(), (rs, rowNum) -> {
            MesHZWorkOrderInfo map = new MesHZWorkOrderInfo();
            map.setSalesOrderNo(rs.getString("SaleOrderNo"));
            map.setMasterWorkOrderNo(rs.getString("PlannedOrderNo"));
            map.setWorkOrderNo(rs.getString("WONo"));
            map.setRealStartTime(rs.getString("RealStartTime"));
            map.setRealEndTime(rs.getString("RealEndTime"));
            return map;
        });
        log.error("mes查询到焊装数据: {} 条", query.size());
        if (CollectionUtils.isEmpty(query)) {
            //todo: ping通
            log.error("连接mesSQL SERVER失败!");
            return;
        }
        for (MesHZWorkOrderInfo info : query){
            List<MesHZWorkOrderInfo> orderNo = mapper.findList(new WhereFilter() {{
                addFilter("work_order_no", Operation.EQUAL, info.getWorkOrderNo());
            }}, MesWorkOrderInfo.class);
            if (CollecUtils.isEmpty(orderNo)){
                mapper.insert(info);
            }
        }
        log.info("--------同步mes焊装数据结束--------");
    }
}
