/**
 * 统一对内接口模块
 * 收费系统6.X-统一对内接口（inner）.docx
 * @date 2022-03
 * @author 何华东
 */
import { TyApiConfig } from "./Config";
import { ApiRequestBase, ApiResponse, throwIfResCodeNoSuccess, axiosInstance, generateAxiosInstance } from './TyApiCommon'
import { Singleton, ObjectFactory, Factory } from 'typescript-ioc';
import { AxiosRequestConfig, AxiosInstance } from 'axios';

/**
 * 统一对内接口客户端
 * 
 * ```ts
 * import { Singleton, Inject } from "typescript-ioc";
 * import { IService } from "mslib"
 * import { TyApi, TyExternalApi, ApiResponse, TyInternalApiClient } from "refs/nslib";
 * 
 * export class PostCarInInfo implements IService {
 *    //去掉注释
 *    //@Inject
 *    internalApiClient:TyInternalApiClient;
 * 
 *    //去掉注释
 *    //@TyApi.Service()
 *    async doService(input: TyExternalApi.PostCarInInfoInput): Promise<ApiResponse> {
 *        let row = await this.internalApiClient.carLeave({
 *           
 *        });
 * 
 *        return ApiResponse.success;
 *    }
 * }
 * ``` 
 */
@Singleton
export default class TyInternalApiClient {
    async post(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse> {
        let axiosResponse = await axiosInstance.post<ApiResponse>(url, data, config);
        return axiosResponse.data;
    }
    /**
     * 2.2.7 获取图片存储根地址
     */
    async getImageBaseUrl(request: { parkId: any; serviceCode?: 'GetImageBaseUrl' }): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.2.7 获取图片存储根地址' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/normal/getImageBaseUrl', request, config);

        return axiosResponse.data;
    }
    /**
     * 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.5.4 内部车修改有效期
     */
    async modifyValidTime(request: ModifyValidTimeRequest): Promise<ApiResponse> {
        let config: any = { apiDesc: 'modifyValidTime' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/card/ModifyValidTime', request, config);

        return axiosResponse.data;
    }

    /**
    * 2.11.1 预约车退款状态回调
    */
    async parkingBookingRefundCallback(request: ReqBookingRefund): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.11.1 预约车退款状态回调' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('nkcInnerApi/booking/parkingBookingRefundCallback', 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: any, config: AxiosRequestConfig) {
        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 ModifyValidTimeRequest extends ApiRequestBase {
    cardId: string;
    beginTime: string;
    endTime: string;
    userId?: string;
    userName?: 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 ReqBookingRefund extends ApiRequestBase {
    readonly serviceCode = 'parkingBookingRefundCallback';

    /**
     * 订单号
     */
    orderNo: string;

    /**
     * 退款状态 0未退款 1退款中 2退款成功 3退款失败
     */
    status: number;

    /**
         * 退款时间 yyyy-MM-dd HH:mm:ss
         */
    refundTime: string;

    /**
     * 退款金额（分）
     */
    refundAmount: number;
}

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

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 }