Merge pull request '优化行政区划同步功能' (#193) from koy07555/Admin.NET.Pro:优化行政区划同步功能 into main
Reviewed-on: http://101.43.53.74:3000/Admin.NET/Admin.NET.Pro/pulls/193
This commit is contained in:
commit
65f46bb755
@ -1,4 +1,4 @@
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
||||
//
|
||||
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
||||
//
|
||||
@ -282,56 +282,91 @@ public class SysRegionService : IDynamicApiController, ITransient
|
||||
[DisplayName("同步行政区划数据(国家地名信息库)")]
|
||||
public async Task<int> SyncRegionMca(long code)
|
||||
{
|
||||
var url = $"https://dmfw.mca.gov.cn/9095/xzqh/getList?code={code}&maxLevel=2";
|
||||
var url = $"https://dmfw.mca.gov.cn/9095/xzqh/getList?code={code}&maxLevel=4";
|
||||
|
||||
var httpClient = new HttpClient();
|
||||
var res = await httpClient.GetAsync<SysRegion>(url);
|
||||
if (res == null) return 0;
|
||||
var regionLevel0 = await httpClient.GetAsync<SysRegion>(url);
|
||||
if (regionLevel0 == null) return 0;
|
||||
|
||||
var areaList = new List<SysRegion>()
|
||||
var areaList = new List<SysRegion>();
|
||||
if (regionLevel0.Code != "00" && regionLevel0.Level > 0 && !string.IsNullOrEmpty(regionLevel0.Name))
|
||||
{
|
||||
new SysRegion
|
||||
areaList.Add(new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(res.Code),
|
||||
Id = Convert.ToInt64(regionLevel0.Code),
|
||||
Pid = 0,
|
||||
Code = res.Code,
|
||||
Name = res.Name,
|
||||
Type = res.Type,
|
||||
Level = res.Level,
|
||||
}
|
||||
};
|
||||
|
||||
foreach (var item in res.Children)
|
||||
{
|
||||
var region = new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(item.Code),
|
||||
Pid = Convert.ToInt64(res.Code),
|
||||
Code = item.Code,
|
||||
Name = item.Name,
|
||||
Type = item.Type,
|
||||
Level = item.Level,
|
||||
};
|
||||
areaList.Add(region);
|
||||
|
||||
foreach (var child in item.Children)
|
||||
{
|
||||
areaList.Add(new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(child.Code),
|
||||
Pid = region.Id,
|
||||
Code = child.Code,
|
||||
Name = child.Name,
|
||||
Type = child.Type,
|
||||
Level = child.Level,
|
||||
});
|
||||
}
|
||||
Code = regionLevel0.Code,
|
||||
Name = regionLevel0.Name,
|
||||
Type = regionLevel0.Type,
|
||||
Level = regionLevel0.Level,
|
||||
});
|
||||
}
|
||||
|
||||
// 如果存在指定行政区划则删除
|
||||
if (await _sysRegionRep.IsAnyAsync(u => u.Id == code))
|
||||
foreach (var regionLevel1 in regionLevel0.Children)
|
||||
{
|
||||
var region1 = new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(regionLevel1.Code),
|
||||
Pid = Convert.ToInt64(regionLevel0.Code),
|
||||
Code = regionLevel1.Code,
|
||||
Name = regionLevel1.Name,
|
||||
Type = regionLevel1.Type,
|
||||
Level = regionLevel1.Level,
|
||||
};
|
||||
if (areaList.Any(u => u.Id == region1.Id))
|
||||
Console.WriteLine($"1 级:{region1.Id} - {region1.Name} 已存在");
|
||||
else
|
||||
areaList.Add(region1);
|
||||
foreach (var regionLevel2 in regionLevel1.Children)
|
||||
{
|
||||
var region2 = new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(regionLevel2.Code),
|
||||
Pid = Convert.ToInt64(regionLevel1.Code),
|
||||
Code = regionLevel2.Code,
|
||||
Name = regionLevel2.Name,
|
||||
Type = regionLevel2.Type,
|
||||
Level = regionLevel2.Level,
|
||||
};
|
||||
if (areaList.Any(u => u.Id == region2.Id))
|
||||
Console.WriteLine($"2 级:{region2.Id} - {region2.Name} 已存在");
|
||||
else
|
||||
areaList.Add(region2);
|
||||
foreach (var regionLevel3 in regionLevel2.Children)
|
||||
{
|
||||
var region3 = new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(regionLevel3.Code),
|
||||
Pid = Convert.ToInt64(regionLevel2.Code),
|
||||
Code = regionLevel3.Code,
|
||||
Name = regionLevel3.Name,
|
||||
Type = regionLevel3.Type,
|
||||
Level = regionLevel3.Level,
|
||||
};
|
||||
if (areaList.Any(u => u.Id == region3.Id))
|
||||
Console.WriteLine($"3 级:{region3.Id} - {region3.Name}");
|
||||
else
|
||||
areaList.Add(region3);
|
||||
foreach (var regionLevel4 in regionLevel3.Children)
|
||||
{
|
||||
var region4 = new SysRegion
|
||||
{
|
||||
Id = Convert.ToInt64(regionLevel4.Code),
|
||||
Pid = Convert.ToInt64(regionLevel3.Code),
|
||||
Code = regionLevel4.Code,
|
||||
Name = regionLevel4.Name,
|
||||
Type = regionLevel4.Type,
|
||||
Level = regionLevel4.Level,
|
||||
};
|
||||
areaList.Add(region4);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (code==0)
|
||||
await _sysRegionRep.AsDeleteable().ExecuteCommandAsync();
|
||||
else if (await _sysRegionRep.IsAnyAsync(u => u.Id == code)) // 如果存在指定行政区划则删除
|
||||
await DeleteRegion(new DeleteRegionInput { Id = code });
|
||||
|
||||
return await _sysRegionRep.AsInsertable(areaList).ExecuteCommandAsync();
|
||||
}
|
||||
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" label-position="top">
|
||||
<el-row :gutter="10">
|
||||
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
|
||||
<el-form-item label="指定区划代码" prop="code" :rules="[{ required: true, message: '区划代码不能为空', trigger: 'blur' }]">
|
||||
<el-form-item label="指定区划代码 (0或不输入,导入全部数据)" prop="code" >
|
||||
<el-input v-model="state.ruleForm.code" placeholder="指定区划代码" clearable />
|
||||
</el-form-item>
|
||||
</el-col>
|
||||
@ -18,7 +18,11 @@
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<span class="dialog-footer">
|
||||
<a href="https://dmfw.mca.gov.cn/interface.html" target="_blank" style="float: left">中国·国家地名信息库</a>
|
||||
<div>
|
||||
<a href="https://dmfw.mca.gov.cn/9095/xzqh/getList?code=&maxLevel=4" target="_blank" style="float: left">国家地名信息库数据</a>
|
||||
|
||||
<a href="https://dmfw.mca.gov.cn/interface.html" target="_blank" style="float: left; margin-left: 15px">接口说明</a>
|
||||
</div>
|
||||
<el-button @click="cancel">取 消</el-button>
|
||||
<el-button type="primary" :loading="state.loading" @click="submit">确 定</el-button>
|
||||
</span>
|
||||
@ -75,10 +79,17 @@ const submit = () => {
|
||||
position: 'bottom-right',
|
||||
});
|
||||
state.loading = true;
|
||||
await getAPI(SysRegionApi).apiSysRegionSyncRegionMcaCodePost(state.ruleForm.code);
|
||||
closeDialog();
|
||||
|
||||
ElMessage.success('生成成功');
|
||||
try {
|
||||
if (state.ruleForm.code == '') {
|
||||
await getAPI(SysRegionApi).apiSysRegionSyncRegionMcaCodePost(0);
|
||||
} else {
|
||||
await getAPI(SysRegionApi).apiSysRegionSyncRegionMcaCodePost(state.ruleForm.code);
|
||||
}
|
||||
closeDialog();
|
||||
ElMessage.success('生成成功');
|
||||
} finally {
|
||||
state.loading = false;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user