/**
 * 统一对内接口模块
 * 收费系统6.X-统一对内接口（inner）.docx
 * @date 2022-03
 * @author 何华东
 */

import { ApiRequestBase, ApiResponse, throwIfResCodeNoSuccess, axiosInstance } from './TyApiCommon'
import { Singleton } from 'typescript-ioc';

/**
 * 统一对内接口客户端
 */
@Singleton
export default class TyInternalApiClient {
    /**
     * 2.4.6 在场车辆清理
     */
    async carLeave(request: CarLeaveRequest): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.4.6 在场车辆清理' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/carcome/carLeave', request, config);

        return axiosResponse.data;
    }

    /**
     * 2.13.2 重新统计汇总报表
     */
    async reStatistics(request: ReStatisticsRequest): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.13.2 重新统计汇总报表' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/report/reStatistics', request, config);

        return axiosResponse.data;
    }

    /**
     * MQ透传接口
     */
    async msgToLocal(request: MsgToLocalReuqest, url: string) {
        let config: any = { headers: { 'url': url } };
        let axiosResponse = await axiosInstance.post<ApiResponse<string>>('/non_standard/MsgToLocal', request, config);

        return axiosResponse.data;
    }

    /**
     * 3.1
     * 通道接口
     * 这是一个大类接口，子接口见ChannelApi
     */
    async channelSend(request: ChannelSendReuqst): Promise<ApiResponse> {
        let config: any = { apiDesc: '3.1 通道接口' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/normal/ChannelSend', request, config);

        return axiosResponse.data;
    }

    /**
     * SQL通道查询单条数据
     */
    async selectOne(request: SelectOneRequest): Promise<any> {
        let config = { headers: { 'kt-lotcodes': request.parkId, version: '1.0' }, apiDesc: 'sql通道-selectOne' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/dbproxy/selectOne', request, config);
        throwIfResCodeNoSuccess(axiosResponse.data);

        return axiosResponse.data.data;
    }

    /**
     * SQL通道查询多条数据
     */
    async list(request: ListRequest): Promise<[]> {
        let config = { headers: { 'kt-lotcodes': request.parkId, version: '1.0' }, apiDesc: 'sql通道-list' };
        let axiosResponse = await axiosInstance.post<ApiResponse<[]>>('/dbproxy/list', request, config);
        throwIfResCodeNoSuccess(axiosResponse.data);

        return axiosResponse.data.data;
    }

    /**
     * SQL通道批量执行语句
     * @returns 
     */
    async execute(request: ExecuteRequest): Promise<ApiResponse> {
        let config = { headers: { 'kt-lotcodes': request.parkId, version: '1.0' }, apiDesc: 'sql通道-execute' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/dbproxy/execute', request, config);
        return axiosResponse.data;
    }
}

export class GetNodeStatusListRequest extends ApiRequestBase {
    /**
     * 通道类型 默认全部,1进口,2出口,3中间入口,4中间出口
     */
    nodeType?: number;
}

export class CarLeaveRequest extends ApiRequestBase {
    billId?: string;
    cardNo?: string;
    carNo?: string;
}

export class ReStatisticsRequest extends ApiRequestBase {
    readonly serviceCode = 'InnerReStatistics';

    /**
     * 开始时间(yyyy-MM-dd)
     */
    startDate: string;

    /**
     * 截止时间(yyyy-MM-dd)
     */
    endDate: string;

    remark: string;

    /**
     * 是否延迟汇总(第二天执行汇总任务)，默认是
     */
    isDelay?: boolean;

    /**
     * 统计类型，1：收入日报，2：车流日报，3：全部统计
     */
    statisticType: 1 | 2 | 3;
}

export class ChannelSendReuqst<T = any> extends ApiRequestBase {
    cmd: string;
    data: T
}

export class MsgToLocalReuqest extends ApiRequestBase {

}

export class SelectOneRequest extends ApiRequestBase {
    sql: string;
}

export class ListRequest extends ApiRequestBase {
    sql: string;
}

export class ExecuteRequest extends ApiRequestBase {
    sqlList: string[];

    /**
     * 是否开启事务,1是，0否
     */
    openTx: 1 | 0;
}

export { TyInternalApiClient }
