/*
 * @Author: Kane
 * @Date: 2023-10-17 11:18:19
 * @LastEditors: Kane
 * @FilePath: /task_schedule/src/utils/BIReport.ts
 * @Description: BI报表相关的代码
 * 
 * Copyright (c) ${2023} by Kane, All Rights Reserved. 
 */
import { service as instance } from "./api/request.ts";
import { API_URL } from "./api/config.ts";
import { type AxiosResponse } from "axios";
interface BIReportType
{
    reportTypeCode: number;
    reportTypeName: string;
}

/**
 * 导入报表的请求对象
 */
interface ImportBIReportRequest
{
    filePath: string,
    reportType: number,
    firstRow: number,
    sheetIndex: number,
}

/**
 * 导入报表的相应对象
 */
interface ImportBIReportResponse
{
    success: boolean, // 请求成功标志
    message: string, // 请求结果说明
    importedCount: number, // 导入记录的数量
}

// BI坐席渗透率报表记录
interface BITelsalerAttachingRateRecord
{
    departmentName: string; // 部门
    telsalerName: string; // 经办人
    motoPremium: number; // 车险保费
    nomotoPremium: number; // 非车险保费
    motoPremiumProportion: number; // 车险保费占比
    attachingRate: number; // 保费渗透率
    attachingRateChange: number; // 渗透率环比上月
    customerHandleRate: number; // 当月客户渗透率
    customerHandleRateChange: number; // 客户渗透率环比上月
    noMotoPremiumPerCustomer: number; // 当月车非客均保费
    noMotoPremiumPerCustomerChange: number; // 客均保费环比上月
}

interface QueryBITelsalerAttachingRateReportResponse
{
    success: boolean,
    message: string,
    records: BITelsalerAttachingRateRecord[];
}

/**
 * BI坐席续保率报表记录
 */
interface BITelsalerRenewalRateRecord
{
    责任部门: string;
    责任人: string;
    机构目标值: number;
    到期数全月: number;
    序时到期数占比: number;
    个车续保率序时: number;
    个车续保率全月: number;
    环比昨日: number;
    环比上月: number;
    平均提前签单天数: number;
    环比: number;
}

interface QueryBITelsalerRenewalRateReportResponse
{
    success: boolean;
    message: string;
    records: BITelsalerRenewalRateRecord[];
}

/** 机构 ***/
// 机构渗透率
/**
 * 机构渗漏率报表记录
 */
interface BIDepartmentAttachingRateRecord
{
    departmentName: string;
    departmentObject: number;
    objectGap: number;
    motoPremium: number;
    motoPremiumProPortion: number;
    nomotoPremium: number;
    attachingRate: number;
    attachingRateChange: number;
    motoInsuranceCustomerCount: number;
    customerHandleRate: number;
    customerHandleRateChange: number;
    premiumPerCustomer: number;
    premiumPerCustomerChange: number;
}

interface QueryBIDepartmentAttachingRateResponse
{
    success: boolean;
    message: string;
    records: BIDepartmentAttachingRateRecord[];
}

// 机构续保率
interface BIDepartmentRenewalRateRecord
{
    责任部门: string;
    机构目标值: number;
    到期数全月: number;
    序时到期数占比: number;
    个车续保率序时: number;
    个车续保率全月: number;
    环比昨日: number;
    环比上月: number;
    平均提前签单天数: number;
    环比: number;
}

interface QueryBIDepartmentRenewalRateResponse
{
    success: boolean;
    message: string;
    records: BIDepartmentRenewalRateRecord[];
}

type ImportBIReportResponseHandler = ( response: ImportBIReportResponse, error: any ) => void;
type QueryBITelsalerAttachingRateDataHandler = ( response: QueryBITelsalerAttachingRateReportResponse, error: any ) => void;
type QueryBITelsalerRenewalRateDataHandler = ( response: QueryBITelsalerRenewalRateReportResponse, error: any ) => void;
type QueryBIDepartmentAttachingRateDataHandler = ( response: QueryBIDepartmentAttachingRateResponse, error: any ) => void;
type QueryBIDepartmentRenewalRateDataHandler = ( response: QueryBIDepartmentRenewalRateResponse, error: any ) => void;

/**
 * 请求导入BI报表
 * @param request 请求参数
 * @param handler 调用者提供的请求结果处理函数
 */
function importBIReport( request: ImportBIReportRequest, handler: ImportBIReportResponseHandler ): void
{
    const importResponse: ImportBIReportResponse = {
        success: false,
        message: "",
        importedCount: 0,
    };

    instance.request({
        url: API_URL.URL_IMPORT_REPORT,
        method: "post",
        data: request,
    })
        .then(( response: AxiosResponse<any, any> ): any =>
        {
            const data: ImportBIReportResponse = response.data;

            importResponse.success = data.success ?? false;
            importResponse.message = data.message ?? "请求完成，但服务器未返回请求结果消息！";
            importResponse.importedCount = data.importedCount ?? 0;

            handler( importResponse, null );
        })
        .catch(( error: any ): any =>
        {
            importResponse.message = "请求导入报表错误，请查看控制台！";
            console.log( error );

            handler( importResponse, error );
        });
}

/**
 * 请求BI坐席渗透率数据
 * @param handler 处理请求结果的handler函数。
 */
function queryBITelsalerAttachingRateData( handler: QueryBITelsalerAttachingRateDataHandler ): void
{
    const queryResponse: QueryBITelsalerAttachingRateReportResponse = {
        success: false,
        message: "",
        records: [],
    };

    instance.request({
        url: API_URL.URL_BI_TELSALER_ATTACHINGRATE,
        method: "post",
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data = response.data ?? {};

            queryResponse.success = data.success ?? false;
            queryResponse.message = data.message ?? "";
            queryResponse.records = data.records ?? [];

            console.log( "请求BI坐席渗透率数据", queryResponse );
            handler( queryResponse, null );
        })
        .catch(( error ): void =>
        {
            queryResponse.message = "请求BI坐席渗漏率报表数据时出现错误，请查看控制台消息！";
            queryResponse.success = false;

            console.log( error );

            handler( queryResponse, error );
        });
}

/**
 * 请求BI坐席续保率数据
 * @param handler 调用者提供的处理请求结果的函数。
 */
function queryBITelsalerRenewalData( handler: QueryBITelsalerRenewalRateDataHandler ): void
{
    const queryResponse: QueryBITelsalerRenewalRateReportResponse = {
        success: false,
        message: "",
        records: [],
    };

    instance.request({
        url: API_URL.URL_BI_TELSALER_RENEWALRATE,
        method: "post",
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data = response.data ?? {};

            queryResponse.success = data.success ?? false;
            queryResponse.message = data.message ?? "";
            queryResponse.records = data.records ?? [];

            console.log( "请求BI坐席续保率数据", queryResponse );
            handler( queryResponse, null );
        })
        .catch(( error: any ): void =>
        {
            queryResponse.success = false;
            queryResponse.message = "查询BI坐席续保率数据失败，请查看控制台！";

            console.log( error );

            handler( queryResponse, error );
        });
}

/**
 * 请求机构渗透率数据
 * @param handler 调用者提供的处理请求结果的函数。
 */
function queryDepartmentAttachingRateData( handler: QueryBIDepartmentAttachingRateDataHandler ): void
{
    const queryResponse: QueryBIDepartmentAttachingRateResponse = {
        success: false,
        message: "",
        records: [],
    };

    instance.request({
        url: API_URL.URL_BI_DEPARTMENT_ATTACHINGRATE,
        method: "post",
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data = response.data ?? {};

            queryResponse.success = data.success ?? false;
            queryResponse.message = data.message ?? "请求完成，但结果未知！";
            queryResponse.records = data.records ?? [];

            handler( queryResponse, null );
        })
        .catch(( error: any ): void =>
        {
            queryResponse.success = false;
            queryResponse.message = "查询BI机构续渗透失败，请查看控制台！";
            queryResponse.records = [];

            console.log( "查询BI机构渗透率失败:", error );

            handler( queryResponse, error );
        });
}

/**
 * 请求BI机构续保率数据
 * @param handler 调用者提供的处理请求结果的函数。
 */
function queryDepartmentRenewalRateData( handler: QueryBIDepartmentRenewalRateDataHandler ): void
{
    const queryResponse: QueryBIDepartmentRenewalRateResponse = {
        success: false,
        message: "",
        records: [],
    };

    instance.request({
        url: API_URL.URL_BI_DEPARTMENT_RENEWALRATE,
        method: "post",
    })
        .then(( response: AxiosResponse<any, any> ): void =>
        {
            const data = response.data ?? {};

            queryResponse.success = data.success ?? false;
            queryResponse.message = data.message ?? "请求完成，但结果未知！";
            queryResponse.records = data.records ?? [];

            handler( queryResponse, null );
        })
        .catch(( error: any ): void =>
        {
            queryResponse.success = false;
            queryResponse.message = "查询BI机构续保率失败，请查看控制台！";
            queryResponse.records = [];

            console.log( "查询BI机构续保率失败:", error );

            handler( queryResponse, error );
        });
}

export {
    type BIReportType,
    type ImportBIReportRequest,
    type ImportBIReportResponse,
    type ImportBIReportResponseHandler,
    type BITelsalerAttachingRateRecord,
    type QueryBITelsalerAttachingRateReportResponse,
    type QueryBITelsalerAttachingRateDataHandler,
    type QueryBITelsalerRenewalRateDataHandler,
    type BITelsalerRenewalRateRecord,
    type QueryBITelsalerRenewalRateReportResponse,
    type BIDepartmentAttachingRateRecord,
    type QueryBIDepartmentAttachingRateResponse,
    type QueryBIDepartmentAttachingRateDataHandler,
    type BIDepartmentRenewalRateRecord,
    type QueryBIDepartmentRenewalRateResponse,
    type QueryBIDepartmentRenewalRateDataHandler,
    importBIReport,
    queryBITelsalerAttachingRateData,
    queryBITelsalerRenewalData,
    queryDepartmentAttachingRateData,
    queryDepartmentRenewalRateData
};
