123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- using WingCloudServer.IpOfflineTools.Model;
- namespace WingCloudServer.IpOfflineTools.Service
- {
- /// <summary>
- /// 数据处理
- /// </summary>
- public class IPServiceGuide
- {
- /// <summary>
- /// 文件读取
- /// </summary>
- /// <param name="sourceFilePath">源文件地址</param>
- /// <returns>源数据列表</returns>
- public List<IPBasicInfo> ReadFile(string sourceFilePath)
- {
- var list = new List<IPBasicInfo>();
- if (string.IsNullOrEmpty(sourceFilePath))
- {
- return list;
- }
- string[] strArray = File.ReadAllLines(sourceFilePath);
- for (int i = 0; i < strArray.Length; i++)
- {
- var tempList = strArray[i].Split(' ').Where(c => !string.IsNullOrEmpty(c)).ToList();
- if (tempList?.Count > 0)
- {
- var model = new IPBasicInfo()
- {
- BeginIp = tempList[0],
- EndIp = tempList[1],
- CountryName = tempList[2]
- };
- list.Add(model);
- }
- }
- return list;
- }
- /// <summary>
- /// 保存读取
- /// </summary>
- /// <param name="countryList">源数据</param>
- /// <param name="savePath">保存文件路径</param>
- /// <param name="fileName">保存文件名</param>
- /// <param name="pCount">每个文件最多保存条数,如果为0,则保存为一个文件</param>
- /// <returns>源数据列表</returns>
- public bool WriteFile(List<CountryEntity> countryList, string savePath, string fileName, int pCount = 0)
- {
- bool result = false;
- var resultList = new List<IpEntity>();
- foreach(var item in countryList)
- {
- resultList.AddRange(item.IpList);
- }
- if (resultList != null && resultList.Count > 0)
- {
- if (!Directory.Exists(savePath))
- {
- Directory.CreateDirectory(savePath);
- }
- if (pCount > 0)
- {
- var factName = Path.GetFileNameWithoutExtension(fileName);
- var factNameExtension = Path.GetExtension(fileName);
- var resultLength = resultList.Count;
- int fileCount = (resultList.Count / pCount) + ((resultList.Count % pCount) > 0 ? 1: 0);
- for (var i = 0; i < fileCount; i++)
- {
- int index = i * pCount;
- int length = pCount;
- if (resultLength < (index + pCount))
- {
- length = resultLength - index;
- }
- var tempList = resultList.GetRange(index, length);
- var json = Newtonsoft.Json.JsonConvert.SerializeObject(tempList);
- if (!string.IsNullOrWhiteSpace(json))
- {
- var tempSavePath = savePath + factName + "_" + i + factNameExtension;
- if (File.Exists(tempSavePath))
- {
- File.Delete(tempSavePath);
- }
- byte[] fileData = System.Text.Encoding.UTF8.GetBytes(json);
- //存储文件
- using (var fw = File.Open(tempSavePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
- {
- fw.Write(fileData, 0, fileData.Length);
- }
- }
- }
- result = true;
- }
- else
- {
- var newSavePath = savePath + fileName;
- if (File.Exists(newSavePath))
- {
- File.Delete(newSavePath);
- }
- var json = Newtonsoft.Json.JsonConvert.SerializeObject(resultList);
- if (!string.IsNullOrWhiteSpace(json))
- {
- byte[] fileData = System.Text.Encoding.UTF8.GetBytes(json);
- //存储文件
- using (var fw = File.Open(newSavePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
- {
- fw.Write(fileData, 0, fileData.Length);
- }
- result = true;
- }
- }
- }
- return result;
- }
- /// <summary>
- /// 从源数据中读取国家信息
- /// </summary>
- /// <param name="list">源数据</param>
- /// <returns>新的国家数据</returns>
- public List<CountryEntity> GetCountryFromIpList(List<IPBasicInfo> list)
- {
- var waitList = new List<CountryEntity>();
- var countryEntityList = new List<CountryEntity>();
- if (list?.Count > 0)
- {
- //获取大陆省份和直辖市
- var provinceList = CommonHelper.GetProvinceList();
- var cityDic = CommonHelper.GetCityDictionary();
- //获取所有的国家和地区
- var countryList = list.Select(c => c.CountryName).Distinct().ToList();
- if (countryList?.Count > 0)
- {
- //获取所有的省份和国家,区分大陆省份,港澳台,其他国家
- var valList = new List<string>();
- foreach (var item in provinceList)
- {
- var tempList = countryList.FindAll(c => c.Contains(item));
- valList.AddRange(tempList);
- var cityList = cityDic[item];
- if (cityList?.Count > 0)
- {
- foreach (var cityItem in cityList)
- {
- if (!waitList.Exists(c => c.CountryName == "中国" && c.ProvinceName == item && c.CityName == cityItem))
- {
- var ipTempEntity = list.Find(d => d.CountryName.Contains(item) && d.CountryName.Contains(cityItem));
- if (ipTempEntity == null)
- {
- continue;
- }
- var strCityItem = cityItem;
- if (item == "北京" || item == "天津" || item == "上海" || item == "重庆")
- {
- strCityItem = item;
- }
- var ce = new CountryEntity()
- {
- CountryName = "中国",
- ProvinceName = item,
- CityName = strCityItem,
- DefaultIp = ipTempEntity.EndIp
- };
- waitList.Add(ce);
- }
- }
- }
- }//大陆省份数据
- var otherProviceList = new List<string>()
- {
- "香港",
- "澳门",
- "台湾"
- };
- var otherValList = new List<string>();
- foreach (var item in otherProviceList)
- {
- var tempList = countryList.FindAll(c => c.Contains(item));
- otherValList.AddRange(tempList);
- if (!waitList.Exists(c => c.CountryName == "中国香港" && c.ProvinceName == item))
- {
- var ipTempEntity = list.Find(d => d.CountryName.Contains(item));
- if (ipTempEntity == null)
- {
- continue;
- }
- var ce = new CountryEntity()
- {
- CountryName = "中国香港",
- ProvinceName = item,
- CityName = "",
- DefaultIp = ipTempEntity.EndIp,
- };
- waitList.Add(ce);
- }
- }//港澳台数据
- var otherCountryList = countryList.FindAll(c => !valList.Contains(c) && !otherValList.Contains(c));//其他国家的数据
- if (otherCountryList?.Count > 0)
- {
- foreach (var item in otherCountryList)
- {
- if (!waitList.Exists(c => c.CountryName == "境外" && c.ProvinceName == item))
- {
- var ipTempEntity = list.Find(d => d.CountryName.Contains(item));
- if (ipTempEntity == null)
- {
- continue;
- }
- var ce = new CountryEntity()
- {
- CountryName = "境外",
- ProvinceName = item,
- CityName = "",
- DefaultIp = ipTempEntity.EndIp,
- };
- waitList.Add(ce);
- }
- }
- }
- //封装数据,方便后续处理
- if (valList?.Count > 0)
- {
- var countryEntity = new CountryEntity()
- {
- CountryName = "中国",
- ChildList = valList
- };
- countryEntityList.Add(countryEntity);
- }
- if (otherValList?.Count > 0)
- {
- var countryEntity = new CountryEntity()
- {
- CountryName = "香港",
- ChildList = otherValList
- };
- countryEntityList.Add(countryEntity);
- }
- if (otherCountryList?.Count > 0)
- {
- var countryEntity = new CountryEntity()
- {
- CountryName = "境外",
- ChildList = otherCountryList
- };
- countryEntityList.Add(countryEntity);
- }
- }
- }
- else
- {
- return new List<CountryEntity>();
- }
- if (waitList?.Count > 0)
- {
- foreach (var waitItem in waitList)
- {
- var tempResult = CommonHelper.GetIpLocationInfo(waitItem.DefaultIp);
- waitItem.Lat = tempResult?.location?.lat ?? 0;
- waitItem.Lng = tempResult?.location?.lng ?? 0;
- }
- }
- else
- {
- waitList = new List<CountryEntity>();
- }
- //计算真实的定位信息
- var newCountryInfo = CaculateInfo(countryEntityList, list, waitList);
- return newCountryInfo;
- }
- /// <summary>
- /// ip信息计算
- /// </summary>
- /// <param name="list">源数据</param>
- /// <param name="ipList">ip列表</param>
- /// <param name="waitList">待处理数据列表</param>
- /// <returns>新的国家数据</returns>
- private List<CountryEntity> CaculateInfo(List<CountryEntity> oriList, List<IPBasicInfo> ipList, List<CountryEntity> waitList)
- {
- var resultList = new List<CountryEntity>();
- //先计算中国
- var chinaEntity = new CountryEntity()
- {
- CountryName = "中国大陆"
- };
- var bjTempList = oriList?.Find(c => c.CountryName == "中国")?.ChildList ?? new List<string>();
- foreach (var item in bjTempList)
- {
- var waitInfo = waitList.Find(c => c.CountryName == "中国" && item.Contains(c.ProvinceName) && item.Contains(c.CityName)) ?? new CountryEntity();
- var ipTempList = ipList.FindAll(d => d.CountryName == item);
- if (ipTempList?.Count > 0)
- {
- foreach (var ipTempEntity in ipTempList)
- {
- IpEntity ipInfo = new IpEntity()
- {
- LongStartIP = CommonHelper.IpToLong(ipTempEntity.BeginIp),
- LongEndIP = CommonHelper.IpToLong(ipTempEntity.EndIp),
- Lat = waitInfo.Lat,
- Lng = waitInfo.Lng,
- Country = "CN",
- Province = waitInfo.ProvinceName,
- City = waitInfo.CityName,
- Districts = "",
- IsChinaMainland = true,
- };
- chinaEntity.IpList.Add(ipInfo);
- }
- }
- }
- resultList.Add(chinaEntity);
- //在计算香港
- var hkEntity = new CountryEntity()
- {
- CountryName = "中国香港"
- };
- var hkTempList = oriList?.Find(c => c.CountryName == "香港")?.ChildList ?? new List<string>();
- foreach (var item in hkTempList)
- {
- var waitInfo = waitList.Find(c => c.CountryName == "中国香港" && c.ProvinceName == item) ?? new CountryEntity();
- var ipTempList = ipList.FindAll(d => d.CountryName == item);
- if (ipTempList?.Count > 0)
- {
- foreach (var ipTempEntity in ipTempList)
- {
- IpEntity ipInfo = new IpEntity()
- {
- LongStartIP = CommonHelper.IpToLong(ipTempEntity.BeginIp),
- LongEndIP = CommonHelper.IpToLong(ipTempEntity.EndIp),
- Lat = waitInfo.Lat,
- Lng = waitInfo.Lng,
- Country = "HK",
- Province = waitInfo.ProvinceName,
- City = "",
- Districts = "",
- IsChinaMainland = false,
- };
- hkEntity.IpList.Add(ipInfo);
- }
- }
- }
- resultList.Add(hkEntity);
- //在计算其他,这里就要将经纬度计算,更近的归属于相应的节点
- var otherTempList = oriList?.Find(c => c.CountryName != "香港" && c.CountryName != "中国")?.ChildList ?? new List<string>();
- var otherEntity = new CountryEntity()
- {
- CountryName = "境外"
- };
- foreach (var item in otherTempList)
- {
- var waitInfo = waitList.Find(c => c.CountryName == "境外" && c.ProvinceName == item) ?? new CountryEntity();
- var ipTempList = ipList.FindAll(d => d.CountryName == item);
- if (ipTempList?.Count > 0)
- {
- foreach (var ipTempEntity in ipTempList)
- {
- IpEntity ipInfo = new IpEntity()
- {
- LongStartIP = CommonHelper.IpToLong(ipTempEntity.BeginIp),
- LongEndIP = CommonHelper.IpToLong(ipTempEntity.EndIp),
- Lat = waitInfo.Lat,
- Lng = waitInfo.Lng,
- Country = "JW",
- Province = waitInfo.ProvinceName,
- City = "",
- Districts = "",
- IsChinaMainland = false,
- };
- otherEntity.IpList.Add(ipInfo);
- }
- }
- }
- resultList.Add(otherEntity);
- return resultList;
- }
- }
- }
|