/*
初始化customer_agency,customer_department,customer_grid
*/


INSERT INTO epmet_gov_org.customer_grid ( ID, CUSTOMER_ID, GRID_NAME, SYNC_FLAG, DEL_FLAG, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME, MANAGE_DISTRICT, TOTAL_USER, PID, PIDS ) SELECT
t1.id,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.NAME AS GRID_NAME,
'1',
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,
t1.NAME AS MANAGE_DISTRICT,
t2.total AS TOTAL_USER,
t1.pid,
concat(
	SUBSTRING_INDEX( t1.pids, ',', 1 ),
	':',
	SUBSTRING_INDEX( SUBSTRING_INDEX( t1.pids, ',', 2 ), ',',- 1 ),
	':',
SUBSTRING_INDEX( t1.pids, ',',- 1 )) pids 
FROM
	esua_epdc_admin.sys_dept t1
	LEFT JOIN ( SELECT count( id ) total, dept_id FROM esua_epdc_admin.sys_user GROUP BY dept_id ) t2 ON t1.id = t2.dept_id 
WHERE
	t1.type_key = 'grid_party' ;

INSERT INTO epmet_gov_org.customer_agency (
	ID,
	CUSTOMER_ID,
	PID,
	PIDS,
	ALL_PARENT_NAME,
	ORGANIZATION_NAME,
	LEVEL,
	SYNC_FLAG,
	DEL_FLAG,
	CREATED_BY,
	CREATED_TIME,
	UPDATED_BY,
	UPDATED_TIME,
	TOTAL_USER 
) SELECT
t1.id,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.pid,
CASE
		t1.type_key 
		WHEN 'street_party' THEN
		t1.pids 
		WHEN 'community_party' THEN
		concat(
			SUBSTRING_INDEX( t1.pids, ',', 1 ),
			':',
		SUBSTRING_INDEX( t1.pids, ',',- 1 )) 
	END AS pids,
CASE
		t1.type_key 
		WHEN 'street_party' THEN
		'市北区委' 
		WHEN 'community_party' THEN
		t5.ALL_PARENT_NAME 
	END AS ALL_PARENT_NAME,
	t1.NAME AS ORGANIZATION_NAME,
CASE
		t1.type_key 
		WHEN 'district_party' THEN
		'district' 
		WHEN 'street_party' THEN
		'street' 
		WHEN 'community_party' THEN
		'community' 
	END AS LEVEL,
	'1',
	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,
	t2.total AS TOTAL_USER 
FROM
	esua_epdc_admin.sys_dept t1
	LEFT JOIN ( SELECT count( id ) total, dept_id FROM esua_epdc_admin.sys_user GROUP BY dept_id ) t2 ON t1.id = t2.dept_id
	LEFT JOIN (
	SELECT
		concat( '市北区委-', t4.NAME ) AS ALL_PARENT_NAME,
		t3.id 
	FROM
		esua_epdc_admin.sys_dept t3
		LEFT JOIN esua_epdc_admin.sys_dept t4 ON t3.pid = t4.id 
	WHERE
		t3.type_key = 'community_party' 
	) t5 ON t1.id = t5.id 
WHERE
	t1.type_key = 'district_party' 
	OR t1.type_key = 'street_party' 
	OR t1.type_key = 'community_party' ;

INSERT INTO epmet_gov_org.customer_department (
	ID,
	CUSTOMER_ID,
	AGENCY_ID,
	DEPARTMENT_NAME,
	TOTAL_USER,
	DEL_FLAG,
	CREATED_BY,
	CREATED_TIME,
	UPDATED_BY,
	UPDATED_TIME
) SELECT
t1.id,
'b09527201c4409e19d1dbc5e3c3429a1',
t1.pid as AGENCY_ID,
t1.name as DEPARTMENT_NAME,
t2.total AS TOTAL_USER,
	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_dept t1
	LEFT JOIN ( SELECT count( id ) total, dept_id FROM esua_epdc_admin.sys_user GROUP BY dept_id ) t2 ON t1.id = t2.dept_id 
WHERE
	t1.type_key = 'district_dept' or t1.type_key = 'street_dept' ;









