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,-- 管控區(qū)域 id,
b.region_name,-- 管控區(qū)域名稱,
b.area_id,-- 項(xiàng)目 id,
b.area_name, -- 項(xiàng)目名稱
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:狀態(tài)值,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:狀態(tài)值,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:狀態(tài)值,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:狀態(tài)值,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 是創(chuàng)建的存儲過程的名字
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客戶端上面創(chuàng)建定時器
通過sql命令創(chuàng)建定時任務(wù)
事件名稱為e_personnel_project_authority 每二十分鐘執(zhí)行一次 執(zhí)行的內(nèi)容為 personnel_project_authority 這個存儲過程
CREATE EVENT e_personnel_project_authority
ON SCHEDULE EVERY 20 MINUTE
DO CALL personnel_project_authority();