<?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.EmsPcsDataMapper">

    <resultMap type="EmsPcsData" id="EmsPcsDataResult">
        <result property="id"    column="id"    />
        <result property="dataUpdateTime"    column="data_update_time"    />
        <result property="workStatus"    column="work_status"    />
        <result property="gridStatus"    column="grid_status"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="controlMode"    column="control_mode"    />
        <result property="totalActivePower"    column="total_active_power"    />
        <result property="dailyAcChargeEnergy"    column="daily_ac_charge_energy"    />
        <result property="aPhaseVoltage"    column="a_phase_voltage"    />
        <result property="aPhaseCurrent"    column="a_phase_current"    />
        <result property="totalReactivePower"    column="total_reactive_power"    />
        <result property="dailyAcDischargeEnergy"    column="daily_ac_discharge_energy"    />
        <result property="bPhaseVoltage"    column="b_phase_voltage"    />
        <result property="bPhaseCurrent"    column="b_phase_current"    />
        <result property="totalApparentPower"    column="total_apparent_power"    />
        <result property="pcsModuleTemperature"    column="pcs_module_temperature"    />
        <result property="cPhaseVoltage"    column="c_phase_voltage"    />
        <result property="cPhaseCurrent"    column="c_phase_current"    />
        <result property="totalPowerFactor"    column="total_power_factor"    />
        <result property="pcsEnvironmentTemperature"    column="pcs_environment_temperature"    />
        <result property="acFrequency"    column="ac_frequency"    />
        <result property="branchStatus"    column="branch_status"    />
        <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="dateMonth"    column="date_month"    />
        <result property="dateDay"    column="date_day"    />
        <result property="totalAcChargeEnergy"    column="total_ac_charge_energy"    />
        <result property="totalAcDischargeEnergy"    column="total_ac_discharge_energy"    />
        <result property="acChargeActivePower"    column="ac_charge_active_power"    />
        <result property="acCapacitiveReactivePower"    column="ac_capacitive_reactive_power"    />
        <result property="acDischargeActivePower"    column="ac_discharge_active_power"    />
        <result property="acInductiveReactivePower"    column="ac_inductive_reactive_power"    />
        <result property="maxCapacitivePowerCapacity"    column="max_capacitive_power_capacity"    />
        <result property="maxInductivePowerCapacity"    column="max_inductive_power_capacity"    />
        <result property="maxChargePowerCapacity"    column="max_charge_power_capacity"    />
        <result property="maxDischargePowerCapacity"    column="max_discharge_power_capacity"    />
        <result property="acSwitchStatus"    column="ac_switch_status"    />
        <result property="dcSwitchStatus"    column="dc_switch_status"    />
        <result property="remoteControlStatus"    column="remote_control_status"    />
        <result property="sysUCurrent"    column="sys_u_current"    />
        <result property="sysVCurrent"    column="sys_v_current"    />
        <result property="sysWCurrent"    column="sys_w_current"    />
        <result property="dwFrequency"    column="dw_frequency"    />
        <result property="uTemperature"    column="u_temperature"    />
        <result property="vTemperature"    column="v_temperature"    />
        <result property="wTemperature"    column="w_temperature"    />
        <result property="module1Temp"    column="module1_temp"    />
        <result property="module2Temp"    column="module2_temp"    />
        <result property="module3Temp"    column="module3_temp"    />
        <result property="module4Temp"    column="module4_temp"    />
    </resultMap>

    <sql id="selectEmsPcsDataVo">
        select id, data_update_time, work_status, grid_status, device_status, control_mode, total_active_power, daily_ac_charge_energy,
               a_phase_voltage, a_phase_current, total_reactive_power, daily_ac_discharge_energy, b_phase_voltage, b_phase_current,
               total_apparent_power, pcs_module_temperature, c_phase_voltage, c_phase_current, total_power_factor, pcs_environment_temperature,
               ac_frequency, branch_status, discharge_status, dc_power, dc_voltage, dc_current, create_by, create_time, update_by, update_time,
               remark, site_id, device_id, date_month, date_day, total_ac_charge_energy, total_ac_discharge_energy, ac_charge_active_power,
               ac_capacitive_reactive_power, ac_discharge_active_power, ac_inductive_reactive_power, max_capacitive_power_capacity,
               max_inductive_power_capacity, max_charge_power_capacity, max_discharge_power_capacity, ac_switch_status, dc_switch_status,
               remote_control_status, sys_u_current, sys_v_current, sys_w_current, dw_frequency, u_temperature, v_temperature, w_temperature,
               module1_temp, module2_temp, module3_temp, module4_temp from ems_pcs_data
    </sql>

    <select id="selectEmsPcsDataList" parameterType="EmsPcsData" resultMap="EmsPcsDataResult">
        <include refid="selectEmsPcsDataVo"/>
        <where>
            <if test="dataUpdateTime != null "> and data_update_time = #{dataUpdateTime}</if>
            <if test="workStatus != null  and workStatus != ''"> and work_status = #{workStatus}</if>
            <if test="gridStatus != null  and gridStatus != ''"> and grid_status = #{gridStatus}</if>
            <if test="deviceStatus != null  and deviceStatus != ''"> and device_status = #{deviceStatus}</if>
            <if test="controlMode != null  and controlMode != ''"> and control_mode = #{controlMode}</if>
            <if test="totalActivePower != null "> and total_active_power = #{totalActivePower}</if>
            <if test="dailyAcChargeEnergy != null "> and daily_ac_charge_energy = #{dailyAcChargeEnergy}</if>
            <if test="aPhaseVoltage != null "> and a_phase_voltage = #{aPhaseVoltage}</if>
            <if test="aPhaseCurrent != null "> and a_phase_current = #{aPhaseCurrent}</if>
            <if test="totalReactivePower != null "> and total_reactive_power = #{totalReactivePower}</if>
            <if test="dailyAcDischargeEnergy != null "> and daily_ac_discharge_energy = #{dailyAcDischargeEnergy}</if>
            <if test="bPhaseVoltage != null "> and b_phase_voltage = #{bPhaseVoltage}</if>
            <if test="bPhaseCurrent != null "> and b_phase_current = #{bPhaseCurrent}</if>
            <if test="totalApparentPower != null "> and total_apparent_power = #{totalApparentPower}</if>
            <if test="pcsModuleTemperature != null "> and pcs_module_temperature = #{pcsModuleTemperature}</if>
            <if test="cPhaseVoltage != null "> and c_phase_voltage = #{cPhaseVoltage}</if>
            <if test="cPhaseCurrent != null "> and c_phase_current = #{cPhaseCurrent}</if>
            <if test="totalPowerFactor != null "> and total_power_factor = #{totalPowerFactor}</if>
            <if test="pcsEnvironmentTemperature != null "> and pcs_environment_temperature = #{pcsEnvironmentTemperature}</if>
            <if test="acFrequency != null "> and ac_frequency = #{acFrequency}</if>
            <if test="branchStatus != null  and branchStatus != ''"> and branch_status = #{branchStatus}</if>
            <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="dateMonth != null "> and date_month = #{dateMonth}</if>
            <if test="dateDay != null "> and date_day = #{dateDay}</if>
            <if test="totalAcChargeEnergy != null "> and total_ac_charge_energy = #{totalAcChargeEnergy}</if>
            <if test="totalAcDischargeEnergy != null "> and total_ac_discharge_energy = #{totalAcDischargeEnergy}</if>
            <if test="acChargeActivePower != null "> and ac_charge_active_power = #{acChargeActivePower}</if>
            <if test="acCapacitiveReactivePower != null "> and ac_capacitive_reactive_power = #{acCapacitiveReactivePower}</if>
            <if test="acDischargeActivePower != null "> and ac_discharge_active_power = #{acDischargeActivePower}</if>
            <if test="acInductiveReactivePower != null "> and ac_inductive_reactive_power = #{acInductiveReactivePower}</if>
            <if test="maxCapacitivePowerCapacity != null "> and max_capacitive_power_capacity = #{maxCapacitivePowerCapacity}</if>
            <if test="maxInductivePowerCapacity != null "> and max_inductive_power_capacity = #{maxInductivePowerCapacity}</if>
            <if test="maxChargePowerCapacity != null "> and max_charge_power_capacity = #{maxChargePowerCapacity}</if>
            <if test="maxDischargePowerCapacity != null "> and max_discharge_power_capacity = #{maxDischargePowerCapacity}</if>
            <if test="acSwitchStatus != null  and acSwitchStatus != ''"> and ac_switch_status = #{acSwitchStatus}</if>
            <if test="dcSwitchStatus != null  and dcSwitchStatus != ''"> and dc_switch_status = #{dcSwitchStatus}</if>
            <if test="remoteControlStatus != null  and remoteControlStatus != ''"> and remote_control_status = #{remoteControlStatus}</if>
            <if test="sysUCurrent != null "> and sys_u_current = #{sysUCurrent}</if>
            <if test="sysVCurrent != null "> and sys_v_current = #{sysVCurrent}</if>
            <if test="sysWCurrent != null "> and sys_w_current = #{sysWCurrent}</if>
            <if test="dwFrequency != null "> and dw_frequency = #{dwFrequency}</if>
            <if test="uTemperature != null "> and u_temperature = #{uTemperature}</if>
            <if test="vTemperature != null "> and v_temperature = #{vTemperature}</if>
            <if test="wTemperature != null "> and w_temperature = #{wTemperature}</if>
            <if test="module1Temp != null "> and module1_temp = #{module1Temp}</if>
            <if test="module2Temp != null "> and module2_temp = #{module2Temp}</if>
            <if test="module3Temp != null "> and module3_temp = #{module3Temp}</if>
            <if test="module4Temp != null "> and module4_temp = #{module4Temp}</if>
        </where>
    </select>

    <select id="selectEmsPcsDataById" parameterType="Long" resultMap="EmsPcsDataResult">
        <include refid="selectEmsPcsDataVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsPcsData" parameterType="EmsPcsData" useGeneratedKeys="true" keyProperty="id">
        insert into ems_pcs_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dataUpdateTime != null">data_update_time,</if>
            <if test="workStatus != null">work_status,</if>
            <if test="gridStatus != null">grid_status,</if>
            <if test="deviceStatus != null">device_status,</if>
            <if test="controlMode != null">control_mode,</if>
            <if test="totalActivePower != null">total_active_power,</if>
            <if test="dailyAcChargeEnergy != null">daily_ac_charge_energy,</if>
            <if test="aPhaseVoltage != null">a_phase_voltage,</if>
            <if test="aPhaseCurrent != null">a_phase_current,</if>
            <if test="totalReactivePower != null">total_reactive_power,</if>
            <if test="dailyAcDischargeEnergy != null">daily_ac_discharge_energy,</if>
            <if test="bPhaseVoltage != null">b_phase_voltage,</if>
            <if test="bPhaseCurrent != null">b_phase_current,</if>
            <if test="totalApparentPower != null">total_apparent_power,</if>
            <if test="pcsModuleTemperature != null">pcs_module_temperature,</if>
            <if test="cPhaseVoltage != null">c_phase_voltage,</if>
            <if test="cPhaseCurrent != null">c_phase_current,</if>
            <if test="totalPowerFactor != null">total_power_factor,</if>
            <if test="pcsEnvironmentTemperature != null">pcs_environment_temperature,</if>
            <if test="acFrequency != null">ac_frequency,</if>
            <if test="branchStatus != null">branch_status,</if>
            <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="dateMonth != null">date_month,</if>
            <if test="dateDay != null">date_day,</if>
            <if test="totalAcChargeEnergy != null">total_ac_charge_energy,</if>
            <if test="totalAcDischargeEnergy != null">total_ac_discharge_energy,</if>
            <if test="acChargeActivePower != null">ac_charge_active_power,</if>
            <if test="acCapacitiveReactivePower != null">ac_capacitive_reactive_power,</if>
            <if test="acDischargeActivePower != null">ac_discharge_active_power,</if>
            <if test="acInductiveReactivePower != null">ac_inductive_reactive_power,</if>
            <if test="maxCapacitivePowerCapacity != null">max_capacitive_power_capacity,</if>
            <if test="maxInductivePowerCapacity != null">max_inductive_power_capacity,</if>
            <if test="maxChargePowerCapacity != null">max_charge_power_capacity,</if>
            <if test="maxDischargePowerCapacity != null">max_discharge_power_capacity,</if>
            <if test="acSwitchStatus != null">ac_switch_status,</if>
            <if test="dcSwitchStatus != null">dc_switch_status,</if>
            <if test="remoteControlStatus != null">remote_control_status,</if>
            <if test="sysUCurrent != null">sys_u_current,</if>
            <if test="sysVCurrent != null">sys_v_current,</if>
            <if test="sysWCurrent != null">sys_w_current,</if>
            <if test="dwFrequency != null">dw_frequency,</if>
            <if test="uTemperature != null">u_temperature,</if>
            <if test="vTemperature != null">v_temperature,</if>
            <if test="wTemperature != null">w_temperature,</if>
            <if test="module1Temp != null">module1_temp,</if>
            <if test="module2Temp != null">module2_temp,</if>
            <if test="module3Temp != null">module3_temp,</if>
            <if test="module4Temp != null">module4_temp,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dataUpdateTime != null">#{dataUpdateTime},</if>
            <if test="workStatus != null">#{workStatus},</if>
            <if test="gridStatus != null">#{gridStatus},</if>
            <if test="deviceStatus != null">#{deviceStatus},</if>
            <if test="controlMode != null">#{controlMode},</if>
            <if test="totalActivePower != null">#{totalActivePower},</if>
            <if test="dailyAcChargeEnergy != null">#{dailyAcChargeEnergy},</if>
            <if test="aPhaseVoltage != null">#{aPhaseVoltage},</if>
            <if test="aPhaseCurrent != null">#{aPhaseCurrent},</if>
            <if test="totalReactivePower != null">#{totalReactivePower},</if>
            <if test="dailyAcDischargeEnergy != null">#{dailyAcDischargeEnergy},</if>
            <if test="bPhaseVoltage != null">#{bPhaseVoltage},</if>
            <if test="bPhaseCurrent != null">#{bPhaseCurrent},</if>
            <if test="totalApparentPower != null">#{totalApparentPower},</if>
            <if test="pcsModuleTemperature != null">#{pcsModuleTemperature},</if>
            <if test="cPhaseVoltage != null">#{cPhaseVoltage},</if>
            <if test="cPhaseCurrent != null">#{cPhaseCurrent},</if>
            <if test="totalPowerFactor != null">#{totalPowerFactor},</if>
            <if test="pcsEnvironmentTemperature != null">#{pcsEnvironmentTemperature},</if>
            <if test="acFrequency != null">#{acFrequency},</if>
            <if test="branchStatus != null">#{branchStatus},</if>
            <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="dateMonth != null">#{dateMonth},</if>
            <if test="dateDay != null">#{dateDay},</if>
            <if test="totalAcChargeEnergy != null">#{totalAcChargeEnergy},</if>
            <if test="totalAcDischargeEnergy != null">#{totalAcDischargeEnergy},</if>
            <if test="acChargeActivePower != null">#{acChargeActivePower},</if>
            <if test="acCapacitiveReactivePower != null">#{acCapacitiveReactivePower},</if>
            <if test="acDischargeActivePower != null">#{acDischargeActivePower},</if>
            <if test="acInductiveReactivePower != null">#{acInductiveReactivePower},</if>
            <if test="maxCapacitivePowerCapacity != null">#{maxCapacitivePowerCapacity},</if>
            <if test="maxInductivePowerCapacity != null">#{maxInductivePowerCapacity},</if>
            <if test="maxChargePowerCapacity != null">#{maxChargePowerCapacity},</if>
            <if test="maxDischargePowerCapacity != null">#{maxDischargePowerCapacity},</if>
            <if test="acSwitchStatus != null">#{acSwitchStatus},</if>
            <if test="dcSwitchStatus != null">#{dcSwitchStatus},</if>
            <if test="remoteControlStatus != null">#{remoteControlStatus},</if>
            <if test="sysUCurrent != null">#{sysUCurrent},</if>
            <if test="sysVCurrent != null">#{sysVCurrent},</if>
            <if test="sysWCurrent != null">#{sysWCurrent},</if>
            <if test="dwFrequency != null">#{dwFrequency},</if>
            <if test="uTemperature != null">#{uTemperature},</if>
            <if test="vTemperature != null">#{vTemperature},</if>
            <if test="wTemperature != null">#{wTemperature},</if>
            <if test="module1Temp != null">#{module1Temp},</if>
            <if test="module2Temp != null">#{module2Temp},</if>
            <if test="module3Temp != null">#{module3Temp},</if>
            <if test="module4Temp != null">#{module4Temp},</if>
        </trim>
    </insert>

    <update id="updateEmsPcsData" parameterType="EmsPcsData">
        update ems_pcs_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="dataUpdateTime != null">data_update_time = #{dataUpdateTime},</if>
            <if test="workStatus != null">work_status = #{workStatus},</if>
            <if test="gridStatus != null">grid_status = #{gridStatus},</if>
            <if test="deviceStatus != null">device_status = #{deviceStatus},</if>
            <if test="controlMode != null">control_mode = #{controlMode},</if>
            <if test="totalActivePower != null">total_active_power = #{totalActivePower},</if>
            <if test="dailyAcChargeEnergy != null">daily_ac_charge_energy = #{dailyAcChargeEnergy},</if>
            <if test="aPhaseVoltage != null">a_phase_voltage = #{aPhaseVoltage},</if>
            <if test="aPhaseCurrent != null">a_phase_current = #{aPhaseCurrent},</if>
            <if test="totalReactivePower != null">total_reactive_power = #{totalReactivePower},</if>
            <if test="dailyAcDischargeEnergy != null">daily_ac_discharge_energy = #{dailyAcDischargeEnergy},</if>
            <if test="bPhaseVoltage != null">b_phase_voltage = #{bPhaseVoltage},</if>
            <if test="bPhaseCurrent != null">b_phase_current = #{bPhaseCurrent},</if>
            <if test="totalApparentPower != null">total_apparent_power = #{totalApparentPower},</if>
            <if test="pcsModuleTemperature != null">pcs_module_temperature = #{pcsModuleTemperature},</if>
            <if test="cPhaseVoltage != null">c_phase_voltage = #{cPhaseVoltage},</if>
            <if test="cPhaseCurrent != null">c_phase_current = #{cPhaseCurrent},</if>
            <if test="totalPowerFactor != null">total_power_factor = #{totalPowerFactor},</if>
            <if test="pcsEnvironmentTemperature != null">pcs_environment_temperature = #{pcsEnvironmentTemperature},</if>
            <if test="acFrequency != null">ac_frequency = #{acFrequency},</if>
            <if test="branchStatus != null">branch_status = #{branchStatus},</if>
            <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="dateMonth != null">date_month = #{dateMonth},</if>
            <if test="dateDay != null">date_day = #{dateDay},</if>
            <if test="totalAcChargeEnergy != null">total_ac_charge_energy = #{totalAcChargeEnergy},</if>
            <if test="totalAcDischargeEnergy != null">total_ac_discharge_energy = #{totalAcDischargeEnergy},</if>
            <if test="acChargeActivePower != null">ac_charge_active_power = #{acChargeActivePower},</if>
            <if test="acCapacitiveReactivePower != null">ac_capacitive_reactive_power = #{acCapacitiveReactivePower},</if>
            <if test="acDischargeActivePower != null">ac_discharge_active_power = #{acDischargeActivePower},</if>
            <if test="acInductiveReactivePower != null">ac_inductive_reactive_power = #{acInductiveReactivePower},</if>
            <if test="maxCapacitivePowerCapacity != null">max_capacitive_power_capacity = #{maxCapacitivePowerCapacity},</if>
            <if test="maxInductivePowerCapacity != null">max_inductive_power_capacity = #{maxInductivePowerCapacity},</if>
            <if test="maxChargePowerCapacity != null">max_charge_power_capacity = #{maxChargePowerCapacity},</if>
            <if test="maxDischargePowerCapacity != null">max_discharge_power_capacity = #{maxDischargePowerCapacity},</if>
            <if test="acSwitchStatus != null">ac_switch_status = #{acSwitchStatus},</if>
            <if test="dcSwitchStatus != null">dc_switch_status = #{dcSwitchStatus},</if>
            <if test="remoteControlStatus != null">remote_control_status = #{remoteControlStatus},</if>
            <if test="sysUCurrent != null">sys_u_current = #{sysUCurrent},</if>
            <if test="sysVCurrent != null">sys_v_current = #{sysVCurrent},</if>
            <if test="sysWCurrent != null">sys_w_current = #{sysWCurrent},</if>
            <if test="dwFrequency != null">dw_frequency = #{dwFrequency},</if>
            <if test="uTemperature != null">u_temperature = #{uTemperature},</if>
            <if test="vTemperature != null">v_temperature = #{vTemperature},</if>
            <if test="wTemperature != null">w_temperature = #{wTemperature},</if>
            <if test="module1Temp != null">module1_temp = #{module1Temp},</if>
            <if test="module2Temp != null">module2_temp = #{module2Temp},</if>
            <if test="module3Temp != null">module3_temp = #{module3Temp},</if>
            <if test="module4Temp != null">module4_temp = #{module4Temp},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsPcsDataById" parameterType="Long">
        delete from ems_pcs_data where id = #{id}
    </delete>

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

    <select id="getPcsDataBySiteId" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        select   CONCAT(t.date_month,'-',t.date_day)	as ammeterDate,
                  sum(t.daily_ac_charge_energy) as chargedCap,
                  sum(t.daily_ac_discharge_energy) as disChargedCap
        from  ems_pcs_data t
        inner join( SELECT p.site_id,  p.device_id, p.date_month,p.date_day, MAX(p.data_update_time) AS max_update_time
                    FROM ems_pcs_data p
                    <include refid="commonFilter"/>
                    GROUP BY p.site_id,p.device_id,p.date_month,p.date_day
        ) latest ON t.site_id = latest.site_id
            AND t.device_id = latest.device_id
            AND t.date_month = latest.date_month
            AND t.date_day = latest.date_day
            AND t.data_update_time = latest.max_update_time
        group by ammeterDate,t.date_month,t.date_day
        order by t.date_month desc, t.date_day desc
    </select>

    <select id="getPcsTotalChargeData" resultType="map">
        SELECT
            SUM(p.total_ac_charge_energy) AS totalChargedCap,
            SUM(p.total_ac_discharge_energy) AS totalDischargedCap,
            SUM(p.total_active_power) AS gridNrtPower
        FROM ems_pcs_data p
        INNER JOIN ems_site_setting s ON p.site_id = s.site_id
        INNER JOIN ( SELECT site_id, device_id, MAX(data_update_time) AS max_update_time
                        FROM ems_pcs_data
                        <if test="siteId !=null and siteId !=''">
                            where site_id = #{siteId}
                        </if>
            GROUP BY site_id, device_id
        ) latest ON p.site_id = latest.site_id
                 AND p.device_id = latest.device_id
                 AND p.data_update_time = latest.max_update_time
        <if test="siteId !=null and siteId !=''">
            where p.site_id = #{siteId}
        </if>
    </select>

    <select id="getSiteRunningHeadInfo" parameterType="String" resultType="com.xzzn.ems.domain.vo.SiteMonitorRunningHeadInfoVo">
        SELECT SUM(pd.total_active_power)        as totalActivePower,
               SUM(pd.total_reactive_power)      as totalReactivePower,
               sum(pd.daily_ac_charge_energy)    as dayChargedCap,
               sum(pd.daily_ac_discharge_energy) as dayDisChargedCap
        FROM (
                 SELECT
                     t.*, ROW_NUMBER() OVER(PARTITION BY t.device_id ORDER BY t.data_update_time desc) AS rn
                 FROM (
                 SELECT p.device_id, MAX(p.data_update_time) AS max_update_time
                 FROM ems_pcs_data p
                 where p.site_id = #{siteId}
                 GROUP BY p.device_id
             ) latest inner join ems_pcs_data t ON latest.device_id = t.device_id
            AND latest.max_update_time = t.data_update_time
            AND t.site_id = #{siteId}
             ) AS pd
        WHERE rn = 1
    </select>

    <select id="getElectDataList" resultType="com.xzzn.ems.domain.vo.ElectricIndexList">
        SELECT  DATE_FORMAT(t.data_update_time, '%Y-%m') AS dateMonth,
                SUM(t.daily_ac_charge_energy) AS chargeEnergy,
                SUM(t.daily_ac_discharge_energy) AS disChargeEnergy
        FROM ems_pcs_data t
        INNER JOIN ( SELECT  site_id, device_id, date_month, date_day, MAX(data_update_time) AS max_update_time
                              FROM ems_pcs_data
                              GROUP BY site_id, device_id, date_month, date_day
        ) max_dates ON t.site_id = max_dates.site_id
            AND t.device_id = max_dates.device_id
            AND t.date_month = max_dates.date_month
            AND t.date_day = max_dates.date_day
            AND t.data_update_time = max_dates.max_update_time
        GROUP BY dateMonth
        ORDER BY dateMonth
    </select>

    <select id="getPcsDetailInfoBySiteId" parameterType="String" resultType="com.xzzn.ems.domain.vo.PcsDetailInfoVo">
        SELECT td.device_name as deviceName,
               td.communication_status as communicationStatus,
               tmp.site_id as siteId,
               tmp.device_id as deviceId,
               tmp.data_update_time as dataUpdateTime,tmp.work_status as workStatus,
               tmp.grid_status as gridStatus,tmp.device_status as deviceStatus,tmp.control_mode as controlMode,
               tmp.total_active_power as totalActivePower,tmp.daily_ac_charge_energy as dailyAcChargeEnergy,
               tmp.a_phase_voltage as aPhaseVoltage,tmp.a_phase_current as aPhaseCurrent,
               tmp.total_reactive_power as totalReactivePower,tmp.daily_ac_discharge_energy as dailyAcDischargeEnergy,
               tmp.b_phase_voltage as bPhaseVoltage,tmp.b_phase_current as bPhaseCurrent,
               tmp.total_apparent_power as totalApparentPower,tmp.pcs_module_temperature as pcsModuleTemperature,
               tmp.c_phase_current as cPhaseVoltage,tmp.c_phase_current as cPhaseCurrent,
               tmp.total_power_factor as totalPowerFactor,
               tmp.pcs_environment_temperature as pcsEnvironmentTemperature,tmp.ac_frequency as acFrequency
        FROM ems_pcs_data tmp
                 LEFT JOIN ems_devices_setting td on tmp.device_id = td.device_id and tmp.site_id = td.site_id
                 INNER JOIN ( SELECT p.site_id,  p.device_id, MAX(p.data_update_time) AS max_update_time
                                FROM ems_pcs_data p
                                WHERE p.site_id = #{siteId}
                                GROUP BY p.site_id,p.device_id
                ) latest on tmp.device_id = latest.device_id and tmp.data_update_time = latest.max_update_time
        WHERE tmp.site_id = #{siteId}
        order by tmp.device_id
    </select>

    <sql id="commonFilter">
        WHERE p.site_id = #{siteId}
        <if test="startDate != null">
            AND p.data_update_time &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND p.data_update_time &lt; #{endDate}
        </if>
    </sql>

    <select id="getPcsDataByDay" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        select latest.dateDay as ammeterDate,
               sum(t.total_ac_charge_energy) as chargedCap,
               sum(t.total_ac_discharge_energy) as disChargedCap
        from  ( SELECT p.site_id,  p.device_id,DATE(p.data_update_time) as dateDay, MAX(p.data_update_time) AS max_update_time
                FROM ems_pcs_data p
                <include refid="commonFilter"/>
                GROUP BY p.site_id,p.device_id,dateDay
            ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
            AND latest.device_id = t.device_id
            AND latest.max_update_time = t.data_update_time
        group by ammeterDate
        order by ammeterDate desc
    </select>

    <select id="getStoragePowerList"  parameterType="com.xzzn.ems.domain.vo.DateSearchRequest"  resultType="com.xzzn.ems.domain.vo.EnergyStoragePowVo">
        WITH ranked AS (
            SELECT
            *,
            date_format(data_update_time, '%Y-%m-%d %H:00:00') + INTERVAL FLOOR(MINUTE(data_update_time) / 5) * 5 MINUTE AS group_time,
            ROW_NUMBER() OVER (PARTITION BY device_id, date_format(data_update_time, '%Y-%m-%d %H:00:00'), FLOOR(MINUTE(data_update_time) / 5) ORDER BY data_update_time) as rn
            FROM
            ems_pcs_data
            where site_id = #{siteId}
            and data_update_time &gt;= #{startDate}
            and data_update_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        )
        SELECT
            site_id,
            device_id as deviceId,
            total_active_power as pcsTotalActPower,
            total_reactive_power as pcsTotalReactivePower,
            DATE_FORMAT(group_time, '%Y-%m-%d') as dateDay,
            DATE_FORMAT(group_time, '%H:%i:00') as createDate,
            group_time as groupTime
        FROM
        ranked
        WHERE
        rn = 1
    </select>

    <select id="getPcsDataByMonth" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        select DATE_FORMAT(latest.max_update_time, '%Y-%m') as ammeterDate,
                sum(t.total_ac_charge_energy) as chargedCap,
                sum(t.total_ac_discharge_energy) as disChargedCap
        from  ( SELECT p.site_id,  p.device_id,p.date_month,MAX(p.data_update_time) AS max_update_time
                FROM ems_pcs_data p
                <include refid="commonFilter"/>
                GROUP BY p.site_id,p.device_id,p.date_month
            ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
            AND latest.device_id = t.device_id
            AND latest.max_update_time = t.data_update_time
            and latest.date_month = t.date_month
        group by ammeterDate
        order by ammeterDate desc
    </select>

    <select id="getPcsDataByHour" resultType="com.xzzn.ems.domain.vo.SiteMonitorDataVo">
        select  latest.dateHour as ammeterDate,
                sum(t.daily_ac_charge_energy) as chargedCap,
                sum(t.daily_ac_discharge_energy) as disChargedCap
        from  ( SELECT p.site_id,  p.device_id,DATE_FORMAT(p.data_update_time, '%H:00') AS dateHour,MAX(p.data_update_time) as max_update_time
                FROM ems_pcs_data p
                <include refid="commonFilter"/>
                GROUP BY p.site_id,p.device_id,dateHour
            ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
            AND latest.device_id = t.device_id
            AND latest.max_update_time = t.data_update_time
        group by ammeterDate
        order by ammeterDate desc
    </select>

    <sql id="statisCommonFilter">
        WHERE p.site_id = #{siteId}
        <if test="startDate != null">
            AND p.data_update_time &gt;= #{startDate}
        </if>
        <if test="endDate != null">
            AND p.data_update_time &lt; #{endDate}
        </if>
    </sql>

    <sql id="dataTypeChooseFilter">
        <choose>
            <when test='dataType == "1"'>
                t.total_active_power as activePower
            </when>
            <when test='dataType == "2"'>
                t.total_reactive_power as reactivePower
            </when>
            <when test='dataType == "3"'>
                t.sys_u_current as uCurrent,
                t.sys_v_current as vCurrent,
                t.sys_w_current as wCurrent
            </when>
            <otherwise>
                0 as activePower
            </otherwise>
        </choose>
    </sql>


    <select id="getPcsActivePowerByDay" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsStatisListVo">
        select  latest.dateDay as statisDate,
                t.device_id as deviceId,
            <include refid="dataTypeChooseFilter"/>
        from  ( SELECT p.site_id,  p.device_id,DATE(p.data_update_time ) AS dateDay,MAX(p.data_update_time) as max_update_time
                FROM ems_pcs_data p
                <include refid="statisCommonFilter"/>
                GROUP BY p.site_id,p.device_id,dateDay
            ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
                AND latest.device_id = t.device_id
                AND latest.max_update_time = t.data_update_time
        order by statisDate desc
    </select>

    <select id="getPcsActivePowerByMonth" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsStatisListVo">
        select  DATE_FORMAT(latest.max_update_time, '%Y-%m') as statisDate,
                t.device_id as deviceId,
                <include refid="dataTypeChooseFilter"/>
        from  ( SELECT p.site_id,  p.device_id,p.date_month,MAX(p.data_update_time) as max_update_time
                FROM ems_pcs_data p
                <include refid="statisCommonFilter"/>
                GROUP BY p.site_id,p.device_id,p.date_month
            ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
        AND latest.device_id = t.device_id
        AND latest.max_update_time = t.data_update_time
        and latest.date_month = t.date_month
        order by statisDate desc
    </select>

    <select id="getPcsActivePowerByHour" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsStatisListVo">
        select  latest.dateHour as statisDate,
                t.device_id as deviceId,
                <include refid="dataTypeChooseFilter"/>
        from  ( SELECT p.site_id,  p.device_id,DATE_FORMAT(p.data_update_time, '%H:00') AS dateHour,MAX(p.data_update_time) as max_update_time
                FROM ems_pcs_data p
                <include refid="statisCommonFilter"/>
                GROUP BY p.site_id,p.device_id,dateHour
            ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
                AND latest.device_id = t.device_id
                AND latest.max_update_time = t.data_update_time
        order by statisDate desc
    </select>

    <select id="getPcsActivePowerByMinutes" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsStatisListVo">
        WITH ranked AS (
        SELECT
        *,
        DATE_FORMAT(DATE_ADD(DATE_FORMAT(p.data_update_time, '%Y-%m-%d %H:00:00'), INTERVAL CEIL(MINUTE(p.data_update_time) / 5) * 5 MINUTE)
        , '%Y-%m-%d %H:%i:%s') AS statisDate,
        ROW_NUMBER() OVER (PARTITION BY p.device_id, date_format(p.data_update_time, '%Y-%m-%d %H:00:00'), CEIL(MINUTE(p.data_update_time) / 5) ORDER BY p.data_update_time DESC) as rn
        FROM
        ems_pcs_data p
        <include refid="statisCommonFilter"/>
        )
        select
        t.device_id as deviceId,
        t.statisDate,
        <include refid="dataTypeChooseFilter"/>
        FROM
        ranked as t
        where t.rn = 1
    </select>

    <select id="getFXMaxTemp" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsMaxTempVo">
        SELECT t.site_id,t.device_id as deviceId,
                GREATEST(
                        COALESCE(t.u_temperature, -999),
                        COALESCE(t.v_temperature, -999),
                        COALESCE(t.w_temperature, -999)
                ) AS temp,
               DATE_FORMAT(latest.dateHour, '%Y-%m-%d') AS dateDay,
               DATE_FORMAT(latest.dateHour, '%H:%i:00') AS createDate
        from  ( SELECT p.site_id, p.device_id,DATE_FORMAT(p.data_update_time, '%Y-%m-%d %H:00:00') AS dateHour,MAX(p.data_update_time) as max_update_time
                FROM ems_pcs_data p
                WHERE p.site_id =  #{siteId}
                AND p.data_update_time &gt;= #{startDate}
                AND p.data_update_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
                AND minute(p.data_update_time) = 0
                GROUP BY  p.site_id, p.device_id,dateHour
        ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
                AND latest.device_id = t.device_id
                AND latest.max_update_time = t.data_update_time
                group by deviceId,dateDay,createDate,temp
                order by dateDay,createDate
    </select>

    <select id="getDDSMaxTemp" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsMaxTempVo">
        SELECT t.site_id,t.device_id as deviceId,
                GREATEST(
                    COALESCE(t.module1_temp, -999),
                    COALESCE(t.module2_temp, -999),
                    COALESCE(t.module3_temp, -999),
                    COALESCE(t.module4_temp, -999)
                ) AS temp,
               DATE_FORMAT(latest.dateHour, '%Y-%m-%d') AS dateDay,
               DATE_FORMAT(latest.dateHour, '%H:%i:00') AS createDate
        from  ( SELECT p.site_id, p.device_id,DATE_FORMAT(p.data_update_time, '%Y-%m-%d %H:00:00') AS dateHour,MAX(p.data_update_time) as max_update_time
                FROM ems_pcs_data p
                WHERE p.site_id =  #{siteId}
                AND p.data_update_time &gt;= #{startDate}
                AND p.data_update_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
                AND minute(p.data_update_time) = 0
                GROUP BY  p.site_id, p.device_id,dateHour
        ) latest inner join ems_pcs_data t ON latest.site_id = t.site_id
            AND latest.device_id = t.device_id
            AND latest.max_update_time = t.data_update_time
            group by deviceId,dateDay,createDate,temp
            order by dateDay,createDate
    </select>

    <select id="getPcsMaxTemp" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.PcsMaxTempVo">
        WITH ranked AS (
            SELECT
                *,
                date_format(data_update_time, '%Y-%m-%d %H:00:00') + INTERVAL FLOOR(MINUTE(data_update_time) / 5) * 5 MINUTE AS group_time,
                ROW_NUMBER() OVER (PARTITION BY device_id, date_format(data_update_time, '%Y-%m-%d %H:00:00'), FLOOR(MINUTE(data_update_time) / 5) ORDER BY data_update_time) as rn
            FROM
                ems_pcs_data
            where site_id = #{siteId}
              and data_update_time &gt;= #{startDate}
              and data_update_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        )
        SELECT
            site_id,
            device_id as deviceId,
            GREATEST(
                    COALESCE(u_temperature, -999),
                    COALESCE(v_temperature, -999),
                    COALESCE(w_temperature, -999),
                    COALESCE(module1_temp, -999),
                    COALESCE(module2_temp, -999),
                    COALESCE(module3_temp, -999),
                    COALESCE(module4_temp, -999)
                ) AS temp,
            DATE_FORMAT(group_time, '%Y-%m-%d') as dateDay,
            DATE_FORMAT(group_time, '%H:%i:00') as createDate,
            group_time as groupTime
        FROM
            ranked
        WHERE
            rn = 1
    </select>
</mapper>