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


    <select id="count" resultType="map">
        select DJBM dept_id,
        (select name from TF_SMPN_DEPARTMENT_X where id = DJBM) dept_name,
        count( GDBH) sum_num,
        sum(decode(IS_WATERMETER_STATUS,2,1,0)) ycl_num,
        sum(decode(IS_WATERMETER_STATUS,1,1,0)) wcl_number,
        WM_CONCAT(GDBH) codes
        from TF_YWPN_GDINFO_W g1
        where IS_WATERMETER_STATUS in (1,2)
        <if test="dto.start != null and dto.end != null">
            and to_char(DJSJ,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        </if>
        <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>
        group by DJBM

    </select>

    <select id="countByWorkOrder" resultType="map">
        select
        (select sum(BW_SL) from TF_YWPN_BWCKDATAIL_W where id in (select SKID from TF_YWPN_BWGDANDCK_Z where GDBH =
        GDID)) cksl_num,
        (
        <include refid="ck_query">
            <property name="column" value="BW_BZ"/>
        </include>
        ) ckbz,
        (select real_name from TF_SMPN_USER_X where ID =
        (
        <include refid="ck_query">
            <property name="column" value="CREATE_USER"/>
        </include>
        )
        ) clry_name,
        (case when (select count(*) from TF_YWPN_BWGDANDCK_Z where GDID = GDBH) > 0 then 1 else 0 end ) clzt,
        (
        <include refid="ck_query">
            <property name="column" value="CREATE_DATE"/>
        </include>
        ) clsj,
        GDBH,
        GDLX,
        (select GDLX_MC from TF_YWPN_GDGLTYPEBM_W where id = GDLX) gdlx_mc,
        GDMS,
        DJRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = DJRY) sqry,
        DJBM,
        (select name from TF_SMPN_DEPARTMENT_X where id = DJBM) sqbm,
        DJSJ,
        FINISH_TIME,
        IS_WATERMETER_STATUS
        from TF_YWPN_GDINFO_W
        <where>
            <if test="dto.start != null and dto.end != null">
                and to_char(DJSJ,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </if>
            <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 == 0">
                    and IS_WATERMETER_STATUS = 1
                </when>
                <when test="dto.state == 1">
                    and IS_WATERMETER_STATUS = 2
                </when>
                <otherwise>
                    and IS_WATERMETER_STATUS in (1,2)
                </otherwise>
            </choose>
        </where>
    </select>

    <sql id="ck_query">
        select ${column}
        from TF_YWPN_BWCKDATAIL_W
        where id in (select SKID
                     from TF_YWPN_BWGDANDCK_Z
                     where GDBH = GDID
                       and ROWNUM = 1)
    </sql>

    <select id="queryTableInfo" resultType="com.tofly.watermeter.custom.entity.meterBusiness.vo.OutBoundVo">
        select
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where CCODE = to_char(r.BW_SBCJ) and PCID = 2) sbcj,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where CCODE = to_char(r.BW_SBKJ) and PCID = 1) sbkj,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where CCODE = to_char(r.BW_SBLX) and PCID = 3) sblx,
        BW_SBBH sbbh,
        BW_LQBM,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = BW_LQBM) lbbm,
        BW_LQRY,
        (select REAL_NAME from TF_SMPN_USER_X where id = BW_LQRY) lbry,
        c.CREATE_USER,
        (select REAL_NAME from TF_SMPN_USER_X where id = c.CREATE_USER) user_name,
        c.CREATE_DATE
        from TF_YWPN_BWCKDATAIL_W c left join TF_YWPN_BWRKSBDATAIL_W r on c.ID = r.CK_ID
        where CK_ID in
        <foreach collection="ckIdList" item="ckid" open="(" separator="," close=")">
            #{ckid}
        </foreach>
        <if test="start != null and start != '' and end != null and end != ''">
            and to_char(c.CREATE_DATE,'yyyy-mm-dd') between #{start} and #{end}
        </if>
    </select>

    <update id="updateWorkOrder">
        update TF_YWPN_GDINFO_W
        set IS_WATERMETER_STATUS = 2
        where GDBH = #{gdbh}
    </update>

    <select id="getGdbh" resultType="string">
        select gdbh
        from TF_YWPN_GDINFO_W
        where IS_WATERMETER_STATUS = 1
          and gdbh = #{dto.code}
    </select>

    <select id="getWareHousinginfo" resultType="map">
        SELECT
        ( SELECT CNAME FROM TF_YWPN_SYSCHILDCODE_D WHERE CCODE = TO_CHAR( BW_SBCJ ) AND PCID = 2 ) sbcj,
        ( SELECT CNAME FROM TF_YWPN_SYSCHILDCODE_D WHERE CCODE = TO_CHAR( BW_SBKJ ) AND PCID = 1 ) sbkj,
        ( SELECT CNAME FROM TF_YWPN_SYSCHILDCODE_D WHERE CCODE = TO_CHAR( BW_SBLX ) AND PCID = 3 ) sblx,
        BW_SBBH,
        id
        FROM
        TF_YWPN_BWRKSBDATAIL_W
        WHERE
        ( BW_SBZT = '1' OR BW_SBZT = '8' )
        <if test="dto.excludeId != null and dto.excludeId.size() != 0">
            and id in
            <foreach collection="dto.excludeId" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_SBCJ in
            <foreach collection="dto.vendor" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and BW_SBLX in
            <foreach collection="dto.type" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <if test="dto.caliber != null and dto.caliber.size() != 0">
            and BW_SBKJ in
            <foreach collection="dto.caliber" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <if test="dto.startCode != null and dto.startCode != ''">
            and bw_sbbh &gt;= #{dto.startCode}
        </if>
        <if test="dto.endCode != null and dto.endCode != ''">
            and bw_sbbh &lt;= #{dto.endCode}
        </if>
        order by bw_sbbh
    </select>

    <select id="getCountInfo" resultType="com.tofly.watermeter.custom.entity.meterBusiness.dto.RemoveTableDto">
        select bw_sbcj sbcj, bw_sblx sblx, bw_sbkj sbkj, count(*) num
        from TF_YWPN_BWRKSBDATAIL_W
        where 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>
        group by bw_sbcj, bw_sblx, bw_sbkj
    </select>

    <select id="getWorkOrderCode" resultType="long">
        select LCJD
        from TF_YWPN_GDINFO_W
        where GDBH = #{orderCode}
    </select>

    <select id="outBoundCount" resultType="long">
        select sum(BW_SL)
        from TF_YWPN_BWCKDATAIL_W c
        where exists(select SKID from TF_YWPN_BWGDANDCK_Z g where c.id = g.SKID and GDID in
        <foreach collection="codes.split(',')" item="cd" index="idx" separator="," open="(" close=")">
            <if test="(idx % 999) == 998">NULL ) OR GDID IN (</if>
            #{cd}
        </foreach>
        )
    </select>

</mapper>