<?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.project.mapper.BizProjectMapper">
    
    <resultMap type="BizProject" id="BizProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="ownerId"    column="owner_id"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <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="finishTime"    column="finish_time"    />
        <result property="deadline"    column="deadline"    />
        <result property="startTime"    column="start_time"    />
        <result property="projectLabel"    column="project_label"    />
        <result property="moduleCount" column="moduleCount" />
        <result property="completedModuleCount" column="completedModuleCount" />
    </resultMap>

    <sql id="selectBizProjectVo">
        select project_id, project_name, owner_id, status, del_flag, create_by, create_time, update_by, update_time, finish_time, deadline, start_time, project_label from biz_project
    </sql>

    <select id="selectBizProjectList" parameterType="BizProject" resultMap="BizProjectResult">
        select 
            p.project_id, p.project_name, p.owner_id, p.status, p.del_flag, 
            p.create_by, p.create_time, p.update_by, p.update_time, 
            p.finish_time, p.deadline, p.start_time, p.project_label,
            coalesce(cnt.total, 0) as moduleCount,
            coalesce(cnt.completed, 0) as completedModuleCount
        from biz_project p
        left join (
            select project_id, 
                count(*) as total,
                sum(case when status = '2' then 1 else 0 end) as completed
            from biz_module
            where del_flag = '0'
            group by project_id
        ) cnt on cnt.project_id = p.project_id
        <where>  
            <if test="projectName != null  and projectName != ''"> and project_name like concat('%', #{projectName}, '%')</if>
            <if test="ownerId != null "> and owner_id = #{ownerId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="finishTime != null "> and finish_time = #{finishTime}</if>
            <if test="deadline != null "> and deadline = #{deadline}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="projectLabel != null  and projectLabel != ''"> and project_label = #{projectLabel}</if>
            and del_flag = '0'
            <!-- 权限控制：普通用户仅能查看自己创建的项目 -->
            <if test="params != null and params.currentUserId != null">
                and owner_id = #{params.currentUserId}
            </if>
        </where>
    </select>
    
    <select id="selectBizProjectByProjectId" parameterType="Long" resultMap="BizProjectResult">
        <include refid="selectBizProjectVo"/>
        where project_id = #{projectId}
    </select>

    <insert id="insertBizProject" parameterType="BizProject" useGeneratedKeys="true" keyProperty="projectId">
        insert into biz_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectName != null and projectName != ''">project_name,</if>
            <if test="ownerId != null">owner_id,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</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="finishTime != null">finish_time,</if>
            <if test="deadline != null">deadline,</if>
            <if test="startTime != null">start_time,</if>
            <if test="projectLabel != null">project_label,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectName != null and projectName != ''">#{projectName},</if>
            <if test="ownerId != null">#{ownerId},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</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="finishTime != null">#{finishTime},</if>
            <if test="deadline != null">#{deadline},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="projectLabel != null">#{projectLabel},</if>
         </trim>
    </insert>

    <update id="updateBizProject" parameterType="BizProject">
        update biz_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            <if test="ownerId != null">owner_id = #{ownerId},</if>
            <if test="status != null">status = #{status},</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>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="deadline != null">deadline = #{deadline},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="projectLabel != null">project_label = #{projectLabel},</if>
        </trim>
        where project_id = #{projectId}
    </update>

    <delete id="deleteBizProjectByProjectId" parameterType="Long">
        update biz_project set del_flag = '2' where project_id = #{projectId}
    </delete>

    <delete id="deleteBizProjectByProjectIds" parameterType="String">
        update biz_project set del_flag = '2' where project_id in
        <foreach item="projectId" collection="array" open="(" separator="," close=")">
            #{projectId}
        </foreach>
    </delete>

    <update id="markOverdueProjectsAsInitiating">
        update biz_project
        set status = '2', update_time = now()
        where del_flag = '0'
          and status != '1'
          and deadline is not null
          and deadline <![CDATA[<]]> now()
    </update>

    <update id="markInitiatingToOngoingByStartTime">
        update biz_project
        set status = '0', update_time = now()
        where del_flag = '0'
          and status = '2'
          and start_time is not null
          and start_time <![CDATA[<=]]> now()
    </update>
</mapper>