<?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.ruoyi.system.mapper.ZxActivityMapper">

    <resultMap type="ZxActivity" id="ZxActivityResult">
        <result property="activityId" column="activity_id"/>
        <result property="activityTitle" column="activity_title"/>
        <result property="activityDate" column="activity_date"/>
        <result property="activityTime" column="activity_time"/>
        <result property="activityAddress" column="activity_address"/>
        <result property="activityType" column="activity_type"/>
        <result property="activityDetails" column="activity_details"/>
        <result property="publishTime" column="publish_time"/>
        <result property="publishDept" column="publish_dept"/>
        <result property="publishDeptId" column="publish_dept_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <resultMap id="ZxActivityZxActivityUserResult" type="ZxActivity" extends="ZxActivityResult">
        <collection property="zxActivityUserList" notNullColumn="sub_activity_id" javaType="java.util.List"
                    resultMap="ZxActivityUserResult"/>
        <collection property="zxFjList"  javaType="java.util.List"
                    resultMap="ZxFjResult"/>
    </resultMap>

    <resultMap type="ZxActivityUser" id="ZxActivityUserResult">
        <result property="activityId" column="sub_activity_id"/>
        <result property="userId" column="sub_user_id"/>
        <result property="userName" column="sub_user_name"/>
        <result property="postName" column="sub_post_name"/>
        <result property="phonenumber" column="sub_phonenumber"/>
        <result property="userLevel" column="sub_user_level"/>
        <result property="parentGrade"    column="sub_parent_grade"/>
        <result property="replyTime" column="sub_reply_time"/>
        <result property="isJoin" column="sub_is_join"/>
        <result property="leaveType" column="sub_leave_type"/>
        <result property="leaveReason" column="sub_leave_reason"/>
        <result property="signIn" column="sub_sign_in"/>
        <result property="createBy" column="sub_create_by"/>
        <result property="createTime" column="sub_create_time"/>
        <result property="updateBy" column="sub_update_by"/>
        <result property="updateTime" column="sub_update_time"/>
        <result property="remark" column="sub_remark"/>
    </resultMap>

    <resultMap type="ZxFj" id="ZxFjResult">
        <result property="id" column="fj_id"/>
        <result property="mainId" column="fj_main_id"/>
        <result property="sourceId" column="fj_source_id"/>
        <result property="name" column="fj_name"/>
        <result property="url" column="fj_url"/>
        <result property="type" column="fj_type"/>
        <result property="stytle" column="fj_stytle"/>
        <result property="remark" column="fj_remark"/>
    </resultMap>

    <sql id="selectZxActivityVo">
        select activity_id,
               activity_title,
               activity_date,
               activity_time,
               activity_address,
               activity_type,
               activity_details,
               publish_time,
               publish_dept,
               publish_dept_id,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from zx_activity
    </sql>

    <select id="selectZxActivityList" parameterType="ZxActivity" resultMap="ZxActivityZxActivityUserResult">
        select a.activity_id, a.activity_title, a.activity_date, a.activity_time, a.activity_address, a.activity_type,
        a.activity_details, a.publish_time, a.publish_dept, a.publish_dept_id, a.create_by, a.create_time, a.update_by,
        a.update_time, a.remark,
        b.activity_id as sub_activity_id,b.phonenumber as sub_phonenumber, b.parent_grade as sub_parent_grade,b.user_id as sub_user_id, b.user_name as sub_user_name, b.post_name as
        sub_post_name, b.user_level as sub_user_level, b.reply_time as sub_reply_time, b.is_join as sub_is_join,
        b.leave_type as sub_leave_type, b.leave_reason as sub_leave_reason, b.sign_in as sub_sign_in, b.create_by as
        sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time,
        b.remark as sub_remark
        from zx_activity a
        left join zx_activity_user b on b.activity_id = a.activity_id
        <where>
            <if test="activityTitle != null  and activityTitle != ''">and a.activity_title = #{activityTitle}</if>
            <if test="activityDate != null ">and a.activity_date = #{activityDate}</if>
            <if test="activityTime != null  and activityTime != ''">and a.activity_time = #{activityTime}</if>
            <if test="activityAddress != null  and activityAddress != ''">and a.activity_address = #{activityAddress}
            </if>
            <if test="activityType != null  and activityType != ''">and a.activity_type = #{activityType}</if>
            <if test="activityDetails != null  and activityDetails != ''">and a.activity_details = #{activityDetails}
            </if>
            <if test="publishTime != null ">and a.publish_time = #{publishTime}</if>
            <if test="publishDept != null  and publishDept != ''">and a.publish_dept = #{publishDept}</if>
            <if test="publishDeptId != null ">and a.publish_dept_id = #{publishDeptId}</if>
            <if test="userId != null ">and b.user_id = #{userId}</if>
            <if test="year != null and year != ''"><!-- 开始时间检索 -->
                AND date_format(a.publish_time,'%Y') = #{year}
            </if>
        </where>
        order by a.publish_time DESC
    </select>

    <select id="selectZxActivityByActivityId" parameterType="Long" resultMap="ZxActivityZxActivityUserResult">
        select a.activity_id,
               a.activity_title,
               a.activity_date,
               a.activity_time,
               a.activity_address,
               a.activity_type,
               a.activity_details,
               a.publish_time,
               a.publish_dept,
               a.publish_dept_id,
               a.create_by,
               a.create_time,
               a.update_by,
               a.update_time,
               a.remark,
               b.activity_id  as sub_activity_id,
               b.phonenumber as sub_phonenumber,
               b.parent_grade as sub_parent_grade,
               b.user_id      as sub_user_id,
               b.user_name    as sub_user_name,
               b.post_name    as sub_post_name,
               b.user_level   as sub_user_level,
               b.reply_time   as sub_reply_time,
               b.is_join      as sub_is_join,
               b.leave_type   as sub_leave_type,
               b.leave_reason as sub_leave_reason,
               b.sign_in      as sub_sign_in,
               b.create_by    as sub_create_by,
               b.create_time  as sub_create_time,
               b.update_by    as sub_update_by,
               b.update_time  as sub_update_time,
               b.remark       as sub_remark,
               f.id           as fj_id,
               f.main_id      as fj_main_id,
               f.source_id    as fj_source_id,
               f.name         as fj_name,
               f.url          as fj_url,
               f.type         as fj_type,
               f.stytle       as fj_stytle
        from zx_activity a
                 left join zx_activity_user b on b.activity_id = a.activity_id
                 left join zx_fj f on f.source_id = a.activity_id
        where a.activity_id = #{activityId}
    </select>
    <select id="selectZxActivityUserByActivityId" resultMap="ZxActivityUserResult">
        select b.activity_id  as sub_activity_id,
               b.user_id      as sub_user_id,
               b.user_name    as sub_user_name,
               b.post_name    as sub_post_name,
               b.user_level   as sub_user_level,
               b.reply_time   as sub_reply_time,
               b.is_join      as sub_is_join,
               b.leave_type   as sub_leave_type,
               b.leave_reason as sub_leave_reason,
               b.sign_in      as sub_sign_in,
               b.create_by    as sub_create_by,
               b.create_time  as sub_create_time,
               b.update_by    as sub_update_by,
               b.update_time  as sub_update_time,
               b.remark       as sub_remark
        from zx_activity_user b
        where b.activity_id = #{activityId}
    </select>
    <select id="selectZxActivityUser" resultMap="ZxActivityUserResult">
        select b.activity_id as sub_activity_id, b.user_id as sub_user_id, b.user_name as sub_user_name, b.post_name as
        sub_post_name, b.user_level as sub_user_level,b.parent_grade as sub_parent_grade, b.reply_time as sub_reply_time, b.is_join as sub_is_join,
        b.leave_type as sub_leave_type,b.phonenumber as sub_phonenumber, b.leave_reason as sub_leave_reason, b.sign_in as sub_sign_in, b.create_by as
        sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time,
        b.remark as sub_remark
        from zx_activity_user b
        <where>
            b.activity_id = #{activityId}
            <if test="userName != null  and userName != ''">and b.user_name = #{userName}</if>
        </where>
    </select>
    <select id="selectZxActivityInfo" parameterType="ZxActivity" resultMap="ZxActivityZxActivityUserResult">
        select a.activity_id, a.activity_title, a.activity_date, a.activity_time, a.activity_address, a.activity_type,
        a.activity_details, a.publish_time, a.publish_dept, a.publish_dept_id, a.create_by, a.create_time, a.update_by,
        a.update_time, a.remark,
        b.activity_id as sub_activity_id,b.phonenumber as sub_phonenumber,b.parent_grade as sub_parent_grade, b.user_id as sub_user_id, b.user_name as sub_user_name, b.post_name as
        sub_post_name, b.user_level as sub_user_level, b.reply_time as sub_reply_time, b.is_join as sub_is_join,
        b.leave_type as sub_leave_type, b.leave_reason as sub_leave_reason, b.sign_in as sub_sign_in, b.create_by as
        sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time,
        b.remark as sub_remark
        from zx_activity a
        left join zx_activity_user b on b.activity_id = a.activity_id
        <where>
            a.activity_id = #{activityId}
            <if test="userId != null ">and b.user_id = #{userId}</if>
        </where>
    </select>

    <insert id="insertZxActivity" parameterType="ZxActivity" useGeneratedKeys="true" keyProperty="activityId">
        insert into zx_activity
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityTitle != null and activityTitle != ''">activity_title,</if>
            <if test="activityDate != null">activity_date,</if>
            <if test="activityTime != null">activity_time,</if>
            <if test="activityAddress != null and activityAddress != ''">activity_address,</if>
            <if test="activityType != null and activityType != ''">activity_type,</if>
            <if test="activityDetails != null">activity_details,</if>
            <if test="publishTime != null">publish_time,</if>
            <if test="publishDept != null">publish_dept,</if>
            <if test="publishDeptId != null">publish_dept_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityTitle != null and activityTitle != ''">#{activityTitle},</if>
            <if test="activityDate != null">#{activityDate},</if>
            <if test="activityTime != null">#{activityTime},</if>
            <if test="activityAddress != null and activityAddress != ''">#{activityAddress},</if>
            <if test="activityType != null and activityType != ''">#{activityType},</if>
            <if test="activityDetails != null">#{activityDetails},</if>
            <if test="publishTime != null">#{publishTime},</if>
            <if test="publishDept != null">#{publishDept},</if>
            <if test="publishDeptId != null">#{publishDeptId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateZxActivity" parameterType="ZxActivity">
        update zx_activity
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityTitle != null and activityTitle != ''">activity_title = #{activityTitle},</if>
            <if test="activityDate != null">activity_date = #{activityDate},</if>
            <if test="activityTime != null">activity_time = #{activityTime},</if>
            <if test="activityAddress != null and activityAddress != ''">activity_address = #{activityAddress},</if>
            <if test="activityType != null and activityType != ''">activity_type = #{activityType},</if>
            <if test="activityDetails != null">activity_details = #{activityDetails},</if>
            <if test="publishTime != null">publish_time = #{publishTime},</if>
            <if test="publishDept != null">publish_dept = #{publishDept},</if>
            <if test="publishDeptId != null">publish_dept_id = #{publishDeptId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where activity_id = #{activityId}
    </update>
    <update id="updateReply" parameterType="ZxActivityUser">
        update zx_activity_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="replyTime != null">reply_time = #{replyTime},</if>
            <if test="isJoin != null and isJoin != ''">is_join = #{isJoin},</if>
            <if test="leaveType != null and leaveType != ''">leave_type = #{leaveType},</if>
            <if test="leaveReason != null and leaveReason != ''">leave_reason = #{leaveReason},</if>
            <if test="signIn != null and signIn != ''">sign_in = #{signIn},</if>
        </trim>
        where activity_id = #{activityId} and user_id = #{userId}
    </update>

    <delete id="deleteZxActivityByActivityId" parameterType="Long">
        delete
        from zx_activity
        where activity_id = #{activityId}
    </delete>

    <delete id="deleteZxActivityByActivityIds" parameterType="String">
        delete from zx_activity where activity_id in
        <foreach item="activityId" collection="array" open="(" separator="," close=")">
            #{activityId}
        </foreach>
    </delete>

    <delete id="deleteZxActivityUserByActivityIds" parameterType="String">
        delete from zx_activity_user where activity_id in
        <foreach item="activityId" collection="array" open="(" separator="," close=")">
            #{activityId}
        </foreach>
    </delete>

    <delete id="deleteZxActivityUserByActivityId" parameterType="Long">
        delete
        from zx_activity_user
        where activity_id = #{activityId}
    </delete>

    <insert id="batchZxActivityUser">
        insert into zx_activity_user( activity_id, user_id, user_name, phonenumber ,post_name, user_level, reply_time, is_join,
        leave_type,parent_grade, leave_reason, sign_in, create_by, create_time, update_by, update_time, remark) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.activityId}, #{item.userId}, #{item.userName}, #{item.phonenumber},#{item.postName}, #{item.userLevel},
            #{item.replyTime}, #{item.isJoin}, #{item.leaveType},#{item.parentGrade}, #{item.leaveReason}, #{item.signIn}, #{item.createBy},
            #{item.createTime}, #{item.updateBy}, #{item.updateTime}, #{item.remark})
        </foreach>
    </insert>
</mapper>