/*
 * @Author: Kane
 * @Date: 2023-03-14 16:38:06
 * @LastEditors: Kane
 * @FilePath: /desktop_archievement_backend/src/main/java/com/cpic/xim/web/controllers/archievement/ArchievementQueryController.java
 * @Description: 
 * 业绩查询控制器：
 * 1、queryDepartmentArchievement响应部门业绩查询；
 * 2、
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */

package com.cpic.xim.web.controllers.archievement;

import java.io.IOException;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cpic.xim.utils.archievement.CallerArchievement;
import com.cpic.xim.utils.archievement.DepartmentArchievement;
import com.cpic.xim.utils.ranking.CallerRankingItem;
import com.cpic.xim.utils.ranking.CallerRankingList;
import com.cpic.xim.web.controllers.archievement.RankingList.RankingListRequest;
import com.cpic.xim.web.controllers.archievement.RankingList.RankingListResponse;
import com.cpic.xim.web.controllers.archievement.caller.CallerArchievementQueryRequest;
import com.cpic.xim.web.controllers.archievement.caller.CallerArchievementQueryResponse;
import com.cpic.xim.web.controllers.archievement.department.DepartmentArchievementQueryRequest;
import com.cpic.xim.web.controllers.archievement.department.DepartmentArchievementQueryResult;

@Controller
@RequestMapping( "/archievement" )
public class ArchievementQueryController
{
    // 日志
    private static Logger logger = LoggerFactory.getLogger( ArchievementQueryController.class );

    /**
     * 查询坐席业绩
     */
    // @PostMapping( "/query_caller_archievement.do" )
    // public void queryCallerArchievement()
    // {

    // }

    /**
     * 查询部门业绩
     * @param request DepartmentArchievementQueryRequest 类型的部门参数。
     * @return 返回 DepartmentArchievementQueryResult 对象。
     */
    @PostMapping( "/query_department_archievement.do" )
    @ResponseBody
    public DepartmentArchievementQueryResult queryDepartmentArchievement(
            @RequestBody DepartmentArchievementQueryRequest request )
    {
        DepartmentArchievementQueryResult result = new DepartmentArchievementQueryResult();
        DepartmentArchievement departmentArch = null;

        try
        {
            // 查询业绩
            departmentArch =
                    DepartmentArchievement.getDepartmentArchievement( request.getDepartmentCode() );

            result.setTotalArchievement( departmentArch.getTotalArchievement() );
            result.setInsuranceRenewalRate( departmentArch.getInsuranceRenewalRate() );
            result.setInsuranceRenewalRateTarget( departmentArch.getInsuranceRenewalRateTarget() );
            result.setAttachingRate( departmentArch.getAttachingRate() );
            result.setAttachingRateTarget( departmentArch.getAttachingRateTarget() );

            result.setMensualArchievementList( departmentArch.getMensualArchievementList() );
        }
        catch ( IOException error )
        {

        }

        result.setSuccess( true );
        result.setMessage( "成功" );

        return result;
    }

    @ResponseBody
    @PostMapping( "/query_caller_archievement.do" )
    public CallerArchievementQueryResponse queryCallerArchievement(
            @RequestBody CallerArchievementQueryRequest request )
    {
        CallerArchievementQueryResponse result = null;

        try
        {
            CallerArchievement archievement =
                    CallerArchievement.getCallerArchievement( request.getCallerCode() );

            result = new CallerArchievementQueryResponse( true, "查询成功", archievement.getCallerCode(),
                    request.getCallName(), archievement.getTotalArchievement(), archievement.getMotoPremiumPresentMonth(),
                    archievement.getMensualArchievementList(),
                    archievement.getInsuranceRenewalRate(), archievement.getAttachingRate() );
        }
        catch ( IOException error )
        {
            result = new CallerArchievementQueryResponse(false, error.getMessage(), null, null, 0, 0, null, null, null);
        }

        return result;
    }

    @RequestMapping( "/query_ranking_list.do" )
    @ResponseBody
    public RankingListResponse queryCallerArchievementRankingList(
            @RequestBody RankingListRequest request )
    {
        String departmentCode = request.getDepartmentCode();
        String year = request.getYear();
        String month = request.getMonth();

        boolean success = false;
        String message = "";

        ArrayList<CallerRankingItem> attachingRateRankingList = null;
        ArrayList<CallerRankingItem> renewalRateRankingList = null;


        try
        {
            CallerRankingList rankingList =
                    CallerRankingList.getCallerRankingList( departmentCode, year, month );

            attachingRateRankingList = rankingList.getAttachingRateRankingList();
            renewalRateRankingList = rankingList.getRenewalRateRankingList();

            success = true;
            message = "调用成功！";
        }
        catch ( IOException error )
        {
            success = false;
            message = "获取排行榜失败，原因：" + error.getMessage();

            logger.error( message, error );
        }

        RankingListResponse response = new RankingListResponse( success, message, departmentCode,
                year, month, attachingRateRankingList, renewalRateRankingList );

        return response;
    }
}
