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

    <update id="updateEndGroupActivity">
        update ta_share_record a
        LEFT JOIN ta_share_activity b on a.group_activity_id = b.group_activity_id
        set a.`status` = '2'
        where (NOW() > b.end_time or b.activity_status = '2') and a.`status` = '1' and a.status != '0'
    </update>

    <update id="updateEndGroupActivityByGroupActivityId">
        <if test="groupActivityId != '' and groupActivityId != null">
            update ta_share_record a set a.status = '2' where a.group_activity_id = #{groupActivityId} and a.status != 0
        </if>
    </update>

    <select id="recordSuccessList" resultType="com.yunzhi.marketing.entity.TaShareRecord">
        (select
        t.record_id,
        t.group_activity_id as groupActivityId,
        t.nickname,
        t.phone,
        t.create_time,
        t.end_time,
        t.verification_status,
        t.verification_code
        from ta_share_record t where 1=1
        <if test="status != null">
            and t.status = #{status}
        </if>
        <if test="groupActivityId != null and groupActivityId != ''">
            and t.group_activity_id = #{groupActivityId}
        </if>
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
	    <if test="phone != null and phone != ''">
            and t.phone = #{phone}
        </if> order by t.end_time desc limit 100000000)
        union all
        (select
        a.child_record_id as record_id,
        a.group_activity_id as groupActivityId,
        a.nickname as joinNickName,
        a.phone as joinPhone,
        a.create_time as joinCreateTime,
        a.end_time as joinEndTime,
        a.verification_status as joinVerificationStatus,
        a.verification_code as joinVerificationCode
        from ta_share_child_record a where 1=1
        <if test="status !=null">
            and a.status = #{status}
        </if>
        <if test="groupActivityId != null and groupActivityId != ''">
            and a.group_activity_id = #{groupActivityId}
        </if>
        <if test="orgId != null and orgId != ''">
            and a.org_id = #{orgId}
        </if>
        <if test="phone != null and phone != ''">
            and a.phone = #{phone}
        </if> order by a.end_time desc limit 100000000)
    </select>

    <select id="recordFailAndProcessingList" resultType="com.yunzhi.marketing.po.TaShareRecordPO">
        select
        t.record_id,
        t.group_activity_id,
        t.person_id,
        t.nickname,
        t.phone,
        t.create_time,
        a.group_buy_people,
        t.join_people,
        t.status,
        t.verification_status,
        t.verification_code
        from ta_share_record t
        left join ta_share_activity a on t.group_activity_id = a.group_activity_id
        where 1=1
        <if test="status !=null and status != ''">
            and t.status = #{status}
        </if>
        <if test="groupActivityId != null and groupActivityId != ''">
            and t.group_activity_id = #{groupActivityId}
        </if>
        <if test="orgId != null and orgId != ''">
            and t.org_id = #{orgId}
        </if>
        <if test="phone != null and phone != ''">
            and t.phone = #{phone}
        </if> order by t.create_time desc
    </select>
    <select id="getShareRecordSuccessExportList" resultType="com.yunzhi.marketing.excel.ShareRecordSuccessExport">
        (
            select
            t.nickname,
            t.phone,
            t.create_time,
            t.end_time,
            t.verification_status
            from ta_share_record t
            where 1=1
            and t.status = #{status}
            <if test="recordId != null and recordId != ''">
                and t.record_id = #{recordId}
            </if>
            <if test="groupActivityId != null and groupActivityId != ''">
                and t.group_Activity_Id = #{groupActivityId}
            </if>
        )
        union all
        (
            select
            t.nickname,
            t.phone,
            t.create_time,
            t.end_time,
            t.verification_status
            from ta_share_child_record t
            where 1=1
            and t.status = #{status}
            <if test="recordId != null and recordId != ''">
                and t.record_id = #{recordId}
            </if>
            <if test="groupActivityId != null and groupActivityId != ''">
                and t.group_Activity_Id = #{groupActivityId}
            </if>
        )
        limit #{pageCode}, #{pageSize}
    </select>
    <select id="getShareRecordFailExportList" resultType="com.yunzhi.marketing.excel.ShareRecordFailExport">
        select
        t.nickname,
        t.phone,
        t.create_time
        from ta_share_record t
        where 1=1 and t.status = #{status}
        <if test="recordId != null and recordId != ''">
            and t.record_id = #{recordId}
        </if>
        <if test="groupActivityId != null and groupActivityId != ''">
            and t.group_Activity_Id = #{groupActivityId}
        </if>
        limit #{pageCode}, #{pageSize}
    </select>
    <select id="getShareRecordList" resultType="com.yunzhi.marketing.entity.TaShareRecord">
        SELECT
            t.record_id AS id,
            t.record_id,
            t.group_activity_id,
            t.nickname,
            t.phone,
            t.avatarurl,
            t.create_time,
            t.end_time,
            t.STATUS,
            t.verification_status,
            t.verification_code,
            a.activity_name,
            a.list_img as main_img,
            a.activity_status,
            a.end_time AS end_date,
            1 as groupStatus
        FROM
            ta_share_record t
            LEFT JOIN ta_share_activity a ON t.group_activity_id = a.group_activity_id
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
	UNION ALL
        SELECT
            t.child_record_id AS id,
            t.record_id,
            t.group_activity_id,
            t.nickname,
            t.phone,
            t.avatarurl,
            t.create_time,
            t.end_time,
            t.STATUS,
            t.verification_status,
            t.verification_code,
            a.activity_name,
            a.main_img,
            a.activity_status,
            a.end_time AS end_date,
            0 as groupStatus
        FROM
            ta_share_child_record t
            LEFT JOIN ta_share_activity a ON t.group_activity_id = a.group_activity_id
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
        order by status,create_time desc
    </select>

    <select id="countGroupBuyPeople" resultType="java.lang.Integer">
        select count(1) From ta_share_record t where t.status = '0' and t.group_activity_id = #{groupActivityId} GROUP BY t.group_activity_id
    </select>
</mapper>
