package com.emr.service.ipml;

import com.emr.dao.FinishedDateTimeMapper;
import com.emr.entity.FinishedDateTime;
import com.emr.service.FinishedDateTimeService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.Reader;
import java.util.Date;

/**
 * oracle数据获取
 */
@Service
public class FinishedDateTimeServiceImpl implements FinishedDateTimeService {
    /**
     * 实际查询方法
     *
     * @param INP_NO   编号
     * @param VISIT_ID 住院次数
     * @return 最后完成时间
     * @throws IOException 注意传入两个参数都是Long，返回一条date类型数据或null，可用tostring()转
     */
    @Override
    public Date findByID(Long INP_NO, Long VISIT_ID) throws IOException {
        //加载资源
        String resource = "config/mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        //创建session
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        SqlSession session = factory.openSession();
        //获取映射对象
        FinishedDateTimeMapper mapper = session.getMapper(FinishedDateTimeMapper.class);
        FinishedDateTime result = mapper.findByID(INP_NO, VISIT_ID);
        if (result != null) {
            Date dateTime = result.getFINISHED_DATE_TIME();
            return dateTime;
        }
        return null;
    }
}