<?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.nantian.cloud.ggjg.mapper.MlkMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.nantian.cloud.ggjg.entity.Mlk">
        <id column="team_title" property="teamTitle"/>
        <result column="area_code" property="areaCode"/>
        <result column="team_type" property="teamType"/>
        <result column="code" property="code"/>
        <result column="org_amount" property="orgAmount"/>
        <result column="business_time" property="businessTime"/>
        <result column="level" property="level"/>
        <result column="f009" property="f009"/>
        <result column="f101" property="f101"/>
        <result column="f102" property="f102"/>
        <result column="f1021" property="f1021"/>
        <result column="f103" property="f103"/>
        <result column="f1031" property="f1031"/>
        <result column="f1032" property="f1032"/>
        <result column="f1033" property="f1033"/>
        <result column="f110" property="f110"/>
        <result column="f111" property="f111"/>
        <result column="f120" property="f120"/>
        <result column="f121" property="f121"/>
        <result column="f130" property="f130"/>
        <result column="f131" property="f131"/>
        <result column="f140" property="f140"/>
        <result column="f141" property="f141"/>
        <result column="f150" property="f150"/>
        <result column="f151" property="f151"/>
        <result column="f1501" property="f1501"/>
        <result column="f1511" property="f1511"/>
        <result column="f1502" property="f1502"/>
        <result column="f1512" property="f1512"/>
        <result column="f160" property="f160"/>
        <result column="f161" property="f161"/>
        <result column="f1601" property="f1601"/>
        <result column="f1611" property="f1611"/>
        <result column="f1602" property="f1602"/>
        <result column="f1612" property="f1612"/>
        <result column="f104" property="f104"/>
        <result column="f1041" property="f1041"/>
        <result column="f170" property="f170"/>
        <result column="f171" property="f171"/>
        <result column="f180" property="f180"/>
        <result column="f181" property="f181"/>
        <result column="f105" property="f105"/>
        <result column="f190" property="f190"/>
        <result column="f191" property="f191"/>
        <result column="f192" property="f192"/>
    </resultMap>

    <select id="queryTjb" parameterType="java.util.HashMap" resultType="java.util.LinkedHashMap">
         <![CDATA[

        select t.* from(
        select x.*,(x.c1+x.c2+x.c3+x.c4+x.c5+x.c6+x.c7+x.c8) sum from (
                        SELECT
        h.TeamId,h.TeamTitle,h.BusinessTime,h.code,d.AreaCode,
        (select  count(case when xj.`TeamType`= '01' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c1,
        (select  count(case when xj.`TeamType`= '021' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c2,
        (select  count(case when xj.`TeamType`= '022' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c3,
        (select  count(case when xj.`TeamType`= '023' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c4,
        (select  count(case when xj.`TeamType`= '024' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c5,
        (select  count(case when xj.`TeamType`= '025' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c6,
        (select  count(case when xj.`TeamType`= '026' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c7,
        (select  count(case when xj.`TeamType`= '03' then 1 else null end ) as c from team_history xj where  left(xj.code,3) =left(h.code,3) and BusinessTime = h.BusinessTime and IsManage=0) c8
        from	team_history h join team_detail d on h.TeamId=d.TeamId
        WHERE
            length(h.CODE) = 3
            AND h.BusinessTime = #{map.businessTime}   and h.IsManage=1
            union
                    SELECT
        h.TeamId,h.TeamTitle,h.BusinessTime,h.code,d.AreaCode,
        (select  count(case when xj.`TeamType`= '01' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c1,
        (select  count(case when xj.`TeamType`= '021' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c2,
        (select  count(case when xj.`TeamType`= '022' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c3,
        (select  count(case when xj.`TeamType`= '023' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c4,
        (select  count(case when xj.`TeamType`= '024' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c5,
        (select  count(case when xj.`TeamType`= '025' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c6,
        (select  count(case when xj.`TeamType`= '026' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c7,
        (select  count(case when xj.`TeamType`= '03' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c8
        from	team_history h join team_detail d on h.TeamId=d.TeamId
        WHERE
            length(h.CODE) = 6
            AND h.BusinessTime = #{map.businessTime}  and h.IsManage=1
            ) x order by x.code
        )t order by t.AreaCode asc

        ]]>
    </select>

    <select id="queryTjbCity" resultType="java.util.Map">
        select t.* from(
            select x.*,(x.c1+x.c2+x.c3+x.c4+x.c5+x.c6+x.c7+x.c8) sum from (
                SELECT h.TeamId,h.TeamTitle,h.BusinessTime,h.code,d.AreaCode,
                (select  count(case when xj.`TeamType`= '01' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c1,
                (select  count(case when xj.`TeamType`= '021' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c2,
                (select  count(case when xj.`TeamType`= '022' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c3,
                (select  count(case when xj.`TeamType`= '023' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c4,
                (select  count(case when xj.`TeamType`= '024' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c5,
                (select  count(case when xj.`TeamType`= '025' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c6,
                (select  count(case when xj.`TeamType`= '026' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c7,
                (select  count(case when xj.`TeamType`= '03' then 1 else null end ) as c from team_history xj where  left(xj.code,6) =left(h.code,6) and BusinessTime = h.BusinessTime and IsManage=0) c8
                from team_detail d join team_history h on d.TeamId=h.TeamId
                where d.AreaCode like concat(#{map.code},"%") and length(h.code)=6 AND h.BusinessTime = #{map.businessTime} and h.IsManage=1
                union
                SELECT h.TeamId,h.TeamTitle,h.BusinessTime,h.code,d.AreaCode,
                (select  count(case when xj.`TeamType`= '01' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c1,
                (select  count(case when xj.`TeamType`= '021' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c2,
                (select  count(case when xj.`TeamType`= '022' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c3,
                (select  count(case when xj.`TeamType`= '023' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c4,
                (select  count(case when xj.`TeamType`= '024' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c5,
                (select  count(case when xj.`TeamType`= '025' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c6,
                (select  count(case when xj.`TeamType`= '026' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c7,
                (select  count(case when xj.`TeamType`= '03' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c8
                from team_detail d join team_history h on d.TeamId=h.TeamId
                where d.AreaCode like concat(#{map.code},"%") and length(h.code)=9 AND h.BusinessTime = #{map.businessTime} and h.IsManage=1
             ) x order by x.code
        )t order by t.AreaCode asc
    </select>

    <select id="queryTjbCounty" resultType="java.util.Map">
        select t.* from(
            select x.*,(x.c1+x.c2+x.c3+x.c4+x.c5+x.c6+x.c7+x.c8) sum from (
                SELECT h.TeamId,h.TeamTitle,h.BusinessTime,h.code,d.AreaCode,
                (select  count(case when xj.`TeamType`= '01' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c1,
                (select  count(case when xj.`TeamType`= '021' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c2,
                (select  count(case when xj.`TeamType`= '022' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c3,
                (select  count(case when xj.`TeamType`= '023' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c4,
                (select  count(case when xj.`TeamType`= '024' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c5,
                (select  count(case when xj.`TeamType`= '025' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c6,
                (select  count(case when xj.`TeamType`= '026' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c7,
                (select  count(case when xj.`TeamType`= '03' then 1 else null end ) as c from team_history xj where  left(xj.code,9) =left(h.code,9) and BusinessTime = h.BusinessTime and IsManage=0) c8
                from team_detail d join team_history h on d.TeamId=h.TeamId
                where d.AreaCode like concat(#{map.code},"%") and length(h.code)=9 AND h.BusinessTime = #{map.businessTime} and h.IsManage=1
             ) x order by x.code
        )t order by t.sum desc
    </select>

    <select id="getAreaByCode" resultType="java.lang.String">
        select meaning from dict_region where id = #{code}
    </select>


</mapper>
