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

import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.monitoring.domain.StationPassInfo;
import net.vtstar.zhongtong.avi.monitoring.mapper.StationPassInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

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

/**
 * @Author: xiadongwei
 * @Date: 2019/12/13 15:18
 */
@Service
public class StationPassInfoService {

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

    @Autowired
    private StationPassInfoMapper passInfoMapper;

    /*//创建过站信息记录
    public Long createPassInfo(String workOrderNo, String rfid, String areaNo, String stationNo){
        StationPassInfo info = new StationPassInfo();
        info.setVIN(workOrderNo);
        info.setRfid(rfid);
        info.setAreaNo(areaNo);
        info.setStationNo(stationNo);
        info.setInTime(new Date());
        passInfoMapper.insert(info);
        return info.getId();
    }

    public void endPassInfo(PaintshopStation station){
        if (null == station || null == station.getPassInfoId()){
            return;
        }
        StationPassInfo passInfo = passInfoMapper.getById(station.getPassInfoId(), StationPassInfo.class);
        if (null == passInfo){
            return;
        }
        passInfo.setOutTime(new Date());
        passInfoMapper.update(passInfo);
        //todo:!!!!!!!!!!!!!!!!
        String sql = "INSERT INTO VEHICLE_PASSING_INFO  VALUES (?,?,?,?,?,?,?)";
        mesSqlServerJdbcTemplate.update(sql, new Object[]{passInfo.getVIN(), passInfo.getRfid(), passInfo.getAreaNo(),
                passInfo.getStationNo(), DateUtils.parseDateToString(passInfo.getInTime(), "yyyy-MM-dd HH:mm:ss"),
                DateUtils.parseDateToString(passInfo.getOutTime(), "yyyy-MM-dd HH:mm:ss"), 1});
    }*/
    //创建过站信息记录
    public Long createPassInfo(String workOrderNo, String rfid, String areaNo, String stationNo){
        StationPassInfo info = new StationPassInfo();
        info.setVIN(workOrderNo);
        if (null == workOrderNo){
            info.setVIN("");
        }
        info.setRfid(rfid);
        info.setAreaNo(areaNo);
        info.setStationNo(stationNo);
        info.setInTime(new Date());
        passInfoMapper.insert(info);
        String sql = "INSERT INTO VEHICLE_PASSING_INFO  VALUES (?,?,?,?,?,?,?)";
        mesSqlServerJdbcTemplate.update(sql, new Object[]{info.getVIN(), info.getRfid(), info.getAreaNo(),
                info.getStationNo(), DateUtils.parseDateToString(info.getInTime(), "yyyy-MM-dd HH:mm:ss"),
                info.getOutTime(), 1});
        return info.getId();
    }

    public void endPassInfo(PaintshopStation station){
        StationPassInfo info = new StationPassInfo();
        info.setVIN(station.getSkid().getWorkOrderNo());
        if (null == station.getSkid().getWorkOrderNo()){
            info.setVIN("");
        }
        info.setRfid(station.getSkid().getRfid());
        info.setAreaNo(station.getAreaName());
        info.setStationNo(station.getCode());
        info.setOutTime(new Date());
        passInfoMapper.insert(info);
        //todo:!!!!!!!!!!!!!!!!
        String sql = "INSERT INTO VEHICLE_PASSING_INFO  VALUES (?,?,?,?,?,?,?)";
        mesSqlServerJdbcTemplate.update(sql, new Object[]{info.getVIN(), info.getRfid(), info.getAreaNo(),
                info.getStationNo(), info.getInTime(),
                DateUtils.parseDateToString(info.getOutTime(), "yyyy-MM-dd HH:mm:ss"), 1});
    }
}
