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;
using Furion.FriendlyException;
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.LoginStatisticsList = new List();
dto.TotalUsers = users.Count;
dto.LogSummaryList = logs.GroupBy(x => x.LoginTime.Date).Select(x => new LogSummary() { DateTime = x.Key, Visitors = x.Count() }).ToList();
var uniqueVisitorList = logs.DistinctBy(x => x.UserID).ToList();
var today = DateTime.Today;
var last7Days = today.AddDays(-7);
var last30Days = today.AddDays(-30);
if (uniqueVisitorList != null && uniqueVisitorList.Count > 0)
{
dto.TodayUV = uniqueVisitorList.Where(x => x.LoginTime >= today).Count();
dto.Last7DaysUV = uniqueVisitorList.Where(x => x.LoginTime >= last7Days).Count();
dto.Last30DaysUV = uniqueVisitorList.Where(x => x.LoginTime >= last30Days).Count();
}
try
{
LogHelper.Info("生成列表!");
var logGroups = logs.GroupBy(x => x.CorpID);
foreach (var logGroup in logGroups)
{
var corp = corps.Find(x => x.ID == logGroup.Key);
var corpName = corp?.ShortName;
var list = new List();
foreach (var log in logGroup)
{
var area = RandomValues(ProvinceCodeDic, 1).First().Key;
var softName = string.Empty;
switch (log.SoftType)
{
case IStation.SoftType.CS_客户端:
softName = "客户端";
break;
case IStation.SoftType.BS_网页端:
softName = "网页端";
break;
case IStation.SoftType.Wechat_微信小程序:
softName = "小程序";
break;
case IStation.SoftType.App_移动端:
softName = "APP";
break;
default:
softName = "未知";
break;
}
var item = new LoginLogItem(log, corpName, area, softName);
list.Add(item);
}
var statistics = list.GroupBy(x => x.SoftName).Select(x => new LoginStatistics() { CorpName = corpName, Software = x.Key, Visitors = x.Count() });
dto.LoginStatisticsList.AddRange(statistics);
dto.LoginLogList.AddRange(list);
}
LogHelper.Info("生成文件!");
var ReportPath = Path.Combine(Settings.File.FileStorageFolder, @"UserLoginLogReport");
if (!Directory.Exists(ReportPath))
Directory.CreateDirectory(ReportPath);
var filePath = Path.Combine(ReportPath, "UserLoginLogReport" + ".xml");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
XmlHelper.SaveObjectXmlFile(filePath, dto);
LogHelper.Info("保存文件!");
}
catch (Exception ex)
{
LogHelper.Error(ex.Message);
}
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;
}
}
}