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.equipment.domain.MesWorkOrderInfo;
import net.vtstar.zhongtong.avi.equipment.mapper.MesWorkOrderMapper;
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;

/**
 * mes工单同步数据
 * @Author: xiadongwei
 * @Date: 2019/11/4 18:20
 */
@Slf4j
@Component
@DisallowConcurrentExecution
public class MesWorkOrderJob extends QuartzJobBean {

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

    @Autowired
    private MesWorkOrderMapper 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)){
            log.error("ping: {}失败, 此次同步mes数据操作结束!", ipAddress);
            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 SaleOrderNo, PreWONo, WONo, MaterialNo, ORDER_COUNT, UNIT, " +
                "WorkShopCode, MLine, StartDate, EndDate, DeliveryDate, Status, CrtDate FROM APS_PAINTING_WORKORDER " +
                "WHERE WorkShopCode = 'HZ' or WorkShopCode = 'TZ'");
        List<MesWorkOrderInfo> query = mesSqlServerJdbcTemplate.query(sql.toString(), (rs, rowNum) -> {
            MesWorkOrderInfo map = new MesWorkOrderInfo();
            map.setSalesOrderNo(rs.getString("SaleOrderNo"));
            map.setMasterWorkOrderNo(rs.getString("PreWONo"));
            map.setWorkOrderNo(rs.getString("WONo"));
            map.setVehicleModel(rs.getString("MaterialNo"));
            map.setOrderQuantity(rs.getInt("ORDER_COUNT"));
            map.setUnit(rs.getString("UNIT"));
            map.setWorkshop(rs.getString("WorkShopCode"));
            map.setProductionLine(rs.getString("MLine"));
            map.setStartDate(rs.getDate("StartDate"));
            map.setEndDate(rs.getDate("EndDate"));
            map.setDeliveryDate(rs.getDate("DeliveryDate"));
            map.setStatus(rs.getString("Status"));
            map.setCrtDate(rs.getDate("CrtDate"));
            return map;
        });
        log.error("mes查询到数据: {} 条", query.size());
        if (CollectionUtils.isEmpty(query)) {
            //todo: ping通
            log.error("连接mesSQL SERVER失败!");
            return;
        }
        for (MesWorkOrderInfo info : query){
            List<MesWorkOrderInfo> 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数据结束--------");
    }
}
