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

import { Singleton } from "typescript-ioc";
import { ApiRequestBase, ApiResponse, axiosInstance, throwIfResCodeNoSuccess } from './KTApiCommon'
import { AxiosResponse } from "axios";

/**
 * 对内API客户端类
 */
@Singleton
export default class InternalApiClient {
    /**
     * 2.4.6 在场车辆清理
     */
     async carLeave(request: CarLeaveRequest | any): Promise<AxiosResponse<ApiResponse>> {
        return await axiosInstance.post<ApiResponse>('/nkcInnerApi/carcome/carLeave', request);
    }

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

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

        return axiosResponse.data.data;
    }

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

        return axiosResponse.data.data;
    }

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

export class CarLeaveRequest extends ApiRequestBase {
    readonly serviceCode: string = 'carLeave';

    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 { InternalApiClient }
