//#import $owl_resell_brokerage:services/modelService.jsx
//#import $owl_resell_brokerage_log:services/modelService.jsx

/**
 * 1）生成子单：佣金余额和锁定金额同时增加
 * 2）子单签收：佣金确认，减少锁定金额
 * 3）子单取消或者退货：佣金撤销，佣金余额和锁定金额同时减少
 * 4）可用佣金=佣金余额-锁定金额
 */
var ResellBrokerageUtil = (function () {
    var f = {
        getBrokerageId: function (userId) {
            return "owl_resell_brokerage_" + userId;
        },
        getBalance: function (userId) {
            var brokerageId = f.getBrokerageId(userId);
            var jResellBrokerage = owl_resell_brokerageService.get(brokerageId);
            if (jResellBrokerage) {
                return jResellBrokerage.balance || 0;
            }
            return 0;
        },
        getLockBalance: function (userId) {
            var brokerageId = f.getBrokerageId(userId);
            var jResellBrokerage = owl_resell_brokerageService.get(brokerageId);
            if (jResellBrokerage) {
                return jResellBrokerage.lock_balance || 0;
            }
            return 0;
        },
        updateBrokerageBalance: function (userId, amount, isNeedUpdateLockBalance, reason, transaction_no, relatedObjType, relatedObjId, relatedObjCode, operatorUserId, remark) {
            amount = Number(amount);
            var brokerageId = f.getBrokerageId(userId);
            $.log("\ndoCalcResellOrderBrokerage.jsx......jOrderBrokerage add ok....brokerageId=" + brokerageId);
            owl_resell_brokerageService.lock(brokerageId);
            try {
                var jResellBrokerage = owl_resell_brokerageService.get(brokerageId);
                var before_balance = 0;
                var before_lock_balance = 0;
                var after_balance = 0;
                var after_lock_balance = 0;
                if (!jResellBrokerage) {
                    jResellBrokerage = {};
                    jResellBrokerage.user_id = userId;
                    jResellBrokerage.balance = 0;
                    jResellBrokerage.lock_balance = 0;
                    jResellBrokerage.create_time = new Date().getTime();

                    jResellBrokerage = owl_resell_brokerageService.add(jResellBrokerage);
                }

                before_balance = Number(jResellBrokerage.balance || 0).toFixed(2);
                before_lock_balance = Number(jResellBrokerage.lock_balance || 0).toFixed(2);
                $.log("\ndoCalcResellOrderBrokerage.jsx......jOrderBrokerage add ok....before_balance=" + before_balance + "...before_lock_balance=" + before_lock_balance);


                jResellBrokerage.balance = Number(Number(Number(jResellBrokerage.balance) + amount).toFixed(2));
                if(isNeedUpdateLockBalance) {
                    jResellBrokerage.lock_balance = Number(Number(Number(jResellBrokerage.lock_balance) + amount).toFixed(2));
                }
                after_balance = jResellBrokerage.balance;
                after_lock_balance = jResellBrokerage.lock_balance;

                $.log("\ndoCalcResellOrderBrokerage.jsx......jOrderBrokerage add ok....after_balance=" + after_balance + "...after_lock_balance=" + after_lock_balance);
                owl_resell_brokerageService.update(jResellBrokerage);

                var jResellBrokerageLog = {};
                jResellBrokerageLog.account_id = brokerageId;
                jResellBrokerageLog.user_id = userId;
                jResellBrokerageLog.action_type = amount >= 0 ? "increase" : "reduce";
                jResellBrokerageLog.transaction_no = transaction_no;
                jResellBrokerageLog.relatedObjType = relatedObjType;
                jResellBrokerageLog.relatedObjId = relatedObjId;
                jResellBrokerageLog.relatedObjCode = relatedObjCode;
                jResellBrokerageLog.before_balance = before_balance;
                jResellBrokerageLog.before_lock_balance = before_lock_balance;
                jResellBrokerageLog.transaction_amount = Number(Number(amount).toFixed(2));
                jResellBrokerageLog.after_balance = Number(after_balance).toFixed(2);
                jResellBrokerageLog.after_lock_balance = Number(after_lock_balance).toFixed(2);
                jResellBrokerageLog.reason = reason;
                jResellBrokerageLog.remark = remark;
                jResellBrokerageLog.create_time = new Date().getTime();
                jResellBrokerageLog.owl_createUserId = operatorUserId;
                owl_resell_brokerage_logService.add(jResellBrokerageLog);

                return jResellBrokerageLog;
            } finally {
                owl_resell_brokerageService.unlock(brokerageId);
            }
        },
        releaseBrokerageLockBalance: function (userId, amount, reason, transaction_no, relatedObjType, relatedObjId, relatedObjCode, operatorUserId, remark) {
            amount = Number(amount);
            var brokerageId = f.getBrokerageId(userId);
            owl_resell_brokerageService.lock(brokerageId);
            try {
                var jResellBrokerage = owl_resell_brokerageService.get(brokerageId);
                var before_balance = 0;
                var before_lock_balance = 0;
                var after_balance = 0;
                var after_lock_balance = 0;
                if (!jResellBrokerage) {
                    jResellBrokerage = {};
                    jResellBrokerage.user_id = userId;
                    jResellBrokerage.balance = 0;
                    jResellBrokerage.lock_balance = 0;
                    jResellBrokerage.create_time = new Date().getTime();

                    jResellBrokerage = owl_resell_brokerageService.add(jResellBrokerage);
                }

                var lock_balance = Number(jResellBrokerage.lock_balance) + amount;
                if(Number(lock_balance) < 0) {
                    lock_balance = 0;
                }

                before_balance = Number(jResellBrokerage.balance || 0).toFixed(2);
                before_lock_balance = Number(jResellBrokerage.lock_balance || 0).toFixed(2);
                jResellBrokerage.lock_balance = Number(Number(lock_balance).toFixed(2));
                after_balance = jResellBrokerage.balance;
                after_lock_balance = jResellBrokerage.lock_balance;
                owl_resell_brokerageService.update(jResellBrokerage);

                var jResellBrokerageLog = {};
                jResellBrokerageLog.account_id = brokerageId;
                jResellBrokerageLog.user_id = userId;
                jResellBrokerageLog.action_type = "release";
                jResellBrokerageLog.transaction_no = transaction_no;
                jResellBrokerageLog.relatedObjType = relatedObjType;
                jResellBrokerageLog.relatedObjId = relatedObjId;
                jResellBrokerageLog.relatedObjCode = relatedObjCode;
                jResellBrokerageLog.before_balance = before_balance;
                jResellBrokerageLog.before_lock_balance = before_lock_balance;
                jResellBrokerageLog.transaction_amount = Number(Number(amount).toFixed(2));
                jResellBrokerageLog.after_balance = Number(after_balance).toFixed(2);
                jResellBrokerageLog.after_lock_balance = Number(after_lock_balance).toFixed(2);
                jResellBrokerageLog.reason = reason;
                jResellBrokerageLog.remark = remark;
                jResellBrokerageLog.create_time = new Date().getTime();
                jResellBrokerageLog.owl_createUserId = operatorUserId;
                owl_resell_brokerage_logService.add(jResellBrokerageLog);

                return jResellBrokerageLog;
            } finally {
                owl_resell_brokerageService.unlock(brokerageId);
            }
        }

    }
    return f;
})();

