<?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.xzzn.ems.mapper.EmsPcsBranchDataMapper">

    <resultMap type="EmsPcsBranchData" id="EmsPcsBranchDataResult">
        <result property="id"    column="id"    />
        <result property="dischargeStatus"    column="discharge_status"    />
        <result property="dcPower"    column="dc_power"    />
        <result property="dcVoltage"    column="dc_voltage"    />
        <result property="dcCurrent"    column="dc_current"    />
        <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"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="branchId"    column="branch_id"    />
        <result property="gridUVoltage"    column="grid_u_voltage"    />
        <result property="gridVVoltage"    column="grid_v_voltage"    />
        <result property="gridWVoltage"    column="grid_w_voltage"    />
        <result property="outputUCurrent"    column="output_u_current"    />
        <result property="outputVCurrent"    column="output_v_current"    />
        <result property="outputWCurrent"    column="output_w_current"    />
        <result property="apparentPower"    column="apparent_power"    />
        <result property="activePower"    column="active_power"    />
        <result property="reactivePower"    column="reactive_power"    />
        <result property="powerFactor"    column="power_factor"    />
        <result property="frequency"    column="frequency"    />
        <result property="internalTemp"    column="internal_temp"    />
        <result property="uIgbtTemp"    column="u_igbt_temp"    />
        <result property="vIgbtTemp"    column="v_igbt_temp"    />
        <result property="wIgbtTemp"    column="w_igbt_temp"    />
        <result property="gridStatus"    column="grid_status"    />
        <result property="availablePower"    column="available_power"    />
        <result property="totalLoadRatio"    column="total_load_ratio"    />
        <result property="acLeakageCurrent"    column="ac_leakage_current"    />
        <result property="insulationResistance"    column="insulation_resistance"    />
    </resultMap>

    <sql id="selectEmsPcsBranchDataVo">
        select id, discharge_status, dc_power, dc_voltage, dc_current, create_by, create_time, update_by, update_time, remark, site_id, device_id, branch_id, grid_u_voltage, grid_v_voltage, grid_w_voltage, output_u_current, output_v_current, output_w_current, apparent_power, active_power, reactive_power, power_factor, frequency, internal_temp, u_igbt_temp, v_igbt_temp, w_igbt_temp, grid_status, available_power, total_load_ratio, ac_leakage_current, insulation_resistance from ems_pcs_branch_data
    </sql>

    <select id="selectEmsPcsBranchDataList" parameterType="EmsPcsBranchData" resultMap="EmsPcsBranchDataResult">
        <include refid="selectEmsPcsBranchDataVo"/>
        <where>
            <if test="dischargeStatus != null  and dischargeStatus != ''"> and discharge_status = #{dischargeStatus}</if>
            <if test="dcPower != null "> and dc_power = #{dcPower}</if>
            <if test="dcVoltage != null "> and dc_voltage = #{dcVoltage}</if>
            <if test="dcCurrent != null "> and dc_current = #{dcCurrent}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
            <if test="deviceId != null  and deviceId != ''"> and device_id = #{deviceId}</if>
            <if test="branchId != null "> and branch_id = #{branchId}</if>
            <if test="gridUVoltage != null "> and grid_u_voltage = #{gridUVoltage}</if>
            <if test="gridVVoltage != null "> and grid_v_voltage = #{gridVVoltage}</if>
            <if test="gridWVoltage != null "> and grid_w_voltage = #{gridWVoltage}</if>
            <if test="outputUCurrent != null "> and output_u_current = #{outputUCurrent}</if>
            <if test="outputVCurrent != null "> and output_v_current = #{outputVCurrent}</if>
            <if test="outputWCurrent != null "> and output_w_current = #{outputWCurrent}</if>
            <if test="apparentPower != null "> and apparent_power = #{apparentPower}</if>
            <if test="activePower != null "> and active_power = #{activePower}</if>
            <if test="reactivePower != null "> and reactive_power = #{reactivePower}</if>
            <if test="powerFactor != null "> and power_factor = #{powerFactor}</if>
            <if test="frequency != null "> and frequency = #{frequency}</if>
            <if test="internalTemp != null "> and internal_temp = #{internalTemp}</if>
            <if test="uIgbtTemp != null "> and u_igbt_temp = #{uIgbtTemp}</if>
            <if test="vIgbtTemp != null "> and v_igbt_temp = #{vIgbtTemp}</if>
            <if test="wIgbtTemp != null "> and w_igbt_temp = #{wIgbtTemp}</if>
            <if test="gridStatus != null  and gridStatus != ''"> and grid_status = #{gridStatus}</if>
            <if test="availablePower != null "> and available_power = #{availablePower}</if>
            <if test="totalLoadRatio != null "> and total_load_ratio = #{totalLoadRatio}</if>
            <if test="acLeakageCurrent != null "> and ac_leakage_current = #{acLeakageCurrent}</if>
            <if test="insulationResistance != null "> and insulation_resistance = #{insulationResistance}</if>
        </where>
    </select>

    <select id="selectEmsPcsBranchDataById" parameterType="Long" resultMap="EmsPcsBranchDataResult">
        <include refid="selectEmsPcsBranchDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsPcsBranchData" parameterType="EmsPcsBranchData" useGeneratedKeys="true" keyProperty="id">
        insert into ems_pcs_branch_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dischargeStatus != null">discharge_status,</if>
            <if test="dcPower != null">dc_power,</if>
            <if test="dcVoltage != null">dc_voltage,</if>
            <if test="dcCurrent != null">dc_current,</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>
            <if test="siteId != null">site_id,</if>
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="branchId != null">branch_id,</if>
            <if test="gridUVoltage != null">grid_u_voltage,</if>
            <if test="gridVVoltage != null">grid_v_voltage,</if>
            <if test="gridWVoltage != null">grid_w_voltage,</if>
            <if test="outputUCurrent != null">output_u_current,</if>
            <if test="outputVCurrent != null">output_v_current,</if>
            <if test="outputWCurrent != null">output_w_current,</if>
            <if test="apparentPower != null">apparent_power,</if>
            <if test="activePower != null">active_power,</if>
            <if test="reactivePower != null">reactive_power,</if>
            <if test="powerFactor != null">power_factor,</if>
            <if test="frequency != null">frequency,</if>
            <if test="internalTemp != null">internal_temp,</if>
            <if test="uIgbtTemp != null">u_igbt_temp,</if>
            <if test="vIgbtTemp != null">v_igbt_temp,</if>
            <if test="wIgbtTemp != null">w_igbt_temp,</if>
            <if test="gridStatus != null">grid_status,</if>
            <if test="availablePower != null">available_power,</if>
            <if test="totalLoadRatio != null">total_load_ratio,</if>
            <if test="acLeakageCurrent != null">ac_leakage_current,</if>
            <if test="insulationResistance != null">insulation_resistance,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dischargeStatus != null">#{dischargeStatus},</if>
            <if test="dcPower != null">#{dcPower},</if>
            <if test="dcVoltage != null">#{dcVoltage},</if>
            <if test="dcCurrent != null">#{dcCurrent},</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>
            <if test="siteId != null">#{siteId},</if>
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
            <if test="branchId != null">#{branchId},</if>
            <if test="gridUVoltage != null">#{gridUVoltage},</if>
            <if test="gridVVoltage != null">#{gridVVoltage},</if>
            <if test="gridWVoltage != null">#{gridWVoltage},</if>
            <if test="outputUCurrent != null">#{outputUCurrent},</if>
            <if test="outputVCurrent != null">#{outputVCurrent},</if>
            <if test="outputWCurrent != null">#{outputWCurrent},</if>
            <if test="apparentPower != null">#{apparentPower},</if>
            <if test="activePower != null">#{activePower},</if>
            <if test="reactivePower != null">#{reactivePower},</if>
            <if test="powerFactor != null">#{powerFactor},</if>
            <if test="frequency != null">#{frequency},</if>
            <if test="internalTemp != null">#{internalTemp},</if>
            <if test="uIgbtTemp != null">#{uIgbtTemp},</if>
            <if test="vIgbtTemp != null">#{vIgbtTemp},</if>
            <if test="wIgbtTemp != null">#{wIgbtTemp},</if>
            <if test="gridStatus != null">#{gridStatus},</if>
            <if test="availablePower != null">#{availablePower},</if>
            <if test="totalLoadRatio != null">#{totalLoadRatio},</if>
            <if test="acLeakageCurrent != null">#{acLeakageCurrent},</if>
            <if test="insulationResistance != null">#{insulationResistance},</if>
        </trim>
    </insert>

    <update id="updateEmsPcsBranchData" parameterType="EmsPcsBranchData">
        update ems_pcs_branch_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="dischargeStatus != null">discharge_status = #{dischargeStatus},</if>
            <if test="dcPower != null">dc_power = #{dcPower},</if>
            <if test="dcVoltage != null">dc_voltage = #{dcVoltage},</if>
            <if test="dcCurrent != null">dc_current = #{dcCurrent},</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>
            <if test="siteId != null">site_id = #{siteId},</if>
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="branchId != null">branch_id = #{branchId},</if>
            <if test="gridUVoltage != null">grid_u_voltage = #{gridUVoltage},</if>
            <if test="gridVVoltage != null">grid_v_voltage = #{gridVVoltage},</if>
            <if test="gridWVoltage != null">grid_w_voltage = #{gridWVoltage},</if>
            <if test="outputUCurrent != null">output_u_current = #{outputUCurrent},</if>
            <if test="outputVCurrent != null">output_v_current = #{outputVCurrent},</if>
            <if test="outputWCurrent != null">output_w_current = #{outputWCurrent},</if>
            <if test="apparentPower != null">apparent_power = #{apparentPower},</if>
            <if test="activePower != null">active_power = #{activePower},</if>
            <if test="reactivePower != null">reactive_power = #{reactivePower},</if>
            <if test="powerFactor != null">power_factor = #{powerFactor},</if>
            <if test="frequency != null">frequency = #{frequency},</if>
            <if test="internalTemp != null">internal_temp = #{internalTemp},</if>
            <if test="uIgbtTemp != null">u_igbt_temp = #{uIgbtTemp},</if>
            <if test="vIgbtTemp != null">v_igbt_temp = #{vIgbtTemp},</if>
            <if test="wIgbtTemp != null">w_igbt_temp = #{wIgbtTemp},</if>
            <if test="gridStatus != null">grid_status = #{gridStatus},</if>
            <if test="availablePower != null">available_power = #{availablePower},</if>
            <if test="totalLoadRatio != null">total_load_ratio = #{totalLoadRatio},</if>
            <if test="acLeakageCurrent != null">ac_leakage_current = #{acLeakageCurrent},</if>
            <if test="insulationResistance != null">insulation_resistance = #{insulationResistance},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsPcsBranchDataById" parameterType="Long">
        delete from ems_pcs_branch_data where id = #{id}
    </delete>

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

    <select id="getPcsBranchInfoList" resultType="com.xzzn.ems.domain.vo.PcsBranchInfo">
        SELECT tmp.branch_id as branchId, tmp.discharge_status as dischargeStatus,
               tmp.dc_power as dcPower, tmp.dc_voltage as dcVoltage,
               tmp.dc_current as dcCurrent, tmp.site_id as siteId, tmp.device_id as deviceId
        FROM (
                 SELECT  t.*, ROW_NUMBER() OVER ( PARTITION BY t.branch_id  ORDER BY t.update_time DESC ) AS rn
                 FROM ems_pcs_branch_data t
                 WHERE  t.site_id = #{siteId} AND t.device_id = #{deviceId}
             ) tmp
        WHERE rn = 1
        ORDER BY tmp.branch_id
    </select>

    <insert id="insertPcsBranchDataList" parameterType="java.util.List">
        INSERT INTO ems_pcs_branch_data (
        discharge_status,
        dc_power,
        dc_voltage,
        dc_current,
        site_id,
        device_id,
        branch_id,
        grid_u_voltage,
        grid_v_voltage,
        grid_w_voltage,
        output_u_current,
        output_v_current,
        output_w_current,
        apparent_power,
        active_power,
        reactive_power,
        power_factor,
        frequency,
        internal_temp,
        u_igbt_temp,
        v_igbt_temp,
        w_igbt_temp,
        grid_status,
        available_power,
        total_load_ratio,
        ac_leakage_current,
        insulation_resistance,
        create_by,
        create_time,
        update_by,
        update_time,
        remark
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.dischargeStatus},
            #{item.dcPower},
            #{item.dcVoltage},
            #{item.dcCurrent},
            #{item.siteId},
            #{item.deviceId},
            #{item.branchId},
            #{item.gridUVoltage},
            #{item.gridVVoltage},
            #{item.gridWVoltage},
            #{item.outputUCurrent},
            #{item.outputVCurrent},
            #{item.outputWCurrent},
            #{item.apparentPower},
            #{item.activePower},
            #{item.reactivePower},
            #{item.powerFactor},
            #{item.frequency},
            #{item.internalTemp},
            #{item.uIgbtTemp},
            #{item.vIgbtTemp},
            #{item.wIgbtTemp},
            #{item.gridStatus},
            #{item.availablePower},
            #{item.totalLoadRatio},
            #{item.acLeakageCurrent},
            #{item.insulationResistance},
            #{item.createBy},
            #{item.createTime},
            #{item.updateBy},
            #{item.updateTime},
            #{item.remark}
            )
        </foreach>
    </insert>
</mapper>