<?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="org.springblade.modules.mdjfdj.mapper.JdrhApptjMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="mdjfdjResultMap" type="org.springblade.modules.mdjfdj.entity.JdrhApptj">
        <result column="id" property="id"/>
        <result column="dept_name" property="deptName"/>
        <result column="yhl" property="yhl"/>
        <result column="zrmdsl" property="zrmdsl"/>
        <result column="tjsl" property="tjsl"/>
        <result column="mdsl" property="mdsl"/>
        <result column="fwsl" property="fwsl"/>
        <result column="zfl" property="zfl"/>
        <result column="zrzfl" property="zrzfl"/>
        <result column="zrfwsl" property="zrfwsl"/>
        <result column="jccs" property="jccs"/>
        <result column="zrjxcj" property="zrjxcj"/>
        <result column="zrjxjc" property="zrjxjc"/>
        <result column="cssl" property="cssl"/>
        <result column="dept_category" property="deptCategory"/>
        <result column="sort" property="sort"/>
        <result column="tjrq" property="tjrq"/>
        <result column="bmid" property="bmid"/>
        <result column="bt" property="bt"/>
        <result column="parent_id" property="parentId"/>

    </resultMap>

    <select id="get2DaysAgoTj" resultType="org.springblade.modules.mdjfdj.entity.JdrhApptj">
        select * from jdrh_apptj where tjrq::date = now()::date - interval '2 day' and bmid = #{bmid} order by sort
    </select>

    <select id="getAppTj" resultType="org.springblade.modules.mdjfdj.entity.JdrhApptj">
        WITH T AS (
            SELECT
            COUNT( 1 ) AS jccs,
            ( string_to_array( b.ancestors, ',' ) ) [ 3 ] AS sjid,
            ci.create_dept
            FROM
            jdrh.csjc_info ci
            LEFT JOIN jdrh.blade_dept b ON ci.create_dept = b.ID
            WHERE
            1 = 1 and ci.create_time::date &lt; now()::date
            AND ci.is_deleted = 0
            GROUP BY
            ci.create_dept,
            b.ancestors
        ),
        t1 AS (
            SELECT
            COUNT( 1 ) AS zrjccs,
            ( string_to_array( b.ancestors, ',' ) ) [ 3 ] AS sjid,
            ci.create_dept
            FROM
            jdrh.csjc_info ci
            LEFT JOIN jdrh.blade_dept b ON ci.create_dept = b.ID
            WHERE
            1 = 1
            AND ci.create_time :: DATE  = now()::date  - INTERVAL '1 DAY '
            AND ci.is_deleted = 0
            GROUP BY
            ci.create_dept,
            b.ancestors
        ),
        t2 AS (
            SELECT
            COUNT ( 1 ) AS cssl,
            ( string_to_array( b.ancestors, ',' ) ) [ 3 ] sjid,
            jd.create_dept
            FROM
            jdrh.cs_info jd
            LEFT JOIN jdrh.blade_dept b ON jd.create_dept = b.ID
            WHERE
            1 = 1
            and jd.create_time::date &lt; now()::date
            AND jd.is_deleted = 0
            GROUP BY
            jd.create_dept ,
            b.ancestors
        ),
        t3 AS (
            SELECT
            COUNT ( 1 ) AS zrcssl,
            ( string_to_array( b.ancestors, ',' ) ) [ 3 ] sjid,
            jd.create_dept
            FROM
            jdrh.cs_info jd
            LEFT JOIN jdrh.blade_dept b ON jd.create_dept = b.ID
            WHERE
            1 = 1
            AND jd.create_time :: DATE  = now()::date  - INTERVAL '1 DAY '
            AND jd.is_deleted = 0
            GROUP BY
            jd.create_dept ,
            b.ancestors
        ),
        mdsl AS (
            SELECT
                ( string_to_array( b.ancestors, ',' ) ) [ 3 ] AS sjid,
                COUNT ( 1 ) AS mdsl,
                dj.jfsssq
            FROM
            jdrh.mdjf_dj dj
            LEFT JOIN jdrh.blade_dept b ON dj.jfsssq = b.ID
            where 1=1 and dj.is_deleted = 0 and dj.create_time::date &lt; now()::date
            GROUP BY
            dj.jfsssq,
            sjid
        ),
        tjsl AS (
            SELECT
                COUNT ( 1 ) FILTER ( WHERE tj.tjzt = '已流转' ) AS fls,
                ( string_to_array( dept_1.ancestors, ',' ) ) [ 3 ] AS sjid,
                tj.create_dept
            FROM
                jdrh.blade_dept dept_1
                LEFT JOIN jdrh.mdjf_tj tj ON tj.create_dept = dept_1.
                ID LEFT JOIN jdrh.mdjf_dj dj ON dj.ID = tj.jf_id
            WHERE 1=1 and
                tj.is_deleted = 0  and tj.create_time::date &lt; now()::date
              AND dj.is_deleted = 0 and tj.is_deleted = 0
            GROUP BY
                tj.create_dept,
                sjid
        ),
        zrmdsl AS (
            SELECT
            ( string_to_array( b.ancestors, ',' ) ) [ 3 ] AS sjid,
            COUNT ( 1 ) AS zrmdsl,
            dj.jfsssq
            FROM
            jdrh.mdjf_dj dj
            LEFT JOIN jdrh.blade_dept b ON dj.jfsssq = b.ID
            where 1=1 and dj.create_time:: DATE  = now()::date  - INTERVAL '1 DAY ' and dj.is_deleted = 0
            GROUP BY
            dj.jfsssq,
            sjid
        ),
        fwsl AS (
            SELECT COUNT( 1 ) fwsl,
            ( string_to_array( dept.ancestors, ',' ) ) [ 3 ] sjid,
            fw.create_dept
            FROM
            rhzf_fw_info fw
            LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
            WHERE
            1 = 1 and fw.is_deleted = 0 and fw.create_time::date &lt; now()::date
            GROUP BY
            fw.create_dept,
            sjid
        ),
        zrfwsl AS (
            SELECT COUNT( 1 ) zrfwsl,
            ( string_to_array( dept.ancestors, ',' ) ) [ 3 ] sjid,
            fw.create_dept
            FROM
            rhzf_fw_info fw
            LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
            WHERE
            1 = 1 and fw.create_time::date = now()::date - INTERVAL '1 day' and fw.is_deleted = 0
            GROUP BY
            fw.create_dept,
            sjid
        ),
        zfl AS (
            SELECT
            sum(times)  zfl,
            ( string_to_array( dept.ancestors, ',' ) ) [ 3 ] sjid,
            fw.create_dept
            FROM
            rhzf_fw_info fw
            LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
            WHERE
            1 = 1 and fw.is_deleted = 0 and fw.create_time::date &lt; now()::date
            GROUP BY
            fw.create_dept,
            sjid
        ),
        zrzfl AS (
            SELECT
            sum(1)  zrzfl,
            ( string_to_array( dept.ancestors, ',' ) ) [ 3 ] sjid,
            fw.create_dept
            FROM
            rhzf_fw_info fw
            LEFT JOIN blade_dept dept ON dept.ID = fw.create_dept
            WHERE
            1 = 1  and fw.update_time::date = now()::date - INTERVAL '1 day' and fw.is_deleted = 0
            GROUP BY
            fw.create_dept,
            sjid
        ),
        user1 as (
            SELECT
            d.ID,
            d.ancestors ,
            ( string_to_array( d.ancestors, ',' ) ) [ 3 ]  sjid
            FROM
            blade_dept d
            JOIN ( SELECT dept_id FROM blade_user WHERE last_login_time :: DATE = now( ) :: DATE - INTERVAL '1 day' ) A ON d.ID :: TEXT = ( ( string_to_array( A.dept_id, ',' ) ) [ 1 ] )
            GROUP BY
            d.ID,
            d.ancestors
        ),
        mdlz as ( select sum(fls) tjsl,sjid from tjsl group by sjid ),
        yhl  as ( select count(1) yhl,sjid from user1 group by sjid ),
        m1   AS ( SELECT sjid, SUM ( mdsl ) AS mdsl FROM mdsl GROUP BY sjid ),
        m2   AS ( SELECT sjid, SUM ( zrmdsl ) AS zrmdsl FROM zrmdsl GROUP BY sjid ),
        c    AS ( SELECT sjid, SUM ( cssl ) AS cssl FROM t2 GROUP BY sjid ),
        e    AS ( SELECT sjid, SUM ( zrcssl ) AS zrcssl FROM t3 GROUP BY sjid ),
        b    AS ( SELECT sjid, SUM ( jccs ) AS jccs FROM T GROUP BY sjid ) ,
        d    AS ( SELECT sjid, SUM ( zrjccs ) AS zrjccs FROM t1 GROUP BY sjid ) ,
        r1   AS ( SELECT SUM ( fwsl ) AS fwsl, sjid FROM fwsl GROUP BY sjid ),
        r3   AS ( SELECT SUM ( zrfwsl ) AS zrfwsl, sjid FROM zrfwsl GROUP BY sjid ),
        r2   AS ( SELECT SUM ( zfl ) AS zfl, sjid FROM zfl GROUP BY sjid ),
        r4   AS ( SELECT SUM ( zrzfl ) AS zrzfl, sjid FROM zrzfl GROUP BY sjid )
        SELECT
            A.ID as bmid,
            ('警地融合”APP数据采集/更新情况统计表'||(now()::date)::text) as bt,
            A.dept_name,
            A.dept_category,
            A.parent_id,
            COALESCE ( yhl.yhl, 0 ) AS yhl,
            COALESCE ( b.jccs, 0 ) AS jccs,
            COALESCE ( e.zrcssl, 0 ) as zrjxcj,
            COALESCE ( d.zrjccs, 0 ) as zrjxjc,
            COALESCE ( C.cssl, 0 ) AS cssl,
            COALESCE ( mdlz.tjsl, 0 ) AS tjsl,
            COALESCE ( m1.mdsl, 0 ) AS mdsl,
            COALESCE ( m2.zrmdsl, 0 ) AS zrmdsl,
            COALESCE ( r1.fwsl, 0 ) AS fwsl,
            COALESCE ( r2.zfl, 0 ) AS zfl,
            COALESCE ( r4.zrzfl, 0 )  as zrzfl,
            COALESCE ( r3.zrfwsl, 0 )  as zrfwsl,
            (now()::date - interval '1 day') tjrq,
            A.sort sort
        FROM
            jdrh.blade_dept A
            LEFT JOIN b ON A.ID = b.sjid ::BIGINT
            LEFT JOIN C ON A.ID = C.sjid ::BIGINT
            LEFT JOIN d ON A.ID = d.sjid ::BIGINT
            LEFT JOIN e ON A.ID = e.sjid ::BIGINT
            LEFT JOIN m1 ON A.ID = m1.sjid ::BIGINT
            LEFT JOIN m2 ON A.ID = m2.sjid ::BIGINT
            LEFT JOIN r1 ON A.ID = r1.sjid ::BIGINT
            LEFT JOIN r2 ON A.ID = r2.sjid ::BIGINT
            LEFT JOIN r3 ON A.ID = r3.sjid ::BIGINT
            LEFT JOIN r4 ON A.ID = r4.sjid ::BIGINT
            left join yhl on  a.id = yhl.sjid::bigint
            left join mdlz on  a.id = mdlz.sjid::bigint
        WHERE
            A.dept_category = 4 and a.is_deleted = '0'
            AND A.ancestors like '%220000000000%'
            order by a.sort
    </select>



</mapper>
