"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.ChargingBusDeal = void 0;
var nslib_1 = require("../refs/nslib");
var mslib_1 = require("mslib");
var Helper_1 = require("./Helper");
var dayjs = require('dayjs');
/**
 *
 * @date 2022-08-16 10:16:41
 * @author 侯洪莉
 */
var ChargingBusDeal = /** @class */ (function () {
    function ChargingBusDeal() {
    }
    /**
    * 获取使用规则配置信息
    * @param {*} parkId
    * @param {string} pileId 充电桩appID
    * @param {*} opsConfig 统一平台配置
    */
    ChargingBusDeal.prototype.getPileRule = function (parkId, pileId) {
        return __awaiter(this, void 0, void 0, function () {
            var sql, res, info;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        sql = "\n            select  \n                id,\n                name  as pileName,      -- \u5145\u7535\u6869\u540D\u79F0\n                pileId,                 -- \u5145\u7535\u6869ID\n                isEnable,               -- \u662F\u5426\u542F\u7528 0-\u7981\u7528 1-\u542F\u7528\n                discountSource,         -- \u62B5\u6263\u6765\u6E90\n                discountType,           -- \u62B5\u6263\u7C7B\u578B\n                discountMethod,         -- \u62B5\u6263\u65B9\u6CD5\n                discountNumber,         -- \u62B5\u6263\u6570\u91CF\n                chargeTime,             -- \u4F7F\u7528\u62B5\u6263\u7684\u6700\u5C0F\u5145\u7535\u65F6\u95F4\u8981\u6C42\n                onceMaxNum,             -- \u5355\u6B21\u505C\u8F66\u62B5\u6263\u6B21\u6570\u4E0A\u9650\n                dayMaxNum,              -- \u5355\u65E5\u505C\u8F66\u62B5\u6263\u6B21\u6570\u4E0A\u9650\n                onceMaxTime,            -- \u5355\u6B21\u505C\u8F66\u62B5\u6263\u65F6\u957F\u4E0A\u9650\n                onceMaxMoney,           -- \u5355\u6B21\u505C\u8F66\u62B5\u6263\u91D1\u989D\u4E0A\u9650\n                dayMaxTime,             -- \u6BCF\u65E5\u62B5\u6263\u65F6\u957F\u4E0A\u9650\n                dayMaxMoney,            -- \u6BCF\u65E5\u62B5\u6263\u91D1\u989D\u4E0A\u9650\n                isSpecialFeeRule,       -- \u662F\u5426\u4F7F\u7528\u7279\u6B8A\u89C4\u5219\n                carStyle                   -- \u5145\u7535\u8F66\u8F86\u7C7B\u578B\n            from \n                t_" + parkId + "_ns_pile \n            where \n                isDelete  = 0 and isEnable = 1 and pileId='" + pileId + "'\n            limit 1\n        ";
                        return [4 /*yield*/, new nslib_1.TySqlApiClient().selectOne(sql)];
                    case 1:
                        res = _a.sent();
                        info = res || null;
                        return [2 /*return*/, info];
                }
            });
        });
    };
    /**
     * 获取全局配置信息
     *
     * @param {string}
     * @param {*} opsConfig 统一平台配置
     */
    ChargingBusDeal.prototype.getPileConfig = function (parkId) {
        return __awaiter(this, void 0, void 0, function () {
            var session, sql, result, info, configInfo, _i, info_1, item;
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        session = '1103860';
                        sql = "\n            select\n            name, \n            value \n            from t_" + parkId + "_ns_pile_config where session='" + session + "';\n        ";
                        return [4 /*yield*/, new nslib_1.TySqlApiClient().list(sql)];
                    case 1:
                        result = _b.sent();
                        info = result || [];
                        configInfo = {};
                        if (info && info.length > 0) {
                            for (_i = 0, info_1 = info; _i < info_1.length; _i++) {
                                item = info_1[_i];
                                Object.assign(configInfo, (_a = {},
                                    _a[item.name] = item['value'],
                                    _a));
                            }
                        }
                        return [2 /*return*/, configInfo];
                }
            });
        });
    };
    /**
    * 获取全局的限制情况
    * @param {Object} params
    * @param {Object} configRule 全局规则限制
    * @param {Object} parkInfo 停车信息
    *
    * @return {Int} canUse  可以使用额度 Infinity：表示不限额度
    */
    ChargingBusDeal.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*/, Helper_1.Helper.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*/, Helper_1.Helper.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：表示不限额度
     */
    ChargingBusDeal.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*/, Helper_1.Helper.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*/, Helper_1.Helper.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 }];
                }
            });
        });
    };
    return ChargingBusDeal;
}());
exports.ChargingBusDeal = ChargingBusDeal;
//# sourceMappingURL=ChargingBusDeal.js.map