<?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.ems.mapper.PlantCarbonSinkMapper">
    
    <resultMap type="com.ruoyi.ems.domain.PlantCarbonSink" id="PlantCarbonSinkResult">
        <result property="id"    column="id"    />
        <result property="plantMethod"    column="plant_method"    />
        <result property="carbonQuantity"    column="carbon_quantity"    />
        <result property="version"    column="version"    />
    </resultMap>

    <resultMap type="com.ruoyi.ems.domain.PlantCarbonSink" id="RelResult">
        <result property="id"    column="id"    />
        <result property="areaCode"    column="area_code"    />
        <result property="plantSize"   column="plant_size"    />
        <result property="plantMethod"    column="plant_method"    />
        <result property="carbonQuantity"    column="carbon_quantity"    />
    </resultMap>

    <sql id="selectDimPlantCarbonSinkVo">
        select id, plant_method, carbon_quantity, version from dim_plant_carbon_sink
    </sql>

    <select id="selectList" parameterType="com.ruoyi.ems.domain.PlantCarbonSink" resultMap="PlantCarbonSinkResult">
        <include refid="selectDimPlantCarbonSinkVo"/>
        <where>  
        </where>
    </select>
    
    <select id="selectById" parameterType="Long" resultMap="PlantCarbonSinkResult">
        <include refid="selectDimPlantCarbonSinkVo"/>
        where id = #{id}
    </select>

    <select id="selectAreaCfg" parameterType="String" resultMap="RelResult">
        select r.area_code, r.plant_size, s.id, s.plant_method, s.carbon_quantity from adm_area_plant_rel r
            left join dim_plant_carbon_sink s on r.carbon_sink_id = s.id
        where r.area_code = #{areaCode}
    </select>

    <delete id="deleteAreaCfg" parameterType="java.lang.String" >
        Delete from adm_area_plant_rel where area_code = #{areaCode}
    </delete>

    <insert id="addAreaCfgBatch" parameterType="java.util.List">
        insert into adm_area_plant_rel (`area_code`, `plant_size`, `carbon_sink_id`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.areaCode},#{item.plantSize},#{item.id})
        </foreach>
    </insert>

    <insert id="insert" parameterType="com.ruoyi.ems.domain.PlantCarbonSink" useGeneratedKeys="true" keyProperty="id">
        insert into dim_plant_carbon_sink
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="plantMethod != null and plantMethod != ''">plant_method,</if>
            <if test="carbonQuantity != null">carbon_quantity,</if>
            <if test="version != null">version,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="plantMethod != null and plantMethod != ''">#{plantMethod},</if>
            <if test="carbonQuantity != null">#{carbonQuantity},</if>
            <if test="version != null">#{version},</if>
         </trim>
    </insert>

    <update id="update" parameterType="com.ruoyi.ems.domain.PlantCarbonSink">
        update dim_plant_carbon_sink
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantMethod != null and plantMethod != ''">plant_method = #{plantMethod},</if>
            <if test="carbonQuantity != null">carbon_quantity = #{carbonQuantity},</if>
            <if test="version != null">version = #{version},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteById" parameterType="Long">
        delete from dim_plant_carbon_sink where id = #{id}
    </delete>
</mapper>