<?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="cn.roadm.mapper.YueXinReportMapper">
    <resultMap id="YueXinReportResultMap" type="cn.roadm.bo.YueXinReportBO">
        <!-- 主键映射（优化性能，标记主键字段） -->
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="task_id" property="taskId" jdbcType="VARCHAR"/>
        <result column="sp_id" property="spId" jdbcType="VARCHAR"/>
        <result column="mdn" property="mdn" jdbcType="VARCHAR"/>
        <result column="tpl_id" property="tplId" jdbcType="VARCHAR"/>
        <result column="result" property="result" jdbcType="TINYINT"/>
        <result column="stat_desc" property="statDesc" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="parse_time" property="parseTime" jdbcType="TIMESTAMP"/>
        <result column="factory_type" property="factoryType" jdbcType="VARCHAR"/>
        <result column="factory_tpl_id" property="factoryTplId" jdbcType="VARCHAR"/>
        <result column="aim_url" property="aimUrl" jdbcType="VARCHAR"/>
        <result column="msg_id" property="msgId" jdbcType="VARCHAR"/>
        <result column="msg_type" property="msgType" jdbcType="VARCHAR"/>
        <result column="push_state" property="pushState" jdbcType="TINYINT"/>
        <result column="chatbot_id" property="chatbotId" jdbcType="VARCHAR"/>
        <result column="channel_id" property="channelId" jdbcType="VARCHAR"/>
    </resultMap>
    <select id="searchPageYueXinReportList" resultMap="YueXinReportResultMap"
            parameterType="cn.roadm.bo.YueXinReportBO">
        SELECT
        t1.id, task_id, sp_id, mdn, tpl_id, t1.result, stat_desc,
        DATE_FORMAT(t1.create_time,'%Y-%m-%d %H:%i:%s') as create_time,
        DATE_FORMAT(parse_time,'%Y-%m-%d %H:%i:%s') as parse_time, factory_type, t2.local_id as factory_tpl_id,
        aim_url, msg_id, msg_type, push_state, chatbot_id,
        t1.channel_id
        FROM yue_xin_report t1 left join (SELECT km.local_id,km.remote_id,km.channel_id ,kl.spid FROM kp_template_remote km LEFT JOIN aim_template_local kl ON km.local_id = kl.id )  t2
        ON t1.tpl_id=t2.remote_id and t2.channel_id = t1.channel_id and t1.sp_id = t2.spid
        <include refid="yueXinReportConditions"/>
        order by t1.create_time desc
    </select>
    <select id="yueXinReportCount" resultType="java.lang.Long">
        select count(*) from yue_xin_report t1 left join (SELECT km.local_id,km.remote_id,km.channel_id ,kl.spid FROM kp_template_remote km LEFT JOIN aim_template_local kl ON km.local_id = kl.id )  t2
        ON t1.tpl_id=t2.remote_id and t2.channel_id = t1.channel_id and t1.sp_id = t2.spid
        <include refid="yueXinReportConditions"/>
    </select>
    <sql id="yueXinReportConditions">
        <where>
            <if test="spId!=null and spId!=''">
                and t1.sp_id=#{spId}
            </if>
            <if test="tplId!=null and tplId!=''">
                and t2.local_id=#{tplId}
            </if>
            <if test="remoteId!=null and remoteId!=''">
                and t1.tpl_id=#{remoteId}
            </if>
            <if test="mdn != null and mdn != ''">
                and t1.mdn=#{mdn}
            </if>
            <if test="pushState != null">
                and t1.push_state=#{pushState}
            </if>
            <if test="timeFlag==0 and begin!=null and begin.trim()!=''">
                and t1.create_time &gt;=str_to_date(#{begin},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="timeFlag==0 and end!=null and end.trim()!=''">
                and t1.create_time &lt;=str_to_date(#{end},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="timeFlag==1 and begin!=null and begin.trim()!=''">
                and t1.parse_time &gt;=str_to_date(#{begin},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="timeFlag==1 and end!=null and end.trim()!=''">
                and t1.parse_time &lt;=str_to_date(#{end},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    </sql>

</mapper>