<?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.fs.project.mapper.ProjectPermissionMapper">
    
    <resultMap type="ProjectPermission" id="ProjectPermissionResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="projectType"    column="project_type"    />
        <result property="roleIds"    column="role_ids"    />
        <result property="roleNames"    column="role_names"    />
        <result property="deleted"    column="deleted"    />
        <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="companyName" column="company_name"/>
    </resultMap>

    <sql id="selectProjectPermissionVo">
        SELECT
        p.id,
        p.company_id,
        c.company_name,
        p.project_type,
        p.role_ids,
        p.deleted,
        p.create_by,
        p.create_time,
        p.update_by,
        p.update_time,
        GROUP_CONCAT(r.role_name ORDER BY r.role_id SEPARATOR '，') AS role_names
        FROM project_permission p
        LEFT JOIN company c ON c.company_id = p.company_id
        LEFT JOIN company_role r
        ON r.company_id = p.company_id
        AND FIND_IN_SET(r.role_id, p.role_ids)
    </sql>

    <select id="selectProjectPermissionList" parameterType="ProjectPermission" resultMap="ProjectPermissionResult">
        <include refid="selectProjectPermissionVo"/>
        <where>
            <if test="companyId != null "> and p.company_id = #{companyId}</if>
            <if test="projectType != null  and projectType != ''"> and p.project_type = #{projectType}</if>
            <if test="roleIds != null  and roleIds != ''"> and p.role_ids = #{roleIds}</if>
            <if test="deleted != null "> and p.deleted = #{deleted}</if>
        </where>
        GROUP BY p.id

    </select>
    
    <select id="selectProjectPermissionById" parameterType="Long" resultMap="ProjectPermissionResult">
        <include refid="selectProjectPermissionVo"/>
        where p.id = #{id}
        GROUP BY p.id
    </select>
        
    <insert id="insertProjectPermission" parameterType="ProjectPermission" useGeneratedKeys="true" keyProperty="id">
        insert into project_permission
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="projectType != null and projectType != ''">project_type,</if>
            <if test="roleIds != null">role_ids,</if>
            <if test="deleted != null">deleted,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="projectType != null and projectType != ''">#{projectType},</if>
            <if test="roleIds != null">#{roleIds},</if>
            <if test="deleted != null">#{deleted},</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>
         </trim>
    </insert>

    <update id="updateProjectPermission" parameterType="ProjectPermission">
        update project_permission
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="projectType != null and projectType != ''">project_type = #{projectType},</if>
            <if test="roleIds != null">role_ids = #{roleIds},</if>
            <if test="deleted != null">deleted = #{deleted},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteProjectPermissionById" parameterType="Long">
        delete from project_permission where id = #{id}
    </delete>

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


    <!-- 在 ProjectPermissionMapper.xml 中添加 -->
    <select id="selectByUniqueKey" resultType="ProjectPermission">
        SELECT * FROM project_permission
        WHERE company_id = #{companyId}
        AND project_type = #{projectType}
        AND deleted = #{deleted}
        LIMIT 1
    </select>
</mapper>