<?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.models.mapper.BizModuleMapper">
    
    <resultMap type="BizModule" id="BizModuleResult">
        <result property="moduleId"    column="module_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="moduleName"    column="module_name"    />
        <result property="status"    column="status"    />
        <result property="assignee"    column="assignee"    />
        <result property="assignTime"    column="assign_time"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="projectName"    column="project_name"    />
        <result property="designatedUser"    column="designated_user"    />
    </resultMap>

    <sql id="selectBizModuleVo">
        select m.module_id, m.project_id, m.module_name, m.status, m.assignee, m.assign_time, m.finish_time, 
               m.del_flag, m.create_by, m.create_time, m.designated_user, p.project_name
        from biz_module m
        left join biz_project p on m.project_id = p.project_id
    </sql>

    <select id="selectBizModuleList" parameterType="BizModule" resultMap="BizModuleResult">
        <include refid="selectBizModuleVo"/>
        <where>  
            <if test="projectId != null "> and m.project_id = #{projectId}</if>
            <if test="moduleName != null  and moduleName != ''"> and m.module_name like concat('%', #{moduleName}, '%')</if>
            <if test="status != null  and status != ''"> and m.status = #{status}</if>
            <if test="assignee != null  and assignee != ''"> and m.assignee = #{assignee}</if>
            <if test="assignTime != null "> and m.assign_time = #{assignTime}</if>
            <if test="finishTime != null "> and m.finish_time = #{finishTime}</if>
            and m.del_flag = '0'
            <!-- 权限控制：普通用户只能查看自己项目下的模块或被指派的模块；平台管理员可查看所有 -->
            <if test="params != null and params.currentUserId != null and params.isAdmin != true">
                and (
                    -- 自己是项目所有者
                    exists (
                        select 1 from biz_project p 
                        where p.project_id = m.project_id 
                        and p.owner_id = #{params.currentUserId}
                        and p.del_flag = '0'
                    )
                    -- 或被指派了该模块
                    or m.designated_user = #{params.currentUserId}
                )
            </if>
        </where>
    </select>

    <select id="selectByProjectId" parameterType="long" resultMap="BizModuleResult">
        <include refid="selectBizModuleVo"/>
        <where>
            <if test="_parameter != null "> and m.project_id = #{_parameter}</if>
            and m.del_flag = '0'
        </where>
    </select>
    
    <select id="selectBizModuleByModuleId" parameterType="Long" resultMap="BizModuleResult">
        <include refid="selectBizModuleVo"/>
        where module_id = #{moduleId}
    </select>

    <insert id="insertBizModule" parameterType="BizModule" useGeneratedKeys="true" keyProperty="moduleId">
        insert into biz_module
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="moduleName != null">module_name,</if>
            <if test="status != null">status,</if>
            <if test="assignee != null">assignee,</if>
            <if test="assignTime != null">assign_time,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="moduleName != null">#{moduleName},</if>
            <if test="status != null">#{status},</if>
            <if test="assignee != null">#{assignee},</if>
            <if test="assignTime != null">#{assignTime},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateBizModule" parameterType="BizModule">
        update biz_module
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="moduleName != null">module_name = #{moduleName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="assignee != null">assignee = #{assignee},</if>
            <if test="assignTime != null">assign_time = #{assignTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="designatedUser != null">designated_user = #{designatedUser},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where module_id = #{moduleId}
    </update>

    <delete id="deleteBizModuleByModuleId" parameterType="Long">
        update biz_module set del_flag = '2' where module_id = #{moduleId}
    </delete>

    <delete id="deleteBizModuleByModuleIds" parameterType="String">
        update biz_module set del_flag = '2' where module_id in 
        <foreach item="moduleId" collection="array" open="(" separator="," close=")">
            #{moduleId}
        </foreach>
    </delete>

    <update id="updateDesignatedUser" parameterType="BizModule">
        update biz_module
        set designated_user = #{designatedUser},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where module_id = #{moduleId}
    </update>

    <select id="selectAssignedModules" parameterType="Long" resultMap="BizModuleResult">
        select m.module_id, m.project_id, m.module_name, m.status, m.assignee, m.assign_time, m.finish_time, 
               m.del_flag, m.create_by, m.create_time, m.designated_user, m.update_by, m.update_time,
               p.project_name
        from biz_module m
        left join biz_project p on m.project_id = p.project_id
        where m.designated_user = #{userId} and m.del_flag = '0'
        order by m.create_time desc
    </select>
</mapper>