/*
 * @Author: Kane
 * @Date: 2023-04-06 14:14:23
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/utils/account/TeleSalerInfo.java
 * @Description: 
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */

package com.cpic.xim.utils.account;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@SuppressWarnings( "unused" )
public final class TeleSalerInfo
{
    /**
     * @description: 构造函数
     * @param code: 坐席工号
     * @param name: 坐席名称
     * @param teamCode: 团队代码
     * @param teamName: 团队名称
     * @param departmentCode: 部门代码
     * @param departmentName: 部门名称
     */
    private TeleSalerInfo( String code, String name, String teamCode, String teamName,
            String departmentCode, String departmentName)
    {
        this.code = code;
        this.name = name;
        this.teamCode = teamCode;
        this.teamName = teamName;
        this.departmentCode = departmentCode;
        this.departmentName = departmentName;
    }

    /**
     * 查询坐席工号的static方法。
     * @param telsaler 坐席工号
     * @return TeleSalerInfo对象。
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static TeleSalerInfo queryTeleSalerInfo( String telsaler )
            throws ClassNotFoundException, SQLException
    {
        TeleSalerInfo info = null;

        // 到时要改
        String userName = "desktop_archievement_admin";
        String password = "Cpic123456";
        String jdbcURL = "jdbc:oracle:thin:@10.39.0.86:1521:xmcx1";

        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet result = null;

        // String sql = """
        //         SELECT s.saler_code,
        //                s.saler_name,
        //                t.team_code,
        //                t.team,
        //                bm.department_code,
        //                bm.department_name
        //           FROM tele_saler s,
        //                tele_saler_team t,
        //                idst0.bm_t bm
        //          WHERE s.team_code = t.team_code
        //            AND t.department_code = bm.department_code
        //            AND (s.saler_code = ? or s.saler_name = ?) """;
        String sql2 = """
            select  ry.人员工号 ,
                    ry.人员姓名,
                    team.team_code,
                    team.team_name,
                    bm.department_code,
                    bm.department_name
               from twr_telsaler ry,
                    twr_telsaler_team team,
                    idst0.bm_t bm
              where trim(ry.团队名称) = trim(team.team_name)
                and team.department_code = bm.department_code
                AND (ry.人员工号 = ? or ry.人员姓名 = ?)""";
        // String sql = "SELECT DISTINCT s.saler_code, s.saler_name, \n"
        // + " t.team_code, t.team, \n"
        // + " bm.department_code, bm.department_name \n"
        // + " FROM tele_saler s, tele_saler_team t, \n"
        // + " i dst0.bm_t bm \n" + " WHERE s.team_code = t.team_code \n"
        // + " AND t.department_code = bm.department_code \n" + " AND s.saler_code = ? ";

        try
        {
            Class.forName( "oracle.jdbc.driver.OracleDriver" );
            connection = DriverManager.getConnection( jdbcURL, userName, password );
            statement = connection.prepareStatement( sql2 );

            statement.setString( 1, telsaler );
            statement.setString( 2, telsaler );

            result = statement.executeQuery();

            if ( result.next() )
            {
                String code = result.getString( 1 );
                String name = result.getString( 2 );
                String teamCode = result.getString( 3 );
                String teamName = result.getString( 4 );
                String departmentCode = result.getString( 5 );
                String departmentName = result.getString( 6 );

                info = new TeleSalerInfo( code, name, teamCode, teamName, departmentCode,
                        departmentName );
            }
        }
        finally
        {
            try
            {
                if ( result != null )
                {
                    result.close();
                }
            }
            catch ( Exception exception )
            {
                exception.printStackTrace();
            }

            try
            {
                if ( statement != null )
                {
                    statement.close();
                }
            }
            catch ( Exception exception )
            {
                exception.printStackTrace();
            }

            try
            {
                if ( connection != null )
                {
                    connection.close();
                }
            }
            catch ( Exception exception )
            {
                exception.printStackTrace();
            }
        }

        return info;
    }

    public String getCode()
    {
        return code;
    }

    public void setCode( String code )
    {
        this.code = code;
    }

    public String getName()
    {
        return name;
    }

    public void setName( String name )
    {
        this.name = name;
    }

    public String getTeamCode()
    {
        return teamCode;
    }

    public void setTeamCode( String teamCode )
    {
        this.teamCode = teamCode;
    }

    public String getTeamName()
    {
        return teamName;
    }

    public void setTeamName( String teamName )
    {
        this.teamName = teamName;
    }

    public String getDepartmentCode()
    {
        return departmentCode;
    }

    public void setDepartmentCode( String departmentCode )
    {
        this.departmentCode = departmentCode;
    }

    public String getDepartmentName()
    {
        return departmentName;
    }

    public void setDepartmentName( String departmentName )
    {
        this.departmentName = departmentName;
    }

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((teamCode == null) ? 0 : teamCode.hashCode());
        result = prime * result + ((teamName == null) ? 0 : teamName.hashCode());
        result = prime * result + ((departmentCode == null) ? 0 : departmentCode.hashCode());
        result = prime * result + ((departmentName == null) ? 0 : departmentName.hashCode());
        return result;
    }

    @Override
    public boolean equals( Object obj )
    {
        if ( this == obj )
            return true;
        if ( obj == null )
            return false;
        if ( getClass() != obj.getClass() )
            return false;
        TeleSalerInfo other = (TeleSalerInfo) obj;
        if ( code == null )
        {
            if ( other.code != null )
                return false;
        } else if ( !code.equals( other.code ) )
            return false;
        if ( name == null )
        {
            if ( other.name != null )
                return false;
        } else if ( !name.equals( other.name ) )
            return false;
        if ( teamCode == null )
        {
            if ( other.teamCode != null )
                return false;
        } else if ( !teamCode.equals( other.teamCode ) )
            return false;
        if ( teamName == null )
        {
            if ( other.teamName != null )
                return false;
        } else if ( !teamName.equals( other.teamName ) )
            return false;
        if ( departmentCode == null )
        {
            if ( other.departmentCode != null )
                return false;
        } else if ( !departmentCode.equals( other.departmentCode ) )
            return false;
        if ( departmentName == null )
        {
            if ( other.departmentName != null )
                return false;
        } else if ( !departmentName.equals( other.departmentName ) )
            return false;
        return true;
    }

    @Override
    public String toString()
    {
        return "TeleSalerInfo [code=" + code + ", name=" + name + ", teamCode=" + teamCode
                + ", teamName=" + teamName + ", departmentCode=" + departmentCode
                + ", departmentName=" + departmentName + "]";
    }

    private String code;
    private String name;
    private String teamCode;
    private String teamName;
    private String departmentCode;
    private String departmentName;
}
