<?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.mapper.BwzysbjddatailMapper">

    <resultMap id="bwzysbjddatailMap" type="com.tofly.watermeter.entity.Bwzysbjddatail">
        <id property="id" column="ID"/>
        <result property="bwLsbh" column="BW_LSBH"/>
        <result property="bwSjly" column="BW_SJLY"/>
        <result property="bwJyjg" column="BW_JYJG"/>
        <result property="bwJyrq" column="BW_JYRQ"/>
        <result property="bwPhoto" column="BW_PHOTO"/>
        <result property="bwFileid" column="BW_FILEID"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="createTime" column="CREATE_DATE"/>
        <result property="sbbh" column="SBBH"/>
        <result property="bwBz" column="BW_BZ"/>
        <result property="bwJyry" column="BW_JYRY"/>
        <result property="sbid" column="SBID"/>
        <result property="bwSbcj" column="BW_SBCJ"/>
        <result property="bwSblx" column="BW_SBLX"/>
        <result property="bwSbkj" column="BW_SBKJ"/>
        <result property="bwGdbh" column="BW_GDBH"/>
        <result property="bwYhbh" column="BW_YHBH"/>
        <result property="bwYhxm" column="BW_YHXM"/>
        <result property="bwYhdz" column="BW_YHDZ"/>
        <result property="bwLxdh" column="BW_LXDH"/>
        <result property="bwTbsl" column="BW_TBSL"/>
        <result property="bwBsl" column="BW_BSL"/>
        <result property="wxjdyy" column="WXJDYY"/>
        <association property="bwJyryName" column="BW_JYRY"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <association property="sbcjName" column="BW_SBCJ" select="getCodeById"/>
        <association property="sbkjName" column="BW_SBKJ" select="getCodeById"/>
        <association property="sblxName" column="BW_SBLX" select="getCodeById"/>
        <association property="jyjgName" column="BW_JYJG" select="getjyjgName"/>
    </resultMap>

    <select id="getjyjgName" resultType="string">
        select decode(#{jyjg}, '1','合格','2','不合格') from dual
    </select>

    <sql id="statisSQL">
        <if test="cj != null">
            and BW_SBCJ IN
            <foreach collection="cj" item="c" open="(" separator="," close=")">
                #{c}
            </foreach>
        </if>
        <if test="kj != null">
            and BW_SBKJ IN
            <foreach collection="kj" item="k" open="(" separator="," close=")">
                #{k}
            </foreach>
        </if>
        <if test="lx != null">
            and BW_SBLX IN
            <foreach collection="lx" item="l" open="(" separator="," close=")">
                #{l}
            </foreach>
        </if>
        <if test="sta.startMonth != null and sta.endMonth != null">
            and TO_CHAR(BW_JYRQ , 'yyyyMM') BETWEEN #{sta.startMonth} AND #{sta.endMonth}
        </if>
        <if test="sta.startTime != null and sta.endTime != null">
            and BW_JYRQ BETWEEN #{sta.startTime} AND #{sta.endTime}
        </if>
    </sql>

    <select id="getCollectCj" resultMap="getTjMap">
        SELECT <if test="sta.startMonth != null and sta.endMonth != null">TO_CHAR(BW_JYRQ , 'yyyyMM') MONTH ,</if>
               COUNT(*) as ALLNUM , BW_SBCJ as BWSBCJ FROM TF_YWPN_BWZYSBJDDATAIL_W
        <where>
            <include refid="statisSQL"></include>
        </where>
        GROUP BY
        BW_SBCJ
        <if test="sta.startMonth != null and sta.endMonth != null">
            , TO_CHAR(BW_JYRQ , 'yyyyMM') ORDER BY TO_CHAR(BW_JYRQ , 'yyyyMM')
        </if>
    </select>

    <select id="getSpecific" resultMap="getTjMap">
        SELECT <if test="sta.startMonth != null and sta.endMonth != null">TO_CHAR(BW_JYRQ , 'yyyyMM') MONTH ,</if>
               COUNT(*) as SPECIFICNUM , BW_SBCJ as BWSBCJ , BW_SBKJ as BWSBKJ , BW_SBLX as BWSBLX FROM TF_YWPN_BWZYSBJDDATAIL_W
        <where>
            <include refid="statisSQL"></include>
        </where>
        GROUP BY BW_SBCJ,BW_SBKJ,BW_SBLX
        <if test="sta.startMonth != null and sta.endMonth != null">
            , TO_CHAR(BW_JYRQ , 'yyyyMM') ORDER BY TO_CHAR(BW_JYRQ , 'yyyyMM')
        </if>
    </select>



    <resultMap id="getTjMap" type="com.tofly.watermeter.custom.entity.StatisticResult">
        <result property="month" column="MONTH"/>
        <result property="allNum" column="ALLNUM"/>
        <result property="specificNUM" column="SPECIFICNUM"/>
        <result property="num" column="NUM"/>
        <result property="cjId" column="BWSBCJ"/>
        <result property="kjId" column="BWSBKJ"/>
        <result property="lxId" column="BWSBLX"/>
        <association property="sbcj" column="BWSBCJ" select="getCodeById"/>
        <association property="sbkj" column="BWSBKJ" select="getCodeById"/>
        <association property="sblx" column="BWSBLX" select="getCodeById"/>
    </resultMap>
    <select id="getCodeById" resultType="string">
        select CNAME
        from TF_YWPN_SYSCHILDCODE_D
        where ID = #{id}
    </select>

</mapper>
