<?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.EmissionFactorMapper">
    
    <resultMap type="com.ruoyi.ems.domain.EmissionFactor" id="EmissionFactorResult">
        <result property="id"    column="id"    />
        <result property="regionCode"    column="region_code"    />
        <result property="regionName"    column="region_name"    />
        <result property="regionType"    column="region_type"    />
        <result property="factorValue"    column="factor_value"    />
        <result property="factorType"    column="factor_type"    />
        <result property="version"    column="version"    />
    </resultMap>

    <sql id="selectEmissionFactorVo">
        select id, region_code, region_name, region_type, factor_value, factor_type, version from dim_ems_emission_factor
    </sql>

    <select id="selectEmissionFactorList" parameterType="com.ruoyi.ems.domain.EmissionFactor" resultMap="EmissionFactorResult">
        <include refid="selectEmissionFactorVo"/>
        <where>  
            <if test="regionCode != null  and regionCode != ''"> and region_code = #{regionCode}</if>
            <if test="regionName != null  and regionName != ''"> and region_name like concat('%', #{regionName}, '%')</if>
            <if test="regionType != null "> and region_type = #{regionType}</if>
            <if test="factorValue != null "> and factor_value = #{factorValue}</if>
            <if test="factorType != null "> and factor_type = #{factorType}</if>
            <if test="version != null  and version != ''"> and version = #{version}</if>
        </where>
    </select>
    
    <select id="selectEmissionFactorById" parameterType="Long" resultMap="EmissionFactorResult">
        <include refid="selectEmissionFactorVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertEmissionFactor" parameterType="com.ruoyi.ems.domain.EmissionFactor" useGeneratedKeys="true" keyProperty="id">
        insert into dim_ems_emission_factor
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="regionCode != null and regionCode != ''">region_code,</if>
            <if test="regionName != null and regionName != ''">region_name,</if>
            <if test="regionType != null">region_type,</if>
            <if test="factorValue != null">factor_value,</if>
            <if test="factorType != null">factor_type,</if>
            <if test="version != null">version,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="regionCode != null and regionCode != ''">#{regionCode},</if>
            <if test="regionName != null and regionName != ''">#{regionName},</if>
            <if test="regionType != null">#{regionType},</if>
            <if test="factorValue != null">#{factorValue},</if>
            <if test="factorType != null">#{factorType},</if>
            <if test="version != null">#{version},</if>
         </trim>
    </insert>

    <update id="updateEmissionFactor" parameterType="com.ruoyi.ems.domain.EmissionFactor">
        update dim_ems_emission_factor
        <trim prefix="SET" suffixOverrides=",">
            <if test="regionCode != null and regionCode != ''">region_code = #{regionCode},</if>
            <if test="regionName != null and regionName != ''">region_name = #{regionName},</if>
            <if test="regionType != null">region_type = #{regionType},</if>
            <if test="factorValue != null">factor_value = #{factorValue},</if>
            <if test="factorType != null">factor_type = #{factorType},</if>
            <if test="version != null">version = #{version},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmissionFactorById" parameterType="Long">
        delete from dim_ems_emission_factor where id = #{id}
    </delete>

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