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

    <resultMap id="qfcbcqglMap" type="com.tofly.watermeter.entity.Qfcbcqgl">
        <id property="id" column="ID"/>
        <result property="khbh" column="KHBH"/>
        <result property="cqjd" column="CQJD"/>
        <result property="node" column="NODE"/>
        <result property="files" column="FILES"/>
        <result property="createTime" column="CREATE_TIME"/>
        <result property="createUser" column="CREATE_USER"/>
        <result property="state" column="STATE"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <association property="stateName" column="ID"
                     select="getStateName"/>
        <association property="createUserName" column="CREATE_USER"
                     select="com.tofly.baseapi.mapper.ToflyCodeCommonMapper.getUserName"/>
        <collection property="filePaths" column="FILES" select="getFilePaths"/>
    </resultMap>

    <select id="getStateName" resultType="string">
        select decode(STATE, 0, '正常提交', 1, '重复提交')
        from TF_YWPN_QFCBCQGL_W
        where ID = #{id}
    </select>

    <select id="getFilePaths" resultType="string">
        select FILE_PATH
        from TF_YWPN_FILE_W
        where ID in (${files})
    </select>

    <select id="countInfo" resultType="map" fetchSize="100">
        select ROWNUM no, khbh, yhxm, yhdz, lxdh,YSXZ_MC, qymc, qydmc,sbkj,sbcj,sblx, cs_num, zt_num, gs_num,
        fs_num,cs_time,zt_time,gs_time,fs_time
        from
        (select c.KHBH,
        k.YHXM,
        k.YHDZ,
        k.LXDH,
        y.QYMC,
        d.QYDMC,
        q.YSXZ_MC,
        (select cname from TF_YWPN_SYSCHILDCODE_D where pcid = 1 and ccode = SBKJ) sbkj,
        (select cname from TF_YWPN_SYSCHILDCODE_D where pcid = 2 and ccode = SBCJ) sbcj,
        (select cname from TF_YWPN_SYSCHILDCODE_D where pcid = 3 and ccode = SBLX) sblx,
        sum(case when CQJD = 1 then 1 else 0 end) cs_num,
        (select max(CREATE_TIME) from TF_YWPN_QFCBCQGL_W where cqjd = 1 and khbh = c.KHBH) cs_time,
        sum(case when CQJD = 2 then 1 else 0 end) zt_num,
        (select max(CREATE_TIME) from TF_YWPN_QFCBCQGL_W where cqjd = 2 and khbh = c.KHBH) zt_time,
        sum(case when CQJD = 3 then 1 else 0 end) gs_num,
        (select max(CREATE_TIME) from TF_YWPN_QFCBCQGL_W where cqjd = 3 and khbh = c.KHBH) gs_time,
        sum(case when CQJD = 4 then 1 else 0 end) fs_num,
        (select max(CREATE_TIME) from TF_YWPN_QFCBCQGL_W where cqjd = 4 and khbh = c.KHBH) fs_time
        from TF_YWPN_QFCBCQGL_W c
        left join TF_YWPN_QFKHJBXX_W k on c.KHBH = k.YHBH
        left join TF_YWPN_YHBINGQY_W b on c.KHBH = b.YHBH
        left join TF_YWPN_CBQYBM_W y on y.QYBM = b.QYBM
        left join TF_YWPN_CBQYDBM_W d on d.QYDBM = b.QYDBM
        left join TF_YWPN_QFMIDYSXZBM_E q on q.YSXZ_BM = k.YSXZ
        where state = 0
        <if test="input != null and input != ''">
            and c.KHBH || k.YHXM || k.YHDZ || k.LXDH like '%' || #{input} || '%'
        </if>
        <if test="start != null and start != '' and end != null and end != ''">
            and to_char(c.create_time,'yyyy-mm-dd') between #{start} and #{end}
        </if>
        group by c.KHBH, k.YHXM, k.YHDZ, k.LXDH, y.QYMC, d.QYDMC, SBKJ, SBCJ, SBLX,q.YSXZ_MC)
    </select>

    <select id="queryInfoByCode" resultType="map">
        select k.YHBH                                                                     khbh,
               k.YHXM,
               k.YHDZ,
               k.LXDH,
               y.QYMC,
               q.YSXZ_MC,
               d.QYDMC,
               (select cname from TF_YWPN_SYSCHILDCODE_D where pcid = 1 and ccode = SBKJ) sbkj,
               (select cname from TF_YWPN_SYSCHILDCODE_D where pcid = 2 and ccode = SBCJ) sbcj,
               (select cname from TF_YWPN_SYSCHILDCODE_D where pcid = 3 and ccode = SBLX) sblx
        from TF_YWPN_QFKHJBXX_W k
                 left join TF_YWPN_YHBINGQY_W b on k.YHBH = b.YHBH
                 left join TF_YWPN_CBQYBM_W y on y.QYBM = b.QYBM
                 left join TF_YWPN_CBQYDBM_W d on d.QYDBM = b.QYDBM
                 left join TF_YWPN_QFMIDYSXZBM_E q on q.YSXZ_BM = k.YSXZ
        where k.YHBH = #{code}
    </select>

    <select id="rushToOweInfo" resultType="map">
        select c.id, CREATE_TIME, CNAME,STATE,AUDIT_TIME,
        decode(STATE, 0, '正常提交', 1, '重复提交') state_name
        from TF_YWPN_QFCBCQGL_W c
        left join TF_YWPN_SYSCHILDCODE_D s on to_char(c.CQJD) = s.CCODE
        where PCID = 2013 and KHBH = #{code}
        <if test="node != null and node != ''">
            and cqjd in (${node})
        </if>
        <if test="start != null and start != '' and end != null and end != ''">
            and to_char(c.create_time,'yyyy-mm-dd hh24:mi:ss') between #{start} and #{end}
        </if>
        order by c.create_time desc
    </select>

</mapper>
