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

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);
        String sql = "INSERT INTO 'VEHICLE_PASSING_INFO' ('WONo', 'RFID', 'StationNo', 'AreaNo', 'InTime', 'OutTime', 'Status') VALUES ('"
                + passInfo.getVIN() + "', '"
                + passInfo.getRfid() + "', '"
                + passInfo.getStationNo() + "', '"
                + passInfo.getAreaNo() + "', '"
                + passInfo.getInTime() + "', '"
                + passInfo.getOutTime() + "', '"
                + 1 + "', '"
                + "');";
        mesSqlServerJdbcTemplate.update(sql);
    }
}
