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

    <resultMap type="EmsBatteryStack" id="EmsBatteryStackResult">
        <result property="id"    column="id"    />
        <result property="dataUpdateTime"    column="data_update_time"    />
        <result property="workStatus"    column="work_status"    />
        <result property="pcsCommunicationStatus"    column="pcs_communication_status"    />
        <result property="emsCommunicationStatus"    column="ems_communication_status"    />
        <result property="operationStatus"    column="operation_status"    />
        <result property="stackVoltage"    column="stack_voltage"    />
        <result property="stackCurrent"    column="stack_current"    />
        <result property="stackSoc"    column="stack_soc"    />
        <result property="stackSoh"    column="stack_soh"    />
        <result property="maxCellVoltage"    column="max_cell_voltage"    />
        <result property="maxVoltageGroupId"    column="max_voltage_group_id"    />
        <result property="maxVoltageCellId"    column="max_voltage_cell_id"    />
        <result property="minCellVoltage"    column="min_cell_voltage"    />
        <result property="minVoltageGroupId"    column="min_voltage_group_id"    />
        <result property="minVoltageCellId"    column="min_voltage_cell_id"    />
        <result property="maxCellTemp"    column="max_cell_temp"    />
        <result property="maxTempGroupId"    column="max_temp_group_id"    />
        <result property="maxTempCellId"    column="max_temp_cell_id"    />
        <result property="minCellTemp"    column="min_cell_temp"    />
        <result property="minTempGroupId"    column="min_temp_group_id"    />
        <result property="minTempCellId"    column="min_temp_cell_id"    />
        <result property="totalChargeCapacity"    column="total_charge_capacity"    />
        <result property="totalDischargeCapacity"    column="total_discharge_capacity"    />
        <result property="sessionChargeCapacity"    column="session_charge_capacity"    />
        <result property="sessionDischargeCapacity"    column="session_discharge_capacity"    />
        <result property="availableChargeCapacity"    column="available_charge_capacity"    />
        <result property="availableDischargeCapacity"    column="available_discharge_capacity"    />
        <result property="remainingDischargeTime"    column="remaining_discharge_time"    />
        <result property="remainingChargeTime"    column="remaining_charge_time"    />
        <result property="maxDischargePower"    column="max_discharge_power"    />
        <result property="maxChargePower"    column="max_charge_power"    />
        <result property="maxDischargeCurrent"    column="max_discharge_current"    />
        <result property="maxChargeCurrent"    column="max_charge_current"    />
        <result property="dailyDischargeCycles"    column="daily_discharge_cycles"    />
        <result property="dailyChargeCycles"    column="daily_charge_cycles"    />
        <result property="dailyDischargeCapacity"    column="daily_discharge_capacity"    />
        <result property="dailyChargeCapacity"    column="daily_charge_capacity"    />
        <result property="operatingTemp"    column="operating_temp"    />
        <result property="bmsStatus"    column="bms_status"    />
        <result property="bmsChargeStatus"    column="bms_charge_status"    />
        <result property="stackInsulationResistance"    column="stack_insulation_resistance"    />
        <result property="siteId"    column="site_id"    />
        <result property="deviceId"    column="device_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="batteryNumber"    column="battery_number"    />
        <result property="batteryAvgVoltage"    column="battery_avg_voltage"    />
        <result property="batteryDifferentPressure"    column="battery_different_pressure"    />
        <result property="avgTemperature"    column="avg_temperature"    />
        <result property="batteryDifferentTemperature"    column="battery_different_temperature"    />
        <result property="maxInternalResistance"    column="max_internal_resistance"    />
        <result property="minInternalResistance"    column="min_internal_resistance"    />
        <result property="avgInternalResistance"    column="avg_internal_resistance"    />
        <result property="batteryDefferentResistance"    column="battery_defferent_resistance"    />
        <result property="maxResistanceCellId"    column="max_resistance_cell_id"    />
        <result property="minResistanceCellId"    column="min_resistance_cell_id"    />
        <result property="environmentTemperature"    column="environment_temperature"    />
        <result property="environmentHumidity"    column="environment_humidity"    />
        <result property="circuitBreakerStatus"    column="circuit_breaker_status"    />
    </resultMap>

    <sql id="selectEmsBatteryStackVo">
        select id, work_status, pcs_communication_status, ems_communication_status, operation_status, stack_voltage, stack_current, stack_soc, stack_soh, max_cell_voltage, max_voltage_group_id, max_voltage_cell_id, min_cell_voltage, min_voltage_group_id, min_voltage_cell_id, max_cell_temp, max_temp_group_id, max_temp_cell_id, min_cell_temp, min_temp_group_id, min_temp_cell_id, total_charge_capacity, total_discharge_capacity, session_charge_capacity, session_discharge_capacity, available_charge_capacity, available_discharge_capacity, remaining_discharge_time, remaining_charge_time, max_discharge_power, max_charge_power, max_discharge_current, max_charge_current, daily_discharge_cycles, daily_charge_cycles, daily_discharge_capacity, daily_charge_capacity, operating_temp, bms_status, bms_charge_status, stack_insulation_resistance, site_id, device_id, create_time, create_by, update_time, update_by, battery_number, battery_avg_voltage, battery_different_pressure, avg_temperature, battery_different_temperature, max_internal_resistance, min_internal_resistance, avg_internal_resistance, battery_defferent_resistance, max_resistance_cell_id, min_resistance_cell_id, environment_temperature, environment_humidity, circuit_breaker_status from ems_battery_stack
    </sql>

    <select id="selectEmsBatteryStackList" parameterType="EmsBatteryStack" resultMap="EmsBatteryStackResult">
        <include refid="selectEmsBatteryStackVo"/>
        <where>
            <if test="dataUpdateTime != null "> and data_update_time = #{dataUpdateTime}</if>
            <if test="workStatus != null  and workStatus != ''"> and work_status = #{workStatus}</if>
            <if test="pcsCommunicationStatus != null  and pcsCommunicationStatus != ''"> and pcs_communication_status = #{pcsCommunicationStatus}</if>
            <if test="emsCommunicationStatus != null  and emsCommunicationStatus != ''"> and ems_communication_status = #{emsCommunicationStatus}</if>
            <if test="operationStatus != null  and operationStatus != ''"> and operation_status = #{operationStatus}</if>
            <if test="stackVoltage != null "> and stack_voltage = #{stackVoltage}</if>
            <if test="stackCurrent != null "> and stack_current = #{stackCurrent}</if>
            <if test="stackSoc != null "> and stack_soc = #{stackSoc}</if>
            <if test="stackSoh != null "> and stack_soh = #{stackSoh}</if>
            <if test="maxCellVoltage != null "> and max_cell_voltage = #{maxCellVoltage}</if>
            <if test="maxVoltageGroupId != null "> and max_voltage_group_id = #{maxVoltageGroupId}</if>
            <if test="maxVoltageCellId != null "> and max_voltage_cell_id = #{maxVoltageCellId}</if>
            <if test="minCellVoltage != null "> and min_cell_voltage = #{minCellVoltage}</if>
            <if test="minVoltageGroupId != null "> and min_voltage_group_id = #{minVoltageGroupId}</if>
            <if test="minVoltageCellId != null "> and min_voltage_cell_id = #{minVoltageCellId}</if>
            <if test="maxCellTemp != null "> and max_cell_temp = #{maxCellTemp}</if>
            <if test="maxTempGroupId != null "> and max_temp_group_id = #{maxTempGroupId}</if>
            <if test="maxTempCellId != null "> and max_temp_cell_id = #{maxTempCellId}</if>
            <if test="minCellTemp != null "> and min_cell_temp = #{minCellTemp}</if>
            <if test="minTempGroupId != null "> and min_temp_group_id = #{minTempGroupId}</if>
            <if test="minTempCellId != null "> and min_temp_cell_id = #{minTempCellId}</if>
            <if test="totalChargeCapacity != null "> and total_charge_capacity = #{totalChargeCapacity}</if>
            <if test="totalDischargeCapacity != null "> and total_discharge_capacity = #{totalDischargeCapacity}</if>
            <if test="sessionChargeCapacity != null "> and session_charge_capacity = #{sessionChargeCapacity}</if>
            <if test="sessionDischargeCapacity != null "> and session_discharge_capacity = #{sessionDischargeCapacity}</if>
            <if test="availableChargeCapacity != null "> and available_charge_capacity = #{availableChargeCapacity}</if>
            <if test="availableDischargeCapacity != null "> and available_discharge_capacity = #{availableDischargeCapacity}</if>
            <if test="remainingDischargeTime != null "> and remaining_discharge_time = #{remainingDischargeTime}</if>
            <if test="remainingChargeTime != null "> and remaining_charge_time = #{remainingChargeTime}</if>
            <if test="maxDischargePower != null "> and max_discharge_power = #{maxDischargePower}</if>
            <if test="maxChargePower != null "> and max_charge_power = #{maxChargePower}</if>
            <if test="maxDischargeCurrent != null "> and max_discharge_current = #{maxDischargeCurrent}</if>
            <if test="maxChargeCurrent != null "> and max_charge_current = #{maxChargeCurrent}</if>
            <if test="dailyDischargeCycles != null "> and daily_discharge_cycles = #{dailyDischargeCycles}</if>
            <if test="dailyChargeCycles != null "> and daily_charge_cycles = #{dailyChargeCycles}</if>
            <if test="dailyDischargeCapacity != null "> and daily_discharge_capacity = #{dailyDischargeCapacity}</if>
            <if test="dailyChargeCapacity != null "> and daily_charge_capacity = #{dailyChargeCapacity}</if>
            <if test="operatingTemp != null "> and operating_temp = #{operatingTemp}</if>
            <if test="bmsStatus != null  and bmsStatus != ''"> and bms_status = #{bmsStatus}</if>
            <if test="bmsChargeStatus != null  and bmsChargeStatus != ''"> and bms_charge_status = #{bmsChargeStatus}</if>
            <if test="stackInsulationResistance != null "> and stack_insulation_resistance = #{stackInsulationResistance}</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="batteryNumber != null "> and battery_number = #{batteryNumber}</if>
            <if test="batteryAvgVoltage != null "> and battery_avg_voltage = #{batteryAvgVoltage}</if>
            <if test="batteryDifferentPressure != null "> and battery_different_pressure = #{batteryDifferentPressure}</if>
            <if test="avgTemperature != null "> and avg_temperature = #{avgTemperature}</if>
            <if test="batteryDifferentTemperature != null "> and battery_different_temperature = #{batteryDifferentTemperature}</if>
            <if test="maxInternalResistance != null "> and max_internal_resistance = #{maxInternalResistance}</if>
            <if test="minInternalResistance != null "> and min_internal_resistance = #{minInternalResistance}</if>
            <if test="avgInternalResistance != null "> and avg_internal_resistance = #{avgInternalResistance}</if>
            <if test="batteryDefferentResistance != null "> and battery_defferent_resistance = #{batteryDefferentResistance}</if>
            <if test="maxResistanceCellId != null "> and max_resistance_cell_id = #{maxResistanceCellId}</if>
            <if test="minResistanceCellId != null "> and min_resistance_cell_id = #{minResistanceCellId}</if>
            <if test="environmentTemperature != null "> and environment_temperature = #{environmentTemperature}</if>
            <if test="environmentHumidity != null "> and environment_humidity = #{environmentHumidity}</if>
            <if test="circuitBreakerStatus != null  and circuitBreakerStatus != ''"> and circuit_breaker_status = #{circuitBreakerStatus}</if>
        </where>
    </select>

    <select id="selectEmsBatteryStackById" parameterType="Long" resultMap="EmsBatteryStackResult">
        <include refid="selectEmsBatteryStackVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsBatteryStack" parameterType="EmsBatteryStack" useGeneratedKeys="true" keyProperty="id">
        insert into ems_battery_stack
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dataUpdateTime != null">data_update_time,</if>
            <if test="workStatus != null">work_status,</if>
            <if test="pcsCommunicationStatus != null">pcs_communication_status,</if>
            <if test="emsCommunicationStatus != null">ems_communication_status,</if>
            <if test="operationStatus != null">operation_status,</if>
            <if test="stackVoltage != null">stack_voltage,</if>
            <if test="stackCurrent != null">stack_current,</if>
            <if test="stackSoc != null">stack_soc,</if>
            <if test="stackSoh != null">stack_soh,</if>
            <if test="maxCellVoltage != null">max_cell_voltage,</if>
            <if test="maxVoltageGroupId != null">max_voltage_group_id,</if>
            <if test="maxVoltageCellId != null">max_voltage_cell_id,</if>
            <if test="minCellVoltage != null">min_cell_voltage,</if>
            <if test="minVoltageGroupId != null">min_voltage_group_id,</if>
            <if test="minVoltageCellId != null">min_voltage_cell_id,</if>
            <if test="maxCellTemp != null">max_cell_temp,</if>
            <if test="maxTempGroupId != null">max_temp_group_id,</if>
            <if test="maxTempCellId != null">max_temp_cell_id,</if>
            <if test="minCellTemp != null">min_cell_temp,</if>
            <if test="minTempGroupId != null">min_temp_group_id,</if>
            <if test="minTempCellId != null">min_temp_cell_id,</if>
            <if test="totalChargeCapacity != null">total_charge_capacity,</if>
            <if test="totalDischargeCapacity != null">total_discharge_capacity,</if>
            <if test="sessionChargeCapacity != null">session_charge_capacity,</if>
            <if test="sessionDischargeCapacity != null">session_discharge_capacity,</if>
            <if test="availableChargeCapacity != null">available_charge_capacity,</if>
            <if test="availableDischargeCapacity != null">available_discharge_capacity,</if>
            <if test="remainingDischargeTime != null">remaining_discharge_time,</if>
            <if test="remainingChargeTime != null">remaining_charge_time,</if>
            <if test="maxDischargePower != null">max_discharge_power,</if>
            <if test="maxChargePower != null">max_charge_power,</if>
            <if test="maxDischargeCurrent != null">max_discharge_current,</if>
            <if test="maxChargeCurrent != null">max_charge_current,</if>
            <if test="dailyDischargeCycles != null">daily_discharge_cycles,</if>
            <if test="dailyChargeCycles != null">daily_charge_cycles,</if>
            <if test="dailyDischargeCapacity != null">daily_discharge_capacity,</if>
            <if test="dailyChargeCapacity != null">daily_charge_capacity,</if>
            <if test="operatingTemp != null">operating_temp,</if>
            <if test="bmsStatus != null">bms_status,</if>
            <if test="bmsChargeStatus != null">bms_charge_status,</if>
            <if test="stackInsulationResistance != null">stack_insulation_resistance,</if>
            <if test="siteId != null and siteId != ''">site_id,</if>
            <if test="deviceId != null and deviceId != ''">device_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="batteryNumber != null">battery_number,</if>
            <if test="batteryAvgVoltage != null">battery_avg_voltage,</if>
            <if test="batteryDifferentPressure != null">battery_different_pressure,</if>
            <if test="avgTemperature != null">avg_temperature,</if>
            <if test="batteryDifferentTemperature != null">battery_different_temperature,</if>
            <if test="maxInternalResistance != null">max_internal_resistance,</if>
            <if test="minInternalResistance != null">min_internal_resistance,</if>
            <if test="avgInternalResistance != null">avg_internal_resistance,</if>
            <if test="batteryDefferentResistance != null">battery_defferent_resistance,</if>
            <if test="maxResistanceCellId != null">max_resistance_cell_id,</if>
            <if test="minResistanceCellId != null">min_resistance_cell_id,</if>
            <if test="environmentTemperature != null">environment_temperature,</if>
            <if test="environmentHumidity != null">environment_humidity,</if>
            <if test="circuitBreakerStatus != null">circuit_breaker_status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dataUpdateTime != null">#{dataUpdateTime},</if>
            <if test="workStatus != null">#{workStatus},</if>
            <if test="pcsCommunicationStatus != null">#{pcsCommunicationStatus},</if>
            <if test="emsCommunicationStatus != null">#{emsCommunicationStatus},</if>
            <if test="operationStatus != null">#{operationStatus},</if>
            <if test="stackVoltage != null">#{stackVoltage},</if>
            <if test="stackCurrent != null">#{stackCurrent},</if>
            <if test="stackSoc != null">#{stackSoc},</if>
            <if test="stackSoh != null">#{stackSoh},</if>
            <if test="maxCellVoltage != null">#{maxCellVoltage},</if>
            <if test="maxVoltageGroupId != null">#{maxVoltageGroupId},</if>
            <if test="maxVoltageCellId != null">#{maxVoltageCellId},</if>
            <if test="minCellVoltage != null">#{minCellVoltage},</if>
            <if test="minVoltageGroupId != null">#{minVoltageGroupId},</if>
            <if test="minVoltageCellId != null">#{minVoltageCellId},</if>
            <if test="maxCellTemp != null">#{maxCellTemp},</if>
            <if test="maxTempGroupId != null">#{maxTempGroupId},</if>
            <if test="maxTempCellId != null">#{maxTempCellId},</if>
            <if test="minCellTemp != null">#{minCellTemp},</if>
            <if test="minTempGroupId != null">#{minTempGroupId},</if>
            <if test="minTempCellId != null">#{minTempCellId},</if>
            <if test="totalChargeCapacity != null">#{totalChargeCapacity},</if>
            <if test="totalDischargeCapacity != null">#{totalDischargeCapacity},</if>
            <if test="sessionChargeCapacity != null">#{sessionChargeCapacity},</if>
            <if test="sessionDischargeCapacity != null">#{sessionDischargeCapacity},</if>
            <if test="availableChargeCapacity != null">#{availableChargeCapacity},</if>
            <if test="availableDischargeCapacity != null">#{availableDischargeCapacity},</if>
            <if test="remainingDischargeTime != null">#{remainingDischargeTime},</if>
            <if test="remainingChargeTime != null">#{remainingChargeTime},</if>
            <if test="maxDischargePower != null">#{maxDischargePower},</if>
            <if test="maxChargePower != null">#{maxChargePower},</if>
            <if test="maxDischargeCurrent != null">#{maxDischargeCurrent},</if>
            <if test="maxChargeCurrent != null">#{maxChargeCurrent},</if>
            <if test="dailyDischargeCycles != null">#{dailyDischargeCycles},</if>
            <if test="dailyChargeCycles != null">#{dailyChargeCycles},</if>
            <if test="dailyDischargeCapacity != null">#{dailyDischargeCapacity},</if>
            <if test="dailyChargeCapacity != null">#{dailyChargeCapacity},</if>
            <if test="operatingTemp != null">#{operatingTemp},</if>
            <if test="bmsStatus != null">#{bmsStatus},</if>
            <if test="bmsChargeStatus != null">#{bmsChargeStatus},</if>
            <if test="stackInsulationResistance != null">#{stackInsulationResistance},</if>
            <if test="siteId != null and siteId != ''">#{siteId},</if>
            <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="batteryNumber != null">#{batteryNumber},</if>
            <if test="batteryAvgVoltage != null">#{batteryAvgVoltage},</if>
            <if test="batteryDifferentPressure != null">#{batteryDifferentPressure},</if>
            <if test="avgTemperature != null">#{avgTemperature},</if>
            <if test="batteryDifferentTemperature != null">#{batteryDifferentTemperature},</if>
            <if test="maxInternalResistance != null">#{maxInternalResistance},</if>
            <if test="minInternalResistance != null">#{minInternalResistance},</if>
            <if test="avgInternalResistance != null">#{avgInternalResistance},</if>
            <if test="batteryDefferentResistance != null">#{batteryDefferentResistance},</if>
            <if test="maxResistanceCellId != null">#{maxResistanceCellId},</if>
            <if test="minResistanceCellId != null">#{minResistanceCellId},</if>
            <if test="environmentTemperature != null">#{environmentTemperature},</if>
            <if test="environmentHumidity != null">#{environmentHumidity},</if>
            <if test="circuitBreakerStatus != null">#{circuitBreakerStatus},</if>
        </trim>
    </insert>

    <update id="updateEmsBatteryStack" parameterType="EmsBatteryStack">
        update ems_battery_stack
        <trim prefix="SET" suffixOverrides=",">
            <if test="dataUpdateTime != null">data_update_time = #{dataUpdateTime},</if>
            <if test="workStatus != null">work_status = #{workStatus},</if>
            <if test="pcsCommunicationStatus != null">pcs_communication_status = #{pcsCommunicationStatus},</if>
            <if test="emsCommunicationStatus != null">ems_communication_status = #{emsCommunicationStatus},</if>
            <if test="operationStatus != null">operation_status = #{operationStatus},</if>
            <if test="stackVoltage != null">stack_voltage = #{stackVoltage},</if>
            <if test="stackCurrent != null">stack_current = #{stackCurrent},</if>
            <if test="stackSoc != null">stack_soc = #{stackSoc},</if>
            <if test="stackSoh != null">stack_soh = #{stackSoh},</if>
            <if test="maxCellVoltage != null">max_cell_voltage = #{maxCellVoltage},</if>
            <if test="maxVoltageGroupId != null">max_voltage_group_id = #{maxVoltageGroupId},</if>
            <if test="maxVoltageCellId != null">max_voltage_cell_id = #{maxVoltageCellId},</if>
            <if test="minCellVoltage != null">min_cell_voltage = #{minCellVoltage},</if>
            <if test="minVoltageGroupId != null">min_voltage_group_id = #{minVoltageGroupId},</if>
            <if test="minVoltageCellId != null">min_voltage_cell_id = #{minVoltageCellId},</if>
            <if test="maxCellTemp != null">max_cell_temp = #{maxCellTemp},</if>
            <if test="maxTempGroupId != null">max_temp_group_id = #{maxTempGroupId},</if>
            <if test="maxTempCellId != null">max_temp_cell_id = #{maxTempCellId},</if>
            <if test="minCellTemp != null">min_cell_temp = #{minCellTemp},</if>
            <if test="minTempGroupId != null">min_temp_group_id = #{minTempGroupId},</if>
            <if test="minTempCellId != null">min_temp_cell_id = #{minTempCellId},</if>
            <if test="totalChargeCapacity != null">total_charge_capacity = #{totalChargeCapacity},</if>
            <if test="totalDischargeCapacity != null">total_discharge_capacity = #{totalDischargeCapacity},</if>
            <if test="sessionChargeCapacity != null">session_charge_capacity = #{sessionChargeCapacity},</if>
            <if test="sessionDischargeCapacity != null">session_discharge_capacity = #{sessionDischargeCapacity},</if>
            <if test="availableChargeCapacity != null">available_charge_capacity = #{availableChargeCapacity},</if>
            <if test="availableDischargeCapacity != null">available_discharge_capacity = #{availableDischargeCapacity},</if>
            <if test="remainingDischargeTime != null">remaining_discharge_time = #{remainingDischargeTime},</if>
            <if test="remainingChargeTime != null">remaining_charge_time = #{remainingChargeTime},</if>
            <if test="maxDischargePower != null">max_discharge_power = #{maxDischargePower},</if>
            <if test="maxChargePower != null">max_charge_power = #{maxChargePower},</if>
            <if test="maxDischargeCurrent != null">max_discharge_current = #{maxDischargeCurrent},</if>
            <if test="maxChargeCurrent != null">max_charge_current = #{maxChargeCurrent},</if>
            <if test="dailyDischargeCycles != null">daily_discharge_cycles = #{dailyDischargeCycles},</if>
            <if test="dailyChargeCycles != null">daily_charge_cycles = #{dailyChargeCycles},</if>
            <if test="dailyDischargeCapacity != null">daily_discharge_capacity = #{dailyDischargeCapacity},</if>
            <if test="dailyChargeCapacity != null">daily_charge_capacity = #{dailyChargeCapacity},</if>
            <if test="operatingTemp != null">operating_temp = #{operatingTemp},</if>
            <if test="bmsStatus != null">bms_status = #{bmsStatus},</if>
            <if test="bmsChargeStatus != null">bms_charge_status = #{bmsChargeStatus},</if>
            <if test="stackInsulationResistance != null">stack_insulation_resistance = #{stackInsulationResistance},</if>
            <if test="siteId != null and siteId != ''">site_id = #{siteId},</if>
            <if test="deviceId != null and deviceId != ''">device_id = #{deviceId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="batteryNumber != null">battery_number = #{batteryNumber},</if>
            <if test="batteryAvgVoltage != null">battery_avg_voltage = #{batteryAvgVoltage},</if>
            <if test="batteryDifferentPressure != null">battery_different_pressure = #{batteryDifferentPressure},</if>
            <if test="avgTemperature != null">avg_temperature = #{avgTemperature},</if>
            <if test="batteryDifferentTemperature != null">battery_different_temperature = #{batteryDifferentTemperature},</if>
            <if test="maxInternalResistance != null">max_internal_resistance = #{maxInternalResistance},</if>
            <if test="minInternalResistance != null">min_internal_resistance = #{minInternalResistance},</if>
            <if test="avgInternalResistance != null">avg_internal_resistance = #{avgInternalResistance},</if>
            <if test="batteryDefferentResistance != null">battery_defferent_resistance = #{batteryDefferentResistance},</if>
            <if test="maxResistanceCellId != null">max_resistance_cell_id = #{maxResistanceCellId},</if>
            <if test="minResistanceCellId != null">min_resistance_cell_id = #{minResistanceCellId},</if>
            <if test="environmentTemperature != null">environment_temperature = #{environmentTemperature},</if>
            <if test="environmentHumidity != null">environment_humidity = #{environmentHumidity},</if>
            <if test="circuitBreakerStatus != null">circuit_breaker_status = #{circuitBreakerStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsBatteryStackById" parameterType="Long">
        delete from ems_battery_stack where id = #{id}
    </delete>

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

    <select id="getBatteryAveTempList" resultType="com.xzzn.ems.domain.vo.BatteryAveTempVo">
        WITH ranked AS (
        SELECT
        *,
        date_format(create_time, '%Y-%m-%d %H:00:00') + INTERVAL FLOOR(MINUTE(create_time) / 5) * 5 MINUTE AS group_time,
        ROW_NUMBER() OVER (PARTITION BY date_format(create_time, '%Y-%m-%d %H:00:00'), FLOOR(MINUTE(create_time) / 5) ORDER BY create_time) as rn
        FROM
        ems_battery_stack
        where site_id = #{siteId}
        and create_time &gt;= #{startDate}
        and create_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        and avg_temperature is not null
        )
        SELECT
        site_id,
        round(avg(avg_temperature)) as batteryTemp,
        DATE_FORMAT(group_time, '%Y-%m-%d') as dateDay,
        DATE_FORMAT(group_time, '%H:%i:00') as createDate,
        group_time as groupTime
        FROM
        ranked
        GROUP BY site_id,groupTime
    </select>

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

    <sql id="dataTypeChooseFilter">
        <choose>
            <when test='dataType == "1"'>
                t.operating_temp AS temp
            </when>
            <when test='dataType == "2"'>
                t.stack_voltage as voltage
            </when>
            <when test='dataType == "3"'>
                t.stack_current as current
            </when>
            <when test='dataType == "4"'>
                t.stack_soc as soc
            </when>
            <otherwise>
                0 AS temp
            </otherwise>
        </choose>
    </sql>

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

    <select id="getStackDataByDay" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.StackStatisListVo">
        select  latest.dateDay as statisDate,
                t.device_id as deviceId,
        <include refid="dataTypeChooseFilter"/>
        from  ( SELECT p.site_id,  p.device_id,DATE_FORMAT(p.update_time, '%Y-%m-%d') AS dateDay,MAX(p.update_time) as max_update_time
        FROM ems_battery_stack p
        <include refid="statisCommonFilter"/>
        GROUP BY p.site_id,p.device_id,dateDay
        ) latest inner join ems_battery_stack t ON latest.site_id = t.site_id
        AND latest.device_id = t.device_id
        AND latest.max_update_time = t.update_time
        order by statisDate desc
    </select>

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

    <select id="getSiteSumStackInfo" parameterType="String" resultType="com.xzzn.ems.domain.EmsBatteryStack">
        select  AVG(t.stack_soc) as stackSoc,
                AVG(t.stack_soh) as stackSoh
        from ems_battery_stack t
                 INNER JOIN (
            select p.site_id,p.device_id,MAX(p.create_time) as max_time
            from ems_battery_stack p
            where p.site_id = #{siteId}
            group by p.site_id, p.device_id
        ) latest on t.site_id = latest.site_id
            and t.device_id = latest.device_id
            and t.create_time = latest.max_time
        where t.site_id = #{siteId}
    </select>

    <select id="getStackPointByHour" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.StackPointVo">
        select latest.dateHour as statisDate,
                avg(t.stack_soc) as avgSoc,
                avg(t.stack_soh) as avgSoh,
                avg(t.avg_temperature) as avgTemp
        from  ( SELECT p.site_id,  p.device_id, DATE_FORMAT(p.update_time, '%Y-%m-%d %H:00') AS dateHour,MAX(p.update_time) as max_update_time
                FROM ems_battery_stack p
                <include refid="statisCommonFilter"/>
        GROUP BY p.site_id,p.device_id,dateHour
        ) latest inner join ems_battery_stack t ON latest.site_id = t.site_id
        AND latest.device_id = t.device_id
        AND latest.max_update_time = t.update_time
        GROUP BY latest.dateHour
        ORDER BY statisDate
    </select>

    <select id="getStackPointByDay" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.StackPointVo">
            select latest.dateDay as statisDate,
                avg(t.stack_soc) as avgSoc,
                avg(t.stack_soh) as avgSoh,
                avg(t.avg_temperature) as avgTemp
            from  ( SELECT p.site_id,  p.device_id, DATE(p.update_time) AS dateDay,MAX(p.update_time) as max_update_time
                FROM ems_battery_stack p
                <include refid="statisCommonFilter"/>
                GROUP BY p.site_id,p.device_id,dateDay
            ) latest inner join ems_battery_stack t ON latest.site_id = t.site_id
            AND latest.device_id = t.device_id
            AND latest.max_update_time = t.update_time
            GROUP BY latest.dateDay
            ORDER BY statisDate
    </select>

    <select id="getStackPointByMonth" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.StackPointVo">
        select latest.dateMonth as statisDate,
                avg(t.stack_soc) as avgSoc,
                avg(t.stack_soh) as avgSoh,
                avg(t.avg_temperature) as avgTemp
        from  ( SELECT p.site_id,  p.device_id, DATE_FORMAT(p.update_time, '%Y-%m') AS dateMonth,MAX(p.update_time) as max_update_time
                FROM ems_battery_stack p
                <include refid="statisCommonFilter"/>
                GROUP BY p.site_id,p.device_id,dateMonth
        ) latest inner join ems_battery_stack t ON latest.site_id = t.site_id
        AND latest.device_id = t.device_id
        AND latest.max_update_time = t.update_time
        GROUP BY latest.dateMonth
        ORDER BY statisDate
    </select>

    <select id="getStackPointByMinute" parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.StackPointVo">
        WITH ranked AS (
        SELECT
        p.site_id,
        p.device_id,
        p.stack_soc,
        p.stack_soh,
        p.avg_temperature,
        FROM_UNIXTIME(((UNIX_TIMESTAMP(p.update_time) + 299) DIV 300) * 300) AS group_time,
        ROW_NUMBER() OVER (
        PARTITION BY p.device_id, ((UNIX_TIMESTAMP(p.update_time) + 299) DIV 300)
        ORDER BY p.data_update_time DESC
        ) as rn
        FROM
        ems_battery_stack p
        <include refid="statisCommonFilter"/>
        )
        SELECT
        t.site_id,
        DATE_FORMAT(t.group_time, '%Y-%m-%d %H:%i:%s') as statisDate,
        avg(t.stack_soc) as avgSoc,
        avg(t.stack_soh) as avgSoh,
        avg(t.avg_temperature) as avgTemp
        FROM
        ranked as t
        where t.rn = 1
        GROUP BY t.site_id, t.group_time
    </select>

    <select id="getStackDataByMinute"  parameterType="com.xzzn.ems.domain.vo.DateSearchRequest" resultType="com.xzzn.ems.domain.vo.StackStatisListVo">
        WITH ranked AS (
        SELECT
        p.site_id,
        p.device_id,
        p.stack_soc,
        p.stack_soh,
        p.avg_temperature,
        p.operating_temp,
        p.stack_current,
        p.stack_voltage,
        p.data_update_time,
        FROM_UNIXTIME(((UNIX_TIMESTAMP(p.update_time) + 299) DIV 300) * 300) AS group_time,
        ROW_NUMBER() OVER (
        PARTITION BY p.device_id, ((UNIX_TIMESTAMP(p.update_time) + 299) DIV 300)
        ORDER BY p.data_update_time DESC
        ) as rn
        FROM
        ems_battery_stack p
        <include refid="statisCommonFilter"/>
        )
        SELECT
        t.site_id,
        t.device_id as deviceId,
        DATE_FORMAT(t.group_time, '%Y-%m-%d %H:%i:%s') as statisDate,
        <include refid="dataTypeChooseFilter"/>
        FROM
        ranked as t
        where t.rn = 1
    </select>

    <select id="getAveSocList" resultType="com.xzzn.ems.domain.vo.BatteryAveSOCVo">
        WITH ranked AS (
        SELECT
        *,
        date_format(create_time, '%Y-%m-%d %H:00:00') + INTERVAL FLOOR(MINUTE(create_time) / 5) * 5 MINUTE AS group_time,
        ROW_NUMBER() OVER (PARTITION BY date_format(create_time, '%Y-%m-%d %H:00:00'), FLOOR(MINUTE(create_time) / 5) ORDER BY create_time) as rn
        FROM
        ems_battery_stack
        where site_id = #{siteId}
        and create_time &gt;= #{startDate}
        and create_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        )
        SELECT
        site_id,
        round(avg(stack_soc)) as batterySOC,
        DATE_FORMAT(group_time, '%Y-%m-%d') as dateDay,
        DATE_FORMAT(group_time, '%H:%i:00') as createDate,
        group_time as groupTime
        FROM
        ranked
        GROUP BY site_id,groupTime
    </select>
</mapper>
