//#import $owl_aigc_ewallet:services/modelService.jsx
//#import $owl_aigc_ewallet_records:services/modelService.jsx
//#import $owl_aigc_setting:services/modelService.jsx

var AigcEWalletUtil = (function () {
    var f = {
        getEWalletId: function (orgId, userId) {
            return "owl_aigc_ewallet_" + orgId + "_" + userId;
        },
        getBalance: function (orgId, userId) {
            var ewalletId = f.getEWalletId(orgId, userId);
            var jUserEWallet = owl_aigc_ewalletService.get(ewalletId);
            if (jUserEWallet) {
                return jUserEWallet.balance || 0;
            }
            return 0;
        },
        getUnitPrice: function (orgId, priceKey) {
            var id = "owl_aigc_setting_" + orgId;
            var jSetting = owl_aigc_settingService.get(id);
            if (jSetting) {
                var aigc_price_info = jSetting.aigc_price_info;
                if(aigc_price_info) {
                    return aigc_price_info[priceKey];
                }
            }
            return "";
        },
        updateEWalletBalance: function (orgId, orgName, trans_orgId, userId, amount, reason_id, reason_name, transaction_no, relatedObjType, relatedObjId, relatedObjCode, operatorUserId, remark) {
            amount = Number(amount);
            var ewalletId = f.getEWalletId(orgId, userId);
            $.log("AigcEWalletUtil.jsx...updateEWalletBalance...begin...ewalletId=" + ewalletId + "...amount=" + amount);

            var lockId = "lock_" + ewalletId;
            owl_aigc_ewalletService.lock(lockId);
            try {
                var jUserEWallet = owl_aigc_ewalletService.get(ewalletId);
                var before_amount = 0;
                var after_amount = 0;
                if (!jUserEWallet) {
                    jUserEWallet = {};
                    jUserEWallet._orgId = orgId;
                    jUserEWallet.orgName = orgName;
                    jUserEWallet.user_id = userId;
                    jUserEWallet.balance = Number(Number(amount).toFixed(2));
                    jUserEWallet._orgIds = ["0"];
                    jUserEWallet.create_time = new Date().getTime();

                    after_amount = jUserEWallet.balance;

                    owl_aigc_ewalletService.add(jUserEWallet);

                    $.log("AigcEWalletUtil.jsx...updateEWalletBalance...add ok...ewalletId=" + ewalletId + "...amount1=" + amount);
                } else {
                    before_amount = Number(jUserEWallet.balance).toFixed(2);
                    jUserEWallet.balance = Number(Number(Number(jUserEWallet.balance) + amount).toFixed(2));
                    after_amount = jUserEWallet.balance;
                    owl_aigc_ewalletService.update(jUserEWallet);

                    $.log("AigcEWalletUtil.jsx...updateEWalletBalance...update ok...ewalletId=" + ewalletId + "...amount2=" + amount);
                }

                var jEWalletRecord = {};
                jEWalletRecord.user_id = userId;
                jEWalletRecord.account_id = ewalletId;
                jEWalletRecord._orgId = orgId;
                jEWalletRecord.orgName = orgName;
                jEWalletRecord._orgIds = ["0"];
                jEWalletRecord.action_type = amount >= 0 ? "increase" : "reduce";
                jEWalletRecord.transaction_no = transaction_no;
                jEWalletRecord.trans_orgId = trans_orgId;
                jEWalletRecord.relatedObjType = relatedObjType;
                jEWalletRecord.relatedObjId = relatedObjId;
                jEWalletRecord.relatedObjCode = relatedObjCode;
                jEWalletRecord.before_amount = before_amount;
                jEWalletRecord.transaction_amount = Number(Number(amount).toFixed(2));
                jEWalletRecord.after_amount = Number(after_amount).toFixed(2);
                jEWalletRecord.reason_id = reason_id;
                jEWalletRecord.reason_name = reason_name;
                jEWalletRecord.remark = remark;
                jEWalletRecord.create_time = new Date().getTime();
                jEWalletRecord.owl_createUserId = operatorUserId;
                owl_aigc_ewallet_recordsService.add(jEWalletRecord);

                $.log("AigcEWalletUtil.jsx...updateEWalletBalance...end...ewalletId=" + ewalletId + "...amount3=" + amount);

                return jEWalletRecord;
            } finally {
                owl_aigc_ewalletService.unlock(lockId);
            }
        }

    }
    return f;
})();

