package com.cpic.util.staff;

/**
 * Created by Kane on 2017/3/28.
 */

import java.io.*;
import java.sql.*;
import java.text.ParseException;

public class StaffInfo
{
    private String staffCode;
    private String staffName;
    private String sectionOfficeCode;
    private String sectionOfficeName;
    private String departmentCode;
    private String departmentName;

    public StaffInfo( String staffCode ) throws StaffCodeNotExistException, ClassNotFoundException
    {
        if ( staffCode.length() < 3 )
        {
            throw new StaffCodeNotExistException( staffCode + "不存在。");
        }

        String oracleURL = "jdbc:oracle:thin:@10.187.11.164:1521:xmcx1";
        String oracleUserName = "idst0";
        String oraclePassword = "cpic123456";

        this.staffCode = staffCode;

        Class.forName( "oracle.jdbc.driver.OracleDriver" );

        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet result = null;

        String sql =
                "SELECT ry.staff_name,\n" +
                "       ksh.section_office_code,\n" +
                "       ksh.section_office_name,\n" +
                "       bm.department_code,\n" +
                "       bm.department_name\n" +
                "  FROM idst0.rydm_t ry,\n" +
                "       idst0.ks_t   ksh,\n" +
                "       idst0.bm_t   bm\n" +
                " WHERE ry.staff_code = ?\n" +
                "   AND ry.department_code = bm.department_code\n" +
                "   AND ry.section_office_code = ksh.section_office_code";

        try
        {
            conn = DriverManager.getConnection( oracleURL, oracleUserName, oraclePassword );
            stmt = conn.prepareStatement(sql);
            stmt.setString( 1, staffCode );
            result = stmt.executeQuery();

            if ( result.next() )
            {
                staffName = result.getString( "staff_name" );
                sectionOfficeCode = result.getString( "section_office_code" );
                sectionOfficeName = result.getString( "section_office_name" );
                departmentCode = result.getString( "department_code" );
                departmentName = result.getString( "department_name" );
            }

            conn.close();
        }
        catch ( SQLException error )
        {
            //sql错误当做工号错误抛出
            throw new StaffCodeNotExistException( error.getMessage() );
        }
    }

    public String getStaffCode()
    {
        return staffCode;
    }

    public String getStaffName()
    {
        return staffName;
    }

    public String getSectionOfficeCode()
    {
        return sectionOfficeCode;
    }

    public String getSectionOfficeName()
    {
        return sectionOfficeName;
    }

    public String getDepartmentCode()
    {
        return departmentCode;
    }

    public String getDepartmentName()
    {
        return departmentName;
    }
}

