<?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.xhpc.activity.mapper.XhpcChargingPileMapper">

    <resultMap id="BaseResultMap" type="com.xhpc.common.domain.XhpcChargingPile">
        <result property="chargingPileId" column="charging_pile_id"/>
        <result property="chargingStationId" column="charging_station_id"/>
        <result property="name" column="name"/>
        <result property="nationalStandard" column="national_standard"/>
        <result property="power" column="power"/>
        <result property="auxiliaryPowerSupply" column="auxiliary_power_supply"/>
        <result property="inputVoltage" column="input_voltage"/>
        <result property="maxVoltage" column="max_voltage"/>
        <result property="minVoltage" column="min_voltage"/>
        <result property="maxElectricCurrent" column="max_electric_current"/>
        <result property="minElectriCurrent" column="min_electric_current"/>
        <result property="serialNumber" column="serial_number"/>
        <result property="type" column="type"/>
        <result property="programVersion" column="program_version"/>
        <result property="networkLinkType" column="network_link_type"/>
        <result property="gunNumber" column="gun_number"/>
        <result property="communicationProtocolVersion" column="communication_protocol_version"/>
        <result property="communicationOperator" column="communication_operator"/>
        <result property="simCard" column="sim_card"/>
        <result property="rateModelId" column="rate_model_id"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <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="remark" column="remark"/>
        <result property="brandModel" column="brand_model"/>
    </resultMap>
    <sql id="selectXhpcChargingPileVo">
        select charging_pile_id,
               charging_station_id,
               name,
               national_standard,
               power,
               auxiliary_power_supply,
               input_voltage,
               max_voltage,
               min_voltage,
               max_electric_current,
               min_electric_current,
               serial_number,
               type,
               program_version,
               network_link_type,
               gun_number,
               communication_protocol_version,
               communication_operator,
               sim_card,
               rate_model_id,
               status,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark,
               rate_model_id,
               brand_model,
               production_date productionDate,
               manufacture_name manufactureName,
               connector_type connectorType,
               current,
               equipment_type equipmentType
        from xhpc_charging_pile
    </sql>
    <select id="selectXhpcChargingPileList" resultType="java.util.Map">
        select
        cp.charging_pile_id as chargingPileId,
        cp.charging_station_id as chargingStationId,
        concat(cp.name,'号桩') as chargingPileName,
        st.name as chargingStationName,
        cp.serial_number as serialNumber,
        cp.brand_model as brandModel,
        cp.type as type,
        cp.power as power,
        cp.gun_number as gunNumber,
        cp.status as status
        from xhpc_charging_pile as cp
        left join xhpc_charging_station as st on st.charging_station_id =cp.charging_station_id
        where cp.del_flag =0
<!--        <if test="name !=null and name !=''">-->
<!--            and cp.name like CONCAT('%',#{name},'%')-->
<!--        </if>-->
<!--        <if test="type !=null and type !=''">-->
<!--            and cp.type=#{type}-->
<!--        </if>-->
<!--        <if test="serialNumber !=null and serialNumber!=''">-->
<!--            and cp.serial_number=#{serialNumber}-->
<!--        </if>-->
<!--        <if test="chargingStationId !=null and chargingStationId!=''">-->
<!--            and cp.charging_station_id=#{chargingStationId}-->
<!--        </if>-->
          <if test="params.pileSerialNumber!=null and params.pileSerialNumber!=''">
            and  cp.serial_number = #{params.pileSerialNumber}
          </if>
        <if test="params.pileName!=null and params.pileName!=''">
            and concat(cp.name,'号桩') like concat ('%', #{params.pileName},'%')
        </if>
          <if test="params.stationName!=null and params.stationName!=''">
              and st.name like CONCAT('%',#{params.stationName},'%')
          </if>
        <if test="params.number !=0 and params.number ==1">
            and cp.charging_station_id in(select charging_station_id from xhpc_charging_station where operator_id=#{params.operatorId})
        </if>
        <if test="params.number !=0 and params.number ==2">
            and cp.charging_station_id in(select charging_station_id from xhpc_user_privilege where user_id=#{params.operatorId})
        </if>
        <if test="params.tenantId != null and params.tenantId != ''">
            and cp.tenant_id = #{params.tenantId}
        </if>
        order by cp.charging_station_id, cp.charging_pile_id
    </select>

    <insert id="addXhpcChargingPile" parameterType="com.xhpc.common.domain.XhpcChargingPile" useGeneratedKeys="true"
            keyProperty="chargingPileId">
        insert into xhpc_charging_pile
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != chargingStationId ">
                charging_station_id,
            </if>
            <if test="null != name ">
                name,
            </if>
            <if test="null != nationalStandard ">
                national_standard,
            </if>
            <if test="null != power ">
                power,
            </if>
            <if test="null != auxiliaryPowerSupply ">
                auxiliary_power_supply,
            </if>
            <if test="null != inputVoltage ">
                input_voltage,
            </if>
            <if test="null != maxVoltage ">
                max_voltage,
            </if>
            <if test="null != minVoltage ">
                min_voltage,
            </if>
            <if test="null != maxElectricCurrent ">
                max_electric_current,
            </if>
            <if test="null != minElectriCurrent ">
                min_electric_current,
            </if>
            <if test="null != serialNumber ">
                serial_number,
            </if>
            <if test="null != type ">
                type,
            </if>
            <if test="null != programVersion ">
                program_version,
            </if>
            <if test="null != networkLinkType ">
                network_link_type,
            </if>
            <if test="null != gunNumber ">
                gun_number,
            </if>
            <if test="null != communicationProtocolVersion ">
                communication_protocol_version,
            </if>
            <if test="null != communicationOperator ">
                communication_operator,
            </if>
            <if test="null != simCard ">
                sim_card,
            </if>
            <if test="null != rateModelId ">
                rate_model_id,
            </if>
            <if test="null != status ">
                status,
            </if>
            <if test="null != delFlag ">
                del_flag,
            </if>
            <if test="null != createTime ">
                create_time,
            </if>
            <if test="null != createBy and '' != createBy">
                create_by,
            </if>
            <if test="null != updateTime ">
                update_time,
            </if>
            <if test="null != updateBy and '' != updateBy">
                update_by,
            </if>
            <if test="null != remark and '' != remark">
                remark,
            </if>
            <if test="null != brandModel ">
                brand_model,
            </if>
            <if test="null != productionDate ">
                production_date,
            </if>
            <if test="null != manufactureName ">
                manufacture_name,
            </if>
            <if test="null != connectorType ">
                connector_type,
            </if>
            <if test="null != current ">
                current,
            </if>
            <if test="null != equipmentType ">
                equipment_type
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != chargingStationId ">
                #{chargingStationId},
            </if>
            <if test="null != name ">
                #{name},
            </if>
            <if test="null != nationalStandard ">
                #{nationalStandard},
            </if>
            <if test="null != power ">
                #{power},
            </if>
            <if test="null != auxiliaryPowerSupply ">
                #{auxiliaryPowerSupply},
            </if>
            <if test="null != inputVoltage ">
                #{inputVoltage},
            </if>
            <if test="null != maxVoltage ">
                #{maxVoltage},
            </if>
            <if test="null != minVoltage ">
                #{minVoltage},
            </if>
            <if test="null != maxElectricCurrent ">
                #{maxElectricCurrent},
            </if>
            <if test="null != minElectriCurrent ">
                #{minElectriCurrent},
            </if>
            <if test="null != serialNumber ">
                #{serialNumber},
            </if>
            <if test="null != type ">
                #{type},
            </if>
            <if test="null != programVersion ">
                #{programVersion},
            </if>
            <if test="null != networkLinkType ">
                #{networkLinkType},
            </if>
            <if test="null != gunNumber ">
                #{gunNumber},
            </if>
            <if test="null != communicationProtocolVersion ">
                #{communicationProtocolVersion},
            </if>
            <if test="null != communicationOperator ">
                #{communicationOperator},
            </if>
            <if test="null != simCard ">
                #{simCard},
            </if>
            <if test="null != rateModelId ">
                #{rateModelId},
            </if>
            <if test="null != status ">
                #{status},
            </if>
            <if test="null != delFlag ">
                #{delFlag},
            </if>
            <if test="null != createTime ">
                #{createTime},
            </if>
            <if test="null != createBy and '' != createBy">
                #{createBy},
            </if>
            <if test="null != updateTime ">
                #{updateTime},
            </if>
            <if test="null != updateBy and '' != updateBy">
                #{updateBy},
            </if>
            <if test="null != remark and '' != remark">
                #{remark},
            </if>
            <if test="null != brandModel ">
                #{brandModel},
            </if>
            <if test="null != productionDate ">
                #{productionDate},
            </if>
            <if test="null != manufactureName ">
                #{manufactureName},
            </if>
            <if test="null != connectorType ">
                #{connectorType},
            </if>
            <if test="null != current ">
                #{current},
            </if>
            <if test="null != equipmentType ">
                #{equipmentType}
            </if>
        </trim>
    </insert>

    <select id="selectXhpcChargingStationById" resultType="java.util.Map">
        select charging_station_id as chargingStationId,
               rate_model_id       as rateModelId
        from xhpc_charging_station
        where charging_station_id = #{chargingStationId}
    </select>

    <select id="selectXhpcChargingPileById" resultMap="BaseResultMap">
        select
            charging_pile_id,
            charging_station_id,
            name,
            national_standard,
            power,
            auxiliary_power_supply,
            input_voltage,
            max_voltage,
            min_voltage,
            max_electric_current,
            min_electric_current,
            serial_number,
            type,
            program_version,
            network_link_type,
            gun_number,
            communication_protocol_version,
            communication_operator,
            sim_card,
            rate_model_id,
            status,
            del_flag,
            create_time,
            create_by,
            update_time,
            update_by,
            remark,
            rate_model_id,
            brand_model,
            production_date productionDate,
            manufacture_name manufactureName,
            connector_type connectorType,
            current,
            equipment_type equipmentType
        from xhpc_charging_pile
        where charging_pile_id = #{chargingPileId}
    </select>


    <select id="selectXhpcTerminalList" resultType="java.util.Map">
        select
        te.terminal_id as terminalId,
        te.charging_pile_id as chargingPileId,
        te.charging_station_id as chargingStationId,
        te.name as terminalName,
        ct.name as chargingStationName,
        te.pile_serial_number as pileSerialNumber,
        cp.power as power,
        te.serial_number as serialNumber,
        cp.max_voltage as maxVoltage,
        cp.serial_number as serialNumber,
        cp.brand_model as brandModel,
        cp.type as pileType,
        te.status as status,
        te.work_status as workStatus
        from xhpc_terminal as te
        left join xhpc_charging_station as ct on ct.charging_station_id = te.charging_station_id
        left join xhpc_charging_pile as cp on cp.charging_pile_id = te.charging_pile_id
        where te.del_flag=0
        <if test="chargingPileId !=null">
            and te.charging_pile_id=#{chargingPileId}
        </if>
    </select>

    <select id="getXhpcChargingPileBySerialNumber" resultType="com.xhpc.common.domain.XhpcChargingPile">
        select
            charging_pile_id,
            charging_station_id,
            name,
            national_standard,
            power,
            auxiliary_power_supply,
            input_voltage,
            max_voltage,
            min_voltage,
            max_electric_current,
            min_electric_current,
            serial_number,
            type,
            program_version,
            network_link_type,
            gun_number,
            communication_protocol_version,
            communication_operator,
            sim_card,
            rate_model_id,
            status,
            del_flag,
            create_time,
            create_by,
            update_time,
            update_by,
            remark,
            rate_model_id,
            brand_model,
            production_date productionDate,
            manufacture_name manufactureName,
            connector_type connectorType,
            current,
            equipment_type equipmentType
        from xhpc_charging_pile
        where serial_number = #{serialNumber}
    </select>



    <select id="selectByStationId" resultType="com.xhpc.common.domain.XhpcChargingPile">
        select
            charging_pile_id,
            charging_station_id,
            name,
            national_standard,
            power,
            auxiliary_power_supply,
            input_voltage,
            max_voltage,
            min_voltage,
            max_electric_current,
            min_electric_current,
            serial_number,
            type,
            program_version,
            network_link_type,
            gun_number,
            communication_protocol_version,
            communication_operator,
            sim_card,
            rate_model_id,
            status,
            del_flag,
            create_time,
            create_by,
            update_time,
            update_by,
            remark,
            rate_model_id,
            brand_model,
            production_date productionDate,
            manufacture_name manufactureName,
            connector_type connectorType,
            current,
            equipment_type equipmentType
        from xhpc_charging_pile
        where charging_station_id = #{stationId} and del_flag=0
    </select>
</mapper>
