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

    <resultMap id="BaseResultMap" type="com.xhpc.common.domain.XhpcTerminal">
        <result property="terminalId" column="terminal_id"/>
        <result property="chargingPileId" column="charging_pile_id"/>
        <result property="chargingStationId" column="charging_station_id"/>
        <result property="name" column="name"/>
        <result property="serialNumber" column="serial_number"/>
        <result property="pileSerialNumber" column="pile_serial_number"/>
        <result property="gunStatus" column="gun_status"/>
        <result property="workStatus" column="work_status"/>
        <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="rateModelId" column="rate_model_id"/>
        <result property="operatorIdEvcs" column="operator_id_evcs"/>
        <result property="connectorType" column="connector_type" />

    </resultMap>

    <sql id="selectXhpcTerminalVo">
        select terminal_id,
               charging_pile_id,
               charging_station_id,
               name,
               serial_number,
               pile_serial_number,
               gun_status,
               work_status,
               status,
               del_flag,
               create_time,
               create_by,
               update_time,
               update_by,
               remark,
               connector_type
        from xhpc_terminal
    </sql>

    <select id="countXhpcTerminalWorkStatus" resultType="map">
        select (select count(terminal_id)
                from xhpc_terminal
                where status = 0
                  and del_flag = 0
                  and work_status = 0
                  and charging_station_id = #{chargingStationId}) offLine,
               (select count(terminal_id)
                from xhpc_terminal
                where status = 0
                  and del_flag = 0
                  and work_status = 1
                  and charging_station_id = #{chargingStationId}) fault,
               (select count(terminal_id)
                from xhpc_terminal
                where status = 0
                  and del_flag = 0
                  and work_status = 2
                  and charging_station_id = #{chargingStationId}) freeTime,
               (select count(terminal_id)
                from xhpc_terminal
                where status = 0
                  and del_flag = 0
                  and work_status = 3
                  and charging_station_id = #{chargingStationId}) charge
        from xhpc_terminal LIMIT 1
    </select>

    <select id="getXhpcTerminalList" resultType="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.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="chargingStationId !=null">
            and te.charging_station_id=#{chargingStationId}
        </if>
        <if test="serialNumber !=null and serialNumber !=''">
            and te.serial_number like CONCAT('%',#{serialNumber},'%')
        </if>
        <if test="type !=null ">
            and cp.type=#{type}
        </if>
        <if test="status !=null ">
            and cp.status=#{status}
        </if>
        <if test="workStatus !=null and workStatus !=''">
            and te.work_status=#{chargingStationId}
        </if>
        <if test="tenantId !=null and tenantId !=''">
            and te.tenant_id=#{tenantId}
        </if>
    </select>


    <select id="selectXhpcTerminalById" resultMap="BaseResultMap">
        <include refid="selectXhpcTerminalVo"/>
        where terminal_id = #{terminalId}
    </select>

    <update id="updateXhpcTerminal">
        update xhpc_terminal
        <set>
            <if test="name != null and name !=''">
                name = #{name},
            </if>
            <if test="serialNumber != null and serialNumber !=''">
                serial_number = #{serialNumber},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="connectorType != null">
                connector_type = #{connectorType},
            </if>
        </set>
        where terminal_id = #{terminalId}
    </update>

    <insert id="insertXhpcTerminal" parameterType="com.xhpc.common.domain.XhpcTerminal" useGeneratedKeys="true"
            keyProperty="terminalId">
        insert into xhpc_terminal
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="null != chargingPileId ">
                charging_pile_id,
            </if>
            <if test="null != chargingStationId ">
                charging_station_id,
            </if>
            <if test="null != name ">
                name,
            </if>
            <if test="null != serialNumber ">
                serial_number,
            </if>
            <if test="null != pileSerialNumber ">
                pile_serial_number,
            </if>
            <if test="null != gunStatus ">
                gun_status,
            </if>
            <if test="null != workStatus ">
                work_status,
            </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 != rateModelId">
                rate_model_id,
            </if>
            <if test="null != number">
                number,
            </if>
            <if test="null != operatorIdEvcs">
                operator_id_evcs,
            </if>
            <if test="null != tenantId">
                tenant_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="null != chargingPileId ">
                #{chargingPileId},
            </if>
            <if test="null != chargingStationId ">
                #{chargingStationId},
            </if>
            <if test="null != name ">
                #{name},
            </if>
            <if test="null != serialNumber ">
                #{serialNumber},
            </if>
            <if test="null != pileSerialNumber ">
                #{pileSerialNumber},
            </if>
            <if test="null != gunStatus ">
                #{gunStatus},
            </if>
            <if test="null != workStatus ">
                #{workStatus},
            </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 != rateModelId">
                #{rateModelId},
            </if>
            <if test="null != number">
                #{number},
            </if>
            <if test="null != operatorIdEvcs">
                #{operatorIdEvcs},
            </if>
            <if test="null != tenantId">
                #{tenantId},
            </if>
        </trim>
    </insert>


    <select id="selectXhpcTerminalPileMessage" resultType="map">
        select ter.terminal_id           as terminalId,
               ter.charging_station_id   as chargingStationId,
               ter.name                  as name,
               ter.serial_number         as serialNumber,
               ter.gun_status            as gunStatus,
               ter.work_status           as workStatus,
               ter.status                as status,
               ct.name                   as chargingStationName,
               ter.create_time           as createTime,
               cp.serial_number          as pileSerialNumber,
               cp.type                   as type,
               cp.national_standard      as nationalStandard,
               cp.power                  as power,
               cp.auxiliary_power_supply as auxiliaryPowerSupply,
               cp.input_voltage          as inputVoltage,
               cp.max_voltage            as maxVoltage,
               cp.min_voltage            as minVoltage,
               cp.max_electric_current   as maxElectricCurrent,
               cp.min_electric_current   as minElectricCurrent,
               cp.communication_operator as communicationOperator,
               cp.sim_card               as simCard,
               cp.connector_type        as connectorType,
               cp.brand_model            as brandModel,
               db.dict_value              as attribute
        from xhpc_terminal as ter
        left join xhpc_charging_pile as cp on cp.charging_pile_id = ter.charging_pile_id
        left join xhpc_charging_station as ct on ct.charging_station_id = ter.charging_station_id
        left join xhpc_operator as op on op.operator_id = ct.operator_id
        left join xhpc_dict_biz as db on db.code='operator_attribute' and db.dict_key=op.attribute
        where ter.terminal_id = #{terminalId}
    </select>

    <select id="getXhpcRateTimeTypeList" resultType="map">
        <if test="type ==1">
            select
            rt.start_time as startTime,
            replace(rt.end_time,'00:00:00','24:00:00') AS endTime,
            rt.rate_id as rateId,
            rt.rate_value as rateValue,
            ra.power_fee as powerFee,
            ra.service_fee as serviceFee,
            ra.name as rateName
            from xhpc_rate_time as rt
            left join xhpc_rate as ra on ra.rate_id =rt.rate_id
            where rt.charging_station_id=#{chargingStationId} and rt.del_flag =0 and rt.type=1
        </if>
        <if test="type ==2">
            select
            rt.start_time as startTime,
            replace(rt.end_time,'00:00:00','24:00:00') AS endTime,
            rt.rate_id as rateId,
            ra.power_fee as powerFee,
            ra.service_fee as serviceFee,
            ra.name as rateName
            from xhpc_rate_time as rt
            left join xhpc_rate as ra on ra.rate_id =rt.rate_id
            where rt.charging_station_id=#{chargingStationId} and rt.del_flag =0 and rt.type=2
            group by type
        </if>
    </select>

    <select id="getXhpcRateTimeOrderStatistics" resultType="map">
        SELECT
            sum(charging_degree) AS chargingDegreeSum,
            COUNT(charge_order_id) AS realTimeOrderIdCount,
            COUNT(DISTINCT user_id) AS userIdCount
        FROM
            xhpc_charge_order
        WHERE
            terminal_id = #{terminalId}
        and charging_station_id=#{chargingStationId}
        AND to_days(create_time) = to_days(now())
    </select>

    <select id="getWXpNumMessage" resultType="map">
        select ter.name                  as terminalName,
               ter.serial_number         as serialNumber,
               cp.power                  as power,
               cp.type                   as type,
               cp.max_voltage            as maxVoltage,
               cp.auxiliary_power_supply as auxiliaryPowerSupply,
               ct.parking_instructions   as parkingInstructions,
               ct.create_time            as createTime,
               ter.charging_station_id   as chargingStationId
        from xhpc_terminal as ter
                 left join xhpc_charging_pile as cp on cp.charging_pile_id = ter.charging_pile_id
                 left join xhpc_charging_station as ct on ct.charging_station_id = ter.charging_station_id
        where ter.serial_number = #{pNum}
          and ter.del_flag = 0
          order by ter.number desc
          limit 1
    </select>

    <select id="getImageList" resultType="map">
        select
        img_id as imgId,
        url as url
        from xhpc_img where del_flag=0 and status=0
        <if test="imgIds !=null and imgIds.size()>0 ">
            and img_id in
            <foreach collection="imgIds" item="chargingStationId" open="(" separator="," close=")">
                #{chargingStationId}
            </foreach>
        </if>
    </select>

    <select id="getXhpcPileNameAndStationNameAndTerminalNumList" resultType="map">
        SELECT
	        terminal.serial_number AS terminal_serial_number,
	        pile.name AS pile_name,
	        station.name AS station_name
        FROM
	        xhpc_terminal AS terminal,
	        xhpc_charging_pile AS pile,
	        xhpc_charging_station AS station
        WHERE
	        terminal.charging_pile_id = pile.charging_pile_id
	        AND terminal.charging_station_id = station.charging_station_id
	        <if test="terminalSerialNumber!=null">
                And terminal.serial_number like CONCAT('%',#{terminalSerialNumber},'%')
            </if>
            <if test="pileName!=null and pileName.size()>0">
                And terminal.serial_number like CONCAT('%',#{pileName},'%')
            </if>
        <if test="stationName!=null and stationName.size()>0">
            And terminal.serial_number like CONCAT('%',#{stationName},'%')
        </if>
    </select>


    <select id="getTerminal" resultType="String">
        select serial_number as serialNumber
        from xhpc_terminal
        where charging_station_id = #{chargingStationId}
          and status = #{status}
          and del_flag = 0
    </select>
    <select id="selectXhpcTerminalIdByPileIds" resultMap="BaseResultMap">
        <include refid="selectXhpcTerminalVo"/>
        WHERE
        charging_pile_id IN
        <foreach collection="list" separator="," item="pileId" close=")" open="(">
            #{pileId}
        </foreach>
    </select>


    <select id="selectByPileId" resultMap="BaseResultMap">
        <include refid="selectXhpcTerminalVo"/>
        WHERE charging_pile_id = #{pileId} and del_flag=0
    </select>
</mapper>
