<?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.FaultSpotCheckMapper">

    <select id="count" resultType="map">
        select BW_LSBH,BW_YJRQ time,sbkj_name,bw_sbkj, sbkj_name, yjsl, cjsl,cjbl, hgsl, bhgsl,ceil(yjsl * cjbl / 100 -
        cjsl)
        dcjsl
        from (select
        y.BW_SBKJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="y.BW_SBKJ"/>
        </include>
        ) sbkj_name,
        count(*) yjsl,
        BW_YJRQ,
        y.BW_LSBH,
        sum(case when BW_JYJG is not null then 1 else 0 end ) cjsl,
        sum(case when BW_JYJG = 1 then 1 else 0 end ) hgsl,
        sum(case when BW_JYJG = 2 then 1 else 0 end ) bhgsl,
        (select BW_HGSL from TF_YWPN_BWCJBL_W where BW_SBKJ = y.BW_SBKJ and BELONG = 1) cjbl
        from TF_YWPN_BWSBYJGL_W y left join TF_YWPN_BWGZBCJDATAIL_W g on y.SBZSBH = g.BW_SBBH
        where BW_YJLX = 2
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and y.BW_SBCJ in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(BW_YJRQ,'yyyy-mm') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and BW_YJRQ between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        group by y.BW_SBKJ,y.BW_LSBH,bw_yjrq)
    </select>

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

    <select id="queryInfo" resultType="map">
        select id,
        bw_lsbh,
        bw_sbcj,
        bw_sblx,
        bw_sbkj,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="bw_sbcj"/>
        </include>
        ) sbcj_name,
        (
        <include refid="table_code">
            <property name="pcid" value="3"/>
            <property name="code" value="bw_sblx"/>
        </include>
        ) sblx_name,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="bw_sbkj"/>
        </include>
        ) sbkj_name,
        bw_yjlx,
        (
        <include refid="table_code">
            <property name="pcid" value="9"/>
            <property name="code" value="bw_yjlx"/>
        </include>
        ) yjlx_name,
        bw_yjyy,
        bw_jsbm,
        (select name from TF_SMPN_DEPARTMENT_X where id = bw_jsbm) jsbm_name,
        bw_jsry,
        (select REAL_NAME from TF_SMPN_USER_X where id = bw_jsry) jsry_name,
        bw_yjrq,
        BW_YJBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = BW_YJBM) yjbm_name,
        create_user,
        create_date,
        (select REAL_NAME from TF_SMPN_USER_X where id = y.create_user) user_name,
        decode(bw_rkbs,'0','成功入库','1','没有入库') rkbs,
        bw_bz,
        sbzsbh,
        bw_yjry,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_YJRY) yjry_name,
        decode(check_flag,'0','未检查','1','已检查') flag,
        decode(yjxx,'1','正常移交','2','无表号移交','3','表号重复移交') yjxx,
        yjsm
        from TF_YWPN_BWSBYJGL_W y
        where BW_YJLX = 2
        <if test="dto.lsbh != null and dto.lsbh != ''">
            and BW_LSBH = #{dto.lsbh}
        </if>
        <if test="dto.cal != null and dto.cal.size() != 0">
            and BW_SBKJ in
            <foreach collection="dto.cal" item="cal" open="(" separator="," close=")">
                #{cal}
            </foreach>
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_SBCJ in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(BW_YJRQ,'yyyy-mm') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and BW_YJRQ between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
    </select>

    <select id="queryWeekInfo" resultType="map">
        select z.id,
        y.bw_lsbh,
        bw_sbbh,
        z.bw_sbcj,
        z.bw_sblx,
        z.bw_sbkj,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="z.bw_sbcj"/>
        </include>
        ) sbcj_name,
        (
        <include refid="table_code">
            <property name="pcid" value="3"/>
            <property name="code" value="z.bw_sblx"/>
        </include>
        ) sblx_name,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="z.bw_sbkj"/>
        </include>
        ) sbkj_name,
        BW_CJJL,
        (select cname from TF_YWPN_SYSCHILDCODE_D where id = BW_CJJL) cjjl_name,
        (select FILE_PATH from TF_YWPN_FILE_W where id = BW_FILEID) photo_path,
        decode(bw_jyjg,'1','合格','2','不合格') jyjg,
        bw_jyrq,
        z.create_user,
        (select REAL_NAME from TF_SMPN_USER_X where id = z.create_user) user_name,
        z.create_date,
        z.bw_bz
        from TF_YWPN_BWGZBCJDATAIL_W z left join TF_YWPN_BWSBYJGL_W y on z.BW_SBBH = y.SBZSBH
        <where>
            <if test="dto.lsbh != null and dto.lsbh != ''">
                and y.bw_lsbh = #{dto.lsbh}
            </if>
            <if test="dto.cal != null and dto.cal.size() != 0">
                and z.BW_SBKJ in
                <foreach collection="dto.cal" item="cal" open="(" separator="," close=")">
                    #{cal}
                </foreach>
            </if>
            <if test="dto.vendor != null and dto.vendor.size() != 0">
                and z.BW_SBCJ in
                <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
            <if test="dto.start != null and dto.end != null">
                and to_char(BW_YJRQ,'yyyy-mm') between #{dto.start} and #{dto.end}
            </if>
            <choose>
                <when test="dto.weekType == 1">
                    and BW_JYJG = 1
                </when>
                <when test="dto.weekType == 2">
                    and BW_JYJG = 2
                </when>
            </choose>
        </where>
    </select>

</mapper>