<?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.watermeter.custom.mapper.meterBusiness.WeeklyInspectionMapper">

    <select id="count" resultType="map">
        select sbkj,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="sbkj"/>
        </include>
        ) sbkj_name,
        sum(case when SBLX = 1 then 1 else 0 end) ptb_num,
        sum(case when SBLX = 3 then 1 else 0 end) wlb_num,
        sum(case when SBLX = 4 then 1 else 0 end) kb_num,
        sum(case when SBLX = 2 then 1 else 0 end) ycb_num
        from (
        select sbcj,
        sblx,
        sbkj,
        (case
        when SBLX = 3 then
        add_months(nvl(BC_ZJRQ,AZRQ), (#{dto.monthFour}))
        else
        case when SBKJ between 15 and 25 then
        add_months(nvl(BC_ZJRQ,AZRQ), (#{dto.monthOne}))
        when SBKJ between 40 and 50 then
        add_months(nvl(BC_ZJRQ,AZRQ), (#{dto.monthTwo}))
        when SBKJ >= 80 then
        add_months(nvl(BC_ZJRQ,AZRQ),(#{dto.monthThree}))
        end
        end) check_time
        from TF_YWPN_QFKHJBXX_W
        where YHZT != 9 and yhzt != 0
        )
        where 1=1
        <if test="dto.start!=null">
            and  to_char(check_time,'yyyy-mm-dd') &lt; #{dto.start}
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and sbcj in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.cal != null and dto.cal.size() != 0">
            and sbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
        group by sbkj
        order by sbkj
    </select>

    <select id="queryInfo" resultType="map" fetchSize="10000">
        select YHBH,YHXM,YHDZ,LXDH,YSXZ,ysxz_name,SBBH,SBCJ,sbcj_name,SBLX,sblx_name,SBKJ,sbkj_name,ZBRY,zbry_name,dqsj,
        abs(round(months_between(dqsj,sysdate),1)) cqys,AZRQ
        from (select yhbh,
        yhxm,
        yhdz,
        lxdh,
        ysxz,
        (select y.YSXZ from TF_YWPN_QFKHJBXX_YSXZ_W y where y.YHBH = q.YHBH) ysxz_name,
        sbbh,
        sbcj,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="sbcj"/>
        </include>
        ) sbcj_name,
        sblx,
        (
        <include refid="table_code">
            <property name="pcid" value="3"/>
            <property name="code" value="sblx"/>
        </include>
        ) sblx_name,
        sbkj,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="sbkj"/>
        </include>
        ) sbkj_name,
        ZBRY,
        (select REAL_NAME from TF_SMPN_USER_X where ID = ZBRY) zbry_name,
        (case
        when SBLX = 2 then
        add_months(nvl(bc_zjrq,azrq), (#{dto.monthFour}))
        else (case when SBKJ between 15 and 25 then
        add_months(nvl(bc_zjrq,azrq), (#{dto.monthOne}))
        when SBKJ between 40 and 50 then
        add_months(nvl(bc_zjrq,azrq),(#{dto.monthTwo}))
        when SBKJ >= 80 then
        add_months(nvl(bc_zjrq,azrq),(#{dto.monthThree}))
        end)
        end) dqsj,
        azrq
        from TF_YWPN_QFKHJBXX_W q
        where YHZT != 9 and YHZT != 0
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and sbcj in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.cal != null and dto.cal.size() != 0">
            and sbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and sblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
        <if test="dto.str != null and dto.str != ''">
            and yhbh || yhxm || yhdz || lxdh || sbbh like '%' || #{dto.str} || '%'
        </if>)
        where to_char(dqsj,'yyyy-mm-dd') &lt; #{dto.start}
        order by sbkj
    </select>

    <sql id="table_code">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = to_char(${code})
          and pcid = ${pcid}
    </sql>

    <sql id="common_sql">
    </sql>

</mapper>