//#import $owl_aigc_org_ewallet:services/modelService.jsx
//#import $owl_aigc_org_ewallet_records:services/modelService.jsx

var AigcOrgEWalletUtil = (function () {
    var f = {
        getEWalletId: function (orgId) {
            return "owl_aigc_org_ewallet_" + orgId;
        },
        getBalance: function (orgId) {
            var ewalletId = f.getEWalletId(orgId);
            var jUserEWallet = owl_aigc_org_ewalletService.get(ewalletId);
            if (jUserEWallet) {
                return jUserEWallet.balance || 0;
            }
            return 0;
        },
        updateEWalletBalance: function (orgId, orgName, amount, reason_id, reason_name, transaction_no, remark) {
            amount = Number(amount);
            var ewalletId = f.getEWalletId(orgId);
            $.log("AigcOrgEWalletUtil.jsx...updateEWalletBalance...begin...ewalletId=" + ewalletId + "...amount=" + amount);

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

                    after_amount = jUserEWallet.balance;

                    owl_aigc_org_ewalletService.add(jUserEWallet);

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

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

                var jEWalletRecord = {};
                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.before_amount = before_amount;
                jEWalletRecord.transaction_amount = amount;
                jEWalletRecord.after_amount = after_amount;
                jEWalletRecord.reason_id = reason_id;
                jEWalletRecord.reason_name = reason_name;
                jEWalletRecord.remark = remark;
                jEWalletRecord.create_time = new Date().getTime();
                owl_aigc_org_ewallet_recordsService.add(jEWalletRecord);

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

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

    }
    return f;
})();

