<?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="org.springblade.modules.business.datagram.mapper.RtuDatagramMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="entityResultMap"
               type="org.springblade.modules.business.datagram.entity.RtuDatagramInfoEntity">
        <result column="rtu_code" property="rtuCode"/>
        <result column="datagram" property="datagram"/>
        <result column="agreement" property="agreement"/>
        <result column="pick_time" property="pickTime"/>
    </resultMap>

    <select id="datagramCount" resultType="java.lang.Long">
        SELECT
        count(*)
        FROM
        data_rtu_datagram_info n
        LEFT JOIN rtu_info r ON r.rtu_code = n.rtu_code AND r.is_deleted=0
        LEFT JOIN data_ad_cd_b a ON a.adcd = r.ad_code AND a.is_deleted=0
        WHERE
        n.is_deleted = 0
        <if test="dto.beginDate!=null">
            and n.create_time >= #{dto.beginDate}
        </if>
        <if test="dto.endDate!=null">
            and n.create_time &lt; #{dto.endDate}
        </if>
        <if test="dto.adCode!=null">
            and (r.ad_code =#{dto.adCode} or a.ancestors in (SELECT ancestors FROM data_ad_cd_b WHERE ancestors
            like concat(concat('%', #{dto.adCode}), '%')))
        </if>
    </select>

    <select id="selectPage" resultMap="entityResultMap">
        SELECT
        n.*
        FROM
        data_rtu_datagram_info n
        WHERE
        n.is_deleted = 0
        <if test="dto.rtuCode!=null">
            and n.rtu_code = #{dto.rtuCode}
        </if>
        order by n.pick_time desc
    </select>
</mapper>
