DELIMITER $$
CREATE DEFINER=`bigdata`@`%` PROCEDURE `personnel_project_authority`()
begin
truncate table personnel_project_authority;
INSERT into personnel_project_authority
SELECT
a.account_id,-- 员工编号,
b.company_id,-- 公司 id,
b.company_name,-- 公司名称,
b.region_id,-- 管控区域 id,
b.region_name,-- 管控区域名称,
b.area_id,-- 项目 id,
b.area_name, -- 项目名称
b.tenant_id
FROM
( SELECT account_id, tenant_id, object_id FROM lehome_business_oauth2.account_post_relation WHERE department_type = 1 GROUP BY account_id, tenant_id, department_type, object_id ) a
LEFT JOIN (
SELECT
CASE WHEN d.id IS NULL THEN c.id ELSE d.id END AS company_id,
CASE WHEN d.company_name IS NULL THEN c.company_name ELSE d.company_name END AS company_name,
c.id AS region_id,
c.company_name AS region_name,
a.id AS area_id,
a.area_name,
b.tenant_id
FROM
( SELECT * FROM lehome_property_db.area_info WHERE deleted_status = 'Normal' AND enabled_status = 'Enabled' -- deleted_status:状态值,enabled_status:是否启用
-- AND data_tenant_id IN ( 'f3bf88fadc7b324b3b561fe39fea56e7', '53913585f1aaea19a8e0a883d93aacb5' )
) a
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company_area_relation ) b ON a.id = b.area_id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 2 AND STATUS = 1 ) c ON b.company_id = c.id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 1 AND STATUS = 1 ) d ON c.parent_company_id = d.id
WHERE
b.tenant_id IS NOT NULL
AND b.tenant_id != ''
) b ON a.tenant_id = b.tenant_id
WHERE
b.area_id IS NOT NULL UNION ALL
SELECT
a.account_id,-- 员工编号,
b.company_id,
b.company_name,
b.region_id,
b.region_name,
b.area_id,
b.area_name,
b.tenant_id
FROM
(
SELECT
account_id,
object_id
FROM
lehome_business_oauth2.account_post_relation
WHERE
department_type = 4
AND account_id NOT IN ( SELECT account_id FROM lehome_business_oauth2.account_post_relation WHERE department_type = 1 GROUP BY account_id )
GROUP BY
account_id,
object_id
) a
LEFT JOIN (
SELECT
CASE WHEN d.id IS NULL THEN c.id ELSE d.id END AS company_id,
CASE WHEN d.company_name IS NULL THEN c.company_name ELSE d.company_name END AS company_name,
c.id AS region_id,
c.company_name AS region_name,
a.id AS area_id,
a.area_name,
b.tenant_id
FROM
( SELECT * FROM lehome_property_db.area_info WHERE deleted_status = 'Normal' AND enabled_status = 'Enabled' -- deleted_status:状态值,enabled_status:是否启用
-- AND data_tenant_id IN ( 'f3bf88fadc7b324b3b561fe39fea56e7', '53913585f1aaea19a8e0a883d93aacb5' )
) a
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company_area_relation ) b ON a.id = b.area_id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 2 AND STATUS = 1 ) c ON b.company_id = c.id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 1 AND STATUS = 1 ) d ON c.parent_company_id = d.id
WHERE
b.tenant_id IS NOT NULL
AND b.tenant_id != ''
) b ON a.object_id = b.company_id
WHERE
b.area_id IS NOT NULL UNION ALL
SELECT
a.account_id,-- 员工编号,
b.company_id,
b.company_name,
b.region_id,
b.region_name,
b.area_id,
b.area_name,
b.tenant_id
FROM
(
SELECT
account_id,
object_id
FROM
lehome_business_oauth2.account_post_relation
WHERE
department_type = 3
AND account_id NOT IN ( SELECT account_id FROM lehome_business_oauth2.account_post_relation WHERE department_type IN ( 1, 4 ) GROUP BY account_id )
GROUP BY
account_id,
object_id
) a
LEFT JOIN (
SELECT
CASE WHEN d.id IS NULL THEN c.id ELSE d.id END AS company_id,
CASE WHEN d.company_name IS NULL THEN c.company_name ELSE d.company_name END AS company_name,
c.id AS region_id,
c.company_name AS region_name,
a.id AS area_id,
a.area_name,
b.tenant_id
FROM
( SELECT * FROM lehome_property_db.area_info WHERE deleted_status = 'Normal' AND enabled_status = 'Enabled' -- deleted_status:状态值,enabled_status:是否启用
-- AND data_tenant_id IN ( 'f3bf88fadc7b324b3b561fe39fea56e7', '53913585f1aaea19a8e0a883d93aacb5' )
) a
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company_area_relation ) b ON a.id = b.area_id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 2 AND STATUS = 1 ) c ON b.company_id = c.id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 1 AND STATUS = 1 ) d ON c.parent_company_id = d.id
WHERE
b.tenant_id IS NOT NULL
AND b.tenant_id != ''
) b ON a.object_id = b.region_id
WHERE
b.area_id IS NOT NULL UNION ALL
SELECT
a.account_id,-- 员工编号,
b.company_id,
b.company_name,
b.region_id,
b.region_name,
b.area_id,
b.area_name,
b.tenant_id
FROM
(
SELECT
account_id,
object_id
FROM
lehome_business_oauth2.account_post_relation
WHERE
department_type = 2
AND account_id NOT IN ( SELECT account_id FROM lehome_business_oauth2.account_post_relation WHERE department_type IN ( 1, 3, 4 ) GROUP BY account_id )
GROUP BY
account_id,
object_id
) a
LEFT JOIN (
SELECT
CASE WHEN d.id IS NULL THEN c.id ELSE d.id END AS company_id,
CASE WHEN d.company_name IS NULL THEN c.company_name ELSE d.company_name END AS company_name,
c.id AS region_id,
c.company_name AS region_name,
a.id AS area_id,
a.area_name,
b.tenant_id
FROM
( SELECT * FROM lehome_property_db.area_info WHERE deleted_status = 'Normal' AND enabled_status = 'Enabled' -- deleted_status:状态值,enabled_status:是否启用
-- AND data_tenant_id IN ( 'f3bf88fadc7b324b3b561fe39fea56e7', '53913585f1aaea19a8e0a883d93aacb5' )
) a
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company_area_relation ) b ON a.id = b.area_id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 2 AND STATUS = 1 ) c ON b.company_id = c.id
LEFT JOIN ( SELECT * FROM lehome_business_oauth2.company WHERE company_type = 1 AND STATUS = 1 ) d ON c.parent_company_id = d.id
WHERE
b.tenant_id IS NOT NULL
AND b.tenant_id != ''
) b ON a.object_id = b.area_id
WHERE
b.area_name IS NOT NULL;
END$$
DELIMITER ;
personnel_project_authorit 是创建的存储过程的名字
DROP EVENT IF EXISTS `lehome_business_oauth2`.`e_personnel_project_authority`;
CREATE EVENT `lehome_business_oauth2`.`e_personnel_project_authority`
ON SCHEDULE EVERY 20 MINUTE
STARTS '2022-12-09 15:01:00' ON COMPLETION PRESERVE
ENABLE
DO call personnel_project_authority();
在sql客户端上面创建定时器
通过sql命令创建定时任务
事件名称为e_personnel_project_authority 每二十分钟执行一次 执行的内容为 personnel_project_authority 这个存储过程
CREATE EVENT e_personnel_project_authority
ON SCHEDULE EVERY 20 MINUTE
DO CALL personnel_project_authority();