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

    <select id="getActivityList" resultType="com.yunzhi.marketing.activity.entity.Activity">
        SELECT dynamic_id as id,list_img_url as main_img,title,start_date as start_date,enlist_end as end_date,heavy, weight,activity_status,'dynamic' as type
        FROM ta_building_dynamic
        WHERE org_id = #{orgId}  and `status` = #{status}
        <if test="cityId != null and cityId != ''">
          and city_id = #{cityId}
        </if>
        UNION  all
        SELECT group_activity_id as id,list_img as main_img,activity_name as title,start_time as start_date,end_time as end_date,heavy,weight,activity_status,'group' as type
        from ta_share_activity
        WHERE org_id = #{orgId} and `status` = #{status}
        <if test="cityId != null and cityId != ''">
            and city_id = #{cityId}
        </if>
        UNION  all
        SELECT help_activity_id as id,list_img as main_img,title,start_date,end_date,heavy,weight,activity_status,'help' as type
        from ta_help_activity
        WHERE org_id = #{orgId} and `status` = #{status}
        <if test="cityId != null and cityId != ''">
            and city_id = #{cityId}
        </if>
        UNION ALL
        SELECT
        live_activity_id AS id,list_img AS main_img,live_activity_title,live_start_date,live_end_date, weight as heavy, 0 weight,status activity_status,          'live' AS type
        FROM
        ta_live_activity
        WHERE org_id = #{orgId} and `status` = #{status}
        <if test="cityId != null and cityId != ''">
            and city_id = #{cityId}
        </if>
        ORDER BY heavy desc,activity_status,start_date
    </select>
    <select id="getCustomerActivityList" resultType="java.util.Map">
        SELECT
            t.enlist_id AS id,
            t.dynamic_id AS activityId,
            t.create_date AS createDate,
            IF( t.is_checkin = 1, "已签到", "未签到" ) AS `joinStatus`,
            d.list_img_url AS mainImg,
            d.title,
            d.status,
            d.start_date AS startDate,
            d.enlist_end AS endDate,
            'dynamic' AS type
        FROM
            ta_activity_dynamic_enlist t
            LEFT JOIN ta_building_dynamic d ON t.dynamic_id = d.dynamic_id
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
      UNION ALL
        SELECT
            t.record_id AS id,
            t.group_activity_id AS activityId,
            t.create_time AS createDate,
        CASE
            WHEN t.STATUS = 1 THEN
            "进行中"
            WHEN t.STATUS = 0
            AND t.verification_status = 1 THEN
            "成功已核销"
        WHEN t.STATUS = 0
        AND t.verification_status != 1 THEN
        "成功未核销"
        WHEN t.STATUS = 2 THEN
        "失败"
        END `joinStatus`,
            d.list_img AS mainImg,
            d.activity_name AS title,
            d.status,
            d.start_time AS startDate,
            d.end_time AS endDate,
            'group' AS type
        FROM
            ta_share_record t
            LEFT JOIN ta_share_activity d ON t.group_activity_id = d.group_activity_id
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
      UNION ALL
        SELECT
            t.record_id AS id,
            t.group_activity_id AS activityId,
            t.create_time AS createDate,
        CASE
            WHEN t.STATUS = 1 THEN
            "进行中"
            WHEN t.STATUS = 0
            AND t.verification_status = 1 THEN
            "成功已核销"
        WHEN t.STATUS = 0
        AND t.verification_status != 1 THEN
        "成功未核销"
        WHEN t.STATUS = 2 THEN
        "失败"
        END `joinStatus`,
            d.list_img AS mainImg,
            d.activity_name AS title,
            d.status,
            d.start_time AS startDate,
            d.end_time AS endDate,
            'group' AS type
        FROM
            ta_share_child_record t
            LEFT JOIN ta_share_activity d ON t.group_activity_id = d.group_activity_id
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
      UNION ALL
        SELECT
            t.help_record_initiate_id AS id,
            t.help_activity_id AS activityId,
            t.create_date AS createDate,
        CASE
                WHEN t.STATUS = 1 THEN
                "进行中"
                WHEN t.STATUS = 0
                AND t.verification_status = 1 THEN
                    "成功已核销"
                    WHEN t.STATUS = 0
                    AND t.verification_status != 1 THEN
                        "成功未核销"
                        WHEN t.STATUS = 2 THEN
                        "失败"
                    END `joinStatus`,
            d.list_img AS mainImg,
            d.title,
            d.status,
            d.start_date AS startDate,
            d.end_date AS endDate,
            'help' AS type
        FROM
            ta_help_initiate_record t
            LEFT JOIN ta_help_activity d ON t.help_activity_id = d.help_activity_id
        WHERE
            t.org_id = #{orgId}
            AND t.person_id = #{personId}
        ORDER BY
        createDate DESC
    </select>
</mapper>
