<?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.xjrsoft.module.activity.mapper.ActivityInfoMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.activity.dto.ActivityInfoPageDto" resultType="com.xjrsoft.module.activity.vo.ActivityInfoPageVo">
        SELECT t1.id,t2.name AS type_cn,t3.company_name AS enterprise_name,t1.name,t1.place,t1.start_date,t1.end_date,
        t1.type, t4.name AS org_name, t1.enroll_end_time, t1.duty_person,t1.duty_person_mobile, t1.cover_file_id,
        t1.enroll_range, t1.status,t1.content,t1.category,t4.name as org_cn,t1.org_id,
        (SELECT COUNT(*) FROM activity_enroll WHERE delete_mark = 0 AND activity_info_id = t1.id) as enroll_count FROM activity_info t1
        LEFT JOIN xjr_dictionary_detail t2 ON t1.type = t2.code
        LEFT JOIN company_coop t3 ON t1.enterprise_id = t3.id
        LEFT JOIN xjr_department t4 ON t1.org_id = t4.id
        WHERE t1.delete_mark = 0
        <if test="dto.category != null">
            and t1.category = #{dto.category}
        </if>
        <if test="dto.isEffective != null and dto.isEffective == 1">
            and (t1.status = 1 or t1.status = 2) and enroll_end_time >= NOW()
        </if>
        <if test="dto.name != null and dto.name != ''">
            and t1.name like concat('%', #{dto.name},'%')
        </if>
        <if test="dto.type != null and dto.type != ''">
            and t1.type = #{dto.type}
        </if>
        <if test="dto.orgId != null">
            and t1.org_id = #{dto.orgId}
        </if>
        <if test="dto.status != null ">
            and t1.status = #{dto.status}
        </if>
        <if test="dto.activityInfoIds != null and dto.activityInfoIds.size() > 0">
            and t1.id in
            <foreach item="id" index="index" collection="dto.activityInfoIds" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </if>
    </select>
    <select id="getMyPage" parameterType="com.xjrsoft.module.activity.dto.ActivityInfoPageDto" resultType="com.xjrsoft.module.activity.vo.ActivityInfoPageVo">
        SELECT t1.id,t1.name,t1.place,t1.start_date,t1.end_date,
        t1.type, t4.name AS org_name, t1.enroll_end_time, t1.cover_file_id,
        t1.status,t1.content,t1.category,t5.status AS enroll_status,t2.name as type_cn, 1 as is_enroll FROM activity_info t1
        LEFT JOIN xjr_dictionary_detail t2 ON t1.type = t2.code
        LEFT JOIN company_coop t3 ON t1.enterprise_id = t3.id
        LEFT JOIN xjr_department t4 ON t1.org_id = t4.id
        LEFT JOIN activity_enroll t5 ON t1.id = t5.activity_info_id
        WHERE t1.delete_mark = 0 AND t5.user_id = #{dto.loginUserId}
        <if test="dto.category != null">
            and t1.category = #{dto.category}
        </if>
        <if test="dto.isEffective != null and dto.isEffective == 1">
            and (t1.status = 1 or t1.status = 2) and NOW() between t1.start_date and t1.end_date
        </if>
        UNION
        SELECT a1.id, a1.club_activities_name AS NAME,a1.activity_location AS place,a1.start_time AS start_date,a1.end_time AS end_date,
        NULL AS TYPE,NULL AS org_name,NULL AS enroll_end_time,NULL AS cover_file_id,1 AS STATUS,
        a1.club_activities_content AS content,3 AS category,2 AS enroll_status,
        a1.activity_type AS type_cn, 1 AS is_enroll
        FROM club_activities a1 WHERE a1.delete_mark = 0 AND a1.student_id IN
        (SELECT credential_number FROM xjr_user WHERE id = #{dto.loginUserId})
        UNION
        SELECT a2.id, a2.moral_event_name AS NAME,a2.event_address AS place,
        a2.event_start_time AS start_date,a2.event_end_time AS end_date,NULL AS TYPE,NULL AS org_name,NULL AS enroll_end_time,
        NULL AS cover_file_id,1 AS status,
        a2.event_content AS content,4 AS category,2 AS enroll_status
        , a2.event_type AS type_cn, 1 as is_enroll
        FROM moral_event a2 WHERE a2.delete_mark = 0 AND
        (a2.attend_teacher like concat('%',#{dto.loginUserId},'%') OR FIND_IN_SET((
        SELECT class_id FROM base_student_school_roll WHERE user_id = #{dto.loginUserId}
        ),a2.attend_class) > 0
        )
        UNION
        SELECT a3.id,a3.training_name AS NAME,a3.training_address AS place,a3.start_time AS start_date, a3.end_time AS end_date,
        NULL AS TYPE,NULL AS org_name,NULL AS enroll_end_time,NULL AS cover_file_id, STATUS,
        a3.training_topic AS content,5 AS category,2 AS enroll_status,  NULL AS type_cn, 1 as is_enroll FROM wf_teacher_training a3
        INNER JOIN wf_teacher_training_participant a4 ON a3.id = a4.wf_teacher_training_id
        WHERE a3.delete_mark = 0 AND a4.participant_id = #{dto.loginUserId}
        UNION
        SELECT id,party_tissue_name AS NAME,event_address AS place,event_start_time AS start_date, event_end_time AS end_date,event_type AS TYPE,
        NULL AS org_name,NULL AS enroll_end_time,NULL AS cover_file_id,1 AS STATUS, event_content AS content, 6 AS category,
        2 AS enroll_status, NULL AS type_cn, 1 as is_enroll FROM party_build_event
        WHERE delete_mark = 0 AND (
        participant_teacher_name LIKE CONCAT('%',#{dto.loginUserId},'%') OR participant_student_name LIKE CONCAT('%',#{dto.loginUserId},'%')
        )
    </select>
</mapper>