<?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.hzs.sale.ticket.mapper.SaTicketMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="SaTicketExt" type="com.hzs.common.domain.sale.ticket.ext.SaTicketExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="ORDER_CODE" property="orderCode"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="ORDER_AMOUNT" property="orderAmount"/>
        <result column="PRICE" property="price"/>
        <result column="QUANTITY" property="quantity"/>
        <result column="ACT_NAME" property="actName"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="SYSTEM_TYPE" property="systemType"/>
        <collection property="saBuyTicketList" ofType="com.hzs.common.domain.sale.ticket.ext.SaBuyTicketExt">
            <result column="ITEM_ID" property="pkId"/>
            <result column="BUY_NAME" property="buyName"/>
            <result column="PHONE" property="phone"/>
            <result column="ID_CARD" property="idCard"/>
            <result column="SEX" property="sex"/>
            <result column="CLOTH_SIZE" property="clothSize"/>
            <result column="COHABITANT" property="cohabitant"/>
            <result column="EMERGENCY_PHONE" property="emergencyPhone"/>
            <result column="PK_TEAM" property="pkTeam"/>
            <result column="PRICE" property="price"/>
            <result column="VERTEX_NAME" property="vertexName"/>
            <result column="team_name" property="teamName"/>
        </collection>
    </resultMap>

    <select id="querySaTicketByParam" resultMap="SaTicketExt">
        select st.*,sc.pk_id item_id,sc.buy_name,sc.phone,sc.id_card,sc.sex,sc.cloth_size,sc.cohabitant,
        sc.emergency_phone,bv.vertex_name,ac.act_name,
        ac.act_start_date,ac.act_end_date,cm.member_code,cm.member_name,cmt.team_name
        from sa_ticket st
        inner join sa_buy_ticket sc
        on st.pk_id=sc.pk_sa_ticket
        inner join ac_ticket ac
        on ac.pk_id=st.pk_ticket
        left join bd_vertex bv
        on bv.pk_id=sc.pk_team and bv.del_flag=0
        left join cu_member cm
        on cm.pk_id=st.pk_creator and cm.del_flag=0
        left join CU_MEMBER_TEAM cmt on cm.PK_TEAM_CODE=cmt.pk_id and cmt.del_flag=0
        where st.del_flag=0 and sc.del_flag=0 and ac.del_flag=0
        <if test="saTicketParam.pkCountry != null">
            and st.PK_COUNTRY = #{saTicketParam.pkCountry}
        </if>
        <if test="saTicketParam.systemType != null">
            and st.SYSTEM_TYPE = #{saTicketParam.systemType}
        </if>
        <if test="saTicketParam.pkMember != null">
            and st.pk_creator = #{saTicketParam.pkMember}
        </if>
        <if test="saTicketParam.memberCode != null and saTicketParam.memberCode != ''">
            and cm.member_code like '%'||#{saTicketParam.memberCode} || '%'
        </if>
        <if test="saTicketParam.memberName != null and saTicketParam.memberName != ''">
            and cm.member_name like '%'||#{saTicketParam.memberName} || '%'
        </if>
        <if test="saTicketParam.pkTicket != null">
            and st.pk_ticket = #{saTicketParam.pkTicket}
        </if>
        <if test="saTicketParam.actName != null and saTicketParam.actName != ''">
            and ac.act_name = #{saTicketParam.actName}
        </if>
        <if test="saTicketParam.pkTeamListStr != null and saTicketParam.pkTeamListStr.size>0">
            and sc.pk_team in
            <foreach collection="saTicketParam.pkTeamListStr" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="saTicketParam.pkTeamCodeListStr != null and saTicketParam.pkTeamCodeListStr.size>0">
            and cm.PK_TEAM_CODE in
            <foreach collection="saTicketParam.pkTeamCodeListStr" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="saTicketParam.buyName != null and saTicketParam.buyName != ''">
            and sc.buy_name = #{saTicketParam.buyName}
        </if>
        <if test="saTicketParam.phone != null and saTicketParam.phone != ''">
            and sc.phone = #{saTicketParam.phone}
        </if>
        <if test="saTicketParam.orderCode != null and saTicketParam.orderCode != ''">
            and st.order_code = #{saTicketParam.orderCode}
        </if>
        <if test="saTicketParam.phoneList != null and saTicketParam.phoneList.size >0">
            and sc.phone in
            <foreach collection="saTicketParam.phoneList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        order by st.creation_time desc
    </select>

    <select id="queryBuySaTicketNum" resultType="java.lang.Integer">
        select sum(quantity) quantity
        from sa_ticket
        where del_flag = 0
          and pk_ticket = #{pkTicket}
    </select>

    <!-- 查询会员购买票数 -->
    <select id="sumQuantity" resultType="int">
        SELECT nvl(sum(st.quantity), 0) quantity
        FROM SA_TICKET st
        WHERE st.DEL_FLAG = 0
          AND st.PK_TICKET = #{pkTicket}
          AND st.PK_CREATOR = #{pkMember}
    </select>

</mapper>
