<?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.jsowell.pile.mapper.PileConnectorInfoMapper">

    <resultMap type="com.jsowell.pile.domain.PileConnectorInfo" id="PileConnectorInfoResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="pileConnectorCode"    column="pile_connector_code"    />
        <result property="status"    column="status"    />
        <result property="parkNo"    column="park_no"    />
        <result property="pileSn" column="pile_sn"    />
        <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="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectPileConnectorInfoVo">
        select id, name, pile_connector_code, status, park_no, pile_sn, create_by, create_time, update_by, update_time, del_flag from pile_connector_info
    </sql>

    <select id="selectPileConnectorInfoList" parameterType="com.jsowell.pile.domain.PileConnectorInfo"
            resultMap="PileConnectorInfoResult">
        <include refid="selectPileConnectorInfoVo"/>
        where
            del_flag = '0'
            <if test="name != null  and name != ''">
                and name like concat('%', #{name}, '%')
            </if>
            <if test="pileConnectorCode != null  and pileConnectorCode != ''">
                and pile_connector_code = #{pileConnectorCode}
            </if>
            <if test="status != null  and status != ''">
                and status = #{status}
            </if>
            <if test="pileSn != null">
                and pile_sn = #{pileSn}
            </if>
    </select>

    <select id="selectPileConnectorInfoById" parameterType="Integer" resultMap="PileConnectorInfoResult">
        <include refid="selectPileConnectorInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertPileConnectorInfo" parameterType="com.jsowell.pile.domain.PileConnectorInfo" useGeneratedKeys="true" keyProperty="id">
        insert into pile_connector_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="pileConnectorCode != null">pile_connector_code,</if>
            <if test="status != null">status,</if>
            <if test="parkNo != null">park_no,</if>
            <if test="pileSn != null">pile_sn,</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="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="pileConnectorCode != null">#{pileConnectorCode},</if>
            <if test="status != null">#{status},</if>
            <if test="parkNo != null">#{parkNo},</if>
            <if test="pileSn != null">#{pileSn},</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="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updatePileConnectorInfo" parameterType="com.jsowell.pile.domain.PileConnectorInfo">
        update pile_connector_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="pileConnectorCode != null">pile_connector_code = #{pileConnectorCode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="parkNo != null">park_no = #{parkNo},</if>
            <if test="pileSn != null">pile_sn = #{pileSn},</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="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deletePileConnectorInfoById" parameterType="Integer">
        delete from pile_connector_info where id = #{id}
    </delete>

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

    <delete id="deletePileConnectorInfoByPileSnList">
        delete from pile_connector_info where pile_sn in
        <foreach collection="list" item="pileSn" open="(" separator="," close=")">
            #{pileSn,jdbcType=VARCHAR}
        </foreach>
    </delete>

    <select id="getConnectorInfoList" resultType="com.jsowell.pile.vo.web.PileConnectorInfoVO">
        select
            id,
            code,
            status
        from pile_connector_info
        <where>
            <if test="connectorDTO.connectorCode != null and connectorDTO.connectorCode != ''">
                and code = #{connectorDTO.connectorCode,jdbcType=VARCHAR}
            </if>
        </where>
    </select>

    <insert id="batchInsertConnectorInfo">
        insert into pile_connector_info
            (name,pile_connector_code,status,park_no,pile_sn,create_by,update_by,del_flag)
        values
            <foreach collection="connectorList" item="item" separator=",">
                (
                #{item.name,jdbcType=VARCHAR},
                #{item.pileConnectorCode,jdbcType=VARCHAR},
                #{item.status,jdbcType=VARCHAR},
                #{item.parkNo,jdbcType=VARCHAR},
                #{item.pileSn,jdbcType=VARCHAR},
                #{item.createBy,jdbcType=VARCHAR},
                #{item.updateBy,jdbcType=VARCHAR},
                #{item.delFlag,jdbcType=VARCHAR}
                )
            </foreach>
    </insert>

    <select id="selectConnectorListByStationId" resultType="com.jsowell.pile.vo.web.PileConnectorInfoVO">
        SELECT
        t1.id as connectorId,
        t1.station_id as stationId,
        t1.sn as pileSn,
        t1.name as name,
        t2.park_no as parkNo,
        t2.pile_connector_code as pileConnectorCode,
        t2.status,
        t1.merchant_id as merchantId,
        t3.merchant_name as merchantName
        FROM
        pile_basic_info t1
        LEFT JOIN pile_connector_info t2 ON t1.sn = t2.pile_sn
        LEFT JOIN pile_merchant_info t3 ON t1.merchant_id = t3.id
        where t1.del_flag = '0'
        <if test="stationId != null and stationId != ''">
            and station_id = #{stationId,jdbcType=VARCHAR}
        </if>

    </select>

    <select id="getPileConnectorInfoList" resultType="com.jsowell.pile.vo.web.PileConnectorInfoVO">
        select
            t1.id as connectorId,
            t1.pile_connector_code as pileConnectorCode,
            t1.status as status,
            t1.park_no as parkNo,
            t1.pile_sn as pileSn,
            t2.station_id as stationId,
            t2.merchant_id as merchantId,
            t2.business_type as businessType,
            t4.merchant_name as merchantName,
            t3.rated_power as ratedPower,
            IF(t3.charger_pile_type = '2','3',(IF(t3.speed_type = '1','1','2'))) AS type
        from pile_connector_info t1
        join pile_basic_info t2 on t2.sn = t1.pile_sn
        join pile_model_info t3 on t3.id = t2.model_id
        join pile_merchant_info t4 on t2.merchant_id = t4.id
        where t1.del_flag = '0'
        <if test="connectorIds != null and connectorIds.size() != 0">
            and t1.id in
            <foreach collection="connectorIds" item="id" open="(" separator="," close=")">
                #{id,jdbcType=BIGINT}
            </foreach>
        </if>
        <if test="pileSns != null and pileSns.size() != 0">
            and t1.pile_sn in
            <foreach collection="pileSns" item="pileSn" open="(" separator="," close=")">
                #{pileSn,jdbcType=VARCHAR}
            </foreach>
        </if>
        <if test="connectorCodes != null and connectorCodes.size() != 0 ">
            and t1.pile_connector_code in
            <foreach collection="connectorCodes" item="connectorCode" open="(" separator="," close=")">
                #{connectorCode,jdbcType=VARCHAR}
            </foreach>
        </if>
    </select>

    <select id="getPileConnectorInfoByConnectorCode" resultType="com.jsowell.pile.vo.web.PileConnectorInfoVO">
        select
            t1.id as connectorId,
            t1.pile_connector_code as connectorCode,
            t1.status as status,
            t1.pile_sn as pileSn,
            IF(t3.charger_pile_type = '2','3',(IF(t3.speed_type = '1','1','2'))) AS type,
            t2.business_type as businessType,
            t2.station_id as stationId,
            t4.amap_flag as aMapFlag,
            t2.merchant_id as merchantId
        from pile_connector_info t1
        join pile_basic_info t2 on t2.sn = t1.pile_sn
        join pile_model_info t3 on t3.id = t2.model_id
        join pile_station_info t4 on t2.station_id = t4.id
        where t1.del_flag = '0'
        and t1.pile_connector_code = #{pileConnectorCode,jdbcType=VARCHAR}
    </select>

    <update id="updateConnectorStatus">
        update pile_connector_info
        set status = #{connectorStatus,jdbcType=VARCHAR}
        where pile_connector_code = #{pileConnectorCode,jdbcType=VARCHAR}
    </update>

    <update id="updateConnectorStatusByPileSn">
        update pile_connector_info
        set status = #{connectorStatus,jdbcType=VARCHAR}
        where pile_sn = #{pileSn,jdbcType=VARCHAR}
    </update>

    <select id="getUniAppConnectorList" resultType="com.jsowell.pile.vo.base.ConnectorInfoVO">
        SELECT
            t1.pile_connector_code as pileConnectorCode,
            t2.station_id as stationId,
            t1.STATUS as connectorStatus,
            t1.pile_sn as pileSn,
            t2.model_id as modelId,
            t3.speed_type as chargingType,
            t3.rated_power as ratedPower
        FROM
            pile_connector_info t1
        JOIN pile_basic_info t2 ON (t1.pile_sn = t2.sn AND t2.del_flag = '0')
        JOIN pile_model_info t3 ON (t2.model_id = t3.id AND t3.del_flag = '0')
        WHERE t1.del_flag = '0'
          AND t2.station_id = #{stationId,jdbcType=BIGINT}
    </select>
</mapper>