-- ======================================
-- 预警-片区-回款率-30%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '回款率'
      and comm_organ = '片区'
      and index_value = '0.3'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.payment_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.payment_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-客满完成率-90%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '客满完成率'
      and comm_organ = '片区'
      and index_value = '0.9'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.km_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.km_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-客满完成率-95%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '客满完成率'
      and comm_organ = '片区'
      and index_value = '0.95'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.km_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.km_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-当期收缴完成率-80%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '当期收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.8'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.cu_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.cu_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-当期收缴完成率-75%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '当期收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.75'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.cu_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.cu_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-当期收缴完成率-70%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '当期收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.7'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.cu_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.cu_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-往期收缴完成率-75%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '往期收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.75'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.pr_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.pr_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-往期收缴完成率-70%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '往期收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.7'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.pr_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.pr_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-往期收缴完成率-80%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '往期收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.8'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.pr_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.pr_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-投诉关闭率-95%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '投诉关闭率'
      and comm_organ = '片区'
      and index_value = '0.95'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.ts_close_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.ts_close_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-投诉关闭率-90%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '投诉关闭率'
      and comm_organ = '片区'
      and index_value = '0.9'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.ts_close_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.ts_close_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;






-- ======================================
-- 预警-片区-报事关闭率-80%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '报事关闭率'
      and comm_organ = '片区'
      and index_value = '0.8'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.report_close_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.report_close_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;





-- ======================================
-- 预警-片区-报事关闭率-90%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '报事关闭率'
      and comm_organ = '片区'
      and index_value = '0.9'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.report_close_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.report_close_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;





-- ======================================
-- 预警-片区-新零售收入完成率-70%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '新零售收入完成率'
      and comm_organ = '片区'
      and index_value = '0.7'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.xing_revenue_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.xing_revenue_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-水电费收缴率-98%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '水电费收缴率'
      and comm_organ = '片区'
      and index_value = '0.98'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.water_electric_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.water_electric_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-空间运营收入完成率-100%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '空间运营收入完成率'
      and comm_organ = '片区'
      and index_value = '1'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.space_operate_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.space_operate_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;





-- ======================================
-- 预警-片区-综合收缴完成率-75%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '综合收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.75'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.total_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.total_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-综合收缴完成率-80%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '综合收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.8'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.total_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.total_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-综合收缴完成率-70%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '综合收缴完成率'
      and comm_organ = '片区'
      and index_value = '0.7'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.total_coll_comple_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.total_coll_comple_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;



-- ======================================
-- 预警-片区-美居收入完成率-50%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '美居收入完成率'
      and comm_organ = '片区'
      and index_value = '0.5'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.meiju_revenue_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.meiju_revenue_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;





-- ======================================
-- 预警-片区-设备空间巡查完成率-98%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '设备空间巡查完成率'
      and comm_organ = '片区'
      and index_value = '0.98'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.device_space_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.device_space_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;




-- ======================================
-- 预警-片区-资产租售收入完成率-50%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '资产租售收入完成率'
      and comm_organ = '片区'
      and index_value = '0.5'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.rent_revenue_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.rent_revenue_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;





-- ======================================
-- 预警-片区-销户率-30%
-- ======================================

with a as( -- 所有有角色的人员
    select t1.username,t3.name as role_name from finedb.fine_user t1
    inner join finedb.fine_user_role_middle t2 on t1.id = t2.userId
    inner join finedb.fine_custom_role t3 on t2.roleId = t3.id
),
b as( -- 找到单条预警规则**
    select index_name,comm_organ,index_value,mang_organ from ods_index_early_warning
    where index_name = '销户率'
      and comm_organ = '片区'
      and index_value = '0.3'
    limit 1
),
c as( -- 找到关联的用户
    select distinct username from b
    join a on FIND_IN_SET(a.role_name,b.mang_organ)
),
d as( -- 找到规则下受影响的项目**
    select t1.organ_name,t1.account_rate from dim_early_warning_organ_d t1
    inner join b t2 on t1.account_rate < t2.index_value
),
e as( -- 要推送的总部人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '总部人员'
),
f as( -- 要推送的片区人员
    select distinct t2.username,t2.LoginCode,t2.user_type,t2.ProjectAuth from c t1
    inner join v_erp_user_auth t2 on t1.username = t2.LoginCode and t2.user_type = '片区人员'
    inner join d t3 on FIND_IN_SET(t3.organ_name,t2.ProjectAuth)
)
-- 要推送的人员合集
select username,LoginCode,user_type,ProjectAuth from e
union all
select username,LoginCode,user_type,ProjectAuth from f
;