//#import $owl_mall_o:services/modelService.jsx
//#import $owl_mall_ewallet_recharge_order:services/modelService.jsx
//#import $owl_aigc_ewallet_recharge_order:services/modelService.jsx
//#import $owl_data_center_buycoupon_order:services/modelService.jsx
//#import $owl_enjoy_coupon_buycoupon_order:services/modelService.jsx
//#import $owl_data_center_pointcoupon_order:services/modelService.jsx
//#import $owl_payrec:services/modelService.jsx
//#import $owlService:services/order/OrderUtil.jsx

var PayRecordHelper = (function () {
    var f = {
        getPayRecord: function (userId, orderId, jPayInterface, openid, ip, user_agent) {
            var payRecId = owl_payrecService.getId({});
            return f.getPayRecordById(payRecId, userId, orderId, jPayInterface, openid, ip, user_agent);
        },
        getPayRecordById: function (payRecId, userId, orderId, jPayInterface, openid, ip, user_agent) {
            var payInterfaceId = jPayInterface.id;
            var payInterfaceName = jPayInterface.payInterfaceName;

            var curTime = new Date().getTime();

            var orgIds = [];
            var merchantId = "";
            var totalAmount = 0;
            var objCode = "";
            var payType = "product";
            var relatedObjType = "order";
            if (orderId.indexOf("owl_mall_ewallet_recharge_order_") >= 0) {
                //预存款充值订单
                if (jPayInterface.payType != "cash") {
                    return {code: "311", msg: "预存款充值订单只支持现金支付"};
                }
                var jRechargeOrder = owl_mall_ewallet_recharge_orderService.get(orderId);
                if (!jRechargeOrder) {
                    return {code: "312", msg: "预存款充值订单不存在"};
                }
                if (jRechargeOrder.pay_state == "paid") {
                    return {code: "313", msg: "当前预存款充值订单已经支付，无需重复支付"};
                }
                if (jRechargeOrder.process_state == "canceled") {
                    return {code: "314", msg: "当前预存款充值订单已经取消，无法继续支付"};
                }
                payType = "ewalletRecharge";
                relatedObjType = "ewalletRecharge";
                merchantId = jRechargeOrder._orgId;
                objCode = jRechargeOrder.order_code;
                totalAmount = Number(jRechargeOrder.total_pay_price);
                orgIds.push(jRechargeOrder._orgId);
            } else if (orderId.indexOf("owl_aigc_ewallet_recharge_order_") >= 0) {
                //智绘豆充值订单
                if (jPayInterface.payType != "cash") {
                    return {code: "311", msg: "智绘豆充值订单只支持现金支付"};
                }
                var jRechargeOrder = owl_aigc_ewallet_recharge_orderService.get(orderId);
                if (!jRechargeOrder) {
                    return {code: "312", msg: "智绘豆充值订单不存在"};
                }
                if (jRechargeOrder.pay_state == "paid") {
                    return {code: "313", msg: "当前智绘豆充值订单已经支付，无需重复支付"};
                }
                if (jRechargeOrder.process_state == "canceled") {
                    return {code: "314", msg: "当前智绘豆充值订单已经取消，无法继续支付"};
                }
                payType = "aigcEWalletRecharge";
                relatedObjType = "aigcEWalletRecharge";
                merchantId = jRechargeOrder._orgId;
                objCode = jRechargeOrder.order_code;
                totalAmount = Number(jRechargeOrder.total_pay_price);
                orgIds.push(jRechargeOrder._orgId);
            } else if (orderId.indexOf("owl_data_center_buycoupon_order_") >= 0) {
                //买券订单
                var jBuyCouponOrder = owl_data_center_buycoupon_orderService.get(orderId);
                if (!jBuyCouponOrder) {
                    return {code: "321", msg: "买券订单不存在"};
                }
                if (jBuyCouponOrder.pay_state == "paid") {
                    return {code: "322", msg: "当前买券订单已经支付，无需重复支付"};
                }
                if (jBuyCouponOrder.process_state == "canceled") {
                    return {code: "323", msg: "当前买券订单已经取消，无法继续支付"};
                }
                payType = "buyCouponOrder";
                relatedObjType = "buyCouponOrder";
                objCode = jBuyCouponOrder.order_code;
                totalAmount = Number(jBuyCouponOrder.price_info.total_cash_price);
                orgIds.push(jBuyCouponOrder._orgId);
            } else if (orderId.indexOf("owl_enjoy_coupon_buycoupon_order_") >= 0) {
                //买券订单
                var jBuyCouponOrder = owl_enjoy_coupon_buycoupon_orderService.get(orderId);
                if (!jBuyCouponOrder) {
                    return {code: "321", msg: "买券订单不存在"};
                }
                if (jBuyCouponOrder.pay_state == "paid") {
                    return {code: "322", msg: "当前买券订单已经支付，无需重复支付"};
                }
                if (jBuyCouponOrder.process_state == "canceled") {
                    return {code: "323", msg: "当前买券订单已经取消，无法继续支付"};
                }
                payType = "buyCouponOrder";
                relatedObjType = "buyCouponOrder";
                objCode = jBuyCouponOrder.order_code;
                totalAmount = Number(jBuyCouponOrder.price_info.total_price);
                orgIds.push(jBuyCouponOrder._orgId);
            } else if (orderId.indexOf("owl_data_center_pointcoupon_order_") >= 0) {
                //积分+现金兑换中台券订单
                var jPointCouponOrder = owl_data_center_pointcoupon_orderService.get(orderId);
                if (!jPointCouponOrder) {
                    return {code: "321", msg: "积分兑券订单不存在"};
                }
                if (jPointCouponOrder.pay_state == "paid") {
                    return {code: "322", msg: "当前积分兑券订单已经支付，无需重复支付"};
                }
                if (jPointCouponOrder.process_state == "canceled") {
                    return {code: "323", msg: "当前积分兑券订单已经取消，无法继续支付"};
                }
                payType = "pointCouponOrder";
                relatedObjType = "pointCouponOrder";
                objCode = jPointCouponOrder.order_code;
                totalAmount = Number(jPointCouponOrder.price_info.total_cash_price);
                orgIds.push(jPointCouponOrder._orgId);
            } else if (orderId.indexOf("owl_mall_o_") >= 0) {
                var order = owl_mall_oService.get(orderId);
                if (!order) {
                    return {code: "331", msg: "订单不存在"};
                }
                if (order.pay_state == "paid") {
                    return {code: "332", msg: "当前订单已经支付，无需重复支付"};
                }
                if (order.process_state == "canceled") {
                    return {code: "333", msg: "当前订单已经取消，无法继续支付"};
                }
                merchantId = order._orgId;
                objCode = order.order_code;
                totalAmount += Number(order.price_info.total_unpaid_price);
                orgIds = OrderUtil.getOrderOrgIds(order);
            } else {
                $.log("PayRecordHelper.jsx...orderId=" + orderId + "...userId=" + userId);
                return {code: "349", msg: "当前暂不支持的订单类型"};
            }

            if(payInterfaceId == "owl_mall_payinterface_payi_332") {
                if(orgIds.length > 1) {
                    return {code: "350", msg: "惠购余额券不支持合并支付，请取消后重新下单"};
                }
            }

            //todo:判断商家是否支持该支付方式


            var payCode = f.getPayRecordCode(payRecId);

            var jPayRecord = {};
            jPayRecord.id = payRecId;
            jPayRecord.pay_code = payCode;
            jPayRecord.uid = userId;
            jPayRecord.payType = payType;
            jPayRecord.relatedObjType = relatedObjType;
            jPayRecord.relatedObjId = orderId;
            jPayRecord.relatedObjCode = objCode;
            jPayRecord.payState = "unpaid";
            jPayRecord.refundState = "unrefund";
            jPayRecord.createTime = curTime;
            jPayRecord.amount = totalAmount.toFixed(2);
            jPayRecord.payAmount = 0;
            jPayRecord.interfaceId = payInterfaceId;
            jPayRecord.interfaceName = payInterfaceName;
            jPayRecord.ip = ip;
            jPayRecord.ua = user_agent;
            jPayRecord._orgId = "0";
            jPayRecord._orgIds = ["0"];
            if (openid) {
                jPayRecord.openId = openid;
            }

            jPayRecord = owl_payrecService.add(jPayRecord);

            return {
                code: "0",
                msg: "操作成功",
                data: jPayRecord,
                relatedObjType: relatedObjType,
                relatedObjId: orderId,
                relatedObjCode: objCode
            };
        },
        getPayRecordCode: function (payRecId) {
            var now = new Date();
            var year = "" + now.getFullYear();
            var month = now.getMonth() + 1;
            var date = now.getDate();
            // var hour = now.getHours();
            // var minute = "" + now.getMinutes();
            // var second = "" + now.getSeconds();

            year = year.substring(2);
            month = month < 10 ? "0" + month : "" + month;
            date = date < 10 ? "0" + date : "" + date;
            // hour = hour < 10 ? "0" + hour : "" + hour;
            // minute = minute < 10 ? "0" + minute : "" + minute;
            // second = second < 10 ? "0" + second : "" + second;

            var num = payRecId.replace("owl_payrec_", "");

            //80开头是支付记录单号
            var code = "80" + year + month + date + num;

            return code;
        }
    }
    return f;
})();

