<?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.sms.mapper.SmsMessageMapper">

    <resultMap id="smsMessageMap" type="com.tofly.feesapi.sms.entity.SmsMessage">
        <id property="id" column="ID"/>
        <result property="lsh" column="LSH"/>
        <result property="yhbh" column="YHBH"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="cwyf" column="CWYF"/>
        <result property="scds" column="SCDS"/>
        <result property="bcds" column="BCDS"/>
        <result property="txlx" column="TXLX"/>
        <result property="txnr" column="TXNR"/>
        <result property="scsj" column="SCSJ"/>
        <result property="fszt" column="FSZT"/>
        <result property="clry" column="CLRY"/>
        <result property="clsj" column="CLSJ"/>
        <result property="bz" column="BZ"/>
        <result property="sLock" column="S_LOCK"/>
        <result property="yssl" column="YSSL"/>
        <result property="yssf" column="YSSF"/>
        <result property="yswsf" column="YSWSF"/>
        <result property="qf" column="QF"/>
        <result property="yf" column="YF"/>
        <result property="zje" column="ZJE"/>
        <result property="count" column="COUNT"/>
        <result property="czrybm" column="CZRYBM"/>
        <result property="czryxm" column="CZRYXM"/>
        <result property="syje" column="SYJE"/>
    </resultMap>

    <resultMap id="smsMessageYhxxVo" type="com.tofly.feesapi.sms.entity.vo.SmsMessageYhxxVo">
        <result property="yhbh" column="YHBH"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
    </resultMap>

    <select id="getSmsMessageYhxxVoList" resultMap="smsMessageYhxxVo">
        select a.yhbh,a.yhxm,a.yhdz,case when a.zzdh is null then a.lxdh else a.zzdh end lxdh
        from yh_khjbxx a
        where 1=1
        <!--模糊查询 -->
        <if test="smsMessageYhxxDto.searchText!=null">
            AND ( a.yhbh like '%${smsMessageYhxxDto.searchText}%'
            OR a.yhxm like '%${smsMessageYhxxDto.searchText}%'
            OR a.yhdz like '%${smsMessageYhxxDto.searchText}%' )
        </if>
        <if test="smsMessageYhxxDto.sscbb!=null">
            AND a.sscbb = '${smsMessageYhxxDto.sscbb}'
        </if>
        <if test="smsMessageYhxxDto.sscbpq!=null">
            AND a.sscbpq = '${smsMessageYhxxDto.sscbpq}'
        </if>
        <if test="smsMessageYhxxDto.yhbhs!=null">
            AND a.yhbh in(${smsMessageYhxxDto.yhbhs})
        </if>
        and length(case when a.zzdh is null then a.lxdh else a.zzdh end )=11
        and a.yhzt = '1'
    </select>

    <resultMap id="smsMessageYhqfxxVo" type="com.tofly.feesapi.sms.entity.vo.SmsMessageYhxxVo">
        <result property="yhbh" column="YHBH"/>
        <result property="yhxm" column="YHXM"/>
        <result property="yhdz" column="YHDZ"/>
        <result property="lxdh" column="LXDH"/>
        <result property="qfje" column="QFJE"/>
        <result property="nbsyje" column="NBSYJE"/>
    </resultMap>

    <select id="getSmsMessageYhqfxxVoList" resultMap="smsMessageYhqfxxVo">
        select a.yhbh,a.yhxm,a.yhdz,a.lxdh lxdh,a.sscbpq,a.sscbb,(nvl(b.qfje,0)-nvl(c.nbsyje,0)) qfje
        from yh_khjbxx a
        left join yy_zjzh c on a.yhbh=c.yhbh
        right join ( select yhbh,sum(ysje) as qfje from yy_qfmx where sfbz = 0 and sfbc=0 and chbz = 0 group by yhbh ) b
        on a.yhbh = b.yhbh
        where 1=1 and nvl(b.qfje,0)-nvl(c.nbsyje,0)>0
        <!--模糊查询 -->
        <if test="smsMessageYhxxDto.searchText!=null">
            AND ( a.yhbh like '%${smsMessageYhxxDto.searchText}%'
            OR a.yhxm like '%${smsMessageYhxxDto.searchText}%'
            OR a.yhdz like '%${smsMessageYhxxDto.searchText}%' )
        </if>
        <if test="smsMessageYhxxDto.sscbb!=null">
            <choose>
                <when test="smsMessageYhxxDto.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="smsMessageYhxxDto.sscbb.split(',')" open="("
                             separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{smsMessageYhxxDto.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="smsMessageYhxxDto.sscbpq!=null">
            AND a.sscbpq = '${smsMessageYhxxDto.sscbpq}'
        </if>
        <if test="smsMessageYhxxDto.yhbhs!=null">
            AND a.yhbh in
            <foreach item="yhbh" collection="smsMessageYhxxDto.yhbhs" open="(" separator="," close=")">
                #{yhbh}
            </foreach>
        </if>
        <if test="smsMessageYhxxDto.userId!=null">
            AND a.sscbb in (select cbbbm from yh_sjqx where user_id=#{smsMessageYhxxDto.userId})
        </if>
        and length(a.lxdh)=11
        and a.yhzt = '1'
        <!--排除微信用户 -->
        --and b.yhbh not in (select yhbh from wx_user where wxbdbz='1') and qfje>0
        <!--排除物联网表用户 -->
        and a.yhbh not in (select yhbh from yh_cbkxx where sblb='3')
    </select>

    <select id="getSmsMessageyffVo" resultMap="smsMessageYhqfxxVo">
        select a.yhbh,a.yhxm,a.yhdz,a.lxdh  lxdh,c.nbsyje,a.sscbpq,a.sscbb
        from yy_zjzh c
        left join yh_khjbxx a on a.yhbh=c.yhbh
        where 1=1
        <!--模糊查询 -->
        <if test="smsMessageYhxxDto.searchText!=null">
            AND ( a.yhbh like '%${smsMessageYhxxDto.searchText}%'
            OR a.yhxm like '%${smsMessageYhxxDto.searchText}%'
            OR a.yhdz like '%${smsMessageYhxxDto.searchText}%' )
        </if>

        <if test="smsMessageYhxxDto.sscbb!=null">
            <choose>
                <when test="smsMessageYhxxDto.sscbb.indexOf(',')!=-1">
                    AND a.sscbb in
                    <foreach item="item" index="index" collection="smsMessageYhxxDto.sscbb.split(',')" open="("
                             separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND a.sscbb=#{smsMessageYhxxDto.sscbb}
                </otherwise>
            </choose>
        </if>

        <if test="smsMessageYhxxDto.sscbpq!=null">
            AND a.sscbpq = '${smsMessageYhxxDto.sscbpq}'
        </if>
        <if test="smsMessageYhxxDto.userId!=null">
            AND a.sscbb in (select cbbbm from yh_sjqx where user_id=#{smsMessageYhxxDto.userId})
        </if>
        <if test="smsMessageYhxxDto.yhbhs!=null">
            AND a.yhbh in
            <foreach item="yhbh" collection="smsMessageYhxxDto.yhbhs" open="(" separator="," close=")">
                #{yhbh}
            </foreach>

        </if>
        and length(case when a.zzdh is null then a.lxdh else a.zzdh end )=11
        and a.yhzt = '1'

        and c.nbsyje >= 0 and c.nbsyje &lt; 10
        <!--排除微信用户   and a.yhbh not in (select yhbh from wx_user where wxbdbz='1') -->
    </select>
    <select id="getPage" resultType="com.tofly.feesapi.sms.entity.vo.SmsMessageVo">
        select a.*,
        c.name as txlxName,
        ( case when a.fszt = '0' then '待发'
        when a.fszt = '1' then '成功'
        when a.fszt = '2' then '失败'
        else '待发' end )as fsztName
        from sms_message a
        left join yh_khjbxx b
        on a.yhbh = b.yhbh
        left join sms_txlx c on a.txlx = c.code
        where c.code > ' '
        <if test="dto.searchText!=null">
            AND ( a.yhbh like '%${dto.searchText}%'
            OR a.yhxm like '%${dto.searchText}%'
            OR a.yhdz like '%${dto.searchText}%'
            OR a.lxdh like '%${dto.searchText}%')
        </if>
        <if test="dto.txlx!=null">
            AND a.txlx = '${dto.txlx}'
        </if>
        <if test="dto.fszt!=null">
            AND a.fszt = '${dto.fszt}'
        </if>
        <if test="dto.yhbh!=null">
            AND a.yhbh = '${dto.yhbh}'
        </if>
        <if test="dto.lsh!=null">
            AND a.yhbh = '${dto.lsh}'
        </if>
        <if test="dto.sscbb!=null">
            <choose>
                <when test="dto.sscbb.indexOf(',')!=-1">
                    AND b.sscbb in
                    <foreach item="item" index="index" collection="dto.sscbb.split(',')" open="(" separator=","
                             close=")">
                        #{item}
                    </foreach>
                </when>
                <otherwise>
                    AND b.sscbb= #{dto.sscbb}
                </otherwise>
            </choose>
        </if>
        <if test="dto.sscbpq!=null">
            AND b.sscbpq = '${dto.sscbpq}'
        </if>
        <choose>
            <when test="dto.scsj_s!=null and dto.scsj_e!=null">
                AND a.scsj between #{dto.scsj_s} and #{dto.scsj_e}
            </when>
            <when test="dto.scsj_s!=null">
                AND a.scsj >= #{dto.scsj_s}
            </when>
            <when test="dto.scsj_e!=null">
                AND a.scsj &lt;= #{dto.scsj_e}
            </when>
        </choose>
        <choose>
            <when test="dto.clsj_s!=null and dto.clsj_e!=null">
                AND a.scsj between #{dto.clsj_s} and #{dto.clsj_e}
            </when>
            <when test="dto.clsj_s!=null">
                AND a.scsj >= #{dto.clsj_s}
            </when>
            <when test="dto.clsj_e!=null">
                AND a.scsj &lt;= #{dto.clsj_e}
            </when>
        </choose>
    </select>
</mapper>
