/*
 * @Author: Kane
 * @Date: 2023-02-28 23:13:51
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/web/controllers/account/StaffInfoQueryController.java
 * @Description: 员工信息查询controller
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */
package com.cpic.xim.web.controllers.account;

import java.sql.SQLException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cpic.xim.utils.account.CpicXIMStaffInfo;
import com.cpic.xim.utils.account.TeleSalerInfo;


@Controller
//@CrossOrigin
@RequestMapping( "/account" )
public class StaffInfoQueryController
{
    /**
     * @description: 
     * @param account: 前端发送的请求内容，员工的P13账号或P09工号。
     * @return {*}
     */
    @RequestMapping( "/query_staff_info.do" )
    @ResponseBody
    public StaffInfoQueryResult queryStaffInfo( @RequestParam( "account" ) String account )
    {
        StaffInfoQueryResult result = new StaffInfoQueryResult();
        CpicXIMStaffInfo staffInfo = null;

        try
        {
            staffInfo = CpicXIMStaffInfo.getStaffInfo( account );

            result.setStaffInfo( staffInfo );
            result.setSuccess( true );
            result.setMessage( "查询成功" );
        }
        catch ( SQLException error )
        {
            result.setSuccess( false );
            result.setMessage( error.getMessage() );
        }
        catch ( ClassNotFoundException error )
        {
            result.setSuccess( false );
            result.setMessage( error.getMessage() );
        }

        return result;
    }

    /**
     * 查询坐席工号的接口方法。
     * @param telSaler 
     * @return 返回表示查询结果的 TeleSalerQueryResult 对象，转换成JSON。
     */
    @RequestMapping( "/query_telsaler_info.do" )
    @ResponseBody
    public TeleSalerQueryResult queryTeleSalerInfo(
            @RequestBody TelsalerQueryRequest telSaler )
    {
        TeleSalerQueryResult result = new TeleSalerQueryResult();
        TeleSalerInfo saler = null;

        try
        {
            saler = TeleSalerInfo.queryTeleSalerInfo( telSaler.getTelSaler() );

            // 根据返回结果是否为 null，判断是否查询到坐席信息
            if ( saler != null )
            {
                // 不为 null，有查询到
                result.setTelSalerCode( saler.getCode() );
                result.setTelSalerName( saler.getName() );
                result.setTeamCode( saler.getTeamCode() );
                result.setTeamName( saler.getTeamName() );
                result.setDepartmentCode( saler.getDepartmentCode() );
                result.setDepartmentName( saler.getDepartmentName() );

                result.setSuccess( true );
            } else
            {
                // 为 null 则没查询到
                result.setSuccess( false );
                result.setMessage( "未查询到坐席信息！" );
            }
        }
        catch ( SQLException error )
        {
            result.setSuccess( false );
            result.setMessage( "查询工号失败，失败原因：" + error.getMessage() );
        }
        catch ( ClassNotFoundException error )
        {
            result.setSuccess( false );
            result.setMessage( error.getMessage() );
        }

        return result;
    }
}

