<?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.ProjectStandardMapper">

    <resultMap type="com.fs.project.domain.ProjectStandard" id="ProjectStandardResult">
        <id property="id" column="id" />
        <result property="companyId" column="company_id" />
        <result property="projectType" column="project_type" />
        <!-- ★ 唯一的新字段 -->
        <result property="levelConfig" column="level_config" />
        <result property="taskLimitCount" column="task_limit_count" />
        <result property="taskLimitPeriod" column="task_limit_period" />
        <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="delFlag" column="del_flag" />
        <!-- 连表查询公司名称，映射到实体里不存在的字段？你实体里没有companyName，但VO需要。建议在resultMap中加一个扩展字段，或者直接在查询SQL中处理。这里先保留，后续可优化 -->
        <result property="companyName" column="company_name" />
        <!-- 新增字段映射 -->
        <result property="projectId" column="project_id" />
        <result property="taskTypes" column="task_types" />
    </resultMap>

    <sql id="selectProjectStandardVo">
        SELECT
        ps.id,
        ps.company_id,
        c.company_name,
        ps.project_type,
        ps.level_config,
        ps.task_limit_count,
        ps.task_limit_period,
        ps.project_id,
        ps.task_types,
        ps.create_by,
        ps.create_time,
        ps.update_by,
        ps.update_time,
        ps.del_flag
        FROM project_standard ps
        LEFT JOIN company c ON ps.company_id = c.company_id
    </sql>

    <select id="selectProjectStandardList" parameterType="com.fs.project.domain.ProjectStandard" resultMap="ProjectStandardResult">
        <include refid="selectProjectStandardVo"/>
        <where>
            <if test="companyId != null"> AND ps.company_id = #{companyId}</if>
            <if test="projectType != null and projectType != ''"> AND ps.project_type = #{projectType}</if>
            <!-- 如果要按公司名称搜索，放开下面 -->
            <!-- <if test="companyName != null and companyName != ''"> AND c.company_name LIKE CONCAT('%', #{companyName}, '%')</if> -->
            <if test="taskLimitCount != null"> AND ps.task_limit_count = #{taskLimitCount}</if>
            <if test="taskLimitPeriod != null and taskLimitPeriod != ''"> AND ps.task_limit_period = #{taskLimitPeriod}</if>
            <!-- 逻辑删除过滤（如果你的SQL没有自动加，手动加） -->
            AND ps.del_flag = '0'
        </where>
        GROUP BY ps.id

    </select>

    <select id="selectProjectStandardById" parameterType="Long" resultMap="ProjectStandardResult">
        <include refid="selectProjectStandardVo"/>
        WHERE ps.id = #{id}
    </select>

    <!-- 插入：只处理 level_config -->
    <insert id="insertProjectStandard" parameterType="com.fs.project.domain.ProjectStandard" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO project_standard (
        company_id,
        project_id,
        task_types,
        project_type,
        level_config,
        task_limit_count,
        task_limit_period,
        create_by,
        create_time,
        del_flag
        ) VALUES (
        #{companyId},
        #{projectId},
        #{taskTypes},
        #{projectType},
        #{levelConfig},
        #{taskLimitCount},
        #{taskLimitPeriod},
        #{createBy},
        #{createTime},
        #{delFlag}
        )
    </insert>

    <!-- 更新：只处理 level_config -->
    <update id="updateProjectStandard" parameterType="com.fs.project.domain.ProjectStandard">
        UPDATE project_standard
        SET
        project_type = #{projectType},
        level_config = #{levelConfig},
        task_limit_count = #{taskLimitCount},
        task_limit_period = #{taskLimitPeriod},
        project_id = #{projectId},      <!-- 新增 -->
        task_types = #{taskTypes},      <!-- 新增 -->
        update_by = #{updateBy},
        update_time = #{updateTime}
        WHERE id = #{id}
    </update>

    <!-- 物理删除（若依通常用逻辑删除，你用了 @TableLogic，这里可改为逻辑删除）
         如果要用物理删除，保留；否则改为 update set del_flag = '1' -->
    <delete id="deleteProjectStandardById" parameterType="Long">
        UPDATE project_standard SET del_flag = '1' WHERE id = #{id}
    </delete>

    <delete id="deleteProjectStandardByIds" parameterType="String">
        UPDATE project_standard SET del_flag = '1' WHERE id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <!-- 根据公司、项目类型、任务类型查询项目标准配置（匹配 task_types 包含指定任务类型或未限制） -->
    <select id="selectProjectStandardByCompanyIdAndProjectIdAndTaskType" resultMap="ProjectStandardResult">
        <include refid="selectProjectStandardVo"/>
        <where>
            ps.company_id = #{companyId}
            AND ps.project_id = #{projectId}
            AND (
            ps.task_types IS NULL
            OR ps.task_types = ''
            OR FIND_IN_SET(#{taskType}, ps.task_types) > 0
            )
            AND ps.del_flag = '0'
        </where>
        ORDER BY ps.id ASC   <!-- 按ID排序保证结果稳定，可根据业务调整排序 -->
        LIMIT 1              <!-- 确保只返回一条，避免业务层处理多条 -->
    </select>
</mapper>