<?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.hw.ems.base.mapper.BasePublicUserRecordMapper">

    <resultMap type="BasePublicUserRecord" id="BasePublicUserRecordResult">
        <result property="objid"    column="objid"    />
        <result property="publicShareId"    column="public_share_id"    />
        <result property="publicUserId"    column="public_user_id"    />
        <result property="publicUserName"    column="public_user_name"    />
        <result property="publicMonitorIds"    column="public_monitor_ids"    />
        <result property="publicShareRule"    column="public_share_rule"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="allocationRatio"    column="allocation_ratio"    />
        <result property="publicEnergyConsumption"    column="public_energy_consumption"    />
        <result property="ownerEnergyConsumption"    column="owner_energy_consumption"    />
        <result property="sumEnergyConsumption"    column="sumEnergyConsumption"    />
        <result property="energyType"    column="energy_type"    />
        <result property="delStatus"    column="del_status"    />
        <result property="createdBy"    column="created_by"    />
        <result property="createdTime"    column="created_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectBasePublicUserRecordVo">
        select objid, public_share_id, public_user_id, bpu.user_name public_user_name, bpm.public_share_rule, public_monitor_ids, base_public_user_record.start_time, base_public_user_record.end_time,
               allocation_ratio, public_energy_consumption, owner_energy_consumption, ifnull(public_energy_consumption,0) +  ifnull(owner_energy_consumption,0) sumEnergyConsumption,
               energy_type, del_status, created_by, created_time, base_public_user_record.update_by, base_public_user_record.update_time from base_public_user_record
           left join base_public_user bpu on bpu.id = base_public_user_record.public_user_id
          left join base_public_manage bpm on bpm.id = base_public_user_record.public_share_id
    </sql>

    <select id="selectBasePublicUserRecordList" parameterType="BasePublicUserRecord" resultMap="BasePublicUserRecordResult">
        <include refid="selectBasePublicUserRecordVo"/>
        <where>
            del_status = 0
            <if test="publicShareId != null  and publicShareId != ''"> and public_share_id = #{publicShareId}</if>
            <if test="publicUserId != null  and publicUserId != ''"> and public_user_id = #{publicUserId}</if>
            <if test="publicMonitorIds != null  and publicMonitorIds != ''"> and public_monitor_ids = #{publicMonitorIds}</if>
            <if test="startTime != null "> and base_public_user_record.start_time >= #{startTime}</if>
            <if test="endTime != null "> and base_public_user_record.start_time &lt;= #{endTime}</if>
        </where>
    </select>

    <select id="selectBasePublicUserRecordByObjid" parameterType="Long" resultMap="BasePublicUserRecordResult">
        <include refid="selectBasePublicUserRecordVo"/>
        where objid = #{objid}
    </select>

    <insert id="insertBasePublicUserRecord" parameterType="BasePublicUserRecord" useGeneratedKeys="true" keyProperty="objid">
        insert into base_public_user_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="publicShareId != null">public_share_id,</if>
            <if test="publicUserId != null">public_user_id,</if>
            <if test="publicMonitorIds != null">public_monitor_ids,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="allocationRatio != null">allocation_ratio,</if>
            <if test="publicEnergyConsumption != null">public_energy_consumption,</if>
            <if test="ownerEnergyConsumption != null">owner_energy_consumption,</if>
            <if test="energyType != null">energy_type,</if>
            <if test="createdBy != null">created_by,</if>
            <if test="createdTime != null">created_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="publicShareId != null">#{publicShareId},</if>
            <if test="publicUserId != null">#{publicUserId},</if>
            <if test="publicMonitorIds != null">#{publicMonitorIds},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="allocationRatio != null">#{allocationRatio},</if>
            <if test="publicEnergyConsumption != null">#{publicEnergyConsumption},</if>
            <if test="ownerEnergyConsumption != null">#{ownerEnergyConsumption},</if>
            <if test="energyType != null">#{energyType},</if>
            <if test="createdBy != null">#{createdBy},</if>
            <if test="createdTime != null">#{createdTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateBasePublicUserRecord" parameterType="BasePublicUserRecord">
        update base_public_user_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="publicShareId != null">public_share_id = #{publicShareId},</if>
            <if test="publicUserId != null">public_user_id = #{publicUserId},</if>
            <if test="publicMonitorIds != null">public_monitor_ids = #{publicMonitorIds},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="allocationRatio != null">allocation_ratio = #{allocationRatio},</if>
            <if test="publicEnergyConsumption != null">public_energy_consumption = #{publicEnergyConsumption},</if>
            <if test="ownerEnergyConsumption != null">owner_energy_consumption = #{ownerEnergyConsumption},</if>
            <if test="energyType != null">energy_type = #{energyType},</if>
            <if test="delStatus != null">del_status = #{delStatus},</if>
            <if test="createdBy != null">created_by = #{createdBy},</if>
            <if test="createdTime != null">created_time = #{createdTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where objid = #{objid}
    </update>

    <delete id="deleteBasePublicUserRecordByObjid" parameterType="Long">
        delete from base_public_user_record where objid = #{objid}
    </delete>

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