<?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.tofly.feesapi.wxgl.wechatbasicmgt.mapper.WxMessageMapper">

    <resultMap id="wxMessageMap" type="com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxMessage">
        <id property="id" column="ID"/>
        <result property="msgType" column="MSG_TYPE"/>
        <result property="openid" column="OPENID"/>
        <result property="first" column="FIRST"/>
        <result property="keyword1" column="KEYWORD1"/>
        <result property="keyword2" column="KEYWORD2"/>
        <result property="keyword3" column="KEYWORD3"/>
        <result property="keyword4" column="KEYWORD4"/>
        <result property="keyword5" column="KEYWORD5"/>
        <result property="keyword6" column="KEYWORD6"/>
        <result property="remark" column="REMARK"/>
        <result property="addDate" column="ADD_DATE"/>
        <result property="addOcode" column="ADD_OCODE"/>
        <result property="sendState" column="SEND_STATE"/>
        <result property="handleOcode" column="HANDLE_OCODE"/>
        <result property="handleDate" column="HANDLE_DATE"/>
        <result property="handleRemark" column="HANDLE_REMARK"/>
        <result property="isLock" column="IS_LOCK"/>
        <result property="pch" column="PCH"/>
        <result property="userid" column="USERID"/>
    </resultMap>

    <select id="getWxMessagePage" resultType="com.tofly.feesapi.wxgl.wechatbasicmgt.entity.WxMessage">
        select a.*
        from wx_message a
        left join yh_khjbxx b on
        <choose>
            <when test="filter.msgType==02">
                a.keyword2 = b.yhbh
            </when>
            <when test="filter.msgType==15">
                a.keyword1 = b.yhbh
            </when>
            <otherwise>
                a.keyword2 = b.yhbh or a.keyword1 = b.yhbh
            </otherwise>
        </choose>
        where 1=1
        <if test="filter.yhbh!=null">
            and a.keyword2 = #{filter.yhbh}
        </if>
        <if test="filter.addDate!=null">
            and a.add_date >= #{filter.addDate} and a.add_date <![CDATA[<=]]> #{filter.addDate}+1
        </if>
        <if test="filter.msgType!=null">
            and a.msg_type = #{filter.msgType}
        </if>
        <if test="filter.sendState!=null">
            and a.send_state = #{filter.sendState}
        </if>
        <if test="filter.searchText!=null">
            and (a.keyword1 like concat('%',#{filter.searchText},'%')
            or a.keyword2 like concat('%',#{filter.searchText},'%')
            or a.keyword3 like concat('%',#{filter.searchText},'%')
            or a.keyword4 like concat('%',#{filter.searchText},'%')
            or a.keyword5 like concat('%',#{filter.searchText},'%')
            )
        </if>
        <if test="filter.sccbb != null and filter.sccbb != ''">
            <if test="filter.sccbb.indexOf(',') != -1">
                and b.sscbb in
                <foreach item="item" index="index" collection="filter.sccbb.split(',')" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            <if test="filter.sccbb.indexOf(',') == -1">
                and b.sscbb=#{filter.sccbb}
            </if>
        </if>
        <if test="filter.userid != null">
            and b.sscbb in (select cbbbm from yh_sjqx where user_id=#{filter.userid})
        </if>
        <if test="filter.sscbpq != null">
            and b.sscbpq = #{filter.sscbpq}
        </if>

        <if test="filter.sccbb != null">
            and b.sscbb = #{filter.sccbb}
        </if>
        order by a.add_date desc,a.pch asc
    </select>
</mapper>
