-- 片区


${if(len(p_area) = 0,"","/*")}
with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
),hr_date AS
(


SELECT
 B.organ_code   AS organ_code
,B.organ_name   AS organ_name
,SUM(A.bzs)  AS bzs
,SUM(zzs) AS zzs -- 在职人数
FROM
(
    SELECT
    A1.pq  AS pq
    ,SUM(A1.zzs) AS zzs
    ,sum(A1.bzs)  AS bzs
    FROM
    (
          SELECT
            pq	-- 片区
           ,ny
           ,SUM(zzs) AS zzs 	-- 在职数
           ,SUM(bzs) AS bzs -- 编制数
            FROM ods_hr_view_mbl_d
            WHERE pq not in('89','267') -- 排除了佳美、悦汇
            and ny > '2024-10'
            GROUP BY pq,ny,xm
            UNION ALL
            SELECT
            organ_code as pq
            ,concat(left(ym,4),'-',right(ym,2))  as ny
            ,SUM(zzs) AS zzs
            ,SUM(bzs) AS bzs
          FROM ods_hr_history_bz_num
          WHERE 1 = 1
          GROUP BY ym,organ_code
    )A1
    WHERE A1.ny = '${p_startdate}'
    group by A1.pq
)A
RIGHT JOIN
(
         SELECT
    organ_code    -- erp片区编码
     ,organ_name  -- erp片区名称
     ,hr_organ_id  -- hr片区编码
     ,hr_organ_name  -- hr片区名称
    FROM dim_organ_mapping
    WHERE length(hr_organ_id) > 0
    GROUP BY
    organ_code
       ,organ_name
       ,hr_organ_id
       ,hr_organ_name
)B
ON A.pq = B.hr_organ_id
group by B.organ_code, B.organ_name

)
select
   a.organ_name  as p_area  -- 片区/项目
    ,sum(room_num) as room_num        -- 房间总数 【总户数】
    ,sum(check_ins_num) as check_ins_num   -- 入住数   【入住户数】
    ,case when sum(check_ins_num) = 0
              then 0
          else sum(check_ins_num) / sum(room_num)
    end as   occupancy_rate -- 入住率
    ,SUM(IFNULL(b.zzs,0)) AS zzs -- 在职数
    ,SUM(ifnull(b.bzs,0)) AS bzs -- 编制数
    ,CASE WHEN SUM(b.bzs) = 0 THEN 0 ELSE SUM(b.zzs) / SUM(b.bzs) END AS full_rate -- 满编率
from
(
    SELECT
    organ_code
    ,organ_name
    ,SUM(room_num) AS room_num
    ,SUM(check_ins_num) AS check_ins_num
    FROM dws_operation_room_park_info_d
    WHERE 1 = 1
-- ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

    group by
        organ_code
    ,organ_name
) a
left join hr_date b
on a.organ_code = b.organ_code

group by
a.organ_name

${if(len(p_area) = 0,"","*/")}


-- 项目

${if(len(p_area) > 0,"","/*")}
with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
),hr_date AS
(


SELECT
 B.organ_code   AS organ_code
,B.organ_name   AS organ_name
,B.comm_id      AS comm_id
,B.comm_name    AS comm_name
,SUM(A.bzs)  AS bzs
,SUM(zzs) AS zzs -- 在职人数
FROM
(
    SELECT
    A1.pq  AS pq
    ,A1.xm AS xm
    ,SUM(A1.zzs) AS zzs
    ,sum(A1.bzs)  AS bzs
    FROM
    (
          SELECT
            pq	-- 片区
           ,ny
           ,xm
           ,SUM(zzs) AS zzs 	-- 在职数
           ,SUM(bzs) AS bzs -- 编制数
            FROM ods_hr_view_mbl_d
            WHERE pq not in('89','267') -- 排除了佳美、悦汇
            GROUP BY pq,ny,xm
            -- 没有项目的历史数据
        --    UNION ALL
        --    SELECT
        --    organ_code as pq
        --    ,concat(left(ym,4),'-',right(ym,2))  as ny
        --    ,'' AS xm
        --    ,SUM(zzs) AS zzs
        --    ,SUM(bzs) AS bzs
        --  FROM ods_hr_history_bz_num
        --  WHERE 1 = 1
        --  GROUP BY ym,organ_code
    )A1
    WHERE A1.ny = '${p_startdate}'
    group by A1.pq,A1.xm
)A
RIGHT JOIN
(
         SELECT
    organ_code    -- erp片区编码
     ,organ_name  -- erp片区名称
     ,hr_organ_id  -- hr片区编码
     ,hr_organ_name  -- hr片区名称
     ,hr_comm_id    -- hr项目id
     ,hr_comm_name  -- hr项目名称
     ,comm_id
     ,comm_name
    FROM dim_organ_mapping
    WHERE length(hr_comm_id) > 0
    GROUP BY
    organ_code
       ,organ_name
       ,hr_organ_id
       ,hr_organ_name
       ,hr_comm_id
       ,hr_comm_name
       ,comm_id
       ,comm_name
)B
ON A.pq = B.hr_organ_id
AND A.xm = B.hr_comm_id
group by B.organ_code, B.organ_name, B.comm_id, B.comm_name

)
select
    a.comm_name  as p_area  -- 片区/项目
   --  organ_name      -- 片区名称
   -- ,comm_name       -- 项目名称
    ,sum(room_num) as room_num        -- 房间总数 【总户数】
    ,sum(check_ins_num) as check_ins_num   -- 入住数   【入住户数】
    ,case when sum(check_ins_num) = 0
              then 0
          else sum(check_ins_num) / sum(room_num)
    end as   occupancy_rate -- 入住率
    ,SUM(IFNULL(b.zzs,0)) AS zzs -- 在职数
    ,SUM(ifnull(b.bzs,0)) AS bzs -- 编制数
    ,CASE WHEN SUM(b.bzs) = 0 THEN 0 ELSE SUM(b.zzs) / SUM(b.bzs) END AS full_rate -- 满编率
from
(
 SELECT
    organ_code
    ,organ_name
    ,comm_id
    ,comm_name
    ,SUM(room_num) AS room_num
    ,SUM(check_ins_num) AS check_ins_num
    FROM dws_operation_room_park_info_d
    WHERE 1 = 1
-- ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

    group by
        organ_code
    ,organ_name
        ,comm_id
    ,comm_name
) a
left join hr_date b
on a.organ_code = b.organ_code
and a.comm_id = b.comm_id
where 1=1
${IF(LEN(p_area)!=0," and a.organ_code IN ('"+p_area+"')","")}
-- ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN a.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","a.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

group by
a.comm_name


${if(len(p_area) > 0,"","*/")}




-- ======================================
-- 住服比异常项目数
-- ======================================





with one_to_many AS(
SELECT
a.username AS username -- 用户名
,SUBSTRING_INDEX(SUBSTRING_INDEX(a.mang_project, ',',b.help_topic_id + 1),',',-1) AS mang_project -- 管理项目名
FROM ods_one_to_many_perm a
JOIN mysql.help_topic b
on b.help_topic_id < (LENGTH(a.mang_project) - length(replace(a.mang_project,',','')) + 1)
WHERE a.username  = '${fine_username}'
),hr_date AS
(


SELECT
 B.organ_code   AS organ_code
,B.organ_name   AS organ_name
,B.comm_id      AS comm_id
,B.comm_name    AS comm_name
,SUM(A.bzs)  AS bzs
,SUM(zzs) AS zzs -- 在职人数
FROM
(
    SELECT
    A1.pq  AS pq
    ,A1.xm AS xm
    ,SUM(A1.zzs) AS zzs
    ,sum(A1.bzs)  AS bzs
    FROM
    (
          SELECT
            pq	-- 片区
           ,ny
           ,xm
           ,SUM(zzs) AS zzs 	-- 在职数
           ,SUM(bzs) AS bzs -- 编制数
            FROM ods_hr_view_mbl_d
            WHERE pq not in('89','267') -- 排除了佳美、悦汇
            GROUP BY pq,ny,xm
            -- 没有项目的历史数据
        --    UNION ALL
        --    SELECT
        --    organ_code as pq
        --    ,concat(left(ym,4),'-',right(ym,2))  as ny
        --    ,'' AS xm
        --    ,SUM(zzs) AS zzs
        --    ,SUM(bzs) AS bzs
        --  FROM ods_hr_history_bz_num
        --  WHERE 1 = 1
        --  GROUP BY ym,organ_code
    )A1
    WHERE A1.ny = '${p_ym}'
    group by A1.pq,A1.xm
)A
RIGHT JOIN
(
         SELECT
    organ_code    -- erp片区编码
     ,organ_name  -- erp片区名称
     ,hr_organ_id  -- hr片区编码
     ,hr_organ_name  -- hr片区名称
     ,hr_comm_id    -- hr项目id
     ,hr_comm_name  -- hr项目名称
     ,comm_id
     ,comm_name
    FROM dim_organ_mapping
    WHERE length(hr_comm_id) > 0
    GROUP BY
    organ_code
       ,organ_name
       ,hr_organ_id
       ,hr_organ_name
       ,hr_comm_id
       ,hr_comm_name
       ,comm_id
       ,comm_name
)B
ON A.pq = B.hr_organ_id
AND A.xm = B.hr_comm_id
group by B.organ_code, B.organ_name, B.comm_id, B.comm_name

)
SELECT
count(1) AS CNT
FROM
(
select
    a.comm_name  as p_area  -- 片区/项目
   --  organ_name      -- 片区名称
   -- ,comm_name       -- 项目名称
    ,sum(room_num) as room_num        -- 房间总数 【总户数】
    ,sum(check_ins_num) as check_ins_num   -- 入住数   【入住户数】
    ,case when sum(check_ins_num) = 0
              then 0
          else sum(check_ins_num) / sum(room_num)
    end as   occupancy_rate -- 入住率
    ,SUM(IFNULL(b.zzs,0)) AS zzs -- 在职数
    ,CASE WHEN sum(check_ins_num) = 0
    THEN 0 ELSE SUM(IFNULL(b.zzs,0)) / sum(check_ins_num) END AS zfb -- 住服比
    ,CASE WHEN SUM(b.bzs) = 0 THEN 0 ELSE SUM(b.zzs) / SUM(b.bzs) END AS full_rate -- 满编率
from
(
 SELECT
    organ_code
    ,organ_name
    ,comm_id
    ,comm_name
    ,SUM(room_num) AS room_num
    ,SUM(check_ins_num) AS check_ins_num
    FROM dws_operation_room_park_info_d
    WHERE 1 = 1
-- ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

    group by
        organ_code
    ,organ_name
        ,comm_id
    ,comm_name
) a
left join hr_date b
on a.organ_code = b.organ_code
and a.comm_id = b.comm_id
where 1=1
${IF(LEN(p_area)!=0," and a.organ_code IN ('"+JOINARRAY(p_area,"','")+"')","")}
-- ${if(left(fine_role,2)=="项目","and comm_name = '"+GETUSERDEPARTMENTS(3)+"'","")}
AND CASE WHEN (SELECT mang_project FROM ods_one_to_many_perm WHERE username  = '${fine_username}' AND mang_organ IS NULL) IS NOT NULL
         THEN a.comm_name IN (SELECT mang_project FROM one_to_many WHERE username  = '${fine_username}')
          ELSE ${if(left(fine_role,2)=="项目","a.comm_name = '"+GETUSERDEPARTMENTS(3)+"'","1=1")} END

group by
a.comm_name
)T
where T.zfb > 0.1

