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

    <select id="getDeptId" resultType="long">
        select DEPARTMENT_ID
        from TF_SMPN_USER_X
        where ID = #{userId}
    </select>

    <select id="countChangeTable" resultType="map">
        select BW_XBKJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="BW_XBKJ"/>
        </include>
        ) sbkj_name,
        sum(case when BW_XBLX = 1 then 1 else 0 end) jxb_num,
        sum(case when BW_XBLX = 3 then 1 else 0 end) icb_num,
        sum(case when BW_XBLX = 4 then 1 else 0 end) ycb_num,
        sum(case when BW_XBLX = 2 then 1 else 0 end) wlb_num
        from TF_YWPN_BWHBDATAIL_W
        where to_char(BW_HBRQ,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_XBCJ in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and bw_hbbm in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        group by BW_XBKJ
    </select>

    <select id="queryChangeTable" resultType="map">
        select BW_KHBH,
        h.ID,
        BW_KHXM,
        REMARK,
        BW_JBBH,
        BW_KHDZ,
        BW_KHDH,
        (select ysxz_mc from TF_YWPN_QFMIDYSXZBM_E where YSXZ_BM = YSXZ) ysxz_name,
        BW_JBCJ,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="BW_JBCJ"/>
        </include>
        ) jbcj_name,
        BW_JBLX,
        (
        <include refid="table_code">
            <property name="pcid" value="3"/>
            <property name="code" value="BW_JBLX"/>
        </include>
        ) jblx_name,
        BW_JBKJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="BW_JBKJ"/>
        </include>
        ) jbkj_name,
        BW_JBZD,
        BW_JBQD,
        abs(BW_JBZD - BW_JBQD) val,
        (
        <include refid="table_code">
            <property name="pcid" value="3"/>
            <property name="code" value="BW_XBLX"/>
        </include>
        ) xblx_name,
        BW_XBLX,
        (
        <include refid="table_code">
            <property name="pcid" value="2"/>
            <property name="code" value="BW_XBCJ"/>
        </include>
        ) xbcj_name,
        BW_XBCJ,
        (
        <include refid="table_code">
            <property name="pcid" value="1"/>
            <property name="code" value="BW_XBKJ"/>
        </include>
        ) xbkj_name,
        BW_XBKJ,
        BW_XBBH,
        BW_XBQD,
        BW_HBLX,
        BW_HBBM,
        AUDIT_OPINION,
        decode(IS_SYNC,0,'暂存',1,'已同步',2,'未同步',3,'无需同步') syn,
        MSG,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = BW_HBBM) hbbm,
        BW_HBRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_HBRY) hbry,
        BW_HBRQ,
        BW_HBYY,
        JSFS,
        h.AZFS,
        IMAGE,
        SIGNATURE,
        (select file_path from TF_YWPN_FILE_W where id = SIGNATURE) SIGNATURE_PATH
        from TF_YWPN_QFKHJBXX_W k
        left join TF_YWPN_BWHBDATAIL_W h on h.BW_KHBH = k.YHBH
        where to_char(BW_HBRQ,'yyyy-MM-dd') between #{dto.start} and #{dto.end}
        <if test="dto.input != null and dto.input != ''">
            and bw_khbh || BW_KHXM || BW_KHDZ || BW_XBBH like '%' || #{dto.input} || '%'
        </if>
        <if test="dto.user != null and dto.user != ''">
            and BW_HBRY in
            <foreach collection="dto.user.split(',')" item="u" open="(" separator="," close=")">
                #{u}
            </foreach>
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_XBCJ in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and bw_hbbm in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        <if test="dto.cal != null and dto.cal.size() != 0">
            and bw_xbkj in
            <foreach collection="dto.cal" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="dto.isSyn != null">
            <choose>
                <when test="dto.isSyn == 2">
                    and (is_sync = 2 or is_sync = 3)
                </when>
                <otherwise>
                    and is_sync = #{dto.isSyn}
                </otherwise>
            </choose>

        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and bw_xblx in
            <foreach collection="dto.type" item="t" open="(" separator="," close=")">
                #{t}
            </foreach>
        </if>
    </select>

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

    <select id="changeTableCause" resultType="map">
        select CNAME HBYY_MC, CCODE HBYY_BM
        from TF_YWPN_SYSCHILDCODE_D
        where PCID = 66327
        order by CCODE
    </select>

    <select id="changeTableCountByType" resultType="map">
        select BW_XBLX,
        (
        <include refid="table_code">
            <property name="pcid" value="3"/>
            <property name="code" value="BW_XBLX"/>
        </include>
        ) sblx_name,
        sum(case when IS_SYNC = 0 then 1 else 0 end) zc_num,
        sum(case when IS_SYNC = 1 then 1 else 0 end) tb_num,
        sum(case when IS_SYNC = 2 or IS_SYNC = 3 then 1 else 0 end) wtb_num
        from TF_YWPN_BWHBDATAIL_W
        where to_char(BW_HBRQ,'yyyy-MM-dd') between #{dto.start} and #{dto.end}
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_XBCJ in
            <foreach collection="dto.vendor" item="v" open="(" separator="," close=")">
                #{v}
            </foreach>
        </if>
        <if test="dto.dept != null and dto.dept.size() != 0">
            and bw_hbbm in
            <foreach collection="dto.dept" item="d" open="(" separator="," close=")">
                #{d}
            </foreach>
        </if>
        group by BW_XBLX
    </select>

</mapper>