//#import $owl_mall_org_ewallet:services/modelService.jsx
//#import $owl_mall_org_ewallet_records:services/modelService.jsx

var OrgEWalletUtil = (function () {
    var f = {
        getEWalletId: function (orgId) {
            return "owl_mall_org_ewallet_" + orgId;
        },
        getBalance: function (orgId) {
            var ewalletId = f.getEWalletId(orgId);
            var jUserEWallet = owl_mall_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("OrgEWalletUtil.jsx...updateEWalletBalance...begin...ewalletId=" + ewalletId + "...amount=" + amount);

            var lockId = "lock_" + ewalletId;
            owl_mall_org_ewalletService.lock(lockId);
            try {
                var jUserEWallet = owl_mall_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_mall_org_ewalletService.add(jUserEWallet);

                    $.log("OrgEWalletUtil.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_mall_org_ewalletService.update(jUserEWallet);

                    $.log("OrgEWalletUtil.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_mall_org_ewallet_recordsService.add(jEWalletRecord);

                $.log("OrgEWalletUtil.jsx...updateEWalletBalance...end...ewalletId=" + ewalletId + "...amount3=" + amount);

                return jEWalletRecord;
            } finally {
                owl_mall_org_ewalletService.unlock(lockId);
            }
        }

    }
    return f;
})();

