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

    <select id="getCustomerInfo" resultType="map">
        select YHXM, YHDZ, YHBH, LXDH
        from TF_YWPN_QFKHJBXX_W
        where YHZT != 9
        and SBBH is null
        <if test="dto != null and dto != ''">
            and YHBH || YHDZ || LXDH || YHXM like '%' || #{dto} || '%'
        </if>
    </select>

    <select id="getTableInfo" resultType="map">
        select
        BW_SBBH,
        r.BW_SBCJ,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        r.bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        r.bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        CONTRACT,
        CONTRACT_CODE
        from TF_YWPN_BWRKSBDATAIL_W r left join TF_YWPN_BWCKDATAIL_W c on r.CK_ID = c.ID
        where BW_SBZT = 2
        <if test="sbcj != null">
            and r.BW_SBCJ = #{sbcj}
        </if>
        <if test="sbkj != null">
            and r.bw_sbkj = #{sbkj}
        </if>
        <if test="sblx != null">
            and r.bw_sblx = #{sblx}
        </if>
        <if test="dto != null and dto != ''">
            and bw_sbbh like '%' || #{dto} || '%'
        </if>
    </select>

    <!--    <select id="installCount" resultType="map">-->
    <!--        select count(*) xz_num,-->
    <!--        AZBM,-->
    <!--        (select name from TF_SMPN_DEPARTMENT_X where ID = AZBM) dept_name,-->
    <!--        (select sum(BW_SL) from TF_YWPN_BWCKDATAIL_W where BW_LQBM = AZBM and to_char(CREATE_DATE,'yyyy-mm-dd') between-->
    <!--        #{dto.start} and #{dto.end}) lb_num,-->
    <!--        (select count(*) from TF_YWPN_BWSBYJGL_W where BW_YJLX = 1 and BW_JSBM = AZBM and to_char(bw_yjrq,'yyyy-mm-dd')-->
    <!--        between #{dto.start} and #{dto.end}) tb_num-->
    <!--        from TF_YWPN_BWRKSBDATAIL_W-->
    <!--        where BW_SBZT = 3-->
    <!--        and to_char(bw_azrq,'yyyy-mm-dd') between #{dto.start} and #{dto.end}-->
    <!--        <if test="dto.dept != null and dto.dept.size() != 0">-->
    <!--            and AZBM in-->
    <!--            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">-->
    <!--                #{d}-->
    <!--            </foreach>-->
    <!--        </if>-->
    <!--        group by AZBM-->
    <!--    </select>-->

    <select id="installCount" resultType="map">
        select count(*) xz_num,
        AZBM,
        (select name from TF_SMPN_DEPARTMENT_X where ID = AZBM) dept_name,
        sum(case when ISSYC = 1 then 1 else 0 end) tb_num,
        sum(case when ISSYC = 2 then 1 else 0 end) wtb_num
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_SBZT = 3 and ISSYC != 0 and ISSYC is not null
        and to_char(bw_azrq,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.dept != null and dto.dept.size() != 0">
            and AZBM in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        group by AZBM
    </select>

    <select id="getGdInfo" resultType="map">
        select * from TF_YWPN_GDINFO_W where GDBH in
        <foreach collection="workOrderCode" item="code" open="(" separator="," close=")">
            #{code}
        </foreach>
    </select>

    <select id="countByNewTable" resultType="map">
        select BW_SBBH,
        r.ID,
        r.BW_SBCJ,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        r.bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        r.bw_sbkj,
        BW_SBQD,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        (select name from TF_SMPN_DEPARTMENT_X where ID = AZBM) dept_name,
        BW_AZRY,
        (select REAL_NAME from TF_SMPN_USER_X where ID = BW_AZRY) azry_name,
        BW_AZRQ,
        decode(r.AZFS,'1','立式','2','卧式') azfs_name,
        AZBM,
        (select name from TF_SMPN_DEPARTMENT_X where id = AZBM) azbm_name,
        BW_KHBH,
        BW_KHXM,
        BW_KHDZ,
        BW_KHDH,
        r.CREATE_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = r.CREATE_USER) user_name,
        r.CREATE_DATE,
        JYZ,
        BZBH,
        SBAZDZ,
        decode(ISSYC,'1','已同步','2','未同步') sftb,
        MSG,
        decode(SFNCYS,'0','否','1','是') ncys,
        CONTRACT,
        CONTRACT_CODE,
        SBYT,SBYT_NAME,YSXZ,YSXZ_NAME
        from TF_YWPN_BWRKSBDATAIL_W r left join TF_YWPN_BWCKDATAIL_W c on r.CK_ID = c.ID
        where BW_SBZT = 3 and ISSYC != 0 and ISSYC is not null
        and to_char(BW_AZRQ,'yyyy-MM-dd') between #{dto.start} and #{dto.end}
        <choose>
            <when test="dto.state == 1">
                and ISSYC = 1
            </when>
            <when test="dto.state == 2">
                and ISSYC = 2
            </when>
        </choose>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and AZBM in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
    </select>

    <select id="theStagingQuery" resultType="map">
        select r.ID, BW_KHBH,BW_KHXM,BW_KHDZ,BW_KHDH,BW_SBBH,r.BW_SBCJ,YSXZ,YSXZ_NAME,SBYT,SBYT_NAME,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbcj"/>
            <property name="pcid" value="2"/>
        </include>
        ) sbcj_name,
        r.bw_sblx,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sblx"/>
            <property name="pcid" value="3"/>
        </include>
        ) sblx_name,
        r.bw_sbkj,
        (
        <include refid="codeQuery">
            <property name="ccode" value="r.bw_sbkj"/>
            <property name="pcid" value="1"/>
        </include>
        ) sbkj_name,
        GDDH,
        BW_AZRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_AZRY) azry_name,
        BW_AZRQ,
        BW_SBQD,
        decode(AZFS,'1','立式','2','卧式') azfs,
        SFNCYS,
        decode(SFNCYS,'0','否','1','是') ncys,
        JYZ,
        BZBH,
        SBAZDZ,
        CONTRACT,
        CONTRACT_CODE
        from TF_YWPN_BWRKSBDATAIL_W r left join TF_YWPN_BWCKDATAIL_W c on r.CK_ID = c.ID
        where ISSYC = 0
        <if test="dto != null and dto.user != null and dto.user.size() != 0">
            and BW_AZRY in
            <foreach collection="dto.user" item="u" open="(" separator="," close=")">
                #{u}
            </foreach>
        </if>
        <if test="dto != null and dto.input != null and dto.input != ''">
            and BW_KHXM || BW_KHBH || BW_KHDZ || BW_SBBH like '%' || #{dto.input} || '%'
        </if>
        <if test="dto != null and dto.SBBH != null and dto.SBBH != ''">
            and BW_SBBH like '%' || #{dto.SBBH} || '%'
        </if>
        <if test="dto != null and dto.SBKJ != null and dto.SBKJ != ''">
            and r.bw_sbkj = #{dto.SBKJ}
        </if>
        <if test="dto != null and dto.SBCJ != null and dto.SBCJ != ''">
            and r.bw_sbcj = #{dto.SBCJ}
        </if>
        <if test="dto != null and dto.SBLX != null and dto.SBLX != ''">
            and r.bw_sblx = #{dto.SBLX}
        </if>
        <if test="dto != null and dto.BZBH != null and dto.BZBH != ''">
            and bzbh like '%' || #{dto.BZBH} || '%'
        </if>
        order by bw_sbcj,bw_sbkj,bw_sblx
    </select>

    <select id="queryClientInfo" resultType="com.tofly.watermeter.entity.Qfkhjbxx">
        select *
        from TF_YWPN_QFKHJBXX_W
        where SBBH in
        <foreach collection="code" item="cd" index="idx" separator="," open="(" close=")">
            <if test="(idx % 999) == 998">NULL ) OR SBBH IN (</if>
            #{cd}
        </foreach>
    </select>

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

    <select id="queryInstallTable" resultType="string">
        select BW_SBBH
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_SBZT = 2 and BW_SBBH in
        <foreach collection="code" item="cd" index="idx" separator="," open="(" close=")">
            <if test="(idx % 999) == 998">NULL ) OR BW_SBBH IN (</if>
            #{cd}
        </foreach>
    </select>

</mapper>