/**
 * 统一对内接口模块
 * 收费系统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.1 入口识别车牌修改
     */
    async carInModify(request: {
        parkId:any;
        nodeId:any;
        serial:any;
        carStyle:'';
        carNo:any;
        carNo2?:any;
        carNoColor?:any;
        imgName?:any;
        userId?:any;
        serviceCode: 'CarInModify';
    }): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.4.12 出口识别车牌修改' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/control/carInModify', 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.4.9 下发服务时间
     */
    async pushServiceDate(request: PushServiceDateRequest): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.4.9 下发服务时间' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/control/pushServiceDate', request, config);

        return axiosResponse.data;
    }

    /**
     * 2.4.12 出口识别车牌修改
     */
    async carOutModify(request: {
        parkId:any;
        nodeId:any;
        deviceIp?:string;
        serial:any;
        carStyle?:any;
        carNo?:any;
        carNo2?:any;
        carNoColor?:any;
        carBrand?:any;
        imgName?:any;
        operName?:any;
        recogEnable?:any;
        carOutTime?:any;
        /**
         * 修改项 0-车牌 1-车型  2-车牌颜色 3-车辆品来
         */
        modifyItem:number;
        serviceCode: 'CarOutModify';
    }): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.4.12 出口识别车牌修改' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/control/carOutModify', 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;
    }

    /**
     * 2.14.1 非标表历史数据迁移备份
     */
    async nsReportBackup(request: ReStatisticsRequest): Promise<ApiResponse> {
        let config: any = { apiDesc: '2.14.1 非标表历史数据迁移备份' };
        let axiosResponse = await axiosInstance.post<ApiResponse>('/nkcInnerApi/backup/nsReportBackup', request, config);

        return axiosResponse.data;
    }

    /**
     * SAAS统一接口协议-2.16.1系统公告推送
     */
    async addSysNotice(request: {
        parkId:any;
        serviceCode:'addSysNotice';
        allRole:string;
        classId:string;
        content:string;
        enabledTimeStart:string;
        enabledTimeEnd:string;
        lotIds:any;
        lotVersion:string;
        sendStatus:any;
        spLot:string;
        title:string;
    }) {
        let config: any = { apiDesc: '2.16.1系统公告推送' ,headers:{KT_LOT_ID:request.parkId}};
        let axiosResponse = await axiosInstance.post<ApiResponse<string>>('/saas/saas-inner-api/notice/addSysNotice', request, config);
        return axiosResponse.data;
    }

    /**
     * SAAS统一接口协议-停车场云助手app模板消息发送
     */
    async messageSend(request: {
        parkId:any;
        serviceCode:'saasMessageSend';
        templateId:string;
        parmMap:any;
        appLinkUrl?:string;
        pcLinkUrl?:string;
    }) {
        let config: any = { apiDesc: '停车场云助手app模板消息发送' ,headers:{KT_LOT_ID:request.parkId}};
        let axiosResponse = await axiosInstance.post<ApiResponse<string>>('/saasConsole/kos_platform_api/service/open/message/send', 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 PushServiceDateRequest extends ApiRequestBase {
    /**
     * 服务时间 yyyy-MM-dd HH:mm:ss
     */
    serviceDate: string;
    accountDate?: string;
    sysVersion?: 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 }