<?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.PileMsgRecordMapper">
    <resultMap id="BaseResultMap" type="com.jsowell.pile.domain.PileMsgRecord">
        <!--@mbg.generated-->
        <!--@Table pile_msg_record-->
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="pile_sn" jdbcType="VARCHAR" property="pileSn" />
        <result column="frame_type" jdbcType="VARCHAR" property="frameType" />
        <result column="connector_code" jdbcType="VARCHAR" property="connectorCode" />
        <result column="pile_connector_code" jdbcType="VARCHAR" property="pileConnectorCode" />
        <result column="original_msg" jdbcType="VARCHAR" property="originalMsg" />
        <result column="json_msg" jdbcType="VARCHAR" property="jsonMsg" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id, pile_sn, frame_type, connector_code, pile_connector_code, original_msg, json_msg, create_time
    </sql>
    <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List" />
        from pile_msg_record
        where id = #{id,jdbcType=INTEGER}
    </select>

    <insert id="insertSelective" parameterType="com.jsowell.pile.domain.PileMsgRecord">
        <!--@mbg.generated-->
        insert into pile_msg_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="pileSn != null and pileSn != ''">
                pile_sn,
            </if>
            <if test="frameType != null and frameType != ''">
                frame_type,
            </if>
            <if test="connectorCode != null and connectorCode != ''">
                connector_code,
            </if>
            <if test="pileConnectorCode != null and pileConnectorCode != ''">
                pile_connector_code,
            </if>
            <if test="originalMsg != null and originalMsg != ''">
                original_msg,
            </if>
            <if test="jsonMsg != null and jsonMsg != ''">
                json_msg,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="pileSn != null and pileSn != ''">
                #{pileSn,jdbcType=VARCHAR},
            </if>
            <if test="frameType != null and frameType != ''">
                #{frameType,jdbcType=VARCHAR},
            </if>
            <if test="connectorCode != null and connectorCode != ''">
                #{connectorCode,jdbcType=VARCHAR},
            </if>
            <if test="pileConnectorCode != null and pileConnectorCode != ''">
                #{pileConnectorCode,jdbcType=VARCHAR},
            </if>
            <if test="originalMsg != null and originalMsg != ''">
                #{originalMsg,jdbcType=VARCHAR},
            </if>
            <if test="jsonMsg != null and jsonMsg != ''">
                #{jsonMsg,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
        </trim>
    </insert>

    <select id="getByPileSn" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from pile_msg_record
        where pile_sn = #{pileSn,jdbcType=VARCHAR}
    </select>

    <select id="getByConnectorCode" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from pile_msg_record
        where connector_code = #{connectorCode,jdbcType=VARCHAR}
    </select>

    <select id="getByConnectorCodeList" resultMap="BaseResultMap">
        SELECT
            t1.id, t1.pile_sn, t1.frame_type, t1.connector_code, t1.original_msg, t1.json_msg, t1.create_time
        FROM
            pile_msg_record t1,
            (
                SELECT
                    pmr.connector_code,
                    MAX(pmr.create_time) AS lastTime
                FROM
                    pile_msg_record pmr
                WHERE
                    pmr.connector_code IN
                    <foreach collection="connectorCodeList" item="connectorCode" open="(" separator="," close=")">
                        #{connectorCode,jdbcType=VARCHAR}
                    </foreach>
                    AND pmr.frame_type IN ('11', '13')
                GROUP BY pmr.connector_code
            ) t2
        WHERE
            t1.connector_code = t2.connector_code
            AND t1.create_time = t2.lastTime
            AND t1.frame_type IN ('11', '13')
    </select>

    <select id="getPileFeedList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from pile_msg_record
        where pile_sn = #{pileSn,jdbcType=VARCHAR}
        order by create_time desc
    </select>
</mapper>