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

    <resultMap id="ModelParamResult" type="com.ruoyi.ccdi.project.domain.CcdiModelParam">
        <id property="id" column="id"/>
        <result property="projectId" column="project_id"/>
        <result property="modelCode" column="model_code"/>
        <result property="modelName" column="model_name"/>
        <result property="paramCode" column="param_code"/>
        <result property="paramName" column="param_name"/>
        <result property="paramDesc" column="param_desc"/>
        <result property="paramValue" column="param_value"/>
        <result property="paramUnit" column="param_unit"/>
        <result property="sortOrder" column="sort_order"/>
        <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="remark" column="remark"/>
    </resultMap>

    <sql id="selectModelParamVo">
        select id, project_id, model_code, model_name, param_code, param_name, param_desc,
               param_value, param_unit, sort_order, create_by, create_time, update_by, update_time, remark
        from ccdi_model_param
    </sql>

    <select id="selectByProjectAndModel" resultMap="ModelParamResult">
        <include refid="selectModelParamVo"/>
        where project_id = #{projectId} and model_code = #{modelCode}
        order by sort_order asc, id asc
    </select>

    <select id="selectDistinctModels" resultMap="ModelParamResult">
        select distinct model_code, model_name
        from ccdi_model_param
        where project_id = #{projectId}
        order by model_code
    </select>

    <!-- 根据项目ID查询所有模型参数 -->
    <select id="selectByProjectId" resultType="CcdiModelParam">
        <include refid="selectModelParamVo"/>
        WHERE project_id = #{projectId}
        ORDER BY model_code, sort_order, id
    </select>
    <update id="batchUpdateParamValues">
        update ccdi_model_param
        <set>
            <trim prefix="param_value = case" suffix="end,">
                <foreach collection="list" item="item">
                    when id = #{item.id} then #{item.paramValue}
                </foreach>
            </trim>
            <trim prefix="update_by = case" suffix="end,">
                <foreach collection="list" item="item">
                    when id = #{item.id} then #{item.updateBy}
                </foreach>
            </trim>
            update_time = sysdate()
        </set>
        where id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <!-- 更新参数值 -->
    <update id="updateParamValue">
        UPDATE ccdi_model_param
        SET param_value = #{paramValue},
            update_by = #{updateBy},
            update_time = NOW()
        WHERE project_id = #{projectId}
          AND model_code = #{modelCode}
          AND param_code = #{paramCode}
    </update>

    <!-- 批量插入参数 -->
    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ccdi_model_param (
            project_id, model_code, model_name, param_code, param_name,
            param_desc, param_value, param_unit, sort_order, remark,
            create_by, create_time, update_by, update_time
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
                #{item.projectId}, #{item.modelCode}, #{item.modelName},
                #{item.paramCode}, #{item.paramName}, #{item.paramDesc},
                #{item.paramValue}, #{item.paramUnit}, #{item.sortOrder},
                #{item.remark}, #{item.createBy}, NOW(), #{item.updateBy}, NOW()
            )
        </foreach>
    </insert>

</mapper>
