<?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.smile.mapper.SmileAgentMapper">

    <select id="listAgentByShowIdAndTicketId" resultType="com.liquidnet.service.smile.entity.SmileAgent">
        select id,performance_id from smile_agent
        <where>
            <if test="performanceId!=''">
                AND performance_id = #{performanceId}
            </if>
            <if test="ticketId!=''">
                AND ticket_id = #{ticketId}
            </if>
        </where>
    </select>

    <update id="updateAgentById">
        update smile_agent
        set ord_carry   = #{ordCarry},
            total_carry = #{totalCarry}
        where id = #{id}
    </update>

    <select id="getUserOrgByPerIdAndUidAndAgentMaster" resultType="java.util.Map">
        SELECT
            kotr.agent_id_master uid,
            SUM( ( kot.price_actual -  kot.price_express )* kotr.agent_distributions_master ) totalPrice
        FROM
            kylin_order_ticket_relations AS kotr
                INNER JOIN kylin_order_tickets AS kot ON kot.order_tickets_id = kotr.order_id
        WHERE
                kot.order_tickets_id IN (
                SELECT
                    kotr.order_id
                FROM
                    kylin_order_ticket_relations AS kotr
                        LEFT JOIN kylin_order_ticket_status AS kots ON kots.order_id = kotr.order_id
                WHERE
                    kots.STATUS IN (1) )
          AND kotr.performance_id = #{performancesId}
          AND kotr.ticket_id IN (
            SELECT
                sa.ticket_id
            FROM
                smile_agent AS sa
            WHERE
                sa.del_tag = 0
              AND sa.performance_id = #{performancesId}
        ) AND 	kotr.agent_id_master >0  GROUP BY kotr.agent_id_master
    </select>

    <select id="selectAgentList" resultType="com.liquidnet.service.smile.entity.dao.SmileAgentDao">
        SELECT sa.id,
               sa.performance_id,
               sa.ticket_id,
               sa.ord_carry,
               sa.total_carry,
               kt.title ticket_name
        FROM smile_agent sa
                 INNER JOIN kylin_tickets kt ON kt.tickets_id = sa.ticket_id
        WHERE sa.performance_id = #{performancesId}
          AND sa.del_tag = 0
    </select>

    <select id="selectAgentCarryGtZeroByPerId" resultType="com.liquidnet.service.smile.entity.SmileAgent">
        SELECT id,
               performance_id,
               ticket_id
        FROM smile_agent
        WHERE performance_id = #{performancesId}
          AND del_tag = 0
    </select>

    <select id="selectMapByPerId" resultType="com.liquidnet.service.smile.entity.dto.ShowBaseVoDto">
        SELECT
            sukotr.`name`,
            sukotr.city,
            sukotr.uid AS agent_id,
            sukotr.ticket_id AS tid,
            sukotr.id_card as idCard,
            sukotr.type,
            sukotr.phone,
            sukotr.state,
            sukotr.agentName,
            kot.price,
            SUM(kot.price_actual) price_real,
            sukotr.agent_distributions AS `use`,
            SUM( kot.number ) number,
            SUM( (kot.price_actual - kot.price_express) * sukotr.agent_distributions ) totalPrice,
            SUM( (kot.price_actual - kot.price_express) ) realTotalPrice,
            SUM( kot.price_voucher ) voucherPrice,
            SUM( kot.price_express )  totalPriceExpress,
            SUM( kot.price_total - kot.price_express ) faceTotalPrice,
            MAX(sukotr.agent_distributions ) agentDistributions
        FROM
            kylin_order_tickets AS kot
                INNER JOIN (
                SELECT
                    su.uid,
                    su.`name`,
                    su.city,
                    su.type,
                    su.phone,
                    su.state,
                    su.id_card,
                    agentSu.name agentName,
                    kotr.order_id,
                    kotr.performance_id,
                    kotr.ticket_id,
                    kotr.agent_distributions
                FROM
                    smile_user AS su
                        INNER JOIN kylin_order_ticket_relations AS kotr ON kotr.agent_id = su.uid
                        INNER JOIN kylin_order_ticket_status AS kots ON kots.order_id = kotr.order_id
                        INNER JOIN smile_user AS agentSu ON agentSu.uid = su.agent_id
                WHERE
                        su.uid IN (
                        SELECT kotr.agent_id
                        FROM kylin_order_ticket_relations as kotr
                                 LEFT JOIN kylin_order_ticket_status AS kots ON kotr.order_id = kots.order_id
                                 INNER JOIN smile_user AS su ON su.uid = kotr.agent_id
                        WHERE kots.status IN (1)
                          AND kotr.agent_id != 0
                          AND (kotr.agent_distributions_master !=0 or kotr.agent_distributions!=0)
                          AND kotr.performance_id = #{performancesId}
                        group by kotr.agent_id
                    )
                  AND kotr.performance_id = #{performancesId}
                  AND (kotr.agent_distributions_master !=0 or kotr.agent_distributions!=0)
                  AND kots.`status` IN (1)
            ) AS sukotr ON sukotr.order_id = kot.order_tickets_id
        GROUP BY
            sukotr.uid,
            sukotr.ticket_id
        ORDER BY
            CASE sukotr.type
            WHEN 2 THEN 1
            WHEN 1 THEN 2
            WHEN 5 THEN 3
            ELSE 4
        END
    </select>

    <update id="upTakeByPerId">
        update smile_agent
        set type = #{type}
        where performance_id = #{perId}
    </update>

</mapper>
