/**
 * 统一对内接口模块
 * 收费系统6.X-统一对内接口（inner）.docx
 */

import { ApiRequestBase, ApiResponse, throwIfResCodeNoSuccess } from './TyApiCommon'
import { AxiosInstance, AxiosResponse } from "axios";

/**
 * 统一对内接口客户端
 */
export default class TyInternalApiClient {
    axiosInstance: AxiosInstance;

    constructor(axiosInstance: AxiosInstance) {
        this.axiosInstance = axiosInstance;
    }

    /**
     * 2.4.6 在场车辆清理
     */
    async carLeave(request: CarLeaveRequest): Promise<AxiosResponse<ApiResponse>> {
        let config: any = { apiDesc: '2.4.6 在场车辆清理' };
        return await this.axiosInstance.post<ApiResponse>('/nkcInnerApi/carcome/carLeave', request, config);
    }

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

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

        return axiosResponse.data.data;
    }

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

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

export class ChannelSendReuqst<T = any> extends ApiRequestBase {
    readonly serviceCode: string = 'ChannelSend';

    cmd: string;
    data: T
}

export class SelectOneRequest extends ApiRequestBase {
    readonly serviceCode: string = 'selectOne';
    sql: string;
}

export class ListRequest extends ApiRequestBase {
    readonly serviceCode: string = 'list';
    sql: string;
}

export class ExecuteRequest extends ApiRequestBase {
    readonly serviceCode: string = 'execute';
    sqlList: string[];

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

export { TyInternalApiClient }
