<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.liquidnet.service.kylin.mapper.KylinTicketsMapper">

    <resultMap id="ticketAgentResult" type="com.liquidnet.service.kylin.dao.TicketAgentDao">
        <result column="tickets_id" property="ticketsId"/>
        <result column="title" property="title"/>
        <result column="is_agent" property="isAgent"/>
    </resultMap>

    <resultMap id="ticketDamaiResult" type="com.liquidnet.service.kylin.dao.TicketDamaiDao">
        <result column="tickets_id" property="ticketsId"/>
        <result column="title" property="title"/>
        <result column="is_agent" property="isAgent"/>
    </resultMap>




    <!-- 票分销情况-->
    <select id="ticketAgent" parameterType="java.lang.String" resultMap="ticketAgentResult">
        SELECT
            tickets_id ,
            concat(title,' - ',use_start)  as 'title',
            ts.is_agent
        FROM
            kylin_ticket_status AS ts
                LEFT JOIN kylin_ticket_relations AS tr ON tr.ticket_id = ts.ticket_id
                LEFT JOIN kylin_ticket_time_relation AS ttr ON tr.times_id = ttr.times_id
                LEFT JOIN kylin_tickets AS t ON t.tickets_id = ts.ticket_id
        where performance_id = ${performanceIds}
    </select>

    <!-- 票大麦情况-->
    <select id="ticketDamai" parameterType="java.lang.String" resultMap="ticketDamaiResult">
        SELECT
            tickets_id ,
            concat(title,' - ',use_start)  as 'title',
            ts.sync_damai
        FROM
            kylin_ticket_status AS ts
                LEFT JOIN kylin_ticket_relations AS tr ON tr.ticket_id = ts.ticket_id
                LEFT JOIN kylin_ticket_time_relation AS ttr ON tr.times_id = ttr.times_id
                LEFT JOIN kylin_tickets AS t ON t.tickets_id = ts.ticket_id
        where performance_id = ${performanceIds}
    </select>
    <select id="getPerformancesTicketList" resultType="com.liquidnet.service.kylin.dao.PerformancesTicketListDao">
        SELECT
               e.tickets_id, e.title, f.title as time_title,
               count(*) as order_num, sum(a.number) as tickets_num
        FROM kylin_order_tickets a
                 INNER JOIN kylin_order_ticket_status s ON s.order_id = a.order_tickets_id
                 INNER JOIN kylin_order_ticket_relations c ON a.order_tickets_id = c.order_id
                 INNER JOIN kylin_tickets e ON c.ticket_id = e.tickets_id
                 INNER JOIN kylin_ticket_times AS f ON f.ticket_times_id = c.time_id
        where a.get_ticket_type = 'express'
          and s.status = 1
          AND c.performance_id = ${performanceId}
        GROUP BY e.tickets_id

        /*SELECT
            a.tickets_id,
            a.title,
            e.title as time_title,
            count(*) as order_num, count(*) as tickets_num
        FROM
            kylin_tickets AS a
                LEFT JOIN kylin_ticket_status AS b ON b.ticket_id = a.tickets_id
                LEFT JOIN kylin_ticket_relations AS c ON c.ticket_id = a.tickets_id
                LEFT JOIN kylin_ticket_time_relation AS d ON d.times_id = c.times_id
                LEFT JOIN kylin_ticket_times AS e ON e.ticket_times_id = d.times_id
        where d.performance_id = ${performanceId}
        and b.is_express = 1
        and b.status IN (2, 3, 6, 7, 8, 9, 10)*/
    </select>

    <resultMap id="ticketAndStatusDao" type="com.liquidnet.service.kylin.dao.TicketAndStatusDao">
        <collection column="mid" property="mid" />
        <collection column="tickets_id" property="ticketsId" />
        <collection column="title" property="title" />
        <collection column="type" property="type" />
        <collection column="price" property="price" />
        <collection column="use_start" property="useStart" />
        <collection column="use_end" property="useEnd" />
        <collection column="total_general" property="totalGeneral" />
    </resultMap>

    <select id="selectTicketsByIds" resultType="com.liquidnet.service.kylin.dao.TicketAndStatusDao">
        select
        kt.mid,
        kt.tickets_id,
        kt.title,
        kt.`type`,
        kt.price,
        kt.use_start,
        kt.use_end,
        kts.total_general
        from kylin_tickets as kt
        LEFT JOIN kylin_ticket_status as kts ON kt.tickets_id = kts.ticket_id
        where kt.tickets_id in
        <foreach collection="list" index="index" item="ticketsIdList" open="(" separator="," close=")">
            #{ticketsIdList}
        </foreach>
    </select>

    <select id="selectOneByPerIdAndTicketId" resultType="com.liquidnet.service.kylin.dao.ShowAgentDao">
        SELECT sa.id,
               sa.performance_id,
               sa.ticket_id,
               sa.total_carry,
               sa.ord_carry,
               kt.title
        FROM smile_agent sa RIGHT JOIN kylin_tickets kt ON sa.ticket_id = kt.tickets_id
        WHERE kt.tickets_id = #{ticketId}
    </select>
</mapper>
