<?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.ics.admin.mapper.meeting.CustomerTicketMapper">
    
    <resultMap type="com.ics.admin.domain.meeting.CustomerTicket" id="CustomerTicketResult">
        <result property="id"    column="id"    />
        <result property="customerId"    column="customer_id"    />
        <result property="ticketId"    column="ticket_id"    />
        <result property="staffId"    column="staff_id"    />
        <result property="num"    column="num"    />
        <result property="type"    column="type"    />
        <result property="isVerification"    column="is_verification"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectCustomerTicketVo">
        SELECT id, customer_id, ticket_id, num, is_verification, create_time,staff_id,type FROM tb_customer_ticket
    </sql>

    <select id="selectCustomerTicketList" parameterType="CustomerTicket" resultMap="CustomerTicketResult">
        <include refid="selectCustomerTicketVo"/>
        <where>  
        </where>
    </select>
    
    <select id="selectCustomerTicketById" parameterType="Long" resultMap="CustomerTicketResult">
        <include refid="selectCustomerTicketVo"/>
        WHERE id = #{id}
    </select>
    <select id="selectListByCustomerId" resultType="com.ics.admin.domain.meeting.CustomerTicket">
        select tct.id, tct.customer_id, tct.ticket_id, tct.num, tct.is_verification, tct.create_time,tct.staff_id,tct.type
        from tb_customer_ticket tct
        left join tb_ticket tt on tct.ticket_id = tt.id
        where tct.customer_id = #{customerId}
        and tct.is_verification = 0
        and tct.staff_id is  null
        and tt.is_show = 0
        group by tct.ticket_id
    </select>
    <select id="selectListByTicketId" resultMap="CustomerTicketResult">
        select customer_id,count(1) as num from tb_customer_ticket where ticket_id = #{id} GROUP BY customer_id
    </select>
    <select id="selectByTicketIdAndCustomer" resultType="java.lang.Integer">
        select count(1) as num from tb_customer_ticket where ticket_id = #{id} and customer_id =#{customerId}  and is_verification = 0   GROUP BY customer_id

    </select>

    <insert id="insertCustomerTicket" parameterType="CustomerTicket">
        INSERT INTO tb_customer_ticket
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null ">id,</if>
            <if test="customerId != null ">customer_id,</if>
            <if test="ticketId != null ">ticket_id,</if>
            <if test="num != null ">num,</if>
            <if test="isVerification != null ">is_verification,</if>
            <if test="createTime != null ">create_time,</if>
            <if test="staffId != null ">staff_id,</if>
            <if test="type != null ">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null ">#{id},</if>
            <if test="customerId != null ">#{customerId},</if>
            <if test="ticketId != null ">#{ticketId},</if>
            <if test="num != null ">#{num},</if>
            <if test="isVerification != null ">#{isVerification},</if>
            <if test="createTime != null ">#{createTime},</if>
            <if test="staffId != null ">#{staffId},</if>
            <if test="type != null ">#{type},</if>
         </trim>
    </insert>

    <update id="updateCustomerTicket" parameterType="CustomerTicket">
        UPDATE tb_customer_ticket
        <trim prefix="SET" suffixOverrides=",">
            <if test="customerId != null ">customer_id = #{customerId},</if>
            <if test="ticketId != null ">ticket_id = #{ticketId},</if>
            <if test="num != null ">num = #{num},</if>
            <if test="isVerification != null ">is_verification = #{isVerification},</if>
            <if test="createTime != null ">create_time = #{createTime},</if>
            <if test="staffId != null ">staff_id = #{staffId},</if>
            <if test="type != null ">staff_id = #{type},</if>
        </trim>
        WHERE id = #{id}
    </update>

    <delete id="deleteCustomerTicketById" parameterType="Long">
        DELETE FROM tb_customer_ticket WHERE id = #{id}
    </delete>

    <delete id="deleteCustomerTicketByIds" parameterType="String">
        DELETE FROM tb_customer_ticket where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteCustomerTicketByTicketId">
        DELETE FROM tb_customer_ticket WHERE ticket_id = #{ticketId} and is_verification = 0
    </delete>
    <delete id="deleteCustomerTicketByTicketIdAndCustomer">
        DELETE FROM tb_customer_ticket WHERE ticket_id = #{id} and customer_id = #{customerId} and is_verification = 0

    </delete>

</mapper>