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

    <resultMap type="com.jsowell.pile.domain.ThirdpartySnRelation" id="ThirdpartySnRelationResult">
        <result property="id"    column="id"    />
        <result property="stationId"    column="station_id"    />
        <result property="pileSn"    column="pile_sn"    />
        <result property="thirdpartyType"    column="thirdparty_type"    />
        <result property="thirdpartyPileSn"    column="thirdparty_pile_sn"    />
        <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="delFlag"    column="del_flag"    />
    </resultMap>

    <sql id="selectThirdpartySnRelationVo">
        select id, station_id, pile_sn, thirdparty_type, thirdparty_pile_sn, create_time, create_by, update_time, update_by, del_flag from thirdparty_sn_relation
    </sql>

    <select id="selectThirdpartySnRelationList" parameterType="com.jsowell.pile.domain.ThirdpartySnRelation" resultMap="ThirdpartySnRelationResult">
        <include refid="selectThirdpartySnRelationVo"/>
        <where>
            <if test="pileSn != null  and pileSn != ''"> and pile_sn = #{pileSn}</if>
            <if test="thirdpartyType != null  and thirdpartyType != ''"> and thirdparty_type = #{thirdpartyType}</if>
            <if test="thirdpartyPileSn != null  and thirdpartyPileSn != ''"> and thirdparty_pile_sn = #{thirdpartyPileSn}</if>
            <if test="stationId != null  and stationId != ''"> and station_id = #{stationId}</if>
        </where>
    </select>

    <select id="selectThirdpartySnRelationById" parameterType="Long" resultMap="ThirdpartySnRelationResult">
        <include refid="selectThirdpartySnRelationVo"/>
        where id = #{id}
    </select>

    <insert id="insertThirdpartySnRelation" parameterType="com.jsowell.pile.domain.ThirdpartySnRelation" useGeneratedKeys="true" keyProperty="id">
        insert into thirdparty_sn_relation
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stationId != null">station_id,</if>
            <if test="pileSn != null">pile_sn,</if>
            <if test="thirdpartyType != null">thirdparty_type,</if>
            <if test="thirdpartyPileSn != null">thirdparty_pile_sn,</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="delFlag != null">del_flag,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stationId != null">#{stationId},</if>
            <if test="pileSn != null">#{pileSn},</if>
            <if test="thirdpartyType != null">#{thirdpartyType},</if>
            <if test="thirdpartyPileSn != null">#{thirdpartyPileSn},</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="delFlag != null">#{delFlag},</if>
        </trim>
    </insert>

    <update id="updateThirdpartySnRelation" parameterType="com.jsowell.pile.domain.ThirdpartySnRelation">
        update thirdparty_sn_relation
        <trim prefix="SET" suffixOverrides=",">
            <if test="stationId != null">station_id = #{stationId},</if>
            <if test="pileSn != null">pile_sn = #{pileSn},</if>
            <if test="thirdpartyType != null">thirdparty_type = #{thirdpartyType},</if>
            <if test="thirdpartyPileSn != null">thirdparty_pile_sn = #{thirdpartyPileSn},</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="delFlag != null">del_flag = #{delFlag},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteThirdpartySnRelationById" parameterType="Long">
        delete from thirdparty_sn_relation where id = #{id}
    </delete>

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

    <select id="selectSnRelationListByParams" resultType="com.jsowell.pile.vo.web.ThirdPartySnRelationVO">
        SELECT
        t1.station_id AS stationId,
        t1.pile_sn AS pileSn,
        t1.thirdparty_type AS thirdPartyType,
        t1.thirdparty_pile_sn AS thirdPartyPileSn,
        t2.start_mode AS startMode
        FROM
        thirdparty_sn_relation t1
        JOIN thirdparty_station_relation t2 ON t1.thirdparty_type = t2.third_party_type
        JOIN pile_basic_info t3 on t1.pile_sn = t3.sn
        WHERE
        t1.del_flag = '0'
        <if test="stationId != null and stationId != ''">
            AND t1.station_id = #{stationId,jdbcType=VARCHAR}
        </if>
        <if test="pileSn != null and pileSn != ''">
            AND t1.pile_sn = #{pileSn,jdbcType=VARCHAR}
        </if>
        <if test="pileIds != null and pileIds.size() != 0">
            AND t3.id in
            <foreach collection="pileIds" item="pileId" open="(" separator="," close=")">
                #{pileId,jdbcType=BIGINT}
            </foreach>
        </if>
    </select>

    <select id="getRelationVOList" resultType="com.jsowell.pile.vo.web.ThirdPartySnRelationVO">
        SELECT
            pile_sn AS pileSn,
            thirdparty_type AS thirdpartyType,
            thirdparty_pile_sn AS thirdPartySn
        FROM
            thirdparty_sn_relation
        where del_flag = '0'
        <if test="dto.pileSn != null and dto.pileSn != ''">
            and pile_sn = #{dto.pileSn,jdbcType=VARCHAR}
        </if>
    </select>

    <update id="updateThirdPartySnRelation">
        UPDATE thirdparty_sn_relation
        SET thirdparty_pile_sn = #{dto.thirdPartySn,jdbcType=VARCHAR}
        WHERE
            pile_sn = #{dto.pileSn,jdbcType=VARCHAR}
          AND thirdparty_type = #{dto.thirdPartyType,jdbcType=VARCHAR}
    </update>
</mapper>