using Microsoft.AspNetCore.Mvc; using System.Net; using System.Net.Http.Headers; using Microsoft.Extensions.Hosting.Internal; using Microsoft.AspNetCore.Http.Extensions; using IStation.Untity; using Furion.DynamicApiController; using System.ComponentModel.DataAnnotations; using Mapster; using Microsoft.AspNetCore.Authorization; namespace IStation.Application { /// /// 测试报表 /// [Route("OpenApi/Test/Report")] [ApiDescriptionSettings("OpenApi", Name = "测试报表", Order = 1001)] public class Report_ShysController : IDynamicApiController { public readonly static Dictionary ProvinceCodeDic = new Dictionary { { "四川省", "510000" }, { "北京市", "110000" }, { "天津市", "120000" }, { "河北省", "130000" }, { "山西省", "140000" }, { "内蒙古自治区", "150000" }, { "辽宁省", "210000" }, { "吉林省", "220000" }, { "黑龙江省", "230000" }, { "上海市", "310000" }, { "江苏省", "320000" }, { "浙江省", "330000" }, { "安徽省", "340000" }, { "福建省", "350000" }, { "江西省", "360000" }, { "山东省", "370000" }, { "河南省", "410000" }, { "湖北省", "420000" }, { "湖南省", "430000" }, { "广东省", "440000" }, { "广西自治区", "450000" }, { "海南省", "460000" }, { "重庆市", "500000" }, { "贵州省", "520000" }, { "云南省", "530000" }, { "西藏自治区", "540000" }, { "陕西省", "610000" }, { "甘肃省", "620000" }, { "青海省", "630000" }, { "宁夏自治区", "640000" }, { "新疆自治区", "650000" }, { "台湾省", "710000" }, { "香港特别行政区", "810000" }, { "澳门特别行政区", "820000" } }; /// /// 用户日志分析报表 /// [AllowAnonymous] [NonUnify] [Route("UserLoginLogReport")] [HttpPost] public UserLoginLogReportDto UserLoginLogReport(DateTime StartDate) { var corps = new Service.Corpration().GetAll(); if (corps == null || corps.Count < 1) return default; var users = new Service.User().GetAll(); if (users == null || users.Count < 1) return default; var logs = new Service.UserLoginLog().GetListByStartDate(StartDate); if (logs == null || logs.Count < 1) return default; var dto = new UserLoginLogReportDto(); dto.LoginLogList = new List(); dto.TotalUsers = users.Count; var uniqueVisitorList = logs.DistinctBy(x => x.UserID).ToList(); var last7Days = DateTime.Now.AddDays(-7); var last30Days = DateTime.Now.AddDays(-30); dto.TodayUV = uniqueVisitorList.Where(x => x.LoginTime >= DateTime.Today).Count(); dto.Last7DaysUV = uniqueVisitorList.Where(x => x.LoginTime >= last7Days).Count(); dto.Last30DaysUV = uniqueVisitorList.Where(x => x.LoginTime >= last30Days).Count(); var logGroups = logs.GroupBy(x => x.CorpID); foreach (var logGroup in logGroups) { var corp = corps.Find(x => x.ID == logGroup.Key); foreach (var log in logGroup) { var area = RandomValues(ProvinceCodeDic, 1).First().Key; var item = new UserLoginLogItem(log, corp.ShortName, area); dto.LoginLogList.Add(item); } } var ReportPath = Path.Combine(Settings.DataFile.SaveFileUrl, @"UserloginLogReport"); if (!Directory.Exists(ReportPath)) Directory.CreateDirectory(ReportPath); var yyMM = DateTime.Today.ToString("yyyy-MM"); var filePath = Path.Combine(ReportPath, yyMM + ".xml"); filePath = filePath.Replace(@"\\", @"/"); XmlHelper.SaveObjectXmlFile(filePath, dto); return dto; } /// /// 获取Dictionary中不重复的随机Dictionary /// /// /// /// 原dictionary /// 返回随机个数(如果dict总个数少于count 则返回dict总个数) /// public static Dictionary RandomValues(Dictionary dict, int count) { Random rand = new Random(); Dictionary dic = new Dictionary(); int size = dict.Count; count = count > size ? size : count; List values = Enumerable.ToList(dict.Keys); while (dic.Count < count) { TKey tk = values[rand.Next(size)]; if (!dic.Keys.Contains(tk)) { dic[tk] = dict[tk]; } } return dic; } } }