//#import $owl_mall_ewallet:services/modelService.jsx
//#import $owl_mall_ewallet_records:services/modelService.jsx

var EWalletUtil = (function () {
    var f = {
        getEWalletId: function (ewallet_type_id, orgId, userId) {
            return "owl_mall_ewallet_" + ewallet_type_id + "_" + orgId + "_" + userId;
        },
        getBalance: function (ewallet_type_id, orgId, userId) {
            var ewalletId = f.getEWalletId(ewallet_type_id, orgId, userId);
            var jUserEWallet = owl_mall_ewalletService.get(ewalletId);
            if (jUserEWallet) {
                return jUserEWallet.balance || 0;
            }
            return 0;
        },
        updateEWalletBalance: function (ewallet_type_id, ewallet_type_name, orgId, orgName, userId, amount, reason_id, reason_name, transaction_no, relatedObjType, relatedObjId, relatedObjCode, operatorUserId, remark) {
            amount = Number(amount);
            var ewalletId = f.getEWalletId(ewallet_type_id, orgId, userId);
            $.log("EWalletUtil.jsx...updateEWalletBalance...begin...ewalletId=" + ewalletId + "...amount=" + amount);

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

                    after_amount = jUserEWallet.balance;

                    owl_mall_ewalletService.add(jUserEWallet);

                    $.log("EWalletUtil.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_mall_ewalletService.update(jUserEWallet);

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

                var jEWalletRecord = {};
                jEWalletRecord.ewallet_type_id = ewallet_type_id;
                jEWalletRecord.ewallet_type_name = ewallet_type_name;
                jEWalletRecord.account_id = ewalletId;
                jEWalletRecord.user_id = userId;
                jEWalletRecord._orgId = orgId;
                jEWalletRecord.orgName = orgName;
                jEWalletRecord._orgIds = ["0", orgId];
                jEWalletRecord.action_type = amount >= 0 ? "increase" : "reduce";
                jEWalletRecord.transaction_no = transaction_no;
                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_mall_ewallet_recordsService.add(jEWalletRecord);

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

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

    }
    return f;
})();

