"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
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.ChargingPileService = void 0;
var ChargingDeduction_1 = require("./ChargingDeduction");
var nslib_1 = require("../refs/nslib");
var ChargingPileDal_1 = require("./ChargingPileDal");
var mslib_1 = require("mslib");
var typescript_ioc_1 = require("typescript-ioc");
var uuid = require("uuid");
var dayjs = require("dayjs");
/**
 * 抵扣限制和抵扣处理
 * @date 2022-08-16 13:56:23
 * @author 侯洪莉
 */
var ChargingPileService = /** @class */ (function () {
    function ChargingPileService() {
    }
    /**
     * 抵扣方式限制判断
     * @param {*}  ReqChargingDeductionData
     * @返回值 resData：{PileRule,FreeData} PileRule：使用规则 FreeData:可免费金额和时长
     */
    ChargingPileService.prototype.getChargingLimit = function (param) {
        return __awaiter(this, void 0, void 0, function () {
            var resData, plateNo, parkId, chargingTime, pileId, carplateDataRst, _a, entryTime, elapsedTime, payable, parkCarInfo, pileRule, configRule, timeCanUse_c, moneyCanUse_c, _b, timeCanUse_1, moneyCanUse_1, _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, chargingTime = param.chargingTime;
                        pileId = param['pileId'];
                        return [4 /*yield*/, this.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*/, this.chargingPileDal.getPileRule(parkId, pileId)];
                    case 4:
                        pileRule = _d.sent();
                        if (!!pileRule) return [3 /*break*/, 6];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg("\u672A\u914D\u7F6E\u6216\u672A\u542F\u7528\u8BE5\u5145\u7535\u6869-" + pileId + "-\u4F7F\u7528\u89C4\u5219")];
                    case 5: return [2 /*return*/, _d.sent()];
                    case 6:
                        //返回参数添加当前抵扣规则
                        Object.assign(resData, {
                            PileRule: pileRule
                        });
                        if (!(+pileRule.chargeTime != 0 && chargingTime < +pileRule.chargeTime)) return [3 /*break*/, 8];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg("充电时长未达到最小充电时长要求")];
                    case 7: return [2 /*return*/, _d.sent()];
                    case 8:
                        if (!(pileRule.discountSource == 1)) return [3 /*break*/, 11];
                        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*/, 10];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failRes("当抵扣来源为第三方时，抵扣类型和抵扣数量为必填值", param)];
                    case 9: return [2 /*return*/, _d.sent()];
                    case 10:
                        pileRule.discountNumber = +param.discountNumber;
                        pileRule.discountType = param.discountType;
                        _d.label = 11;
                    case 11: return [4 /*yield*/, this.chargingPileDal.getPileConfig(parkId)];
                    case 12:
                        configRule = _d.sent();
                        timeCanUse_c = Infinity //全局限制判断本次可用抵扣时间
                        ;
                        moneyCanUse_c = Infinity //全局限制判断本次可用抵扣金额 默认不限制全局
                        ;
                        if (!configRule) return [3 /*break*/, 16];
                        return [4 /*yield*/, this.getConfigRuleLimit(param, configRule, parkCarInfo)];
                    case 13:
                        _b = _d.sent(), timeCanUse_1 = _b.timeCanUse, moneyCanUse_1 = _b.moneyCanUse;
                        if (!(timeCanUse_1 === 0 || moneyCanUse_1 === 0)) return [3 /*break*/, 15];
                        return [4 /*yield*/, ChargingDeduction_1.DeductionResponse.failWithMsg("全局限制未通过，无法抵扣")];
                    case 14: return [2 /*return*/, _d.sent()];
                    case 15:
                        timeCanUse_c = timeCanUse_1;
                        moneyCanUse_c = moneyCanUse_1;
                        _d.label = 16;
                    case 16:
                        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*/, this.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.discountSource === 1) ? pileRule.discountNumber : chargingTime;
                        mslib_1.logger.info("2222222", time);
                        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\uFF0C\u65E0\u53EF\u7528\u62B5\u6263", resData)];
                    case 26: return [2 /*return*/, _d.sent()];
                }
            });
        });
    };
    /**
     * 车辆类型修改和 充电桩抵扣
     * @param {Object} param  ReqChargingDeductionData
     * @param {FreeData,PileRule} freeData 可抵扣信息
  
     */
    ChargingPileService.prototype.discount = function (param, discountData) {
        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*/, this.tyExternalApiClient.modifyCarInfo(modifyData)];
                    case 2:
                        modifyRes = _a.sent();
                        mslib_1.logger.info('修改车辆类型返回结果', JSON.stringify(modifyRes));
                        if (!(modifyRes.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: dayjs(param.startTime).format('YYYY-MM-DD HH:mm:ss'),
                            endTime: dayjs(param.endTime).format('YYYY-MM-DD HH:mm:ss'),
                            orderNo: param.orderNo,
                            stationId: param.stationId || uuid.v4().replace(/-/g, ''),
                            stationName: param.stationName || '充电桩',
                            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.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*/];
                }
            });
        });
    };
    /**
    * 获取全局的限制情况
    * @param {Object} params
    * @param {Object} configRule 全局规则限制
    * @param {Object} parkInfo 停车信息
    *
    * @return {Int} canUse  可以使用额度 Infinity：表示不限额度
    */
    ChargingPileService.prototype.getConfigRuleLimit = function (params, configRule, parkInfo) {
        return __awaiter(this, void 0, void 0, function () {
            var parkId, plateNo, _a, onceMaxNum, _b, dayMaxNum, _c, onceMaxTime, _d, dayMaxTime, _e, dayMaxMoney, _f, onceMaxMoney, inTime, onceUse, dayUse, onceNumCanUse, dayNumCanUse, onceTimeCanUse, onceMoneyCanUse, dayTimeCanUse, dayMoneyCanUse, startTime, endTime, timeCanUse, moneyCanUse;
            return __generator(this, function (_g) {
                switch (_g.label) {
                    case 0:
                        parkId = params.parkId, plateNo = params.plateNo;
                        _a = configRule.onceMaxNum, onceMaxNum = _a === void 0 ? 0 : _a, _b = configRule.dayMaxNum, dayMaxNum = _b === void 0 ? 0 : _b, _c = configRule.onceMaxTime, onceMaxTime = _c === void 0 ? 0 : _c, _d = configRule.dayMaxTime, dayMaxTime = _d === void 0 ? 0 : _d, _e = configRule.dayMaxMoney, dayMaxMoney = _e === void 0 ? 0 : _e, _f = configRule.onceMaxMoney, onceMaxMoney = _f === void 0 ? 0 : _f;
                        inTime = parkInfo.inTime;
                        onceUse = { count: 0, time: 0, money: 0 };
                        dayUse = { count: 0, time: 0, money: 0 };
                        onceNumCanUse = 0, dayNumCanUse = 0;
                        onceTimeCanUse = 0, onceMoneyCanUse = 0;
                        dayTimeCanUse = 0, dayMoneyCanUse = 0;
                        if (!(onceMaxTime > 0 || onceMaxMoney > 0 || onceMaxNum > 0)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.getPaymentCount({ parkId: parkId, plateNo: plateNo, inTime: inTime })];
                    case 1:
                        onceUse = _g.sent();
                        _g.label = 2;
                    case 2:
                        // 可用额度
                        onceNumCanUse = onceMaxNum > 0 ? onceMaxNum - onceUse.count : Infinity;
                        onceTimeCanUse = onceMaxTime > 0 ? onceMaxTime - onceUse.time : Infinity;
                        onceMoneyCanUse = onceMaxMoney > 0 ? onceMaxMoney - onceUse.money : Infinity;
                        if (onceNumCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u5168\u5C40\u9650\u5236\u3011\u5355\u6B21\u62B5\u6263\u6B21\u6570\uFF08" + onceMaxNum + "\u6B21\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (onceTimeCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u5168\u5C40\u9650\u5236\u3011\u5355\u6B21\u62B5\u6263\u65F6\u95F4\u989D\u5EA6\uFF08" + onceMaxTime / 60 + "\u5206\u949F\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (onceMoneyCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u5168\u5C40\u9650\u5236\u3011\u5355\u6B21\u62B5\u6263\u91D1\u989D(" + (onceMaxMoney) / 100 + "\u5143\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (!(dayMaxTime > 0 || dayMaxMoney > 0 || dayMaxNum > 0)) return [3 /*break*/, 4];
                        startTime = dayjs().format('YYYY-MM-DD') + " 00:00:00";
                        endTime = dayjs().format('YYYY-MM-DD') + " 23:59:59";
                        return [4 /*yield*/, this.getPaymentCount({ parkId: parkId, plateNo: plateNo, startTime: startTime, endTime: endTime })];
                    case 3:
                        dayUse = _g.sent();
                        _g.label = 4;
                    case 4:
                        // 可用额度
                        dayNumCanUse = dayMaxNum > 0 ? dayMaxNum - dayUse.count : Infinity;
                        dayTimeCanUse = dayMaxTime > 0 ? dayMaxTime - dayUse.time : Infinity;
                        dayMoneyCanUse = dayMaxMoney > 0 ? dayMaxMoney - dayUse.money : Infinity;
                        if (dayNumCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u5168\u5C40\u9650\u5236\u3011\u5F53\u65E5\u62B5\u6263\u6B21\u6570\uFF08" + dayMaxNum + "\u6B21\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (dayTimeCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u5168\u5C40\u9650\u5236\u3011\u5F53\u65E5\u62B5\u6263\u65F6\u95F4\u989D\u5EA6\uFF08" + dayMaxTime / 60 + "\u5206\u949F\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (dayMoneyCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u5168\u5C40\u9650\u5236\u3011\u5F53\u65E5\u62B5\u6263\u989D\u5EA6\uFF08" + dayMaxMoney / 100 + "\u5143\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        timeCanUse = onceTimeCanUse - dayTimeCanUse < 0 ? onceTimeCanUse : dayTimeCanUse;
                        moneyCanUse = onceMoneyCanUse - dayMoneyCanUse < 0 ? onceMoneyCanUse : dayMoneyCanUse;
                        return [2 /*return*/, { timeCanUse: timeCanUse, moneyCanUse: moneyCanUse }];
                }
            });
        });
    };
    /**
     * 获取使用规则的限制情况
     * @param {Object} params
     * @param {Object} pileRule 使用规则限制
     * @param {Object} parkInfo 停车信息
     *
     * @return {Int} canUse  可以使用额度 Infinity：表示不限额度
     */
    ChargingPileService.prototype.getPileRuleLimit = function (params, pileRule, parkInfo) {
        return __awaiter(this, void 0, void 0, function () {
            var parkId, plateNo, pileId, onceMaxNum, dayMaxNum, onceMaxTime, dayMaxTime, dayMaxMoney, onceMaxMoney, inTime, onceUse, dayUse, onceNumCanUse, dayNumCanUse, onceTimeCanUse, onceMoneyCanUse, dayTimeCanUse, dayMoneyCanUse, startTime, endTime, timeCanUse, moneyCanUse;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (!params.pileId) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u672A\u83B7\u53D6\u5230\u5145\u7535\u6869pileId");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        parkId = params.parkId, plateNo = params.plateNo, pileId = params.pileId;
                        onceMaxNum = pileRule.onceMaxNum, dayMaxNum = pileRule.dayMaxNum, onceMaxTime = pileRule.onceMaxTime, dayMaxTime = pileRule.dayMaxTime, dayMaxMoney = pileRule.dayMaxMoney, onceMaxMoney = pileRule.onceMaxMoney;
                        inTime = parkInfo.inTime;
                        onceUse = { count: 0, time: 0, money: 0 };
                        dayUse = { count: 0, time: 0, money: 0 };
                        onceNumCanUse = 0, dayNumCanUse = 0;
                        onceTimeCanUse = 0, onceMoneyCanUse = 0;
                        dayTimeCanUse = 0, dayMoneyCanUse = 0;
                        if (!(onceMaxTime > 0 || onceMaxMoney > 0 || onceMaxNum > 0)) return [3 /*break*/, 2];
                        return [4 /*yield*/, this.getPaymentCount({ parkId: parkId, plateNo: plateNo, inTime: inTime, pileId: pileId })];
                    case 1:
                        onceUse = _a.sent();
                        _a.label = 2;
                    case 2:
                        // 可用额度
                        onceNumCanUse = onceMaxNum > 0 ? onceMaxNum - onceUse.count : Infinity;
                        onceTimeCanUse = onceMaxTime > 0 ? onceMaxTime - onceUse.time : Infinity;
                        onceMoneyCanUse = onceMaxMoney > 0 ? onceMaxMoney - onceUse.money : Infinity;
                        if (onceNumCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u5355\u6B21\u62B5\u6263\u6B21\u6570\uFF08" + onceMaxNum + "\u6B21\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (onceTimeCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u5355\u6B21\u62B5\u6263\u65F6\u95F4\u989D\u5EA6\uFF08" + onceMaxTime / 60 + "\u5206\u949F\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (onceMoneyCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u5355\u6B21\u4F1A\u5458\u62B5\u6263\u91D1\u989D(" + (onceMaxMoney) / 100 + "\u5143\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (!(dayMaxTime > 0 || dayMaxMoney > 0 || dayMaxNum > 0)) return [3 /*break*/, 4];
                        startTime = dayjs().format('YYYY-MM-DD') + " 00:00:00";
                        endTime = dayjs().format('YYYY-MM-DD') + " 23:59:59";
                        return [4 /*yield*/, this.getPaymentCount({ parkId: parkId, plateNo: plateNo, startTime: startTime, endTime: endTime, pileId: pileId })];
                    case 3:
                        dayUse = _a.sent();
                        _a.label = 4;
                    case 4:
                        // 可用额度
                        dayNumCanUse = dayMaxNum > 0 ? dayMaxNum - dayUse.count : Infinity;
                        dayTimeCanUse = dayMaxTime > 0 ? dayMaxTime - dayUse.time : Infinity;
                        dayMoneyCanUse = dayMaxMoney > 0 ? dayMaxMoney - dayUse.money : Infinity;
                        if (dayNumCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u5F53\u65E5\u62B5\u6263\u6B21\u6570\uFF08" + dayMaxNum + "\u6B21\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (dayTimeCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u5F53\u65E5\u62B5\u6263\u65F6\u95F4\u989D\u5EA6\uFF08" + dayMaxTime / 60 + "\u5206\u949F\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        if (dayMoneyCanUse <= 0) {
                            mslib_1.logger.info("\u3010\u4F7F\u7528\u89C4\u5219\u9650\u5236\u3011\u5F53\u65E5\u62B5\u6263\u989D\u5EA6\uFF08" + dayMaxMoney / 100 + "\u5143\uFF09\u5DF2\u8FBE\u4E0A\u9650");
                            return [2 /*return*/, { timeCanUse: 0, moneyCanUse: 0 }];
                        }
                        timeCanUse = onceTimeCanUse - dayTimeCanUse < 0 ? onceTimeCanUse : dayTimeCanUse;
                        moneyCanUse = onceMoneyCanUse - dayMoneyCanUse < 0 ? onceMoneyCanUse : dayMoneyCanUse;
                        return [2 /*return*/, { timeCanUse: timeCanUse, moneyCanUse: moneyCanUse }];
                }
            });
        });
    };
    /**
    * 统计充电桩抵扣使用情况
    */
    ChargingPileService.prototype.getPaymentCount = function (data) {
        var _a, _b;
        return __awaiter(this, void 0, void 0, function () {
            var parkId, _c, plateNo, _d, inTime, _e, payMethod, _f, startTime, _g, endTime, _h, paySource, payType, count, time, money, options, axiosResponse, listRst, _i, _j, item;
            return __generator(this, function (_k) {
                switch (_k.label) {
                    case 0:
                        parkId = data.parkId, _c = data.plateNo, plateNo = _c === void 0 ? '' : _c, _d = data.inTime, inTime = _d === void 0 ? '' : _d, _e = data.payMethod, payMethod = _e === void 0 ? '' : _e, _f = data.startTime, startTime = _f === void 0 ? '' : _f, _g = data.endTime, endTime = _g === void 0 ? '' : _g, _h = data.pileId, paySource = _h === void 0 ? '' : _h;
                        payType = 1009;
                        count = 0, time = 0, money = 0;
                        options = {
                            parkId: parkId,
                            plateNo: plateNo,
                            pageIndex: 1,
                            pageSize: 100,
                            inTime: inTime || undefined,
                            startTime: startTime || undefined,
                            endTime: endTime || undefined,
                        };
                        return [4 /*yield*/, nslib_1.axiosInstance.post('/api/wec/GetParkingPaymentList', options)];
                    case 1:
                        axiosResponse = _k.sent();
                        nslib_1.throwIfResCodeNoSuccess(axiosResponse.data);
                        listRst = axiosResponse.data;
                        mslib_1.logger.info(JSON.stringify(listRst));
                        if (listRst.resCode != '0' || ((_a = JSON.parse(listRst === null || listRst === void 0 ? void 0 : listRst.data)) === null || _a === void 0 ? void 0 : _a.plateList.length) == 0) {
                            return [2 /*return*/, { count: count, time: time, money: money }];
                        }
                        for (_i = 0, _j = (_b = JSON.parse(listRst === null || listRst === void 0 ? void 0 : listRst.data)) === null || _b === void 0 ? void 0 : _b.plateList; _i < _j.length; _i++) {
                            item = _j[_i];
                            if ((item.payMethod == payMethod || !payMethod) && item.payType == payType && (item.paySource == parseInt(paySource) || !paySource)) {
                                count += 1;
                                money += (+item.freeMoney);
                                time += (+item.freeTime);
                            }
                        }
                        return [2 /*return*/, { count: count, time: time, money: money }];
                }
            });
        });
    };
    __decorate([
        typescript_ioc_1.Inject,
        __metadata("design:type", ChargingPileDal_1.ChargingPileDal)
    ], ChargingPileService.prototype, "chargingPileDal", void 0);
    __decorate([
        typescript_ioc_1.Inject,
        __metadata("design:type", nslib_1.TyExternalApiClient
        /**
         * 抵扣方式限制判断
         * @param {*}  ReqChargingDeductionData
         * @返回值 resData：{PileRule,FreeData} PileRule：使用规则 FreeData:可免费金额和时长
         */
        )
    ], ChargingPileService.prototype, "tyExternalApiClient", void 0);
    return ChargingPileService;
}());
exports.ChargingPileService = ChargingPileService;
//# sourceMappingURL=ChargingPileService.js.map