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

    <resultMap type="Project" id="ProjectResult">
        <result property="id"    column="id"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName" column="company_name" />
        <result property="projectName"    column="project_name"    />
        <result property="projectTypeId"    column="project_type_id"    />
        <result property="thirdPartyCode"    column="third_party_code"    />
        <result property="productIds"    column="product_ids"    />
        <result property="projectAmount"    column="project_amount"    />
        <result property="laborCost"    column="labor_cost"    />
        <result property="startDate"    column="start_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="serviceFeeRate"    column="service_fee_rate"    />
        <result property="settlementApprovalId"    column="settlement_approval_id"    />
        <result property="taskCreateDeadline"    column="task_create_deadline"    />
        <result property="taskCompleteTime"    column="task_complete_time"    />
        <result property="settlementDeadline"    column="settlement_deadline"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="status"    column="status"    />
        <result property="productNames" column="product_names" />
    </resultMap>

    <sql id="selectProjectVo">
        SELECT
        p.id,
        p.company_id,
        c.company_name AS company_name,
        p.project_name,
        p.status,
        p.project_type_id,
        p.third_party_code,
        p.product_ids,
        GROUP_CONCAT(cp.product_name ORDER BY cp.sort, cp.id SEPARATOR '、') AS product_names,
        p.project_amount,
        p.labor_cost,
        p.start_date,
        p.end_date,
        p.service_fee_rate,
        p.settlement_approval_id,
        p.task_create_deadline,
        p.task_complete_time,
        p.settlement_deadline,
        p.create_time,
        p.update_time,
        p.create_by,
        p.update_by,
        p.del_flag
        FROM project p
        LEFT JOIN company c ON c.company_id = p.company_id
        LEFT JOIN company_product cp
        ON cp.company_id = p.company_id
        AND cp.del_flag = 0
        AND cp.status = 1
        AND FIND_IN_SET(cp.id, p.product_ids) > 0
    </sql>




    <select id="selectProjectList" parameterType="Project" resultMap="ProjectResult">
        <include refid="selectProjectVo"/>
        <where>
            <if test="companyId != null "> and p.company_id = #{companyId}</if>
            <if test="projectName != null  and projectName != ''"> and p.project_name like concat('%', #{projectName}, '%')</if>
            <if test="projectTypeId != null "> and p.project_type_id = #{projectTypeId}</if>
            <if test="thirdPartyCode != null  and thirdPartyCode != ''"> and p.third_party_code = #{thirdPartyCode}</if>

            <!-- ⚠️ 你这里 productIds 现在是“逗号串”，用等号过滤很鸡肋（下面我给你更合理的写法） -->
            <if test="productIds != null  and productIds != ''"> and p.product_ids = #{productIds}</if>

            <if test="projectAmount != null "> and p.project_amount = #{projectAmount}</if>
            <if test="laborCost != null "> and p.labor_cost = #{laborCost}</if>
            <if test="startDate != null "> and p.start_date = #{startDate}</if>
            <if test="endDate != null "> and p.end_date = #{endDate}</if>
            <if test="serviceFeeRate != null "> and p.service_fee_rate = #{serviceFeeRate}</if>
            <if test="settlementApprovalId != null "> and p.settlement_approval_id = #{settlementApprovalId}</if>
            <if test="taskCreateDeadline != null "> and p.task_create_deadline = #{taskCreateDeadline}</if>
            <if test="taskCompleteTime != null "> and p.task_complete_time = #{taskCompleteTime}</if>
            <if test="settlementDeadline != null "> and p.settlement_deadline = #{settlementDeadline}</if>
        </where>

        <!-- ✅ 新增 -->
        GROUP BY p.id
    </select>


    <select id="selectProjectById" parameterType="Long" resultMap="ProjectResult">
        <include refid="selectProjectVo"/>
        where p.id = #{id}
        GROUP BY p.id
    </select>


    <insert id="insertProject" parameterType="Project" useGeneratedKeys="true" keyProperty="id">
        insert into project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="projectName != null and projectName != ''">project_name,</if>
            <if test="projectTypeId != null">project_type_id,</if>
            <if test="thirdPartyCode != null">third_party_code,</if>
            <if test="productIds != null">product_ids,</if>
            <if test="projectAmount != null">project_amount,</if>
            <if test="laborCost != null">labor_cost,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="serviceFeeRate != null">service_fee_rate,</if>
            <if test="settlementApprovalId != null">settlement_approval_id,</if>
            <if test="taskCreateDeadline != null">task_create_deadline,</if>
            <if test="taskCompleteTime != null">task_complete_time,</if>
            <if test="settlementDeadline != null">settlement_deadline,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="delFlag != null">del_flag,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="projectName != null and projectName != ''">#{projectName},</if>
            <if test="projectTypeId != null">#{projectTypeId},</if>
            <if test="thirdPartyCode != null">#{thirdPartyCode},</if>
            <if test="productIds != null">#{productIds},</if>
            <if test="projectAmount != null">#{projectAmount},</if>
            <if test="laborCost != null">#{laborCost},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="serviceFeeRate != null">#{serviceFeeRate},</if>
            <if test="settlementApprovalId != null">#{settlementApprovalId},</if>
            <if test="taskCreateDeadline != null">#{taskCreateDeadline},</if>
            <if test="taskCompleteTime != null">#{taskCompleteTime},</if>
            <if test="settlementDeadline != null">#{settlementDeadline},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="delFlag != null">#{delFlag},</if>
         </trim>
    </insert>

    <update id="updateProject" parameterType="Project">
        update project
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="projectName != null and projectName != ''">project_name = #{projectName},</if>
            <if test="projectTypeId != null">project_type_id = #{projectTypeId},</if>
            <if test="thirdPartyCode != null">third_party_code = #{thirdPartyCode},</if>
            <if test="productIds != null">product_ids = #{productIds},</if>
            <if test="projectAmount != null">project_amount = #{projectAmount},</if>
            <if test="laborCost != null">labor_cost = #{laborCost},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="serviceFeeRate != null">service_fee_rate = #{serviceFeeRate},</if>
            <if test="settlementApprovalId != null">settlement_approval_id = #{settlementApprovalId},</if>
            <if test="taskCreateDeadline != null">task_create_deadline = #{taskCreateDeadline},</if>
            <if test="taskCompleteTime != null">task_complete_time = #{taskCompleteTime},</if>
            <if test="settlementDeadline != null">settlement_deadline = #{settlementDeadline},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteProjectById" parameterType="Long">
        delete from project where id = #{id}
    </delete>

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


    <select id="selectProjectOptions" resultType="com.fs.project.vo.ProjectOptionVO">
        SELECT id AS projectId, project_name AS projectName
        FROM project
        <where>
            status = 0
            <if test="projectName != null and projectName != ''">
                AND project_name LIKE CONCAT('%', #{projectName}, '%')
            </if>
        </where>
        ORDER BY id DESC
        LIMIT 20
    </select>
    <select id="getAllData" resultType="com.fs.project.domain.Project">
        select * from project where company_id=#{companyId}
    </select>


    <update id="updateStatus">
        update project
        set status = #{status},
        update_time = now()
        where id = #{id}
    </update>

</mapper>
