<?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.ccdi.project.mapper.CcdiProjectMapper">

    <resultMap id="ProjectVOResultMap" type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectVO">
        <id property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="description" column="description"/>
        <result property="configType" column="config_type"/>
        <result property="status" column="status"/>
        <result property="isArchived" column="is_archived"/>
        <result property="targetCount" column="target_count"/>
        <result property="highRiskCount" column="high_risk_count"/>
        <result property="mediumRiskCount" column="medium_risk_count"/>
        <result property="lowRiskCount" column="low_risk_count"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createByName" column="create_by_name"/>
    </resultMap>

    <resultMap id="ProjectHistoryListItemResultMap" type="com.ruoyi.ccdi.project.domain.vo.CcdiProjectHistoryListItemVO">
        <id property="projectId" column="project_id"/>
        <result property="projectName" column="project_name"/>
        <result property="description" column="description"/>
        <result property="status" column="status"/>
        <result property="isArchived" column="is_archived"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <!-- 分页查询项目列表 -->
    <select id="selectProjectPage" resultMap="ProjectVOResultMap">
        SELECT
            p.project_id, p.project_name, p.description, p.config_type,
            p.status, p.is_archived, p.target_count, p.high_risk_count,
            p.medium_risk_count, p.low_risk_count, p.create_time,
            p.update_time,
            p.create_by,
            IFNULL(u.nick_name, p.create_by) AS create_by_name
        FROM ccdi_project p
        LEFT JOIN sys_user u ON p.create_by = u.user_name AND u.del_flag = '0'
        <where>
            <if test="queryDTO.projectName != null and queryDTO.projectName != ''">
                AND p.project_name LIKE CONCAT('%', #{queryDTO.projectName}, '%')
            </if>
            <if test="queryDTO.status != null and queryDTO.status != ''">
                AND p.status = #{queryDTO.status}
            </if>
        </where>
        ORDER BY p.update_time DESC
    </select>

    <select id="selectHistoryProjects" resultMap="ProjectHistoryListItemResultMap">
        SELECT
            p.project_id,
            p.project_name,
            p.description,
            p.status,
            p.is_archived,
            p.create_time
        FROM ccdi_project p
        <where>
            p.status in ('1', '2')
            <if test="queryDTO.projectName != null and queryDTO.projectName != ''">
                AND p.project_name LIKE CONCAT('%', #{queryDTO.projectName}, '%')
            </if>
        </where>
        ORDER BY p.update_time DESC
    </select>

    <update id="updateRiskCountsByProjectId">
        update ccdi_project
        set target_count = #{targetCount},
            high_risk_count = #{highRiskCount},
            medium_risk_count = #{mediumRiskCount},
            low_risk_count = #{lowRiskCount},
            update_by = #{updateBy},
            update_time = now()
        where project_id = #{projectId}
          and del_flag = '0'
    </update>
</mapper>
