<?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.base.mapper.EmsBaseEnergyPriceMapper">

    <resultMap type="EmsBaseEnergyPrice" id="EmsBaseEnergyPriceResult">
        <result property="objId" column="obj_id"/>
        <result property="energyId" column="energy_id"/>
        <result property="energyName" column="energy_name"/>
        <result property="measureUnit" column="measure_unit"/>
        <result property="monthDate" column="month_date"/>
        <result property="price" column="price"/>
        <result property="unTaxPrice" column="un_tax_price"/>
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <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="selectEmsBaseEnergyPriceVo">
        select ebep.obj_id,
               ebep.energy_id,
               ebet.energy_name  energy_name,
               ebet.measure_unit measure_unit,
               ebep.month_date,
               ebep.price,
               ebep.un_tax_price,
               ebep.start_date,
               ebep.end_date,
               ebep.create_by,
               ebep.create_time,
               ebep.update_by,
               ebep.update_time,
               ebep.remark
        from ems_base_energy_price ebep
                 left join ems_base_energy_type ebet on ebet.energy_type_id = ebep.energy_id
    </sql>

    <select id="selectEmsBaseEnergyPriceList" parameterType="EmsBaseEnergyPrice" resultMap="EmsBaseEnergyPriceResult">
        <include refid="selectEmsBaseEnergyPriceVo"/>
        <where>
            <if test="energyId != null ">and ebep.energy_id = #{energyId}</if>
            <if test="energyName != null  and energyName != ''">and ebep.energy_name like concat('%', #{energyName}, '%')
            </if>
            <if test="measureUnit != null  and measureUnit != ''">and ebep.measure_unit = #{measureUnit}</if>
            <if test="params.beginMonthDate != null and params.beginMonthDate != '' and params.endMonthDate != null and params.endMonthDate != ''">
                and ebep.month_date between #{params.beginMonthDate} and #{params.endMonthDate}
            </if>
            <if test="price != null ">and ebep.price = #{price}</if>
            <if test="unTaxPrice != null ">and ebep.un_tax_price = #{unTaxPrice}</if>
            <if test="startDate != null ">and ebep.start_date = #{startDate}</if>
            <if test="endDate != null ">and ebep.end_date = #{endDate}</if>
            <if test="createBy != null  and createBy != ''">and ebep.create_by = #{createBy}</if>
            <if test="createTime != null  and createTime != ''">and ebep.create_time = #{createTime}</if>
            <if test="updateBy != null  and updateBy != ''">and ebep.update_by = #{updateBy}</if>
            <if test="updateTime != null  and updateTime != ''">and ebep.update_time = #{updateTime}</if>
            <if test="remark != null  and remark != ''">and ebep.remark = #{remark}</if>
        </where>
        order by ebep.energy_id, ebep.month_date desc
    </select>

    <select id="selectEmsBaseEnergyPriceByObjId" parameterType="Long" resultMap="EmsBaseEnergyPriceResult">
        <include refid="selectEmsBaseEnergyPriceVo"/>
        where ebep.obj_id = #{objId}
    </select>

    <insert id="insertEmsBaseEnergyPrice" parameterType="EmsBaseEnergyPrice" useGeneratedKeys="true"
            keyProperty="objId">
        insert into ems_base_energy_price
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="energyId != null">energy_id,</if>
            <if test="energyName != null">energy_name,</if>
            <if test="measureUnit != null">measure_unit,</if>
            <if test="monthDate != null">month_date,</if>
            <if test="price != null">price,</if>
            <if test="unTaxPrice != null">un_tax_price,</if>
            <if test="startDate != null">start_date,</if>
            <if test="endDate != null">end_date,</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="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="energyId != null">#{energyId},</if>
            <if test="energyName != null">#{energyName},</if>
            <if test="measureUnit != null">#{measureUnit},</if>
            <if test="monthDate != null">#{monthDate},</if>
            <if test="price != null">#{price},</if>
            <if test="unTaxPrice != null">#{unTaxPrice},</if>
            <if test="startDate != null">#{startDate},</if>
            <if test="endDate != null">#{endDate},</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="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateEmsBaseEnergyPrice" parameterType="EmsBaseEnergyPrice">
        update ems_base_energy_price
        <trim prefix="SET" suffixOverrides=",">
            <if test="energyId != null">energy_id = #{energyId},</if>
            <if test="energyName != null">energy_name = #{energyName},</if>
            <if test="measureUnit != null">measure_unit = #{measureUnit},</if>
            <if test="monthDate != null">month_date = #{monthDate},</if>
            <if test="price != null">price = #{price},</if>
            <if test="unTaxPrice != null">un_tax_price = #{unTaxPrice},</if>
            <if test="startDate != null">start_date = #{startDate},</if>
            <if test="endDate != null">end_date = #{endDate},</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="remark != null">remark = #{remark},</if>
        </trim>
        where obj_id = #{objId}
    </update>

    <delete id="deleteEmsBaseEnergyPriceByObjId" parameterType="Long">
        delete
        from ems_base_energy_price
        where obj_id = #{objId}
    </delete>

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