//#import Util.js
//#import $owl_mall_o:services/modelService.jsx
//#import $owl_refund_order:services/modelService.jsx
//#import $owl_data_center_buycoupon_order:services/modelService.jsx
//#import $owl_enjoy_coupon_buycoupon_order:services/modelService.jsx
//#import $owl_order_refund_numtimes:services/modelService.jsx
//#import $payInterfaceService:services/PayInterfaceUtil.jsx
//#import $owlService:services/order/RefundOrderHelper.jsx
//#import $owlService:services/org/OrgUtil.jsx
//#import $owlService:services/order/OrderLogUtil.jsx

var OrderRefundUtil = (function () {
    var f = {
        doExecRefund: function (jRefundOrder, operatorUserId, operatorUserName, remark) {
            var refundOrderId = jRefundOrder.id;
            var orgId = jRefundOrder._orgId;
            var order_id = jRefundOrder.order_id;
            var user_id = jRefundOrder.user_id;
            var relatedObjType = jRefundOrder.relatedObjType;
            var payOrderId = order_id;
            if (jRefundOrder.parent_order_id) {
                payOrderId = jRefundOrder.parent_order_id;
            }

            var lockId = "OrderRefundUtil_doExecRefund_" + payOrderId;
            owl_refund_orderService.lock(lockId);
            try {
                var when = new Date().getTime();
                if (relatedObjType == "buyCouponOrder") {
                    var delayRefundTime = OrgUtil.getOrgSettingValueForBuyCouponOrderInfo(orgId, null, "autoExecRefundTime");
                    if (!delayRefundTime) {
                        //至少延迟1分钟执行退款
                        delayRefundTime = 1;
                    }
                    $.log("OrderRefundUtil.jsx...buyCouponOrder...delayRefundTime=" + delayRefundTime + "...refundOrderId=" + refundOrderId);
                    when += 1000 * 60 * Number(delayRefundTime);
                }
                when += f.getAndSetDelayRefundTimeEx(jRefundOrder);
                $.log("OrderRefundUtil.jsx...end...add to delay task success...refundOrderId=" + refundOrderId + "...when=" + when);

                jRefundOrder = owl_refund_orderService.get(refundOrderId);

                RefundOrderHelper.addProcessLog(jRefundOrder, operatorUserId, operatorUserName, remark);
                jRefundOrder.has_doRefund = "y";
                jRefundOrder.has_doRefund_time = new Date().getTime();
                owl_refund_orderService.update(jRefundOrder);

                var jRefundLog = OrderLogUtil.addRefundLog(refundOrderId, order_id, user_id, when, "", operatorUserId, operatorUserName);
                $.log("OrderRefundUtil.jsx...end...refundOrderId=" + refundOrderId + "...jRefundLog=" + JSON.stringify(jRefundLog));
            } finally {
                owl_refund_orderService.unlock(lockId);
            }
        },

        getAndSetDelayRefundTimeEx: function (jRefundOrder) {
            var refund_recs = jRefundOrder.refund_recs;
            if (refund_recs) {
                var isExistWeixinPay = false;
                var isExistOneCardPay = false;
                for (var i = 0; i < refund_recs.length; i++) {
                    var jRefundInfo = refund_recs[i];
                    if (PayInterfaceUtil.isWeixinPay(jRefundInfo.interfaceId)) {
                        isExistWeixinPay = true;
                    }
                    if (PayInterfaceUtil.isOneCardPay(jRefundInfo.interfaceId)) {
                        isExistOneCardPay = true;
                    }
                }
            }

            var delaySecond = 15;//默认同一个订单延迟15秒后执行退款
            if (isExistWeixinPay) {
                //微信支付的，延迟1分钟后执行退款
                delaySecond = 61;
            }
            $.log("OrderRefundUtil.jsx...refundOrderId=" + jRefundOrder.id + "...isExistWeixinPay=" + isExistWeixinPay + "...isExistOneCardPay=" + isExistOneCardPay + "...delaySecond=" + delaySecond);

            //todo:其实以后可以不管什么订单类型，都统一使用订单退款次数表来控制

            var curTime = new Date().getTime();
            if (jRefundOrder.relatedObjType == "buyCouponOrder") {
                var jBuyCouponOrder = owl_data_center_buycoupon_orderService.get(jRefundOrder.order_id);
                if (jBuyCouponOrder) {
                    var time = 0;
                    if (jBuyCouponOrder.lastRefundTime) {
                        if (curTime - new Date(jBuyCouponOrder.lastRefundTime).getTime() < delaySecond * 1000) {
                            //由于支付接口的限制原因，同一个支付记录，1分钟内不能多次执行退款，所以需要延迟70秒后再执行
                            time = delaySecond * 1000 * (Number(jBuyCouponOrder.totalRefundTimes || 0) + 1);
                        }
                    }
                    jBuyCouponOrder.lastRefundTime = curTime;
                    jBuyCouponOrder.totalRefundTimes = Number(jBuyCouponOrder.totalRefundTimes || 0) + 1;
                    if(jBuyCouponOrder.id.indexOf("owl_enjoy_coupon_buycoupon_order_") >= 0) {
                        owl_enjoy_coupon_buycoupon_orderService.update(jBuyCouponOrder);
                    } else {
                        owl_data_center_buycoupon_orderService.update(jBuyCouponOrder);
                    }

                    $.log("OrderRefundUtil.jsx...buyCouponOrder delay...refundOrderId=" + jRefundOrder.id + "...totalRefundTimes=" + jBuyCouponOrder.totalRefundTimes);
                    return time;
                }
            } else {
                var orderId = jRefundOrder.order_id;
                if (jRefundOrder.parent_order_id) {
                    orderId = jRefundOrder.parent_order_id;
                }
                var jRefundNumTimes = owl_order_refund_numtimesService.get("owl_order_refund_numtimes_" + orderId);
                if (jRefundNumTimes) {
                    var time = 0;
                    if (jRefundNumTimes.lastRefundTime) {
                        if (curTime - new Date(jRefundNumTimes.lastRefundTime).getTime() < delaySecond * 1000) {
                            //由于支付接口的限制原因，同一个支付记录，1分钟内不能多次执行退款，所以需要延迟70秒后再执行
                            time = delaySecond * 1000 * (Number(jRefundNumTimes.totalRefundTimes || 0) + 1);
                        }
                    }
                    jRefundNumTimes.lastRefundTime = curTime;
                    jRefundNumTimes.totalRefundTimes = Number(jRefundNumTimes.totalRefundTimes || 0) + 1;
                    owl_order_refund_numtimesService.update(jRefundNumTimes);

                    $.log("OrderRefundUtil.jsx...order delay...updated...refundOrderId=" + jRefundOrder.id + "...totalRefundTimes=" + jRefundNumTimes.totalRefundTimes);
                    return time;
                } else {
                    jRefundNumTimes = {};
                    jRefundNumTimes.order_id = orderId;
                    jRefundNumTimes.lastRefundTime = curTime;
                    jRefundNumTimes.totalRefundTimes = 1;
                    owl_order_refund_numtimesService.add(jRefundNumTimes);

                    $.log("OrderRefundUtil.jsx...order delay...added...refundOrderId=" + jRefundOrder.id + "...totalRefundTimes=" + jRefundNumTimes.totalRefundTimes);
                    return 0;
                }
            }
            return 0;
        }
    };
    return f;

})();
