<?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.FirstInspectionMapper">
    <!--    <select id="count" resultType="map">
            select BW_SBKJ,
            (select cname from TF_YWPN_SYSCHILDCODE_D where PCID = 1 and CCODE = BW_SBKJ) sbkj_name,
            sum(BW_CJSL) rksl,
            BW_CJBL,
            sum(BW_HGSL) hgsl,
            sum(BW_CJSL) cjzs,
            round(((sum(BW_CJSL) - sum(BW_HGSL)) / nullif(sum(BW_HGSL),0)) * 100,2) hgl,
            (
            <include refid="zjzt_count">
                <property name="state" value="0"/>
            </include>
            ) dzjsl,
            (
            <include refid="zjzt_count">
                <property name="state" value="1"/>
            </include>
            ) yzjsl
            from TF_YWPN_BWSJDATAIL_W a
            <where>
                <if test="dto.vendor != null and dto.vendor.size() != 0">
                    BW_SBCJ in
                    <foreach collection="dto.sbcj" item="code" open="(" separator="," close=")">
                        #{code}
                    </foreach>
                </if>
                <choose>
                    <when test="dto.month == null">
                        and to_char(OPERATE_DATE,'yyyy-mm') = to_char(sysdate,'yyyy-mm')
                    </when>
                    <otherwise>
                        and to_char(OPERATE_DATE,'yyyy-mm') = #{month}
                    </otherwise>
                </choose>
            </where>
            group by BW_SBKJ,BW_CJBL
        </select>-->

    <select id="count" resultType="map">
        select BW_SBKJ,
        (select cname from TF_YWPN_SYSCHILDCODE_D where PCID = 1 and CCODE = to_char(BW_SBKJ)) sbkj_name,
        BW_CJBL,
        sum(BW_HGSL) hgsl,
        sum(BW_BHGS) bhgs,
        sum(BW_CJSL) cjzs,
        sum(BW_SBSL) rksl,
        sum (case when IS_CHECK = 0 then 1 else 0 end) dzjsl,
        sum(case when IS_CHECK = 1 then 1 else 0 end) yzjsl,
        round((sum(BW_HGSL) / nullif(sum(BW_CJSL),0)) * 100,2) hgl
        from TF_YWPN_BWSJDATAIL_W a
        where to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_SBCJ in
            <foreach collection="dto.vendor" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        group by BW_SBKJ,BW_CJBL
        order by BW_SBKJ
    </select>

    <select id="getBatchInfo" resultType="map">
        select id,
        bw_rkid,
        bw_pcbh,
        bw_sbkj,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 1 and CCODE = to_char(BW_SBKJ)) sbkj_name,
        BW_SBCJ,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 2 and CCODE = to_char(BW_SBCJ))sbcj_name,
        BW_SBLX,
        (select cname from TF_YWPN_SYSCHILDCODE_D where PCID = 3 and CCODE = to_char(BW_SBLX)) sblx_name,
        bw_sbsl,
        bw_cjsl,
        bw_cjbl,
        bw_hgsl,
        bw_hgbl,
        bw_jybm,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = BW_JYBM) jybm_name,
        bw_jyrq,
        operate_user,
        (select REAL_NAME from TF_SMPN_USER_X where id = operate_user) user_name,
        operate_date,
        bw_bz,
        decode(is_check,'0','未处理','已处理') state
        from TF_YWPN_BWSJDATAIL_W
        <where>
            <if test="isCheck != null">
                is_check = #{isCheck}
            </if>
            <if test="cal != null">
                and bw_sbkj = #{cal}
            </if>
        </where>
    </select>

    <select id="queryInventory" resultType="map">
        select id,
        bw_lsbh,
        bw_sbcj,
        (
        <include refid="code_query">
            <property name="pcid" value="2"/>
            <property name="code" value="bw_sbcj"/>
        </include>
        ) sbcj_name,
        bw_sblx,
        (
        <include refid="code_query">
            <property name="pcid" value="3"/>
            <property name="code" value="bw_sblx"/>
        </include>
        ) sblx_name,
        bw_sbkj,
        (
        <include refid="code_query">
            <property name="pcid" value="1"/>
            <property name="code" value="bw_sbkj"/>
        </include>
        ) sbkj_name,
        bw_sl,
        create_user,
        (select REAL_NAME from TF_SMPN_USER_X where id = b.create_user) user_name,
        create_date
        from TF_YWPN_BWRKDETAIL_W b
        where BW_SBKJ = #{dto.cal}
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_SBCJ in
            <foreach collection="dto.vendor" item="dept" open="(" separator="," close=")">
                #{dept}
            </foreach>
        </if>
        <choose>
            <when test="dto.start == null and dto.end == null">
                and create_date between add_months(sysdate,-1) and sysdate
            </when>
            <otherwise>
                and to_char(create_date,'yyyy-mm') between #{dto.start} and #{dto.end}
            </otherwise>
        </choose>
    </select>

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

    <select id="queryFirstCheck" resultType="map">
        select id,
        bw_rkid,
        bw_pcbh,
        bw_sbkj,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 1 and CCODE = to_char(BW_SBKJ)) sbkj_name,
        BW_SBCJ,
        (select CNAME from TF_YWPN_SYSCHILDCODE_D where PCID = 2 and CCODE = to_char(BW_SBCJ))sbcj_name,
        BW_SBLX,
        (select cname from TF_YWPN_SYSCHILDCODE_D where PCID = 3 and CCODE = to_char(BW_SBLX)) sblx_name,
        bw_sbsl,
        bw_cjsl,
        bw_cjbl,
        bw_hgsl,
        BW_BHGS,
        bw_hgbl,
        bw_jybm,
        (select NAME from TF_SMPN_DEPARTMENT_X where id = BW_JYBM) jybm_name,
        bw_jyrq,
        operate_user,
        (select REAL_NAME from TF_SMPN_USER_X where id = operate_user) user_name,
        operate_date,
        bw_bz,
        IS_CHECK,
        decode(is_check,'0','未处理','已处理') state
        from TF_YWPN_BWSJDATAIL_W
        where BW_SBKJ = #{dto.cal}
        <if test="dto.vendor != null and dto.vendor.size() != 0">
            and BW_SBCJ in
            <foreach collection="dto.vendor" item="vd" open="(" separator="," close=")">
                #{vd}
            </foreach>
        </if>
        <choose>
            <when test="dto.start == null and dto.end == null">
                and CREATE_DATE between add_months(sysdate,-1) and sysdate
            </when>
            <otherwise>
                and to_char(CREATE_DATE,'yyyy-mm-dd') between #{dto.start} and #{dto.end}
            </otherwise>
        </choose>
        <choose>
            <when test="dto.state == 3">
                and is_check = 0
            </when>
            <when test="dto.state == 4">
                and is_check = 1
            </when>
            <when test="dto.state == 5">
                and is_check = 1 and bw_hgsl is not null
            </when>
            <when test="dto.state == 6">
                and is_check = 1 and bw_bhgs is not null
            </when>
        </choose>
    </select>
</mapper>

