import { Helper } from "./Helper";
import{AppService } from "../refs/nslib"
import { logger } from "mslib";
import{ChargingApi} from "./ChargingApi"
/**
 * 充电桩抵扣
 * @date 2022-08-16 10:15:24
 * @author 侯洪莉
 */
class ChargingDeduction {

    async deductionService(parameters: ReqChargingApiData):Promise<DeductionResponse>{
        let chargingData= parameters
        let mustFields = [
            { "text": "充电桩编号", "value": "pileId" },
            { "text": "车场Id", "value": "parkId" },
            { "text": "车牌号", "value": "plateNo" },
            { "text": "充电订单号", "value": "orderNo" },
            { "text": "充电时长", "value": "chargingTime" },
        ]
        let checkRes = Helper.ParamsIsNullCheck(chargingData, mustFields);
        if (checkRes != true) {
            return DeductionResponse.failWithMsg(checkRes)
        }
        //获取当前车场的统一平台配置文件 
        let tyConfig = await new  AppService().findParkExtendConfig(chargingData.parkId)
         //抵扣限制判断
        let discountLimitRes = await new  ChargingApi().getChargingLimit(chargingData, tyConfig)

         //抵扣限制返回不为0时 无法抵扣
        if (discountLimitRes.resCode != '0') return discountLimitRes;

        //调用充电桩抵扣接口并修改车辆信息
        let discountRes = await new ChargingApi().discount(chargingData, discountLimitRes.data, tyConfig)
        return discountRes
       
       
    }

}

/**
 * 充电桩抵扣请求参数类 
 */
export class ReqChargingApiData {
    /**
     * 充电桩品牌的appId 必填
     */
    pileId: string;   
    /**
     * 科拓车场Id
     */
    parkId: string;
    /**
     * 车牌号
     */
    plateNo: string;
    /**
     * 充电时长
     */
    chargingTime: Number; 
    /**
     * 充电订单号
     */
    orderNo: string;  
    /**
     * 第三方抵扣数量
     */
    discountNumber?:Number; 
    /**
     * 第三方抵扣类型
     */
    discountType?:Number; 
    /**
     * 充电开始时间
     */
    startTime?: string;  
    /**
     * 充电结束时间
     */
    endTime?: string;  
    /**
     * 充电站Id
     */
    stationId?: string; 
    /**
     * 充电站名称
     */
    stationName?: string;   
    /**
     * 设备Id
     */
    deviceId?: string;      
    /**
     * 设备名称
     */
    deviceName?: string   
    /**
     * 车位号
     */
    spaceNo?: string    
    /**
     * 充电量 单位：度，小数点后2位
     */
    power?: Number;   
    /**
     * 电费 单位 分
     */
    elecMoney?: Number;  
    /**
     * 服务费 单位：分
     */
    seviceMoney?: Number;   
    /**
     * 总费用 单位：分
     */
    totalMoney?: Number; 

}


/**
 * 抵扣接口响应类
 */
export class DeductionResponse<T = any> {
    constructor(resCode: any) {
        this.resCode = resCode;
    }

    /**
     * 响应码,0为成功
     */
    resCode: string;

    /**
     * 响应信息
     */
    resMsg?: string;

    /**
     * 响应数据
     */
    data?: T;

    /**
     * 判断响应码是否为成功
     */
    static resCodeIsSuccess(resCode: string) {
        return resCode?.toString() === '0';
    }

    /**
     * 成功响应实例,resCode=0
     */
    static readonly success: DeductionResponse = new DeductionResponse<void>('0');

    /**
     * 创建一个失败响应，默认响应码为-1
     * @param resMsg 
     * @returns 
     */
    static failed(resMsg?: string) {
        let response = new DeductionResponse('-1');
        response.resMsg = resMsg;
        return response;
    }



     static successRes(resMsg: string,data: any) {
        let response = new DeductionResponse('0');
        response.resMsg = resMsg;
        response.data = data;
        return response;
    }

   

    static successWithData(data: any) {
        let response = new DeductionResponse('0');
        response.data = data;
        return response;
    }

    static successWithMsg(resMsg: string) {
        let response = new DeductionResponse('0');
        response.resMsg = resMsg;
        return response;
    }

    static failWithMsg(resMsg: string) {
        let response = new DeductionResponse('-1');
        response.resMsg = resMsg;
        return response;
    }

    static failRes(resMsg: string,data: any,) {
        let response = new DeductionResponse('-1');
        response.data = data;
        response.resMsg = resMsg;
        return response;
    }
}

export {ChargingDeduction}