import { AppService } from "../refs/nslib"
import { logger } from "mslib";
import { ChargingPileService } from "./ChargingPileService"
import { Singleton, Inject, Container } from "typescript-ioc";
/**
 * 充电桩抵扣
 * @date 2022-08-16 10:15:24
 * @author 侯洪莉
 */
class ChargingDeduction {
    @Inject
    appService: AppService
    @Inject
    chargingPileService: ChargingPileService


    async deductionService(parameters: ReqChargingDeductionData): Promise<DeductionResponse> {
        let chargingData = parameters
        //appId作为充电桩Id
        let appId = Container.getValue('config.tyApi').appId
        Object.assign(chargingData, { pileId: appId })

        let mustFields = [
            { "text": "充电桩编号", "value": "pileId" },
            { "text": "车场Id", "value": "parkId" },
            { "text": "车牌号", "value": "plateNo" },
            { "text": "充电订单号", "value": "orderNo" },
            { "text": "充电时长", "value": "chargingTime" },
        ]
        let checkRes = await this.paramsIsNullCheck(chargingData, mustFields);
        if (checkRes != true) {
            return DeductionResponse.failWithMsg(checkRes)
        }

        //抵扣限制判断
        let discountLimitRes = await this.chargingPileService.getChargingLimit(chargingData)

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

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


    }



    /**
     * 验证参数是否为空
     * @param data 参数
     * @param mustFields 需要验证的参数集合 string,number,object,bigint,boolean,symbol,undefined,function
     */
    async paramsIsNullCheck(data, mustFields): Promise<any> {
        for (let i = 0; i < mustFields.length; i++) {
            let item = mustFields[i];
            let val = data[item.value];
            let type = typeof (val);
            switch (type) {
                case 'string':
                    if (val == null || val == '') {
                        return item.text + "为必填项！";
                    }
                    break;
                case 'number':
                    if (Object.is(val, NaN)) {
                        return item.text + "为必填项！";
                    }
                    break;
                case 'object':
                    if (val == null || !val.length) {
                        return item.text + "为必填项！";
                    }
                    break;
                default:
                    if (val == null) {
                        return item.text + "为必填项！";
                    }
                    break;
            }
        }
        return true;
    }

}

/**
 * 充电桩抵扣请求参数类 
 */
export class ReqChargingDeductionData {

    /**
     * 科拓车场Id
     */
    parkId: string;
    /**
     * 车牌号
     */
    plateNo: string;
    /**
     * 充电时长
     */
    chargingTime: Number;
    /**
     * 充电订单号 可以是自己生成一个uuid 保证唯一就可以
     */
    orderNo: string;
    /**
     * 第三方抵扣数量
     */
    discountNumber?: Number;
    /**
     * 第三方抵扣类型
     * 0：抵扣时间
     * 1：抵扣金额
     */
    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 }