/*
初始化customer_staff_agency,customer_staff_department,customer_staff_grid
*/


INSERT INTO epmet_gov_org.customer_staff_grid ( ID, CUSTOMER_ID, USER_ID, GRID_ID, DEL_FLAG, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME ) SELECT
t1.id,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.id AS USER_ID,
t1.dept_id AS GRID_ID,
t1.del_flag,
t1.creator AS CREATED_BY,
t1.create_date AS CREATED_TIME,
t1.updater AS UPDATED_BY,
t1.update_date AS UPDATED_TIME 
FROM
	esua_epdc_admin.sys_user t1
	LEFT JOIN esua_epdc_admin.sys_dept t2 ON t1.dept_id = t2.id 
WHERE
	t2.type_key = 'grid_party';


INSERT INTO epmet_gov_org.customer_staff_department (
	ID,
	CUSTOMER_ID,
	USER_ID,
	DEPARTMENT_ID,
	DEL_FLAG,
	CREATED_BY,
	CREATED_TIME,
	UPDATED_BY,
	UPDATED_TIME 
) SELECT
t1.id ,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.id as USER_ID,
t1.dept_id as DEPARTMENT_ID,
t1.del_flag,
t1.creator as CREATED_BY,
t1.create_date as CREATED_TIME,
t1.updater as UPDATED_BY,
t1.update_date as UPDATED_TIME
from esua_epdc_admin.sys_user t1
left join esua_epdc_admin.sys_dept t2 on t1.dept_id = t2.id
WHERE
	t2.type_key = 'district_dept' 
	OR t2.type_key = 'street_dept';



INSERT INTO epmet_gov_org.customer_staff_agency (
	ID,
	CUSTOMER_ID,
	USER_ID,
	AGENCY_ID,
	DEL_FLAG,
	CREATED_BY,
	CREATED_TIME,
	UPDATED_BY,
	UPDATED_TIME 
) SELECT
t1.id ,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.id as USER_ID,
t1.dept_id as AGENCY_ID,
t1.del_flag,
t1.creator as CREATED_BY,
t1.create_date as CREATED_TIME,
t1.updater as UPDATED_BY,
t1.update_date as UPDATED_TIME
from esua_epdc_admin.sys_user t1
left join esua_epdc_admin.sys_dept t2 on t1.dept_id = t2.id
WHERE
	t2.type_key = 'district_party' 
	OR t2.type_key = 'street_party' 
	OR t2.type_key = 'community_party';

INSERT INTO epmet_user.customer_staff ( ID, CUSTOMER_ID,USER_ID,REAL_NAME,GENDER,EMAIL,MOBILE,HEAD_PHOTO,PASSWORD,DEL_FLAG,REVISION,CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME) SELECT
t1.id as ID,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.id AS USER_ID,
t1.real_name as REAL_NAME,
t1.gender as GENDER,
t1.email as EMAIL,
t1.mobile as MOBILE,
t1.head_url as HEAD_PHOTO,
t1.password as PASSWORD,
t1.del_flag as DEL_FLAG,
'0' as REVISION,
t1.creator AS CREATED_BY,
t1.create_date AS CREATED_TIME,
t1.updater AS UPDATED_BY,
t1.update_date AS UPDATED_TIME
FROM
	esua_epdc_admin.sys_user t1
where t1.user_tag_key ='offers';

INSERT INTO epmet_user.USER (ID, CUSTOMER_ID, DEL_FLAG, REVISION, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME ) SELECT
t1.id AS ID,
'b09527201c4409e19d1dbc5e3c3429a1' AS CUSTOMER_ID,
t1.del_flag AS DEL_FLAG,
'0' AS REVISION,
t1.creator AS CREATED_BY,
t1.create_date AS CREATED_TIME,
t1.updater AS UPDATED_BY,
t1.update_date AS UPDATED_TIME 
FROM
	esua_epdc_admin.sys_user t1
where t1.user_tag_key ='offers';


INSERT INTO epmet_gov_org.staff_org_relation ( ID, CUSTOMER_ID, PIDS, STAFF_ID, ORG_ID, ORG_TYPE, DEL_FLAG, REVISION, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME ) SELECT
t1.id AS ID,
'b09527201c4409e19d1dbc5e3c3429a1' AS CUSTOMER_ID,
CASE
		t2.type_key 
		WHEN 'district_party' THEN
		t2.pids 
		WHEN 'street_party' THEN
		t2.pids 
		WHEN 'community_party' THEN
		concat(
			SUBSTRING_INDEX( t2.pids, ',', 1 ),
			':',
		SUBSTRING_INDEX( t2.pids, ',',- 1 )) 
		WHEN 'district_dept' THEN
		t2.pids 
		WHEN 'street_dept' THEN
		concat(
			SUBSTRING_INDEX( t2.pids, ',', 1 ),
			':',
		SUBSTRING_INDEX( t2.pids, ',',- 1 )) 
		WHEN 'grid_party' THEN
		concat(
			SUBSTRING_INDEX( t2.pids, ',', 1 ),
			':',
			SUBSTRING_INDEX( SUBSTRING_INDEX( t2.pids, ',', 2 ), ',',- 1 ),
			':',
		SUBSTRING_INDEX( t2.pids, ',',- 1 )) 
	END AS PIDS,
	t1.id AS STAFF_ID,
	t2.id AS ORG_ID,
CASE
		t2.type_key 
		WHEN 'district_party' THEN
		'agency' 
		WHEN 'street_party' THEN
		'agency' 
		WHEN 'community_party' THEN
		'agency' 
		WHEN 'district_dept' THEN
		'dept' 
		WHEN 'street_dept' THEN
		'dept' 
		WHEN 'grid_party' THEN
		'grid' 
	END AS ORG_TYPE,
	t1.del_flag AS DEL_FLAG,
	'0' AS REVISION,
	t1.creator AS CREATED_BY,
	t1.create_date AS CREATED_TIME,
	t1.updater AS UPDATED_BY,
	t1.update_date AS UPDATED_TIME 
FROM
	esua_epdc_admin.sys_user t1
	LEFT JOIN esua_epdc_admin.sys_dept t2 ON t1.dept_id = t2.id 
WHERE
	t1.user_tag_key = 'offers';