"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChargingApi = void 0;
var ChargingDeduction_1 = require("./ChargingDeduction");
var nslib_1 = require("../refs/nslib");
var ChargingBusDeal_1 = require("./ChargingBusDeal");
var mslib_1 = require("mslib");
/**
 * 抵扣方式判断和抵扣处理
 * @date 2022-08-16 13:56:23
 * @author 侯洪莉
 */
var ChargingApi = /** @class */ (function () {
    function ChargingApi() {
    }
    /**
     * 抵扣方式限制判断
     * @param {*}  ReqChargingApiData
     * @tyConfig {*}  tyConfig 车场配置文件
     * @返回值 resData：{PileRule,FreeData} PileRule：使用规则 FreeData:可免费金额和时长
     */
    ChargingApi.prototype.getChargingLimit = function (param, tyConfig) {
        return __awaiter(this, void 0, void 0, function () {
            var resData, plateNo, parkId, pileId, startTime, endTime, chargingTime, carplateDataRst, _a, entryTime, elapsedTime, payable, parkCarInfo, configRule, timeCanUse_c, moneyCanUse_c, _b, timeCanUse_1, moneyCanUse_1, pileRule, _c, timeCanUse_p, moneyCanUse_p, timeCanUse, moneyCanUse //本次抵扣可使用时间和金额
            , time, freeTime, money;
            return __generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        resData = {};
                        plateNo = param.plateNo, parkId = param.parkId, pileId = param.pileId, startTime = param.startTime, endTime = param.endTime, chargingTime = param.chargingTime;
                        return [4 /*yield*/, new nslib_1.TyExternalApiClient().getParkingPaymentInfo({ parkId: parkId, plateNo: plateNo })];
                    case 1:
                        carplateDataRst = _d.sent();
                        if (!(carplateDataRst.resCode != '0')) return [3 /*break*/, 3];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg(carplateDataRst.resMsg)];
                    case 2: 
                    // 获取停车信息失败
                    return [2 /*return*/, _d.sent()];
                    case 3:
                        _a = carplateDataRst.data, entryTime = _a.entryTime, elapsedTime = _a.elapsedTime, payable = _a.payable;
                        parkCarInfo = {
                            inTime: entryTime,
                            parkTime: elapsedTime * 60,
                            payMoney: payable,
                        };
                        return [4 /*yield*/, new ChargingBusDeal_1.ChargingBusDeal().getPileConfig(parkId)];
                    case 4:
                        configRule = _d.sent();
                        timeCanUse_c = Infinity //全局限制判断本次可用抵扣时间
                        ;
                        moneyCanUse_c = Infinity //全局限制判断本次可用抵扣金额 默认不限制全局
                        ;
                        if (!configRule) return [3 /*break*/, 8];
                        return [4 /*yield*/, new ChargingBusDeal_1.ChargingBusDeal().getConfigRuleLimit(param, configRule, parkCarInfo)];
                    case 5:
                        _b = _d.sent(), timeCanUse_1 = _b.timeCanUse, moneyCanUse_1 = _b.moneyCanUse;
                        if (!(timeCanUse_1 === 0 || moneyCanUse_1 === 0)) return [3 /*break*/, 7];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg("全局限制未通过，无法抵扣")];
                    case 6: return [2 /*return*/, _d.sent()];
                    case 7:
                        timeCanUse_c = timeCanUse_1;
                        moneyCanUse_c = moneyCanUse_1;
                        _d.label = 8;
                    case 8:
                        mslib_1.logger.info("\u5168\u5C40\u914D\u7F6E\u9650\u5236\u672C\u6B21\u53EF\u4F7F\u7528 timeCanUse_c-----:" + timeCanUse_c + ",moneyCanUse_c:" + moneyCanUse_c);
                        return [4 /*yield*/, new ChargingBusDeal_1.ChargingBusDeal().getPileRule(parkId, pileId)];
                    case 9:
                        pileRule = _d.sent();
                        if (!!pileRule) return [3 /*break*/, 11];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg("\u672A\u914D\u7F6E\u6216\u672A\u542F\u7528\u8BE5\u5145\u7535\u6869-" + pileId + "-\u4F7F\u7528\u89C4\u5219")];
                    case 10: return [2 /*return*/, _d.sent()];
                    case 11:
                        if (!(pileRule.chargeTime != 0 && chargingTime < pileRule.chargeTime)) return [3 /*break*/, 13];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg("充电时长未达到最小充电时长要求")];
                    case 12: return [2 /*return*/, _d.sent()];
                    case 13:
                        if (!(pileRule.discountSource == 1)) return [3 /*break*/, 16];
                        if (!((!(param === null || param === void 0 ? void 0 : param.discountNumber) && (param === null || param === void 0 ? void 0 : param.discountNumber) != 0) || (!(param === null || param === void 0 ? void 0 : param.discountType) && (param === null || param === void 0 ? void 0 : param.discountType) != 0))) return [3 /*break*/, 15];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("当抵扣来源为第三方时，抵扣类型和抵扣数量为必填值", param)];
                    case 14: return [2 /*return*/, _d.sent()];
                    case 15:
                        pileRule.discountNumber = param.discountNumber;
                        pileRule.discountType = param.discountType;
                        _d.label = 16;
                    case 16:
                        //返回参数添加当前抵扣规则
                        Object.assign(resData, {
                            PileRule: pileRule
                        });
                        return [4 /*yield*/, new ChargingBusDeal_1.ChargingBusDeal().getPileRuleLimit(param, pileRule, parkCarInfo)];
                    case 17:
                        _c = _d.sent(), timeCanUse_p = _c.timeCanUse, moneyCanUse_p = _c.moneyCanUse;
                        mslib_1.logger.info("\u4F7F\u7528\u89C4\u5219\u9650\u5236\u672C\u6B21\u53EF\u4F7F\u7528 timeCanUse_p-----:" + timeCanUse_p + "\u79D2,moneyCanUse_p:" + moneyCanUse_p + "\u5206");
                        //Number.isFinite(Infinity);  => false 当全局和单个使用规则都不限制时 赋值Infinity 无限大不能相减
                        if (!Number.isFinite(timeCanUse_p) && !Number.isFinite(timeCanUse_c)) {
                            timeCanUse = Infinity;
                        }
                        else {
                            timeCanUse = timeCanUse_c - timeCanUse_p >= 0 ? timeCanUse_p : timeCanUse_c; //全局可使用和单个可使用 取最小
                        }
                        if (!Number.isFinite(moneyCanUse_p) && !Number.isFinite(moneyCanUse_c)) {
                            moneyCanUse = Infinity;
                        }
                        else {
                            moneyCanUse = moneyCanUse_c - moneyCanUse_p >= 0 ? moneyCanUse_p : moneyCanUse_c;
                        }
                        mslib_1.logger.info("\u672C\u6B21\u53EF\u4F7F\u7528 timeCanUse-----:" + timeCanUse + "\u79D2,moneyCanUse:" + moneyCanUse + "\u5206");
                        if (!(pileRule.discountType === 0 && timeCanUse > 0)) return [3 /*break*/, 21];
                        time = pileRule.discountMethod === 0 ? pileRule.discountNumber : chargingTime;
                        freeTime = void 0;
                        if (pileRule.discountMethod === 0) {
                            freeTime = timeCanUse - time >= 0 ? time : 0;
                        }
                        else {
                            freeTime = timeCanUse - time >= 0 ? time : timeCanUse;
                        }
                        if (!(freeTime <= 0)) return [3 /*break*/, 19];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("\u62B5\u6263\u5931\u8D25\uFF0C\u914D\u7F6E\u7684\u53EF\u62B5\u6263\u503C" + time + "\u79D2>\u672C\u6B21\u53EF\u4F7F\u7528\u503C" + timeCanUse + "\u79D2", resData)];
                    case 18: return [2 /*return*/, _d.sent()];
                    case 19:
                        Object.assign(resData, {
                            FreeData: {
                                freeTime: freeTime,
                                freeMoney: 0 //单位分
                            }
                        });
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.successRes("\u53EF\u62B5\u6263\u65F6\u95F4" + freeTime + "\u79D2", resData)];
                    case 20: return [2 /*return*/, _d.sent()];
                    case 21:
                        if (!(pileRule.discountType === 1 && moneyCanUse > 0)) return [3 /*break*/, 25];
                        money = moneyCanUse >= pileRule.discountNumber ? pileRule.discountNumber : 0;
                        if (!(money <= 0)) return [3 /*break*/, 23];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("\u62B5\u6263\u5931\u8D25\uFF0C\u914D\u7F6E\u7684\u53EF\u62B5\u6263\u503C" + pileRule.discountNumber + "\u5206>\u672C\u6B21\u53EF\u4F7F\u7528\u503C" + moneyCanUse + "\u5206", resData)];
                    case 22: return [2 /*return*/, _d.sent()];
                    case 23:
                        Object.assign(resData, {
                            FreeData: {
                                freeTime: 0,
                                freeMoney: money //单位分
                            }
                        });
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.successRes("\u53EF\u62B5\u6263\u65F6\u95F4" + money + "\u5206", resData)];
                    case 24: return [2 /*return*/, _d.sent()];
                    case 25:
                        Object.assign(resData, {
                            timeCanUse: timeCanUse,
                            moneyCanUse: moneyCanUse
                        });
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("\u62B5\u6263\u5931\u8D25", resData)];
                    case 26: return [2 /*return*/, _d.sent()];
                }
            });
        });
    };
    /**
     * 车辆类型修改和 充电桩抵扣
     * @param {Object} param  ReqChargingApiData
     * @param {FreeData,PileRule} freeData 可抵扣信息
     * @param {Object} tyConfig 配置信息
     */
    ChargingApi.prototype.discount = function (param, discountData, tyConfig) {
        return __awaiter(this, void 0, void 0, function () {
            var FreeData, PileRule, modifyData, modifyRes, reqData, axiosResponse, discountRes, error_1;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        FreeData = discountData.FreeData, PileRule = discountData.PileRule;
                        _a.label = 1;
                    case 1:
                        _a.trys.push([1, 10, , 11]);
                        if (!(PileRule.isSpecialFeeRule == 1)) return [3 /*break*/, 4];
                        modifyData = {
                            parkId: param.parkId,
                            plateNo: param.plateNo,
                            carStyle: PileRule.carStyle,
                        };
                        return [4 /*yield*/, new nslib_1.TyExternalApiClient().modifyCarInfo(modifyData)];
                    case 2:
                        modifyRes = _a.sent();
                        if (!(modifyRes.data.resCode != 0)) return [3 /*break*/, 4];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("\u8F66\u8F86\u7C7B\u578B\u4FEE\u6539\u5931\u8D25--" + modifyRes.resMsg, { param: param, discountData: discountData })];
                    case 3: return [2 /*return*/, _a.sent()];
                    case 4:
                        reqData = {
                            parkId: param.parkId,
                            plateNo: param.plateNo,
                            startTime: param.startTime,
                            endTime: param.endTime,
                            orderNo: param.orderNo,
                            stationId: param.stationId || '11111',
                            stationName: param.stationName || 'xxxx',
                            deviceId: param.deviceId || param.pileId,
                            deviceName: param.deviceName || '无',
                            spaceNo: '无',
                            power: param.power || 0,
                            elecMoney: param.elecMoney || 0,
                            seviceMoney: param.seviceMoney || 0,
                            totalMoney: param.totalMoney || 0,
                            freeType: 0,
                            freeMoney: FreeData.freeMoney,
                            freeTime: FreeData.freeTime
                        };
                        return [4 /*yield*/, nslib_1.axiosInstance.post('/api/wec/SyncChargePilePay', reqData)];
                    case 5:
                        axiosResponse = _a.sent();
                        nslib_1.throwIfResCodeNoSuccess(axiosResponse.data);
                        discountRes = axiosResponse.data;
                        if (!(discountRes.data.resCode == 0)) return [3 /*break*/, 7];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.successRes("\u62B5\u6263\u6210\u529F", { param: param, discountData: discountData })];
                    case 6: return [2 /*return*/, _a.sent()];
                    case 7: return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("\u8F66\u8F86\u4FE1\u606F\u4FEE\u6539\u6210\u529F\uFF0C\u62B5\u6263\u5931\u8D25-" + discountRes.resMsg, { param: param, discountData: discountData })];
                    case 8: return [2 /*return*/, _a.sent()];
                    case 9: return [3 /*break*/, 11];
                    case 10:
                        error_1 = _a.sent();
                        mslib_1.logger.error("\u62B5\u6263\u5F02\u5E38", error_1.message);
                        return [3 /*break*/, 11];
                    case 11: return [2 /*return*/];
                }
            });
        });
    };
    return ChargingApi;
}());
exports.ChargingApi = ChargingApi;
//# sourceMappingURL=ChargingApi.js.map