<?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.huiju.estateagents.redpack.mapper.TaRedPacketMapper">

    <insert id="initRedPacket">
        INSERT INTO ta_red_packet (
        `activity_id`,
        `name`,
        `start_date`,
        `end_date`,
        `activity_end_tip`,
        `qrcode`,
        `top_limit_persons`,
        `building_show_pos`,
        `building_id`,
        `building_name`,
        `poster_show_pos`,
        `join_fields`,
        `org_id`
        ) SELECT
        #{activityId},
        #{name},
        #{startDate},
        #{endDate},
        `activity_end_tip`,
        #{qrCode},
        `top_limit_persons`,
        `building_show_pos`,
        `building_id`,
        `building_name`,
        `poster_show_pos`,
        `join_fields`,
        #{orgId}
        FROM
            ta_red_packet t2
        WHERE
            activity_id = #{templeActivityId}
    </insert>

    <insert id="initRedPacketHelp">
        INSERT INTO ta_red_packet_help (
        `activity_id`,
        `help_succes_persons`,
        `limit_num_per_day`,
        `limit_num_per_person`,
        `limit_num_for_each_person`,
        `success_tip`,
        `warnning_tip`,
        `limit_per_day_illegal_tip`,
        `limit_per_person_illegal_tip`,
        `limit_for_each_person_illegal_tip`
        ) SELECT
        #{activityId},
        `help_succes_persons`,
        `limit_num_per_day`,
        `limit_num_per_person`,
        `limit_num_for_each_person`,
        `success_tip`,
        `warnning_tip`,
        `limit_per_day_illegal_tip`,
        `limit_per_person_illegal_tip`,
        `limit_for_each_person_illegal_tip`
        FROM
            ta_red_packet_help t2
        WHERE
            activity_id =#{templeActivityId}
    </insert>

    <insert id="initRedPacketClient">
        INSERT INTO ta_red_packet_client (
           `activity_id`,
           `mch_name`,
            `blessing`,
            `activity_name`,
            `remark`
         ) SELECT
            #{activityId},
            `mch_name`,
            `blessing`,
            `activity_name`,
            `remark`
        FROM
            ta_red_packet_client t2
        WHERE
            activity_id = #{templeActivityId}
    </insert>

    <insert id="initRedPacketMoney">
        INSERT INTO ta_red_packet_money ( `activity_id`, `budget`, `balance`, `min_packet`, `max_packet`, `limit_packet_num`, `packet_sended_num`, `insufficient_balance_tip` ) SELECT
            #{activityId},
            `budget`,
            `balance`,
            `min_packet`,
            `max_packet`,
            `limit_packet_num`,
            `packet_sended_num`,
            `insufficient_balance_tip` 
        FROM
            ta_red_packet_money t2 
        WHERE
            activity_id =#{templeActivityId}
    </insert>

    <insert id="initRedPacketImages">
        INSERT INTO ta_h5_images ( `activity_type`, `activity_id`, `image_type`, `image_url`, `sort_no` ) SELECT
            `activity_type`,
            #{activityId},
            `image_type`,
            `image_url`,
            `sort_no`
        FROM
            ta_h5_images t2
        WHERE
            t2.activity_id = #{templeActivityId}
    </insert>

    <select id = "listVisitRecordByCondition" resultType="com.huiju.estateagents.entity.TaCustomerFrom">
        SELECT
            t.*,
            t2.nickname,
            t2.NAME,
            t2.avatarurl,
            t2.phone,
            t3.nickname sharePersonNickname,
            t3.phone sharePersonPhone
        FROM
            ta_customer_from t
            LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
            LEFT JOIN ta_person t3 ON t.share_person_id = t2.person_id
        WHERE
            t.org_id = #{orgId}
            AND t.target_type IN ( 'h5', 'dynamic' )
            AND t.target_id = #{drainageId}
            <if test = "startTime != null">
                AND t.create_date &gt;= #{startTime}
            </if>

             <if test = "endTime != null">
                 AND t.create_date &lt;= #{endTime}
            </if>

            <if test="sharePersonPhone != null and sharePersonPhone != ''">
                AND t3.phone = #{sharePersonPhone}
            </if>
            <if test="phone != null and phone != ''">
                AND t2.phone = #{phone}
            </if>
         ORDER BY
            t.create_date desc
    </select>

    <select id = "exportVisitRecordByCondition" resultType="com.huiju.estateagents.excel.redPacket.RedPacketVisitRecordtExport">
        SELECT
        t.create_date
        t2.nickname,
        t2.NAME,
        t2.avatarurl,
        t2.phone,
        t3.nickname sharePersonNickname,
        t3.phone sharePersonPhone
        FROM
        ta_customer_from t
        LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
        LEFT JOIN ta_person t3 ON t.share_person_id = t2.person_id
        WHERE
        t.org_id = #{orgId}
        AND t.target_type IN ( 'h5', 'dynamic' )
        AND t.target_id = #{drainageId}
        <if test = "startTime != null">
            AND t.create_date &gt;= #{startTime}
        </if>

        <if test = "endTime != null">
            AND t.create_date &lt;= #{endTime}
        </if>

        <if test="sharePersonPhone != null and sharePersonPhone != ''">
            AND t3.phone = #{sharePersonPhone}
        </if>
        <if test="phone != null and phone != ''">
            AND t2.phone = #{phone}
        </if>
        ORDER BY
        t.create_date desc
    </select>

</mapper>
