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

    <resultMap id="BaseResultMap" type="com.chinaweal.aiccs.aiccs.statistic.entity.StatisticsResult">
        <id column="id" property="id"/>
        <result column="module_tag" property="moduleTag"/>
        <result column="data_json" property="dataJson"/>
        <result column="create_time" property="createTime"/>
    </resultMap>

    <select id="selectLatest" resultMap="BaseResultMap">
        select id, module_tag, data_json, create_time
        from statistics_result
        <if test="moduleTag != null and moduleTag != ''">
            where module_tag = #{moduleTag}
        </if>
        <if test="moduleTag == null or moduleTag == ''">
            where false
        </if>
        order by create_time desc
        limit 1
    </select>

    <!--    全市各区的经营异常主体情况-->
    <select id="staticAllRegionTsabnlist" resultType="java.util.Map">
        SELECT left(e.SUPORG, 6) region, cast(count(*) as char) as num
        FROM aiccs.tsabnlist t LEFT JOIN aiccs.e_baseinfo e
        ON t.PRIPID = e.PRIPID
        WHERE e.SUPORG IS NOT NULL
        GROUP BY left(e.SUPORG, 6)
    </select>

    <!--    按年统计全市-->
    <select id="staticAbnByYear" resultType="java.util.Map">
        SELECT DATE_FORMAT(ABNTIME, '%Y') years, count(*) num
        FROM aiccs.tsabnlist
        where DATE_FORMAT(ABNTIME, '%Y') != '2013' and DATE_FORMAT(ABNTIME,'%Y') != '2014'
        GROUP BY years
        order by years asc
    </select>

    <!--    按月统计全市-->
    <select id="staticAbnByMonth" resultType="java.util.Map">
        SELECT count(*) num, DATE_FORMAT(ABNTIME, '%m') months
        FROM aiccs.tsabnlist
        WHERE ABNTIME LIKE '2021%'
        GROUP BY months
    </select>

    <!--    按周统计全市-->
    <select id="staticAbnByWeek" resultType="java.util.Map">
        SELECT DATE_FORMAT(ABNTIME, '%x%v') weeks, count(*) num
        FROM aiccs.tsabnlist
        GROUP BY weeks
        ORDER BY weeks DESC
    </select>

    <!--    经营异常列入统计全市-->
    <select id="staticAbnByIn" resultType="java.util.Map">
        (SELECT a.SPECAUSE, count(*) num, 'all' as type
         FROM (SELECT (case
                           when (SPECAUSE = '2' AND ENTTYPE not in ('9100', '9200', '9910')) then '6'
                           when (SPECAUSE = '3' AND ENTTYPE not in ('9100', '9200', '9910')) then '2'
                           when (SPECAUSE = '4' AND ENTTYPE not in ('9100', '9200', '9910')) then '3'
                           when (SPECAUSE = '5' AND ENTTYPE not in ('9100', '9200', '9910')) then '4'
                           when (SPECAUSE = '6' AND ENTTYPE not in ('9100', '9200', '9910')) then '5'
                           else SPECAUSE end) as SPECAUSE
               FROM aiccs.tsabnlist
               WHERE SPECAUSE is not null
                 AND ENTTYPE is not null) a
         GROUP BY a.SPECAUSE
         order by num desc)
        UNION
        (SELECT SPECAUSE, count(*) num, 'individual' as type
         FROM aiccs.tsabnlist
         WHERE SPECAUSE is not null
           and ENTTYPE = '9910'
         GROUP BY SPECAUSE
         order by num desc)
        UNION
        (SELECT SPECAUSE, count(*) num, 'farmer' as type
         FROM aiccs.tsabnlist
         WHERE SPECAUSE is not null
           and ENTTYPE in ('9100', '9200')
         GROUP BY SPECAUSE
         order by num desc)
        UNION
        (SELECT (case
                     when (SPECAUSE = '2' AND ENTTYPE not in ('9100', '9200', '9910')) then '6'
                     when (SPECAUSE = '3' AND ENTTYPE not in ('9100', '9200', '9910')) then '2'
                     when (SPECAUSE = '4' AND ENTTYPE not in ('9100', '9200', '9910')) then '3'
                     when (SPECAUSE = '5' AND ENTTYPE not in ('9100', '9200', '9910')) then '4'
                     when (SPECAUSE = '6' AND ENTTYPE not in ('9100', '9200', '9910')) then '5'
                     else SPECAUSE end) as SPECAUSE,
                count(*)                   num,
                'company'               as type
         FROM aiccs.tsabnlist
         WHERE SPECAUSE is not null
           and ENTTYPE not in ('9100', '9200', '9910')
         GROUP BY SPECAUSE
         order by num desc)

        --         (SELECT SPECAUSE, count(*) num, 'all' as type FROM aiccs.tsabnlist WHERE SPECAUSE is not null  GROUP BY SPECAUSE order by num desc)
--         UNION
--         (SELECT SPECAUSE, count(*) num, 'individual' as type FROM aiccs.tsabnlist WHERE SPECAUSE is not null and ENTTYPE = '9910' GROUP BY SPECAUSE order by num desc)
--         UNION
--         (SELECT SPECAUSE, count(*) num, 'farmer' as type FROM aiccs.tsabnlist WHERE SPECAUSE is not null and ENTTYPE in ('9100','9200') GROUP BY SPECAUSE order by num desc)
--         UNION
--         (SELECT SPECAUSE, count(*) num, 'company' as type FROM aiccs.tsabnlist WHERE SPECAUSE is not null and ENTTYPE not in ('9100','9200','9910') GROUP BY SPECAUSE order by num desc)
--         SELECT SPECAUSE, count(*) num, 'all' as type FROM aiccs.tsabnlist GROUP BY SPECAUSE order by num desc
    </select>

    <!--    经营异常移出统计全市-->
    <select id="staticAbnByOut" resultType="java.util.Map">
        SELECT REMEXCPRES, count(*) num
        FROM aiccs.tsabnlist
        GROUP BY REMEXCPRES
    </select>

    <!--     全市各区的严重违法主体情况-->
    <select id="staticAllRegionTsserill" resultType="java.util.Map">
        SELECT left(e.SUPORG, 6) region, cast(count(DISTINCT t.PRIPID) as char) as num
        FROM aiccs.tsserillegallist t LEFT JOIN aiccs.e_baseinfo e
        ON t.PRIPID = e.PRIPID
        WHERE t.SERILLREA is not null
        GROUP BY left(e.SUPORG, 6)
    </select>

    <!--     按年统计严重违法主体情况-->
    <select id="staticTsserillByYear" resultType="java.util.Map">
        SELECT count(DISTINCT PRIPID) num, DATE_FORMAT(ABNTIME, '%Y') years
        FROM aiccs.tsserillegallist
        WHERE SERILLREA is not null
        GROUP BY years
        order by years asc
    </select>

    <!--     按列入原因统计严重违法主体情况-->
    <select id="staticTsserillByIn" resultType="java.util.Map">
        SELECT LOSTCREDITEXPLAIN, SERILLREA, count(*) num
        FROM aiccs.tsserillegallist
        WHERE SERILLREA is not null
        GROUP BY SERILLREA
        order by num desc
    </select>

    <!--     按移出原因和地区统计严重违法主体情况-->
    <select id="staticTsserillByOutRe" resultType="java.util.Map">
        (SELECT '全市' as regorg, REMEXCPRES, count(1) num FROM aiccs.tsserillegallist WHERE REMEXCPRES is not null GROUP
        BY REMEXCPRES)
        UNION
        (SELECT left(e.SUPORG,6) regorg, REMEXCPRES, count(1) num
        FROM aiccs.tsserillegallist t LEFT JOIN aiccs.e_baseinfo e ON t.PRIPID = e.PRIPID
        WHERE t.REMEXCPRES is not null
        GROUP BY left(e.SUPORG,6), REMEXCPRES)
        <!--        SELECT left(e.SUPORG,6) regorg, REMEXCPRES, count(*) num-->
        <!--        FROM aiccs.tsserillegallist t LEFT JOIN aiccs.e_baseinfo e ON t.PRIPID = e.PRIPID-->
        <!--        WHERE REMEXCPRES is not null-->
        <!--        GROUP BY left(e.SUPORG,6), REMEXCPRES-->
    </select>

    <!--    批量吊销主体分布-->
    <select id="staticRevRegion" resultType="java.util.Map">
        SELECT left(e.REGORG, 6) region, cast(count(*) as char) as num
        FROM (
            (SELECT pripid FROM tsbizrevlist WHERE DECISIONPUBLISHDATE is not null)
            UNION
            (SELECT pripid FROM biz_rev_ent WHERE DECISIONPUBLISHDATE is not null)
            ) t
            LEFT JOIN e_baseinfo e
        ON t.PRIPID = e.PRIPID
        WHERE e.regorg is not null
        GROUP BY left(e.REGORG, 6)
    </select>

    <!--    批量吊销增长分析(年)-->
    <select id="staticRevYear" resultType="java.util.Map">
        SELECT years, num
        FROM (
                 (SELECT DATE_FORMAT(DECISIONPUBLISHDATE, '%Y') years, count(*) num
                  FROM aiccs.biz_rev_ent
                  WHERE DECISIONPUBLISHDATE is not null
                  GROUP BY years)
                 UNION
                 (SELECT DATE_FORMAT(DECISIONPUBLISHDATE, '%Y') years, count(*) num
                  FROM aiccs.tsbizrevlist
                  WHERE DECISIONPUBLISHDATE is not null
                  GROUP BY years)
             ) m
        order by years asc
    </select>

    <!--    联合惩戒主体分布-->
    <select id="staticLockRegion" resultType="java.util.Map">
        SELECT cast(count(*) as char) as num, left(regorg, 6) region
        FROM entlock
        WHERE LOCKSTATE = '1' AND state = '0' AND regorg is not null
        GROUP BY region
    </select>

    <!--    联合惩戒来源分析-->
    <select id="staticLockType" resultType="java.util.Map">
        SELECT count(*) num, punishFrom
        FROM entlock
        WHERE LOCKSTATE = '1'
          AND state = '0'
        GROUP BY punishFrom
    </select>

    <!--    撤销登记主体分布-->
    <select id="staticFraudRegion" resultType="java.util.Map">
        select cast(count(*) as char) as num, left(fe.regorg, 6) region
        from (t_fraud_ent fe join t_biz_fraud bf)
        where ((fe.bizSeq = bf.bizSeq) and (bf.linkType = '4'))
        GROUP BY region
    </select>

    <!--    撤销登记增长分析-->
    <select id="staticFraudYear" resultType="java.util.Map">
        select count(*) num, DATE_FORMAT(bf.create_time, '%Y') years
        from (t_fraud_ent fe
            join t_biz_fraud bf)
        where ((fe.bizSeq = bf.bizSeq) and (bf.linkType = '4'))
        GROUP BY years
    </select>

    <!--    撤销登记涉嫌登记类型分析-->
    <select id="staticFraudType" resultType="java.util.Map">
        select count(*) num, bf.complaintReason
        from (t_fraud_ent fe
            join t_biz_fraud bf)
        where ((fe.bizSeq = bf.bizSeq) and (bf.linkType = '4'))
        GROUP BY bf.complaintReason
    </select>

    <!--    协同监管主体分布-->
    <select id="staticTsusualinspectRegion" resultType="java.util.Map">
        SELECT left(inspectOrg, 6) region, cast(count(*) as char) as num
        FROM tsusualinspect
        WHERE status = '1'
        GROUP BY region
    </select>

    <!--    协同监管结果分析-->
    <select id="staticTsusualinspectType" resultType="java.util.Map">
        SELECT count(case when inspectResult LIKE '%1%' then 1 else null end) one,
               count(case when inspectResult LIKE '%2%' then 1 else null end) two,
               count(case when inspectResult LIKE '%3%' then 1 else null end) three,
               count(case when inspectResult LIKE '%4%' then 1 else null end) four,
               count(case when inspectResult LIKE '%5%' then 1 else null end) five,
               count(case when inspectResult LIKE '%6%' then 1 else null end) six,
               count(case when inspectResult LIKE '%7%' then 1 else null end) seven,
               count(case when inspectResult LIKE '%8%' then 1 else null end) eight
        FROM tsusualinspect
        WHERE status = '1'
--         SELECT inspectResult, count(*) num   FROM tsusualinspect WHERE status = '1' GROUP BY inspectResult
    </select>

</mapper>
