//#import $owl_mall_payinterface:services/modelService.jsx
//#import $owl_data_center_parentorder:services/modelService.jsx
//#import $owl_enjoy_coupon_parentorder:services/modelService.jsx
//#import $owl_enjoy_coupon_bo_coupons:services/modelService.jsx
//#import $owl_enjoy_coupon_buycoupon_order:services/modelService.jsx
//#import $owl_payrec:services/modelService.jsx
//#import $owlService:services/order/PayRecordHelper.jsx

var OrderUseDcTicketHelper = (function () {
    var f = {
        /**
         * 当有使用中台券的时候，通过该方法生成支付记录
         * 特别说明：该方法不做券的状态校验，认为调用该方法的时候，都是有用中台券的情况
         * @param orderId 订单ID
         * @param parentOrderId 中台用券母单ID
         * @param userId
         * @param ip
         * @param user_agent
         * @returns
         */
        doUseTickets: function (orderId, parentOrderId, userId, ip, user_agent) {
            var payInterfaceId = "owl_mall_payinterface_payi_2_1";
            var jPayInterface = owl_mall_payinterfaceService.get(payInterfaceId);
            var jParentOrder = owl_data_center_parentorderService.get(parentOrderId);
            if(!jParentOrder.paymentList) {
                return {code: "701", msg: "母单没有可用的中台券"};
            }
            var paymentList = JSON.parse(jParentOrder.paymentList);
            if(paymentList.length == 0) {
                return {code: "702", msg: "母单没有可用的中台券"};
            }

            var cr = PayRecordHelper.getPayRecord(userId, orderId, jPayInterface, "", ip, user_agent);
            if (cr.code !== "0") {
                return cr;
            }

            var ticketList = [];
            var payAmount = 0;
            for (var i = 0; i < paymentList.length; i++) {
                var jPayInfo = paymentList[i];

                payAmount += Number(jPayInfo.money);

                var jUseInfo = {};
                jUseInfo.rownoid = jPayInfo.rownoid;
                jUseInfo.paycode = jPayInfo.paycode;
                jUseInfo.coptype = jPayInfo.coptype;
                jUseInfo.paytype = jPayInfo.paytype;
                jUseInfo.money = jPayInfo.money;
                jUseInfo.consumers_id = jPayInfo.consumers_id;
                ticketList.push(jUseInfo);

            }
            $.log("getCart.jsx...OrderUseDcTicketHelper...doUseTickets....ticketList=" + JSON.stringify(ticketList));

            //修改支付记录为已支付状态
            var jPayRecord = cr.data;
            jPayRecord.payState = "paid";
            jPayRecord.succeededTime = (new Date()).getTime();
            jPayRecord.transaction_id = parentOrderId;
            jPayRecord.payAmount = payAmount;
            jPayRecord.dc_ticket_info = ticketList;
            owl_payrecService.update(jPayRecord);

            jParentOrder.order_id = jPayRecord.relatedObjId;
            jParentOrder.order_code = jPayRecord.relatedObjCode;
            jParentOrder.pay_id = jPayRecord.id;
            jParentOrder.pay_code = jPayRecord.pay_code;
            owl_data_center_parentorderService.update(jParentOrder);

            return {code: "0", msg: "操作成功"};
        },
        /**
         * 当有使用中台券的时候，通过该方法生成支付记录
         * 特别说明：该方法不做券的状态校验，认为调用该方法的时候，都是有用中台券的情况
         * @param orderId 订单ID
         * @param parentOrderId 中台用券母单ID
         * @param jPayRecord
         * @returns
         */
        doUseTicketsForEnjoyCoupon: function (orderId, parentOrderId, jPayRecord) {
            var jParentOrder = owl_enjoy_coupon_parentorderService.get(parentOrderId);
            if(!jParentOrder.usedCouponList) {
                return {code: "701", msg: "母单没有可用的中台券"};
            }
            var usedCouponList = JSON.parse(jParentOrder.usedCouponList);
            if(!usedCouponList) {
                return {code: "701", msg: "母单没有可用的中台券"};
            }

            if(usedCouponList.length == 0) {
                return {code: "702", msg: "母单没有可用的中台券"};
            }

            var ticketList = [];
            var payAmount = 0;
            for (var i = 0; i < usedCouponList.length; i++) {
                var jPayInfo = usedCouponList[i];

                payAmount += Number(jPayInfo.amount);

                var jUseInfo = {};
                jUseInfo.rowNo = jPayInfo.rowNo;
                jUseInfo.valueType = jPayInfo.valueType;
                jUseInfo.couponBatch = jPayInfo.couponBatch;
                jUseInfo.couponNo = jPayInfo.couponNo;
                jUseInfo.couponName = jPayInfo.couponName;
                jUseInfo.amount = jPayInfo.amount;
                jUseInfo.customer = jPayInfo.customer;
                jUseInfo.lostType = jPayInfo.lostType;
                jUseInfo.payCode = jPayInfo.payCode;
                jUseInfo.payName = jPayInfo.payName;
                ticketList.push(jUseInfo);
            }
            $.log("getCart.jsx...OrderUseDcTicketHelper...doUseTicketsForEnjoyCoupon....ticketList=" + JSON.stringify(ticketList));

            //修改支付记录为已支付状态
            jPayRecord.payState = "paid";
            jPayRecord.succeededTime = (new Date()).getTime();
            jPayRecord.transaction_id = parentOrderId;
            jPayRecord.payAmount = payAmount;
            jPayRecord.ec_ticket_info = ticketList;
            owl_payrecService.update(jPayRecord);

            jParentOrder.order_id = jPayRecord.relatedObjId;
            jParentOrder.order_code = jPayRecord.relatedObjCode;
            jParentOrder.pay_id = jPayRecord.id;
            jParentOrder.pay_code = jPayRecord.pay_code;
            owl_enjoy_coupon_parentorderService.update(jParentOrder);

            if(ticketList.length > 0) {
                for (var i = 0; i < ticketList.length; i++) {
                    var jTicketInfo = ticketList[i];
                    var jCouponInfo = owl_enjoy_coupon_bo_couponsService.get("owl_enjoy_coupon_bo_coupons_" + jTicketInfo.couponNo);
                    if(jCouponInfo && jCouponInfo.buycoupon_order_id) {
                        var jBuyCouponOrder = owl_enjoy_coupon_buycoupon_orderService.get(jCouponInfo.buycoupon_order_id);
                        if(jBuyCouponOrder && jBuyCouponOrder.used_state != "used") {
                            jBuyCouponOrder.used_state = "used";
                            jBuyCouponOrder.used_time = new Date().getTime();
                            owl_enjoy_coupon_buycoupon_orderService.update(jBuyCouponOrder);
                            $.log("getCart.jsx...OrderUseDcTicketHelper...doUseTicketsForEnjoyCoupon...jBuyCouponOrder update to used success...buyCouponOrderId=" + jBuyCouponOrder.id);
                        }
                    }
                }
            }

            return {code: "0", msg: "操作成功"};
        }
    }
    return f;
})();

