select
concat(right(ym,2) + 0 , '月') ym
, sum(plan_num) plan_num
, sum(plan_complete_num) plan_complete_num
, sum(plan_incomplete_num) plan_incomplete_num
, sum(plan_complete_num) / sum(plan_num) plan_complete_rate
, sum(plan_adjust_num) plan_adjust_num
, sum(plan_adjust_num) / sum(plan_num) plan_adjust_rate
from dws_operation_plan_summary_m a
left join (select distinct organ_code, organ_name, comm_id, comm_name
, oa_organ_code, oa_organ_name, oa_comm_id, oa_comm_name from dim_organ_mapping where comm_id is not null) b
on a.subcompanyid = b.oa_comm_id
where 1=1
${if(len(organ)=0,"","and organ_code = '"+organ+"'")}
and left(ym,4) = '2024'
group by ym
order by ym asc



-- 【设备巡检】

-- 【old】
select
    sum(task_finish_num)
     , sum(task_num)
     , sum(task_finish_num) / sum(task_num) inspection_finish_rate
FROM dws_engine_equipment_inspect_task_m
        where task_ym = date_format(current_date(),'%Y%m')
    ${if(len(organ)=0,"","and organ_code = '"+organ+"'")}
;

-- 【new】

select
comm_id
,comm_name
,task_ym
    sum(task_finish_num) / sum(task_num) as inspection_finish_rate
FROM dws_engine_equipment_inspect_task_m
where task_ym = date_format(current_date(),'%Y%m')
group by
    comm_id
       ,comm_name
       ,task_ym
;

    ${if(len(organ)=0,"","and organ_code = '"+organ+"'")}




    设备巡查.select(task_finish_num) / 设备巡查.select(task_num)


-- ----------------------------------
--  report_商业出租率
-- ---------------------------------
select sum(lease_area)/sum(construction_area) rent_rate
FROM dws_business_rent_rate_d a
         left join (select distinct id, erp_id from ods_bs_project_d) b
                   on a.project_id  = b.id
         left join (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping where comm_id is not null) c
                   on b.erp_id = c.comm_id
where 1=1
    ${if(len(organ)=0,"","and c.organ_code = '"+organ+"'")}


-- ----------------------------------
--  report_商业出租率_new
-- ---------------------------------

SELECT
    T.`type`
     ,SUM(T.rentRoomAreaSum) / (SUM(T.rentRoomAreaSum) + SUM(T.notRentRoomAreaSum)) as rent_rate -- 出租率
FROM
    (
        SELECT
            B.`type`
             ,SUM(CASE WHEN A.business_status = 1 THEN A.construction_area ELSE 0 END ) AS rentRoomAreaSum  -- 出租面积
             ,SUM(CASE WHEN A.business_status != 4 AND A.business_status != 1 THEN A.construction_area ELSE 0 END )  AS notRentRoomAreaSum -- 未出租面积
        FROM
            (
                SELECT
                    id
                     ,erp_id
                     ,building_id
                     ,business_status
                     ,construction_area  -- 建筑面积
                FROM dw.ods_bs_room_d  -- 商管房间表
                WHERE del_flag = 0
                  AND enable_flag = 0
                  AND review_status = 2
            )A
                LEFT JOIN
            (
                select
                    id
                     ,project_id
                     ,project_name
                     ,`type` -- 业态 商业街，集中商业，写字楼，底商
                from dim_business_building_d
            )B
            on A.building_id = B.id
                LEFT JOIN
            (
                select
                    distinct
                    organ_code
                           ,organ_name
                           ,comm_id
                           ,comm_name
                from dim_organ_mapping
                where comm_id is not null
            )C
            on A.erp_id = C.comm_id
        where 1=1
            ${if(len(organ)=0,"","and b.organ_code = '"+ organ+"'")}
        GROUP BY
            B.`type`
    )T
GROUP BY
    T.`type`
;




-- ----------------------------------
--  report_商业收缴率
-- ---------------------------------

select sum(received_money) / sum(current_sure_money) collection_rate
FROM dws_business_bill_m a
         left join (select distinct id, erp_id from ods_bs_project_d) b
                   on a.project_id  = b.id
         left join (select distinct organ_code, organ_name, comm_id, comm_name from dim_organ_mapping where comm_id is not null) c
                   on b.erp_id = c.comm_id
where left(accrual_month,7) = '2024-06'
    ${if(len(organ)=0,"","and c.organ_code = '"+organ+"'")}

;


-- ----------------------------------
--  report_商业收缴率_new
-- ---------------------------------

SELECT
    SUM(T.received_money) / SUM(T.current_sure_money)  AS collection_rate  -- 收缴率
FROM
    (
        SELECT
            T1.received_money AS received_money
             ,T1.current_sure_money AS current_sure_money

        FROM
            (
                SELECT
                    a.project_id -- 项目id 关联bs_project表
                     ,SUM(a.received_money) AS received_money			-- 实收金额
                     ,SUM(a.current_sure_money) AS current_sure_money	-- 实际应收金额=应收金额-合同减免金额
                -- ,SUM(a.under_money) AS under_money	-- 欠收金额
                FROM
                    (
                        SELECT
                            contract_id
                             ,accrual_month
                             ,start_period
                             ,bill_type
                             ,received_money -- 实收金额
                             ,current_sure_money	-- 实际应收金额=应收金额-合同减免金额
                             ,current_sure_money - received_money as under_money	-- 欠收金额
                             ,project_id
                        FROM dw.ods_bs_fi_bills_d
                        WHERE save_status = 1  -- 账单状态 0是保存 1是审核过的
                        --	${if(len(project) == 0,"","AND project_id in (" + project + ")")}
                    )a
                        left join
                    (
                        SELECT
                            status
                             ,stop_date
                             ,id
                        FROM dw.ods_bs_contract_d
                        WHERE del_flag = 0
                    )bc
                    on bc.id = a.contract_id
                WHERE ((bc.status != 2  and bc.stop_date is null) OR
                       (bc.stop_date is not null
                           and ((a.accrual_month  <=  DATE_FORMAT(bc.stop_date, '%Y-%m')
                               and a.start_period  <=  bc.stop_date)

                               or a.bill_type = 15)
                           ))
                group by
                    a.project_id
                UNION ALL
                SELECT
                    a.project_id
                     ,SUM(b.received_amount) AS received_money
                     ,SUM(b.received_money)  AS current_sure_money
                -- ,SUM(b.under_money)		AS under_money
                FROM
                    (
                        SELECT
                            receive_no -- 催缴单号
                             ,project_id
                        FROM dw.ods_bs_fi_receive_other_d
                        where recheck_status = 2
                    )a
                        LEFT JOIN
                    (
                        SELECT
                            receive_no
                             ,received_amount
                             ,received_money
                             ,received_money - received_amount AS under_money
                        FROM dw.ods_bs_fi_receive_other_item_d
                    )b
                    ON a.receive_no = b.receive_no
                group by
                    project_id
            )T1
                left join
            (
                select
                    distinct id
                           ,erp_id
                from dw.ods_bs_project_d
            ) b
            on T1.project_id  = b.id
                left join
            (
                select
                    distinct
                    organ_code
                           ,organ_name
                           ,comm_id
                           ,comm_name
                from dim_organ_mapping
                where comm_id is not null
            ) c
            on b.erp_id = c.comm_id
            ${if(len(organ)=0,"","and c.organ_code = '"+organ+"'")}
    )T

;