UPDATE yz_auth_tenant_apply a JOIN yz_system_tenant t ON t.tenant_name = a.tenant_name AND t.delete_time IS NULL SET a.tid = t.id, a.update_time = NOW() WHERE a.type = 2 AND a.status = 1 AND a.delete_time IS NULL AND (a.tid = 0 OR a.tid IS NULL); SELECT a.id AS apply_id, a.status AS apply_status, a.tid, a.tenant_name, a.identity_id AS apply_identity, ai.account AS apply_account, ai.mobile AS apply_mobile, tu.id AS bind_id, tu.identity_id AS bind_identity, bi.account AS bind_account, tu.status AS bind_status, tu.delete_time AS bind_deleted, tu.group_id, t.status AS tenant_status FROM yz_auth_tenant_apply a LEFT JOIN yz_auth_identity ai ON ai.id = a.identity_id LEFT JOIN yz_auth_tenant_user tu ON tu.tid = a.tid LEFT JOIN yz_auth_identity bi ON bi.id = tu.identity_id LEFT JOIN yz_system_tenant t ON t.id = a.tid WHERE a.delete_time IS NULL ORDER BY a.id DESC LIMIT 20; UPDATE yz_auth_tenant_user tu JOIN yz_auth_tenant_apply a ON a.tid = tu.tid AND a.type = 2 AND a.status = 1 AND a.delete_time IS NULL SET tu.identity_id = a.identity_id, tu.status = 1, tu.is_default = 1, tu.delete_time = NULL, tu.update_time = NOW() WHERE tu.identity_id <> a.identity_id; UPDATE yz_auth_tenant_user tu JOIN yz_auth_tenant_apply a ON a.tid = tu.tid AND a.type = 2 AND a.status = 1 AND a.delete_time IS NULL SET tu.status = 1, tu.is_default = 1, tu.delete_time = NULL, tu.update_time = NOW() WHERE tu.identity_id = a.identity_id AND (tu.status <> 1 OR tu.delete_time IS NOT NULL); INSERT INTO yz_auth_tenant_user (tid, identity_id, account, name, phone, email, group_id, org_id, is_default, status, create_time) SELECT a.tid, a.identity_id, COALESCE(NULLIF(i.mobile, ''), NULLIF(i.account, ''), CONCAT('user', a.identity_id)), COALESCE(i.nickname, ''), i.mobile, i.email, 0, 0, 1, 1, NOW() FROM yz_auth_tenant_apply a JOIN yz_auth_identity i ON i.id = a.identity_id WHERE a.type = 2 AND a.status = 1 AND a.tid > 0 AND a.delete_time IS NULL AND NOT EXISTS ( SELECT 1 FROM yz_auth_tenant_user tu WHERE tu.tid = a.tid AND tu.identity_id = a.identity_id ); SELECT tu.identity_id, tu.tid, tu.status AS bind_status, tu.is_default, tu.group_id, t.tenant_name, t.status AS tenant_status FROM yz_auth_tenant_user tu JOIN yz_system_tenant t ON t.id = tu.tid JOIN yz_auth_tenant_apply a ON a.tid = tu.tid AND a.type = 2 AND a.status = 1 AND a.identity_id = tu.identity_id;