//#import $owl_mall_payinterface:services/modelService.jsx
//#import $owl_mall_u:services/modelService.jsx
//#import $owl_discount_ticket:services/modelService.jsx
//#import $owl_discount_ticket_log:services/modelService.jsx
//#import $owl_payrec:services/modelService.jsx
//#import $owlService:services/order/PayRecordHelper.jsx

var OrderUseTicketHelper = (function () {
    var f = {
        /**
         * 生成支付记录，并修改券的状态为已使用。
         * 特别说明：该方法不做券的状态校验，认为调用该方法的时候，券都是被提前检查过，是都是可以正常使用的情况
         */
        doUseTickets: function (orderId, ticketIds, userId, ip, user_agent) {
            if(!ticketIds || ticketIds.length == 0) {
                $.log("OrderUseTicketHelper.doUseTickets, ticketIds.length==0, return; ");
                return {code: "0", msg: "没有券支付金额"};
            }
            var payInterfaceId = "owl_mall_payinterface_payi_2";
            var jPayInterface = owl_mall_payinterfaceService.get(payInterfaceId);
            var jUser = owl_mall_uService.get(userId);

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

            var ticketList = [];
            var payAmount = 0;
            for (var i = 0; i < ticketIds.length; i++) {
                var jTicket = owl_discount_ticketService.get(ticketIds[i]);

                var before_state = f.getStateValue(jTicket);

                var ticketInfo = {};
                ticketInfo.ticket_id = jTicket.id;
                ticketInfo.ticket_type_id = jTicket.ticket_type_id;
                ticketInfo.ticket_type_name = jTicket.ticket_type_name;
                ticketInfo.discount = jTicket.discount;
                ticketList.push(ticketInfo);

                payAmount += Number(jTicket.discount);

                //更新券的使用状态
                jTicket.order_id = jPayRecord.relatedObjId;
                jTicket.order_code = jPayRecord.relatedObjCode;
                jTicket.payrec_id = jPayRecord.id;
                jTicket.payrec_code = jPayRecord.pay_code;
                jTicket.used = "y";
                jTicket.canceled = "y";
                owl_discount_ticketService.update(jTicket);

                var after_state = f.getStateValue(jTicket);

                //添加券日志
                var remark = "下单核销用券。关联的用券订单Id=" + jPayRecord.relatedObjId
                remark += "，关联的用券订单号=" + jPayRecord.relatedObjCode
                remark += "，关联的券支付记录Id=" + jPayRecord.id
                remark += "，关联的券支付记录号=" + jPayRecord.pay_code
                f.addTicketLog(jTicket, before_state, after_state, jUser, remark, ip, user_agent);
            }

            //修改支付记录为已支付状态
            jPayRecord.payState = "paid";
            jPayRecord.succeededTime = (new Date()).getTime();
            jPayRecord.transaction_id = "";
            jPayRecord.payAmount = payAmount;
            jPayRecord.ticket_info = ticketList;
            owl_payrecService.update(jPayRecord);
            return {code: "0", msg: "操作成功"};
        },
        /**
         * 根据支付记录ID退券
         * @param payRecId
         * @returns {{msg: string, code: string}}
         */
        doCancelUseTickets: function (payRecId) {
            var jPayRecord = owl_payrecService.get(payRecId);
            if (!jPayRecord) {
                return {code: "701", msg: "支付记录不存在"};
            }
            if (jPayRecord.interfaceId != "owl_mall_payinterface_payi_2") {
                return {code: "702", msg: "支付记录不是券支付记录"};
            }
            var ticket_info = jPayRecord.ticket_info;
            if (!ticket_info) {
                return {code: "703", msg: "没有券支付信息"};
            }
            var payAmount = 0;
            for (var i = 0; i < ticket_info.length; i++) {
                var jTicket = owl_discount_ticketService.get(ticket_info[i].ticket_id);
                if (!jTicket) {
                    continue;
                }
                //添加券日志
                var remark = "取消订单回退券。关联的用券订单Id=" + jTicket.order_id
                remark += "，关联的用券订单号=" + jTicket.order_code
                remark += "，关联的券支付记录Id=" + jTicket.payrec_id
                remark += "，关联的券支付记录号=" + jTicket.payrec_code

                var before_state = f.getStateValue(jTicket);

                //更新券的状态为未使用
                jTicket.order_id = "";
                jTicket.order_code = "";
                jTicket.payrec_id = "";
                jTicket.payrec_code = "";
                jTicket.used = "n";
                owl_discount_ticketService.update(jTicket);

                var after_state = f.getStateValue(jTicket);

                f.addTicketLog(jTicket, before_state, after_state, {id: "u_sys", realName: "系统"}, remark, "", "");
            }

            //修改支付记录为已支付状态
            jPayRecord.refundState = "refunded";
            owl_payrecService.update(jPayRecord);

            return {code: "0", msg: "操作成功"};
        },
        addTicketLog: function (jTicket, before_state, after_state, jUser, remark, ip, user_agent) {
            var jLog = {};
            jLog.logType = "use";
            jLog.ticket_id = jTicket.id;
            jLog.ticket_type_id = jTicket.ticket_type_id;
            jLog._orgId = jTicket._orgId;
            jLog._orgIds = jTicket._orgIds;
            jLog.before_state = before_state;
            jLog.after_state = after_state;
            jLog.uid = jUser.id;
            jLog.uname = jUser.realName || "";
            jLog.remark = remark;
            jLog.ip = ip;
            jLog.ua = user_agent;
            if(jTicket.order_id) {
                jLog.order_id = jTicket.order_id || "";
                jLog.order_code = jTicket.order_code || "";
            }
            if(jTicket.payrec_id) {
                jLog.payrec_id = jTicket.payrec_id || "";
                jLog.payrec_code = jTicket.payrec_code || "";
            }
            owl_discount_ticket_logService.add(jLog);
        },
        getStateValue: function (jTicket) {
            var canceledState = "正常";
            var usedState = "未使用";
            if (jTicket.canceled == "y") {
                canceledState = "已作废";
            }
            if (jTicket.used == "y") {
                usedState = "已使用";
            }
            return canceledState + "," + usedState;
        }
    }
    return f;
})();

