<?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.yunzhi.marketing.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`,
        #{buildingId},
        `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},
            #{miniAppName},
            `blessing`,
            #{activityName},
            `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.yunzhi.marketing.entity.TaPersonVisitRecord">
            SELECT
            max(t.visit_time) visit_time,
            t2.nickname,
            t2.NAME as name,
            t2.avatarurl,
            t2.phone,
            ifnull( t3.nickname, t3.NAME ) sharePersonNickname,
            t3.phone sharePersonPhone
        FROM
            ta_person_visit_record t
        LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
        LEFT JOIN ta_person t3 ON t.share_person_id = t3.person_id
        WHERE
            t.org_id =  #{orgId}
            and t.event_type = 'h5'
            AND t.target_id = #{drainageId}
            AND (t.share_person_id != NULL  or t.share_person_id != '')
            AND t.share_person_id != t.person_id
            <if test="sharePersonPhone != null and sharePersonPhone != ''">
                AND t3.phone like CONCAT('%',#{sharePersonPhone}, '%')
            </if>
            <if test="phone != null and phone != ''">
                AND t2.phone like CONCAT('%',#{phone}, '%')
            </if>
            <if test = "startTime != null">
                AND t.visit_time &gt;= #{startTime}
            </if>

            <if test = "endTime != null">
                AND t.visit_time &lt;= #{endTime}
            </if>
        GROUP BY
        t.person_id,t.share_person_id
        ORDER BY
        max(t.visit_time) DESC
    </select>

    <select id = "exportVisitRecordByCondition" resultType="com.yunzhi.marketing.excel.redPacket.RedPacketVisitRecordtExport">

        SELECT
        t.visit_time createDate,
        t2.nickname,
        t2.NAME as name,
        t2.avatarurl,
        t2.phone,
        ifnull(t3.nickname,t3.name) sharePersonNickname,
        t3.phone sharePersonPhone
        FROM
        ta_person_visit_record t
        LEFT JOIN ta_person t2 ON t.person_id = t2.person_id
        LEFT JOIN ta_person t3 ON t.share_person_id = t3.person_id
        WHERE
        t.org_id =  #{orgId}
        and t.event_type = 'h5'
        AND t.target_id = #{drainageId}
        AND (t.share_person_id != NULL  or t.share_person_id != '')
        AND t.share_person_id != t.person_id
        <if test="sharePersonPhone != null and sharePersonPhone != ''">
            AND t3.phone like CONCAT('%',#{sharePersonPhone}, '%')
        </if>
        <if test="phone != null and phone != ''">
            AND t2.phone like CONCAT('%',#{phone}, '%')
        </if>
        <if test = "startTime != null">
            AND t.visit_time &gt;= #{startTime}
        </if>

        <if test = "endTime != null">
            AND t.visit_time &lt;= #{endTime}
        </if>
        GROUP BY
        t.person_id,t.share_person_id
        ORDER BY
        max(t.visit_time) DESC
    </select>

</mapper>
