package com.jasamedika.medifirst2000.service.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jasamedika.medifirst2000.dao.SettingDataFixedDao;
import com.jasamedika.medifirst2000.dto.TPatient;
import com.jasamedika.medifirst2000.service.PacsService;
import com.jasamedika.medifirst2000.util.DateUtil;

@Service("PacsService")
public class PacsServiceImpl implements PacsService{

	@Autowired
	private SettingDataFixedDao settingDataFixedDao;
	
	@Override
	public Connection getConnection(String LOG){
		Connection conn = null;
		try{
            StringBuffer dataSource = new StringBuffer();
            dataSource.append("jdbc:oracle:thin:@");
            dataSource.append("192.168.12.11");//settingDataFixedDao.getSettingDataFixed("IPServerPacs"));//192.168.12.11
            dataSource.append(":1521:");
            dataSource.append("zetta");
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(dataSource.toString(), "dodeulbyeol", "tong1#pacs1011$");
            if(conn!=null){
                System.out.println("INJECT PACS SERVER : "+LOG+" -:- "+DateUtil.now());
                System.out.println(">-------------------------------------------------------------------------->\n");
            }
        }catch(ClassNotFoundException | SQLException ex){
            ex.printStackTrace();
        }
		return conn;
	}
	
	@Override
	public TPatient getOne(String noCm){
		TPatient result = new TPatient();
		try{
			Connection connection = getConnection("get one "+noCm);
			StringBuffer query = new StringBuffer();
			query.append("SELECT "
					+ "PATIENT_KEY,"
					+ "INSTITUTION_KEY,"
					+ "PATIENT_ID,"
					+ "PATIENT_NAME,"
					+ "LAST_STUDYDATE,"
					+ "PATIENT_BIRTH_DATE,"
					+ "PATIENT_AGE,"
					+ "PATIENT_SEX,"
					+ "PATIENT_IO,"
					+ "FLAG ");
			query.append("FROM TPATIENT ");
			query.append("WHERE PATIENT_ID=?");
			PreparedStatement ps = connection.prepareStatement(query.toString());
			ps.setObject(1, noCm);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				result.setPATIENT_KEY((BigDecimal)rs.getObject(1));
				result.setINSTITUTION_KEY((BigDecimal)rs.getObject(2));
				result.setPATIENT_ID((String) rs.getObject(3));
				result.setPATIENT_NAME((String) rs.getObject(4));
				result.setLAST_STUDYDATE((String) rs.getObject(5));
				result.setPATIENT_BIRTH_DATE((String) rs.getObject(6));
				result.setPATIENT_AGE((String) rs.getObject(7));
				result.setPATIENT_SEX((String) rs.getObject(8));
				result.setPATIENT_IO((String) rs.getObject(9));
				result.setFLAG((String) rs.getObject(10));
			}
			connection.close();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	@Override
	public List<TPatient> getAll(){
		List<TPatient> result = new ArrayList();
		try{
			Connection connection = getConnection("get all");
			StringBuffer query = new StringBuffer();
			query.append("SELECT "
					+ "PATIENT_KEY,"
					+ "INSTITUTION_KEY,"
					+ "PATIENT_ID,"
					+ "PATIENT_NAME,"
					+ "LAST_STUDYDATE,"
					+ "PATIENT_BIRTH_DATE,"
					+ "PATIENT_AGE,"
					+ "PATIENT_SEX,"
					+ "PATIENT_IO,"
					+ "FLAG ");
			query.append("FROM TPATIENT ORDER BY PATIENT_KEY ASC");
			PreparedStatement ps = connection.prepareStatement(query.toString());
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				TPatient tPatient = new TPatient();
				tPatient.setPATIENT_KEY((BigDecimal)rs.getObject(1));
				tPatient.setINSTITUTION_KEY((BigDecimal)rs.getObject(2));
				tPatient.setPATIENT_ID((String) rs.getObject(3));
				tPatient.setPATIENT_NAME((String) rs.getObject(4));
				tPatient.setLAST_STUDYDATE((String) rs.getObject(5));
				tPatient.setPATIENT_BIRTH_DATE((String) rs.getObject(6));
				tPatient.setPATIENT_AGE((String) rs.getObject(7));
				tPatient.setPATIENT_SEX((String) rs.getObject(8));
				tPatient.setPATIENT_IO((String) rs.getObject(9));
				tPatient.setFLAG((String) rs.getObject(10));
				result.add(tPatient);
			}
			connection.close();
			ps.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	@Override
	public TPatient insert(TPatient value){
		TPatient result = new TPatient();
		try{
			Connection connection = getConnection("insert "+value.getPATIENT_NAME());
			StringBuffer query = new StringBuffer();
			query.append("INSERT INTO TPATIENT ");
			query.append("(PATIENT_KEY,"
					+ "INSTITUTION_KEY, "
					+ "PATIENT_ID,"
					+ "PATIENT_NAME,"
					+ "LAST_STUDYDATE,"
					+ "PATIENT_BIRTH_DATE,"
					+ "PATIENT_AGE,"
					+ "PATIENT_SEX,"
					+ "PATIENT_IO,"
					+ "FLAG) ");
			query.append("VALUES ");
			query.append("(?,?,?,?,?,?,?,?,?,?)");
			PreparedStatement ps = connection.prepareStatement(query.toString());
			ps.setObject(1, value.getPATIENT_KEY());
			ps.setObject(2, value.getINSTITUTION_KEY());
			ps.setObject(3, value.getPATIENT_ID());
			ps.setObject(4, value.getPATIENT_NAME());
			ps.setObject(5, value.getLAST_STUDYDATE());
			ps.setObject(6, value.getPATIENT_BIRTH_DATE());
			ps.setObject(7, value.getPATIENT_AGE());
			ps.setObject(8, value.getPATIENT_SEX());
			ps.setObject(9, value.getPATIENT_IO());
			ps.setObject(10, value.getFLAG());
			ps.executeUpdate();
			connection.close();
			ps.close();
			
			result = getOne(value.getPATIENT_ID());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	@Override
	public TPatient update(TPatient value){
		TPatient result = new TPatient();
		try{
			Connection connection = getConnection("update "+value.getPATIENT_NAME());
			StringBuffer query = new StringBuffer();
			query.append("UPDATE TPATIENT SET ");
			query.append("PATIENT_KEY=?,"
					+ "INSTITUTION_KEY=?, "
					+ "PATIENT_ID=?,"
					+ "PATIENT_NAME=?,"
					+ "LAST_STUDYDATE=?,"
					+ "PATIENT_BIRTH_DATE=?,"
					+ "PATIENT_AGE=?,"
					+ "PATIENT_SEX=?,"
					+ "PATIENT_IO=?,"
					+ "FLAG=? ");
			query.append("WHERE PATIENT_ID=?");
			PreparedStatement ps = connection.prepareStatement(query.toString());
			ps.setObject(1, value.getPATIENT_KEY());
			ps.setObject(2, value.getINSTITUTION_KEY());
			ps.setObject(3, value.getPATIENT_ID());
			ps.setObject(4, value.getPATIENT_NAME());
			ps.setObject(5, value.getLAST_STUDYDATE());
			ps.setObject(6, value.getPATIENT_BIRTH_DATE());
			ps.setObject(7, value.getPATIENT_AGE());
			ps.setObject(8, value.getPATIENT_SEX());
			ps.setObject(9, value.getPATIENT_IO());
			ps.setObject(10, value.getFLAG());
			ps.setObject(11, value.getPATIENT_ID());
			ps.executeUpdate();
			connection.close();
			ps.close();
			
			result = getOne(value.getPATIENT_ID());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
	@Override
	public TPatient delete(String noCm){
		TPatient result = new TPatient();
		try{
			Connection connection = getConnection("delete "+noCm);
			StringBuffer query = new StringBuffer();
			query.append("DELETE FROM TPATIENT WHERE PATIENT_ID=?");
			PreparedStatement ps = connection.prepareStatement(query.toString());
			ps.setObject(1, noCm);
			ps.execute();
			connection.close();
			ps.close();
			
			result = getOne(noCm);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return result;
	}
	
}
