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

    <select id="count" resultType="map">
        select DJBM,
        (select name from TF_SMPN_DEPARTMENT_X d where id = DJBM) djbm_name,
        count(*) jyzs,
        sum(case when FINISH_TIME is not null and BW_JYJG is not null then 1 else 0 end) yjy_num,
        sum(case when FINISH_TIME is not null and BW_JYJG is null then 1 else 0 end) wjy_num,
        sum(case when BW_JYJG = 1 then 1 else 0 end) hg_num,
        sum(case when BW_JYJG = 2 then 1 else 0 end) bhg_num
        from TF_YWPN_GDINFO_W i left join TF_YWPN_BWZYSBJDDATAIL_W g on g.BW_GDBH = i.GDBH
        where (IS_WATERMETER_STATUS = 3 or IS_WATERMETER_STATUS = 4) and FINISH_TIME is not null
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(i.DJSJ,'yyyy-mm') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and i.DJSJ between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        group by DJBM
    </select>

    <select id="queryWorkInfo" resultType="map">
        select g.GDBH,
        GDLX,
        (select gdlx_mc from TF_YWPN_GDGLTYPEBM_W where id = GDLX) gdlx_name,
        GDXF1,
        (select gdlx_mc from TF_YWPN_GDGLTYPEBM_W where id = GDXF1) xf1_name,
        GDXF2,
        (select gdlx_mc from TF_YWPN_GDGLTYPEBM_W where id = GDXF2) xf2_name,
        GDMS,
        DJBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = DJBM) djbm_name,
        DJRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = DJRY) djry_name,
        DJSJ,
        FINISH_TIME,
        <choose>
            <when test="dto.state == 3 or dto.state == 1">
                (case when BW_JYJG is null then KHXM else BW_YHXM end) BW_YHXM,
                (case when BW_JYJG is null then KHDZ else BW_YHDZ end) BW_YHDZ,
                (case when BW_JYJG is null then LXDH else BW_LXDH end) BW_LXDH,
                (case when BW_JYJG is null then KHBH else BW_YHBH end) BW_YHBH,
            </when>
            <otherwise>
                BW_YHXM,
                BW_YHDZ,
                BW_LXDH,
                BW_YHBH,
            </otherwise>
        </choose>
        coalesce(b.SBBH,g.SBBH) sbbh,
        (case when BW_JYJG is not null then '已检验' else '未检验' end) flag,
        BW_JYJG,
        WXJDYY,
        decode(BW_JYJG,'1','合格','2','不合格','3','无需校验') jyjg
        from TF_YWPN_GDINFO_W g
        left join TF_YWPN_BWZYSBJDDATAIL_W b on g.GDBH = b.BW_GDBH
        where FINISH_TIME is not null
        and (IS_WATERMETER_STATUS = 3 or IS_WATERMETER_STATUS = 4)
        <choose>
            <when test="dto.state == 2">
                and BW_JYJG is not null
            </when>
            <when test="dto.state == 3">
                and BW_JYJG is null
            </when>
            <when test="dto.state == 4">
                and BW_JYJG = 1
            </when>
            <when test="dto.state == 5">
                and BW_JYJG = 2
            </when>
        </choose>
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(DJSJ,'yyyy-mm') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and DJSJ between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and djbm in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>

    </select>

    <!--<select id="queryInfo" resultType="map">
        select BW_YHBH,
        BW_YHXM,
        BW_YHDZ,
        BW_LXDH,
        b.SBBH,
        bw_sbcj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        decode(BW_JYJG,'1','合格','2','不合格') jyjg,
        BW_JYRQ,
        CREATE_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = b.CREATE_USER) user_name,
        CREATE_DATE,
        BW_BZ,
        BW_GDBH,
        BW_TBSL,
        BW_BSL
        from TF_YWPN_BWZYSBJDDATAIL_W b
        left join TF_YWPN_GDINFO_W g on b.BW_GDBH = g.GDBH
        where GDLX = 3
        and GDXF1 = 23
        and GDXF2 = 212
        &#45;&#45; and GDXF2 = 173
        <if test="dto.dept != null and dto.dept.size() != 0">
            and DJBM in
            <foreach collection="dto.dept" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <choose>
            <when test="dto.state == 1">

            </when>
            <when test="dto.state == 2">
                and FINISH_TIME is not null
                and BW_JYJG is not null
            </when>
            <when test="dto.state == 3">
                and FINISH_TIME is not null
                and BW_JYJG is null
            </when>
            <when test="dto.state == 4">
                and BW_JYJG = 1
            </when>
            <when test="dto.state == 5">
                and BW_JYJG = 2
            </when>
        </choose>
        <choose>
            <when test="dto.start != null and dto.end != null">
                and to_char(g.DJSJ,'yyyy-mm') between #{dto.start} and #{dto.end}
            </when>
            <otherwise>
                and g.DJSJ between add_months(sysdate,-1) and sysdate
            </otherwise>
        </choose>
    </select>-->

    <select id="queryDisPutInfo" resultType="map">
        select id,
        bw_lsbh,
        bw_sjly,
        decode(bw_jyjg,'1','合格','2','不合格','3','无需检定') jyjg,
        BW_JYJG,
        WXJDYY,
        bw_jyrq,
        create_user,
        (select REAL_NAME from TF_SMPN_USER_X where id = z.create_user) user_name,
        create_date,
        bw_bz,
        sbbh,
        bw_jyry,
        (select REAL_NAME from TF_SMPN_USER_X where id = z.create_user) jyry_name,
        bw_sbcj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        bw_gdbh,
        bw_yhbh,
        bw_yhxm,
        bw_yhdz,
        bw_lxdh,
        bw_tbsl,
        bw_bsl,
        (select FILE_PATH from TF_YWPN_FILE_W where id in (BW_FILEID)) file_path
        from TF_YWPN_BWZYSBJDDATAIL_W z
        where BW_GDBH = #{dto.gdbh}
    </select>

    <sql id="codeQuery">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = ${ccode}
          and pcid = ${pcid}
    </sql>

</mapper>