--------------------------------------------------------- 补充字段
-- alter table lf.sys_meta rename column bstab to tab; alter table lf.sys_meta drop column mdtab;
alter table lf.sys_fme_log add column create_time timestamp(6) without time zone default now();
alter table lf.sys_meta add column layer varchar(50);
comment on column lf.sys_meta.layer is '图层';
select * from lf.sys_fme_log;
select * from lf.sys_meta order by guid,tab; -- delete from lf.sys_meta where id> 31;
-- delete from lf.sys_meta; alter sequence lf.sys_meta_id_seq restart with 1;

select * from lf.sys_token where create_time < '2023-01-10';
-- delete from lf.sys_token where create_time < '2023-01-10';
select * from lf.sys_operate where optime < '2023-01-10';
-- delete from lf.sys_operate where optime < '2023-01-10';
select * from lf.sys_login where optime < '2023-01-10';
-- delete from lf.sys_login where optime < '2023-01-10';
--------------------------------------------------------- 查询URL
select * from lf.sys_menu where position('http://' in url) > 0;
select url, replace(url, 'pipe.cppe.com', '{host}') from lf.sys_menu where position('http://' in url) > 0;
update lf.sys_menu set url = replace(url, 'pipe.cppe.com', '{host}') where position('http://' in url) > 0;
--------------------------------------------------------- 更新菜单+用户
select * from lf.sys_menu where cn_name = '数据库监控';
-- update lf.sys_menu set url = 'http://pipe.cppe.com:8088/server/druid/index.html' where cn_name = '数据库监控';

select * from lf.sys_user; -- Admin@1234_lf
-- update lf.sys_user set pwd = '[REDACTED]

select * from lf.sys_menu order by id;
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/schema/list?tags=入库质检&embed=true&theme=dark&lang=zh&token=' where cn_name = '入库质检';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/schema/list?tags=成果质检&embed=true&theme=dark&lang=zh&token=' where cn_name = '成果质检';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/task/list?embed=true&theme=dark&lang=zh&token=' where cn_name = '任务状态' and id = 69;

update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/schema/list?tags=数据交换&embed=true&theme=dark&lang=zh&token=' where cn_name = '格式转换';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/schema/list?tags=坐标转换&embed=true&theme=dark&lang=zh&token=' where cn_name = '坐标转换';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/schema/list?tags=数据分发&embed=true&theme=dark&lang=zh&token=' where cn_name = '数据分发';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/schema/list?tags=数据治理&embed=true&theme=dark&lang=zh&token=' where cn_name = '数据治理';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/dataxdi/#/task/list?embed=true&theme=dark&lang=zh&token=' where cn_name = '任务状态' and id = 74;

update lf.sys_menu set url = 'http://pipe.cppe.com:88/resm/#/resourceManage?embed=true&theme=dark&lang=zh&token=' where cn_name = '我的资源';
update lf.sys_menu set url = 'http://pipe.cppe.com:88/resm/#/resapproval?embed=true&theme=dark&lang=zh&token=' where cn_name = '资源审核';
--------------------------------------------------------- FME_数据质检
select * from lf.sys_menu where cn_name = '入库质检';
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/schema/list?tags=入库质检&embed=true&token=' where cn_name = '入库质检';

select * from lf.sys_menu where cn_name = '成果质检';
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/schema/list?tags=成果质检&embed=true&token=' where cn_name = '成果质检';

select * from lf.sys_menu where cn_name = '任务状态' and id = 69;
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/task/list?embed=true&token=' where cn_name = '任务状态' and id = 69;
--------------------------------------------------------- FME_数据交换
select * from lf.sys_menu where cn_name = '格式转换';
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/schema/list?tags=数据交换&embed=true&token=' where cn_name = '格式转换';

select * from lf.sys_menu where cn_name = '坐标转换';
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/schema/list?tags=坐标转换&embed=true&token=' where cn_name = '坐标转换';

select * from lf.sys_menu where cn_name = '数据分发';
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/schema/list?tags=数据分发&embed=true&token=' where cn_name = '数据分发';

select * from lf.sys_menu where cn_name = '数据治理';
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/schema/list?tags=数据治理&embed=true&token=' where cn_name = '数据治理';

select * from lf.sys_menu where cn_name = '任务状态' and id = 74;
update lf.sys_menu set url = 'http://10.7.0.186:88/dataxdi/#/task/list?embed=true&token=' where cn_name = '任务状态' and id = 74;
--------------------------------------------------------- FME_服务管理
select * from lf.sys_menu where cn_name = '我的资源';
update lf.sys_menu set url = 'http://10.7.0.186:88/resm/#/resourceManage?embed=true&token=' where cn_name = '我的资源';

select * from lf.sys_menu where cn_name = '资源审核';
update lf.sys_menu set url = 'http://10.7.0.186:88/resm/#/resapproval?embed=true&token=' where cn_name = '资源审核';
--------------------------------------------------------- 初始化元数据
insert into lf.sys_meta (name,dirid) values ('项目WBS导出.xlsx',57);
insert into lf.sys_meta (name,dirid) values ('00116BT02-CR001#ESV-DW-0101 封面.docx',62);
insert into lf.sys_meta (name,dirid) values ('00116BT02-CR001#ESV-DW-0101（大河沿河穿越地形图）0.dwg',62);
insert into lf.sys_meta (name,dirid) values ('00116BT02-CR001#ESV-DW-0101（大河沿河穿越地形图）0.dwl',62);
insert into lf.sys_meta (name,dirid) values ('00116BT02-CR001#ESV-DW-0101（大河沿河穿越地形图）0.dwl2',62);
insert into lf.sys_meta (name,dirid) values ('00116BT02-CR001#ESV-DW-0101（大河沿河穿越纵断面图）0.dwg',62);
insert into lf.sys_meta (name,dirid) values ('00116BT02-CR001#ESV-IN-0101（资料图纸目录）.doc',62);
insert into lf.sys_meta (name,dirid) values ('1类.gdb',74);
insert into lf.sys_meta (name,dirid) values ('2类.gdb',74);
insert into lf.sys_meta (name,dirid) values ('易燃易爆.gdb',74);
insert into lf.sys_meta (name,dirid) values ('地面线.xlsx',64);
insert into lf.sys_meta (name,dirid) values ('控制点.xlsx',64);
insert into lf.sys_meta (name,dirid) values ('离散点.xlsx',64);
insert into lf.sys_meta (name,dirid) values ('平面图索引文件.xlsx',64);
insert into lf.sys_meta (name,dirid) values ('中线成果表.xlsx',64);

update lf.sys_meta set depid=1,type='file',path='2\'||name,sizes=1;

insert into lf.sys_meta (name,dirid) values ('S318 洛陕线穿越工程地质剖面图.dwg',80);
insert into lf.sys_meta (name,dirid) values ('地质剖面图索引表.xlsx',80);
insert into lf.sys_meta (name,dirid) values ('地质资料表.xlsx',80);
insert into lf.sys_meta (name,dirid) values ('勘探点坐标表.xlsx',80);
insert into lf.sys_meta (name,dirid) values ('土壤电阻率表.xlsx',80);

update lf.sys_meta set depid=1,type='file',path='2\'||name,sizes=1 where path is null;

with rs as (select id from lf.sys_dir where name='西气东输四线天然气管道工程（吐鲁番-中卫）（00116BT02）')
select fn_rec_query(a.dirid,'dir'),a.* from lf.sys_meta_new a where dirid = ANY(fn_rec_array((select id from rs), 'dir'));

select * from lf.sys_meta where substr(path,1,2) = '2\';
--------------------------------------------------------- 更新内容 ---------------------------------------------------------
bd.* 140，bs.* 284，lf.* 34，md.* 64
--------------------------------------------------------- 00 lf.sys_dict
update lf.sys_dict set type='integer' where field='gid' and type='double';
update lf.sys_dict set type='integer' where field='verid' and type='double';
update lf.sys_dict set type='integer' where field='createuser' and type='text';
update lf.sys_dict set type='integer' where field='updateuser' and type='text';
--------------------------------------------------------- 01 lf.sys_dep
-- alter sequence lf.sys_meta_id_seq restart with 1;
update lf.sys_operate set modular2='元数据管理' where modular2='源数据管理';
select modular1,modular2 from lf.sys_operate group by modular1,modular2;

delete from lf.sys_dep where id = 59;
update lf.sys_dep set bak = null where bak = ' ';
update lf.sys_dep set code = null;
update lf.sys_dep set code = '00' where pid = 0;
update lf.sys_dep set level = level + 1;
alter table lf.sys_dep alter column code type varchar(24);
select fn_set_tab_codes(1, 1);

select * from lf.sys_dep order by id;
select id,pid,name,code,level,order_num,bak from lf.sys_dep where code is not null order by code;
--------------------------------------------------------- 02 lf.sys_dir
alter table lf.sys_dir alter column code type varchar(200);
alter table lf.sys_dir rename column code to exts;
alter table lf.sys_dir alter column bak type varchar(100);
alter table lf.sys_dir rename column bak to checks;
alter table lf.sys_dir add column bak varchar(1024);

update lf.sys_dir set bak=checks where checks is not null and position('check' in checks)=0;
update lf.sys_dir set checks=null where position('check' in checks)=0;

alter table lf.sys_dir add column code varchar(24);
comment on column lf.sys_dir.exts is '扩展名';
comment on column lf.sys_dir.checks is '检查项';
select fn_set_tab_codes(0, 2);

select id,checks,replace(checks,chr(10),'') from lf.sys_dir where position(chr(10) in checks) > 0;
update lf.sys_dir set checks = replace(checks,chr(10),'') where position(chr(10) in checks) > 0; -- 替换回车键

select * from lf.sys_dir order by id;
select id,pid,name,code,level,order_num,checks,exts from lf.sys_dir where code is not null order by code;
--------------------------------------------------------- 03 lf.sys_user
alter table lf.sys_user add column depcode varchar(24);
comment on column lf.sys_user.depcode is '单位编码';

update lf.sys_user a set depcode = (select code from lf.sys_dep b where b.id = a.depid);

select * from lf.sys_user order by id;
select id,uname,depid,depcode from lf.sys_user order by id;
--------------------------------------------------------- 04 lf.sys_meta
alter table lf.sys_meta add column depcode varchar(24) default '00';
alter table lf.sys_meta add column dircode varchar(24) default '00';
comment on column lf.sys_meta.depcode is '目录编码';
comment on column lf.sys_meta.dircode is '单位编码';

update lf.sys_meta a set depcode = (select code from lf.sys_dep b where b.id = a.depid);
update lf.sys_meta a set dircode = (select code from lf.sys_dir b where b.id = a.dirid);

alter table lf.sys_meta drop column depid;
alter table lf.sys_meta drop column dirid;

-- drop index if exists lf.idx_sys_meta_depcode;
create index idx_sys_meta_depcode on lf.sys_meta (depcode);

-- drop index if exists lf.idx_lf_sys_meta_dircode;
create index idx_lf_sys_meta_dircode on lf.sys_meta (dircode);

select id,name,depcode,dircode,type from lf.sys_meta order by id;
--------------------------------------------------------- 05 lf.sys_flow
alter table lf.sys_flow alter column depid type varchar(24);
alter table lf.sys_flow rename column depid to depcode;

update lf.sys_flow a set depcode = (select code from lf.sys_dep b where b.id = cast(a.depcode as integer));

select * from lf.sys_flow;
--------------------------------------------------------- 06 lf.sys_apply
update lf.sys_apply set depids = '000204,000205,000206' where depids = '20,21,22';
update lf.sys_apply set depids = '000205,000206' where depids = '21,22';

select * from lf.sys_dep where id in (20,21,22);
select * from lf.sys_apply;
--------------------------------------------------------- 07 bd.dlg_agnp *
alter table bd.dlg_agnp alter column depid type varchar(24);
alter table bd.dlg_agnp alter column dirid type varchar(24);

update bd.dlg_agnp a set depid = (select code from lf.sys_dep b where b.id = cast(a.depid as integer));
update bd.dlg_agnp a set dirid = (select code from lf.sys_dir b where b.id = cast(a.dirid as integer));

select * from bd.dlg_agnp limit 20;
select depid from bd.dlg_agnp where depid is not null and depid not like '000307%' group by depid order by depid;
select dirid from bd.dlg_agnp group by dirid order by dirid; select id, name, code from lf.sys_dir where id in (3, 0, 163) order by code;
select depid from bd.dlg_agnp group by depid order by depid; select id, name, code from lf.sys_dep where id in (1, 20, 21, 22, 38, 55) order by code;
--------------------------------------------------------- 08 lf.sys_role
update lf.sys_role set is_admin = 2 where position('数据管理员' in descr) > 0;

select c.* from lf.sys_role a inner join lf.sys_role_user b on a.id = b.roleid
inner join lf.sys_user c on b.userid = c.id
where a.is_admin = 2 and c.depcode = '000204'
order by c.id limit 1;

select c.* from lf.sys_role a inner join lf.sys_role_user b on a.id = b.roleid
inner join lf.sys_user c on b.userid = c.id
where a.is_admin = 2 and a.depid = 20
order by c.id limit 1;

select * from lf.sys_role;
--------------------------------------------------------- 09 测试SQL
with rs as (select code from lf.sys_dir where name='西气东输四线天然气管道工程（吐鲁番-中卫）（00116DT02）')
select fn_rec_query(a.dirid,'dir'),a.* from lf.sys_meta a where dirid = ANY(fn_rec_array((select id from rs), 'dir'));

select a.*,fn_uname(a.create_user) uname,fn_get_fullname(a.depcode, 1) depName,fn_ver(a.verid) verName,fn_get_fullname(a.dircode, 2) dirName
from lf.sys_meta a where a.dircode like (select code from lf.sys_dir where name='西气东输四线天然气管道工程（吐鲁番-中卫）（00116DT02）') || '%';

-- 项目表
select a.*,st_astext(geom) from bs.bs_project a;

-- 按项目统计数据
select name "名称", (select count(*) from lf.sys_meta b where b.dircode like a.code || '%') "文件数",
	(select sum(sizes) from lf.sys_meta b where b.dircode like a.code || '%') "数量(MB)"
from lf.sys_dir a where id > 1 and pid = 0;

-- 按文件类型统计
select type,count(*),sum(sizes) from lf.sys_meta group by type;

-- type类型：1-Shp文件，2-专题图，3-元数据，4-业务数据，5-管道分析，6-统计报告
select * from lf.sys_download;
select * from lf.sys_serve_log

-- 按照专业统计
select code , name "value" from lf.sys_dir a
        where name in ('测量（ESV）', '勘察（EGE）', '地灾（EGD）', '洞库（EGD）') group by name order by name;
select * from lf.sys_meta
where dircode like '040000%' or dircode like '0100000000%' or dircode like '02000000%' or dircode like '030000%';

-- 数据申请
select * from lf.sys_apply a
inner join lf.sys_user b on a.userid = b.id
inner join lf.sys_dep c on b.depid = c.id;

-- 访问次数
select modular1,count(*) from lf.sys_operate group by modular1 order by modular1;

-- 按单位统计
select name,code,(select count(*) from lf.sys_meta b where b.depcode like a.code || '%'),
	(select count(sizes) from lf.sys_meta b where b.depcode like a.code || '%')
from lf.sys_dep a where name in ('测绘室','勘察室','地灾专业','洞库专业');

-- 服务统计
select * from lf.sys_serve_log;

-- 登录次数
select b.uname, count(*) from lf.sys_login a inner join lf.sys_user b on a.userid = b.id group by uname;

-- 按照大类统计
select name,
	(select count(b.id) from lf.sys_meta b where dircode similar to '(' || (select string_agg(code, '|') from lf.sys_dir c where c.name = a.name) || ')%') "count",
	(select coalesce(sum(b.sizes), 0) from lf.sys_meta b where dircode similar to '(' || (select string_agg(code, '|') from lf.sys_dir c where c.name = a.name) || ')%') "sizes"
from lf.sys_dir a
where name in ('基础测绘', '基础地灾', '基础勘察', '合规数据', '管理数据', '测量（ESV）', '勘察（EGE）', '地灾（EGD）', '洞库（EGD）')
group by name
order by name;

select name,
	(select count(b.id) from lf.sys_meta b where dircode similar to fn_dir_similar(a.name)) "count",
	(select coalesce(sum(b.sizes), 0) from lf.sys_meta b where dircode similar to fn_dir_similar(a.name)) "sizes"
from lf.sys_dir a
where name in ('基础测绘', '基础地灾', '基础勘察', '合规数据', '管理数据', '测量（ESV）', '勘察（EGE）', '地灾（EGD）', '洞库（EGD）')
group by name
order by name;
-- select * from lf.sys_meta where dircode similar to fn_dir_similar('基础测绘');
--------------------------------------------------------- 
select (select string_agg(code, ',') from lf.sys_dir where name = a.name) "key", name "value"
from lf.sys_dir a
where name in ('基础地灾', '基础勘察', '合规数据', '管理数据', '测量（ESV）', '勘察（EGE）', '地灾（EGD）', '洞库（EGD）')
group by name
order by key;

select * from lf.sys_meta order by id desc;
select * from lf.sys_download order by id desc;
select * from lf.sys_downlog order by id desc;
select * from lf.sys_meta_down order by id desc;
select downid,count(*),max(create_time) from lf.sys_downlog group by downid having count(*)>1;
--delete from lf.sys_download where id=324;
--delete from lf.sys_meta_down where downid=324;
select * from lf.sys_apply

select * from lf.sys_user order by id desc
select * from lf.sys_dep where id=36











select a.*, fn_tab_count(a.ns, a.tab, '1=1') "rows" from
(select distinct ns,tab,fn_get_entity(tab) entity,tab_desc,tableType from lf.sys_dict
order by tab
limit 10 offset 0) a;

select dirid,depid from bd.dlg_25w_resa group by dirid,depid;
select * from lf.sys_dict where tab='dlg_25w_resa';
--------------------------------------------------------- A0 修改表结构
-- drop function fn_alter_tab_fields();
create or replace function fn_alter_tab_fields()
returns integer as $$
  	declare
		rec record;
		tn  varchar;
		cc  integer := 0;
  	begin
		for rec in (select ns, tab from lf.sys_dict group by ns, tab order by ns, tab)
		loop
			raise notice 'ns = %, tab = %.', rec.ns, rec.tab;
			
			tn := rec.tab;
			if length(tn) > 32 then
				tn := replace(tn, 'standard_frozen_depth_', 's_f_d_');
				tn := replace(tn, 'general_report_on_', 'g_r_o_');
				tn := replace(tn, 'prevention_and_control_', 'p_a_c_');
				tn := replace(tn, 'water_curtain_roadway_', 'w_c_r_');
			end if;
			
			execute format('alter table %s.%s alter column %s type varchar(24)', rec.ns, rec.tab, 'depid');
			execute format('alter table %s.%s alter column %s type varchar(24)', rec.ns, rec.tab, 'dirid');
			
			execute format('update %s.%s a set depid = (select code from lf.sys_dep b where b.id = cast(a.depid as integer))', rec.ns, rec.tab);
			execute format('update %s.%s a set dirid = (select code from lf.sys_dir b where b.id = cast(a.dirid as integer))', rec.ns, rec.tab);
			
			--execute format('drop index if exists %s.idx_%s_depid', rec.ns, rec.tab);
            execute format('create index idx_%s_depid on %s.%s (depid)', tn, rec.ns, rec.tab);
			
			--execute format('drop index if exists %s.idx_%s_dirid', rec.ns, rec.tab);
            execute format('create index idx_%s_dirid on %s.%s (dirid)', tn, rec.ns, rec.tab);
			
			cc := cc + 1;
		end loop;
	
		return cc;
  	end;
$$ language plpgsql;

select fn_alter_tab_fields();

select gid,depid,dirid from bd.b_borehole limit 10;
select depid,dirid from bd.dlg_agnp group by depid,dirid;
select depid,dirid from bd.dlg_25w_resa group by depid,dirid;
select depid,dirid from bd.dlg_25w_boul group by depid,dirid;
select * from lf.sys_dict where tab='dlg_25w_boul';
--------------------------------------------------------- 
