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

    <select id="count" resultType="map">
        select
        BW_SBCJ,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 2 and CCODE = BW_SBCJ) sbcj_name,
        sum(case when BW_YJLX = 3 then 1 else 0 end) zjyj, --周检移交
        sum(case when BW_YJLX = 2 then 1 else 0 end) gzyj, --故障移交
        sum(case when BW_YJLX = 4 then 1 else 0 end) xhyj, --销户移交
        sum(case when BW_YJLX = 5 then 1 else 0 end) shhb, --售后换表
        count(*) sum_num
        from TF_YWPN_BWSBYJGL_W a
        where BW_YJLX != 1
        <if test="dto.dept != null and dto.dept.size() != 0">
            and bw_sbcj in
            <foreach collection="dto.dept" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end !=''">
            and to_char(BW_YJRQ,'yyyy-mm') between #{dto.start} and #{dto.end}
        </if>
        group by BW_SBCJ
    </select>

    <select id="countByTime" resultType="map">
        select
        to_char(BW_YJRQ,'yyyy-mm') yjrq,
        sum(case when BW_YJLX = 3 then 1 else 0 end) zjyj, --周检移交
        sum(case when BW_YJLX = 2 then 1 else 0 end) gzyj, --故障移交
        sum(case when BW_YJLX = 4 then 1 else 0 end) xhyj, --销户移交
        sum(case when BW_YJLX = 5 then 1 else 0 end) shhb, --售后换表
        count(*) sum_num
        from TF_YWPN_BWSBYJGL_W a
        where BW_YJLX != 1
        <if test="dto.dept != null and dto.dept.size() != 0">
            and bw_sbcj in
            <foreach collection="dto.dept" item="dp" open="(" separator="," close=")">
                #{dp}
            </foreach>
        </if>
        <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end !=''">
            and to_char(BW_YJRQ,'yyyy-mm') between #{dto.start} and #{dto.end}
        </if>
        group by to_char(BW_YJRQ,'yyyy-mm')
    </select>

    <select id="queryInfo" resultType="map">
        select id,
        bw_lsbh,
        bw_sbcj,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="2"/>-->
        <!--            <property name="code" value="bw_sbcj"/>-->
        <!--        </include>-->
        <!--        ) sbcj_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBCJ
        and pcid = 2) sbcj_name,
        bw_sblx,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="3"/>-->
        <!--            <property name="code" value="bw_sblx"/>-->
        <!--        </include>-->
        <!--        ) sblx_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBLX
        and pcid = 2) sblx_name,
        bw_sbkj,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="1"/>-->
        <!--            <property name="code" value="bw_sbkj"/>-->
        <!--        </include>-->
        <!--        ) sbkj_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_SBKJ
        and pcid = 1) sbkj_name,
        bw_yjlx,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="9"/>-->
        <!--            <property name="code" value="bw_yjlx"/>-->
        <!--        </include>-->
        <!--        ) yjlx_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = BW_YJLX
        and pcid = 9) yjlx_name,
        bw_yjyy,
        bw_jsbm,
        (select name from TF_SMPN_DEPARTMENT_X where ID = bw_jsbm) jsbm_name,
        bw_jsry,
        (select REAL_NAME from TF_SMPN_USER_X where ID = bw_jsry) jsry_name,
        bw_yjrq,
        create_user,
        create_date,
        sbzsbh,
        bw_yjry,
        (select REAL_NAME from TF_SMPN_USER_X where ID = bw_yjry) yjry_name,
        (select name from TF_SMPN_DEPARTMENT_X where id = (select DEPARTMENT_ID from TF_SMPN_USER_X where id = bw_yjry))
        yjbm_name
        from TF_YWPN_BWSBYJGL_W
        where BW_YJLX != 1
        <if test="dto.type != null and dto.type.size() != 0">
            and BW_YJLX in
            <foreach collection="dto.type" item="tp" open="（" separator="," close=")">
                #{tp}
            </foreach>
        </if>
        <choose>
            <when test="dto.dept != null and dto.dept.size() != 0">
                and bw_sbcj in
                <foreach collection="dto.dept" item="dp" open="(" separator="," close=")">
                    #{dp}
                </foreach>
            </when>
            <otherwise>
                and bw_sbcj is null
            </otherwise>
        </choose>
        <if test="dto.start != null and dto.start != '' and dto.end != null and dto.end !=''">
            and to_char(BW_YJRQ,'yyyy-mm') between #{dto.start} and #{dto.end}
        </if>
    </select>

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

    <insert id="importTurnOver">
        insert into TF_YWPN_BWSBYJGL_W (ID, BW_LSBH, BW_SBCJ, BW_SBLX, BW_SBKJ, BW_YJLX,
        BW_YJYY, BW_JSBM, BW_JSRY, BW_YJRQ, CREATE_USER, CREATE_DATE, BW_RKBS, SBZSBH, BW_YJRY,
        YJXX,USER_NAME,ADDRESS,SBCJ,SBKJ,SBLX,SBBH,BW_YJBM,CHECK_FLAG)
        <foreach collection="list" item="gl" separator="union all">
            select
            #{gl.id,jdbcType = NUMERIC},
            #{gl.bwLsbh,jdbcType = VARCHAR},
            #{gl.bwSbcj,jdbcType = NUMERIC},
            #{gl.bwSblx,jdbcType = NUMERIC},
            #{gl.bwSbkj,jdbcType = NUMERIC},
            #{gl.bwYjlx,jdbcType = NUMERIC},
            #{gl.bwYjyy,jdbcType = VARCHAR},
            #{gl.bwJsbm,jdbcType = NUMERIC},
            #{gl.bwJsry,jdbcType = NUMERIC},
            sysdate,
            #{gl.createUser,jdbcType = NUMERIC},
            sysdate,
            '1',
            #{gl.sbzsbh,jdbcType = VARCHAR},
            #{gl.bwYjry,jdbcType = NUMERIC},
            '1',
            #{gl.userName,jdbcType = VARCHAR} ,
            #{gl.address,jdbcType = VARCHAR},
            #{gl.sbcj,jdbcType = NUMERIC},
            #{gl.sbkj,jdbcType = NUMERIC},
            #{gl.sblx,jdbcType = NUMERIC},
            #{gl.sbbh,jdbcType = VARCHAR},
            #{gl.bwYjbm,jdbcType = NUMERIC},
            #{gl.checkFlag,jdbcType = VARCHAR}
            from DUAL
        </foreach>
    </insert>

    <select id="canTurnOver" resultType="map">
        select id,
        bw_lsbh,
        BW_SBBH,
        bw_sbcj,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="2"/>-->
        <!--            <property name="code" value="bw_sbcj"/>-->
        <!--        </include>-->
        <!--        ) sbcj_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = bw_sbcj
        and pcid = 2) sbcj_name,
        bw_sblx,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="3"/>-->
        <!--            <property name="code" value="bw_sblx"/>-->
        <!--        </include>-->
        <!--        ) sblx_name,-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = bw_sblx
        and pcid = 3) sblx_name,
        bw_sbkj,
        <!--        (-->
        <!--        <include refid="table_code">-->
        <!--            <property name="pcid" value="1"/>-->
        <!--            <property name="code" value="bw_sbkj"/>-->
        <!--        </include>-->
        <!--        ) sbkj_name-->
        (select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ccode = bw_sbkj
        and pcid = 1) sbkj_name
        from TF_YWPN_BWRKSBDATAIL_W
        where BW_SBZT = #{dto.state}
        <if test="dto.vendor != null and dto.vendor != ''">
            and BW_SBCJ = #{dto.vendor}
        </if>
        <if test="dto.cal != null and dto.cal != ''">
            and bw_sbkj = #{dto.cal}
        </if>
        <if test="dto.type != null and dto.type.size() != 0">
            and BW_SBLX in
            <foreach collection="dto.type" item="tp" open="（" separator="," close=")">
                #{tp}
            </foreach>
        </if>
        <if test="dto.str != null and dto.str != ''">
            and bw_sbbh like '%' || #{dto.str} || '%'
        </if>
    </select>

    <select id="queryTableState" resultType="com.tofly.watermeter.entity.Bwrksbdatail">
        select BW_SBCJ,BW_SBKJ,BW_SBLX,BW_SBBH
        from TF_YWPN_BWRKSBDATAIL_W
        <where>
            <if test="codeList != null and codeList.size() != 0">
                bw_sbbh in
                <foreach collection="codeList" item="cd" index="idx" separator="," open="(" close=")">
                    <if test="(idx % 999) == 998">NULL ) OR BW_SBBH IN (</if>
                    #{cd}
                </foreach>
            </if>

            <if test="state != null and state.size() != 0">
                and BW_SBZT in
                <foreach collection="state" item="st" open="(" separator="," close=")">
                    #{st}
                </foreach>
            </if>
        </where>
    </select>

    <update id="updateTableState">
        update TF_YWPN_BWRKSBDATAIL_W
        set BW_SBZT = #{state}
        where bw_sbbh in
        <foreach collection="codeList" item="cd" index="idx" separator="," open="(" close=")">
            <if test="(idx % 999) == 998">NULL ) OR BW_SBBH IN (</if>
            #{cd}
        </foreach>
    </update>

    <select id="scrapStatistics" resultType="map">
        select BW_SBCJ vendor,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 2 and CCODE = BW_SBCJ) vendor_name,
        sum(case when BW_SBZT = 7 then 1 else 0 end) scrap_num,
        count(*) total_num
        from TF_YWPN_BWRKSBDATAIL_W
        <where>
            <if test="dto.vendor != null and dto.vendor != ''">
                BW_SBCJ in
                <foreach collection="dto.vendor.split(',')" item="v" open="(" separator="," close=")">
                    #{v}
                </foreach>
            </if>
        </where>
        group by BW_SBCJ
    </select>

</mapper>