<?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.chinaweal.aiccs.aiccs.abnormal.mapper.AbnStatisticsMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.abnormal.entity.AbnStatistics">
        <id column="STATID" property="statid" />
        <result column="XZQH" property="xzqh" />
        <result column="ENTTYPE" property="enttype" />
        <result column="STATDATE" property="statdate" />
        <result column="TYPE1" property="type1" />
        <result column="TYPE2" property="type2" />
        <result column="TYPE3" property="type3" />
        <result column="TYPE4" property="type4" />
        <result column="TYPE5" property="type5" />
        <result column="TYPE6" property="type6" />
        <result column="TYPE7" property="type7" />
        <result column="TYPE8" property="type8" />
        <result column="TYPE9" property="type9" />
        <result column="TYPE10" property="type10" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <resultMap id="AbnStatisticsDto" type="com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnStatisticsDto">
        <id column="xzqh" property="xzqh" />
        <result column="type1" property="type1" />
        <result column="type2" property="type2" />
        <result column="type3" property="type3" />
        <result column="type4" property="type4" />
        <result column="type5" property="type5" />
        <result column="type6" property="type6" />
        <result column="type7" property="type7" />
        <result column="type8" property="type8" />
        <result column="type9" property="type9" />
        <result column="type10" property="type10" />
        <result column="type11" property="type11" />
        <result column="type12" property="type12" />
        <result column="type13" property="type13" />
        <result column="type14" property="type14" />
        <result column="type15" property="type15" />
        <result column="type16" property="type16" />
        <result column="type17" property="type17" />
        <result column="type18" property="type18" />
        <result column="type19" property="type19" />
        <result column="type20" property="type20" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        STATID, XZQH, ENTTYPE, STATDATE, TYPE1, TYPE2, TYPE3, TYPE4, TYPE5, TYPE6, TYPE7, TYPE8, TYPE9, TYPE10, create_time, update_time
    </sql>

    <select id="queryStatByDate" resultMap="BaseResultMap">
        select zs1.type1,zs1.type2,zs1.type3,zs1.type4,zs1.type5,zs2.type6,zs2.type7,zs2.type8,zs2.type9,zs2.type10 from (
            select
            count(case when SPECAUSE='1' then 1 else null end) as type1,
            count(case when SPECAUSE='2' then 1 else null end) as type2,
            count(case when SPECAUSE='3' then 1 else null end) as type3,
            count(case when SPECAUSE='4' then 1 else null end) as type4,
            count(case when SPECAUSE='5' then 1 else null end) as type5
            from
            (select * from aiccs.tsabnlist t where
                intoDecorg like concat(#{xzqh}, '%')
            and ABNTIME = #{abnTime}
            --      个体户
            <if test="entType eq 'gt'">
                and ENTTYPE = '9910'
            </if>
            --      企业
            <if test="entType eq 'qy'">
                and ENTTYPE not in ('9910','9100','9200')
            </if>
            group by ENTNAME,SPECAUSE) rs
        ) zs1,
        (
            select
            count(case when rs2.SPECAUSE='1' then 1 else null end) as type6,
            count(case when rs2.SPECAUSE='2' then 1 else null end) as type7,
            count(case when rs2.SPECAUSE='3' then 1 else null end) as type8,
            count(case when rs2.SPECAUSE='4' then 1 else null end) as type9,
            count(case when rs2.SPECAUSE='5' then 1 else null end) as type10
            from
            (select * from aiccs.tsabnlist t where
            REDECORG like concat(#{xzqh}, '%')
            and REMDATE = #{abnTime}
            --      个体户
            <if test="entType eq 'gt'">
                and ENTTYPE = '9910'
            </if>
            --      企业
            <if test="entType eq 'qy'">
                and ENTTYPE not in ('9910','9100','9200')
            </if>
            group by ENTNAME,SPECAUSE) rs2
        ) zs2
    </select>

    <select id="queryStatAll" resultMap="AbnStatisticsDto">
        select rs1.xzqh,type1,type2,type3,type4,type5,type6,type7,type8,type9,type10,
        type11,type12,type13,type14,type15,type16,type17,type18,type19,type20
        from (
            select xzqh,sum(type1) type1,sum(type2) type2,sum(type3) type3,
            sum(type4) type4,sum(type5) type5,sum(type6) type6,sum(type7) type7,
            sum(type8) type8,sum(type9) type9,sum(type10) type10
            from aiccs.abn_statistics as2
            where 1=1
            and ENTTYPE = '1'
            <if test="customParamMap.xzqh != null and customParamMap.xzqh neq ''">
                and xzqh = #{customParamMap.xzqh}
            </if>
            <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
                and STATDATE &gt;= #{customParamMap.startDate}
            </if>
            <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
                and STATDATE &lt;= #{customParamMap.endDate}
            </if>
            group by xzqh
        ) rs1, (
            select xzqh,sum(type1) type11,sum(type2) type12,sum(type3) type13,
            sum(type4) type14,sum(type5) type15,sum(type6) type16,sum(type7) type17,
            sum(type8) type18,sum(type9) type19,sum(type10) type20
            from aiccs.abn_statistics as2
            where 1=1
            and ENTTYPE = '2'
            <if test="customParamMap.xzqh != null and customParamMap.xzqh neq ''">
                and xzqh = #{customParamMap.xzqh}
            </if>
            <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
                and STATDATE &gt;= #{customParamMap.startDate}
            </if>
            <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
                and STATDATE &lt;= #{customParamMap.endDate}
            </if>
            group by xzqh
        ) rs2 where rs1.xzqh = rs2.xzqh
        union
        select '汇总' as xzqh,type1,type2,type3,type4,type5,type6,type7,type8,type9,type10,
        type11,type12,type13,type14,type15,type16,type17,type18,type19,type20
        from (
            select sum(type1) type1,sum(type2) type2,sum(type3) type3,
            sum(type4) type4,sum(type5) type5,sum(type6) type6,sum(type7) type7,
            sum(type8) type8,sum(type9) type9,sum(type10) type10
            from aiccs.abn_statistics as2
            where 1=1
            and ENTTYPE = '1'
            <if test="customParamMap.xzqh != null and customParamMap.xzqh neq ''">
                and xzqh = #{customParamMap.xzqh}
            </if>
            <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
                and STATDATE &gt;= #{customParamMap.startDate}
            </if>
            <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
                and STATDATE &lt;= #{customParamMap.endDate}
            </if>
        ) rs1, (
            select sum(type1) type11,sum(type2) type12,sum(type3) type13,
            sum(type4) type14,sum(type5) type15,sum(type6) type16,sum(type7) type17,
            sum(type8) type18,sum(type9) type19,sum(type10) type20
            from aiccs.abn_statistics as2
            where 1=1
            and ENTTYPE = '2'
            <if test="customParamMap.xzqh != null and customParamMap.xzqh neq ''">
                and xzqh = #{customParamMap.xzqh}
            </if>
            <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
                and STATDATE &gt;= #{customParamMap.startDate}
            </if>
            <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
                and STATDATE &lt;= #{customParamMap.endDate}
            </if>
        ) rs2
    </select>

    <select id="statisticsForAbn" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnStatDto">
        select rs1.*, rs2.* from (
        select count(case when SPECAUSE='1' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr1,
        count(case when SPECAUSE='2' and  enttype not in ('9910','9100','9200') then 1 else null end) as qylr2,
        count(case when SPECAUSE='3' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr3,
        count(case when SPECAUSE='4' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr4,
        count(case when SPECAUSE='5' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr5,
        count(case when SPECAUSE='6' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr6,
        count(case when SPECAUSE='1' and enttype = '9910' then 1 else null end) as gtlr1,
        count(case when SPECAUSE='2' and enttype = '9910' then 1 else null end) as gtlr2,
        count(case when (SPECAUSE='3' or SPECAUSE='4') and enttype = '9910' then 1 else null end) as gtlr3,
        count(case when SPECAUSE='1' and enttype in ('9100', '9200') then 1 else null end) as nhlr1,
        count(case when SPECAUSE='2' and enttype in ('9100', '9200') then 1 else null end) as nhlr2,
        count(case when SPECAUSE='3' and enttype in ('9100', '9200') then 1 else null end) as nhlr3
        from (
        select * from aiccs.tsabnlist where 1=1
        <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
            and abntime &gt;= #{customParamMap.startDate}
        </if>
        <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
            and abntime &lt;= #{customParamMap.endDate}
        </if>
        <if test="customParamMap.xzqh != null and customParamMap.xzqh != '' ">
            and intoDecorg like concat(#{customParamMap.xzqh}, '%')
        </if>
        group by pripid, SPECAUSE) rs
        )rs1,
        (
        select count(case when SPECAUSE='1' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc1,
        count(case when SPECAUSE='2' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc2,
        count(case when SPECAUSE='3' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc3,
        count(case when SPECAUSE='4' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc4,
        count(case when SPECAUSE='5' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc5,
        count(case when SPECAUSE='6' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc6,
        count(case when SPECAUSE='1' and enttype = '9910' and OPERATESTATUS = '0' then 1 else null end) as gtyc1,
        count(case when SPECAUSE='2' and enttype = '9910' and OPERATESTATUS = '0' then 1 else null end) as gtyc2,
        count(case when (SPECAUSE='3' or SPECAUSE='4') and enttype = '9910' and OPERATESTATUS = '0' then 1 else null end) as gtyc3,
        count(case when SPECAUSE='1' and enttype in ('9100', '9200') and OPERATESTATUS = '0' then 1 else null end) as nhyc1,
        count(case when SPECAUSE='2' and enttype in ('9100', '9200') and OPERATESTATUS = '0' then 1 else null end) as nhyc2,
        count(case when SPECAUSE='3' and enttype in ('9100', '9200') and OPERATESTATUS = '0' then 1 else null end) as nhyc3,
        count(case when enttype not in ('9910', '9100', '9200') and OPERATESTATUS = '2' then 1 else null end) as qyyy,
        count(case when enttype = '9910' and OPERATESTATUS = '2' then 1 else null end) as gtyy,
        count(case when enttype in ('9100', '9200') and OPERATESTATUS = '2' then 1 else null end) as nhyy
        from (
        select * from aiccs.tsabnlist where 1=1
        <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
            and remdate &gt;= #{customParamMap.startDate}
        </if>
        <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
            and remdate &lt;= #{customParamMap.endDate}
        </if>
        <if test="customParamMap.xzqh != null and customParamMap.xzqh != '' ">
            and redecorg like concat(#{customParamMap.xzqh}, '%')
        </if>
        group by pripid, SPECAUSE) rs
        ) rs2
    </select>

    <select id="statisticsForAbnHz" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnStatDto">
        select rs3.*, rs4.* from
        (
        select 	count(case when enttype not in ('9910','9100','9200') then 1 else null end) as qyyczs ,
        count(case when enttype = '9910' then 1 else null end) gtyczs,
        count(case when enttype in ('9100','9200') then 1 else null end) nzyczs
        from (
        select * from aiccs.tsabnlist where OPERATESTATUS = '0'
        <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
            and remdate &gt;= #{customParamMap.startDate}
        </if>
        <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
            and remdate &lt;= #{customParamMap.endDate}
        </if>
        <if test="customParamMap.xzqh != null and customParamMap.xzqh != '' ">
            and redecorg like concat(#{customParamMap.xzqh}, '%')
        </if>
        group by pripid ) rs
        ) rs3,
        (
        select 	count(case when enttype not in ('9910','9100','9200') then 1 else null end) as qylrzs,
        count(case when enttype = '9910' then 1 else null end) gtlrzs,
        count(case when enttype in ('9100','9200') then 1 else null end) nzlrzs
        from (
        select * from aiccs.tsabnlist where 1=1
        <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
            and abntime &gt;= #{customParamMap.startDate}
        </if>
        <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
            and abntime &lt;= #{customParamMap.endDate}
        </if>
        <if test="customParamMap.xzqh != null and customParamMap.xzqh != '' ">
            and intoDecorg like concat(#{customParamMap.xzqh}, '%')
        </if>
        group by pripid ) rs
        ) rs4
    </select>

    <!-- 区所专项统计查询  -->
    <select id="statisticsForAbnQJ" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnStatDto">
        select *, '2' as "index" from
        (
            select
            org.orgUnitName as "xzqh",
            count(case when SPECAUSE='1' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr1,
            count(case when SPECAUSE='2' and  enttype not in ('9910','9100','9200') then 1 else null end) as qylr2,
            count(case when SPECAUSE='3' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr3,
            count(case when SPECAUSE='4' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr4,
            count(case when SPECAUSE='5' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr5,
            count(case when SPECAUSE='6' and enttype not in ('9910','9100','9200') then 1 else null end) as qylr6,
            count(case when SPECAUSE='1' and enttype = '9910' then 1 else null end) as gtlr1,
            count(case when SPECAUSE='2' and enttype = '9910' then 1 else null end) as gtlr2,
            count(case when (SPECAUSE='3' or SPECAUSE='4') and enttype = '9910' then 1 else null end) as gtlr3,
            count(case when SPECAUSE='1' and enttype in ('9100', '9200') then 1 else null end) as nhlr1,
            count(case when SPECAUSE='2' and enttype in ('9100', '9200') then 1 else null end) as nhlr2,
            count(case when SPECAUSE='3' and enttype in ('9100', '9200') then 1 else null end) as nhlr3
            from
                (select orgUnitName, orgNumber
                from cxaicorg.t_orgunits to2
                where unitType = '1'
                and deleted = '0'
                <if test="customParamMap.orgCode.length() == 6">
                    and parentID = (select orgUnitID from cxaicorg.t_orgunits to3 where to3.orgNumber = #{customParamMap.orgCode})
                </if>
                <if test="customParamMap.orgCode.length() == 8">
                    and orgNumber = #{customParamMap.orgCode}
                </if>
                ) org
            left join
                (select ta.SPECAUSE, ta.enttype, ta.PRIPID, ta.OPERATESTATUS, eb.SUPORG from aiccs.tsabnlist ta
                left join aiccs.e_baseinfo eb on ta.PRIPID = eb.PRIPID
                <where>
                    <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
                        and ta.abntime &gt;= #{customParamMap.startDate}
                    </if>
                    <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
                        and ta.abntime &lt;= #{customParamMap.endDate}
                    </if>
                </where>
                and ta.intoDecorg like concat( #{customParamMap.xzqh}, '%')
                    group by ta.PRIPID, ta.SPECAUSE, ta.enttype, ta.OPERATESTATUS, eb.SUPORG) tmp
            on org.orgNumber = tmp.SUPORG
            group by org.orgNumber
        ) lr, -- 区列入结果
        (
            select
            org.orgUnitName as "xzqh",
            count(case when SPECAUSE='1' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc1,
            count(case when SPECAUSE='2' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc2,
            count(case when SPECAUSE='3' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc3,
            count(case when SPECAUSE='4' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc4,
            count(case when SPECAUSE='5' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc5,
            count(case when SPECAUSE='6' and enttype not in ('9910','9100','9200') and OPERATESTATUS = '0' then 1 else null end) as qyyc6,
            count(case when SPECAUSE='1' and enttype = '9910' and OPERATESTATUS = '0' then 1 else null end) as gtyc1,
            count(case when SPECAUSE='2' and enttype = '9910' and OPERATESTATUS = '0' then 1 else null end) as gtyc2,
            count(case when (SPECAUSE='3' or SPECAUSE='4') and enttype = '9910' and OPERATESTATUS = '0' then 1 else null end) as gtyc3,
            count(case when SPECAUSE='1' and enttype in ('9100', '9200') and OPERATESTATUS = '0' then 1 else null end) as nhyc1,
            count(case when SPECAUSE='2' and enttype in ('9100', '9200') and OPERATESTATUS = '0' then 1 else null end) as nhyc2,
            count(case when SPECAUSE='3' and enttype in ('9100', '9200') and OPERATESTATUS = '0' then 1 else null end) as nhyc3,
            count(case when enttype not in ('9910', '9100', '9200') and OPERATESTATUS = '2' then 1 else null end) as qyyy,
            count(case when enttype = '9910' and OPERATESTATUS = '2' then 1 else null end) as gtyy,
            count(case when enttype in ('9100', '9200') and OPERATESTATUS = '2' then 1 else null end) as nhyy
            from
                (select orgUnitName, orgNumber
                from cxaicorg.t_orgunits to2
                where unitType = '1'
                and deleted = '0'
                <if test="customParamMap.orgCode.length() == 6">
                    and parentID = (select orgUnitID from cxaicorg.t_orgunits to3 where to3.orgNumber = #{customParamMap.orgCode})
                </if>
                <if test="customParamMap.orgCode.length() == 8">
                    and orgNumber = #{customParamMap.orgCode}
                </if>
                ) org
            left join
                (select ta.SPECAUSE, ta.enttype, ta.PRIPID, ta.OPERATESTATUS, eb.SUPORG from aiccs.tsabnlist ta
                left join aiccs.e_baseinfo eb on ta.PRIPID = eb.PRIPID
                <where>
                    <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
                        and ta.remdate &gt;= #{customParamMap.startDate}
                    </if>
                    <if test="customParamMap.endDate != null and customParamMap.endDate != '' ">
                        and ta.remdate &lt;= #{customParamMap.endDate}
                    </if>
                </where>
            and ta.intoDecorg like concat( #{customParamMap.xzqh}, '%')
              group by ta.PRIPID, ta.SPECAUSE, ta.enttype, ta.OPERATESTATUS, eb.SUPORG) tmp
            on org.orgNumber = tmp.SUPORG
            group by org.orgNumber
        ) yc -- 区移出结果
        where lr."xzqh" = yc."xzqh"
    </select>

    <select id="getEntListByStatistics" resultType="com.chinaweal.aiccs.aiccs.abnormal.entity.excel.StatisticsAbnExcelDto">
        select
            eb.UNISCID,
            eb.ENTNAME,
            eb.LEREP,
            eb.DOM,
            (select to2.orgUnitName  from cxaicorg.t_orgunits to2 where eb.REGORG = to2.orgNumber) as 'regOrgCn',
            eb.REGULATORY_NAME_TEMP as 'supOrgCn',
            '其他' as 'source',
            t.SPEREASONS as 'reason'
        from tsabnlist t, e_baseinfo eb where t.PRIPID = eb.PRIPID
        <if test="customParamMap.operateStatus != null and customParamMap.operateStatus != '' ">
            and t.OPERATESTATUS = #{customParamMap.operateStatus}
        </if>

        <if test="customParamMap.startDate != null and customParamMap.startDate != '' ">
            and t.abntime &gt;= #{customParamMap.startDate} and t.abntime &lt;= #{customParamMap.endDate}
        </if>
        <if test="customParamMap.remStartDate != null and customParamMap.remStartDate != '' ">
            and t.remdate &gt;= #{customParamMap.remStartDate}  and t.remdate &lt;= #{customParamMap.remEndDate}
        </if>

        <choose>
            <when test="customParamMap.entType eq 'qy'">
                and t.enttype not in ('9910', '9100', '9200')
            </when>
            <when test="customParamMap.entType eq 'gt'">
                and t.enttype = '9910'
            </when>
            <otherwise>
                and t.enttype in ('9100', '9200')
            </otherwise>
        </choose>

        <choose>
            <when test="customParamMap.content eq 'gtlr3' or customParamMap.content eq 'gtyc3'">
                and t.SPECAUSE in ('3', '4')
            </when>
            <otherwise>
                and t.SPECAUSE = #{customParamMap.cause}
            </otherwise>
        </choose>
        <if test="customParamMap.orgCode.length() == 6">
            and t.intoDecorg like concat(#{customParamMap.xzqh}, '%')
        </if>
        <if test="customParamMap.orgCode.length() == 8">
            and eb.regulatory_temp = #{customParamMap.orgCode}
        </if>
    </select>

</mapper>
