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

    <select id="count" resultType="map">
        select AZBM,count(*) num,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = AZBM) azbm_name
        from TF_YWPN_BWRKSBDATAIL_W
        where
        <include refid="common_condition"/>
        group by AZBM
    </select>

    <select id="countByDept" resultType="map">
        select BW_SBKJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="BW_SBKJ"/>
        </include>
        ) sbkj_name,
        sum(case when BW_SBLX = 1 then 1 else 0 end ) ptb_num,
        sum(case when BW_SBLX = 2 then 1 else 0 end ) wlb_num,
        sum(case when BW_SBLX = 3 then 1 else 0 end ) kb_num,
        sum(case when BW_SBLX = 4 then 1 else 0 end ) ycb_num
        from TF_YWPN_BWRKSBDATAIL_W
        where
        <include refid="common_condition"/>
        group by BW_SBKJ
    </select>

    <select id="queryInfo" resultType="map">
        select BW_KHBH,
        BW_KHXM,
        BW_KHDH,
        BW_KHDZ,
        BW_SBBH,
        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_SBQD,
        decode(AZFS,'1','立式','2','卧式') azfs_name,
        decode(SFNCYS,'0','否','1','是') ncys,
        JYZ,
        BZBH,
        SBAZDZ,
        decode(ISSYC,'1','已同步','2','未同步') sftb,
        MSG,
        b.BW_AZRY,
        (select REAL_NAME from TF_SMPN_USER_X where ID = b.BW_AZRY) azry_name,
        b.BW_AZRQ
        from TF_YWPN_BWRKSBDATAIL_W b
        where
        <include refid="common_condition"/>
    </select>

    <sql id="common_condition">
        BW_SBZT = 3
        and ISSYC != 0 and ISSYC is not null
        <choose>
            <when test="dto.state == 1">
                and ISSYC = 1
            </when>
            <when test="dto.state == 2">
                and ISSYC = 2
            </when>
        </choose>
        and to_char(BW_AZRQ,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.dept != null and dto.dept.size() != null">
            and AZBM in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != null">
            and bw_sbcj in
            <foreach collection="dto.vendor" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        <if test="dto.cal != null and dto.cal.size() != null">
            and bw_sbkj in
            <foreach collection="dto.cal" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != null">
            and bw_sblx in
            <foreach collection="dto.type" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
    </sql>

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