ningshuxia
2022-12-12 0e9f816989a07641b336faa11dfa7341221f9c2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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
{
    /// <summary>
    /// 测试报表
    /// </summary>
    [Route("OpenApi/Test/Report")]
    [ApiDescriptionSettings("OpenApi", Name = "测试报表", Order = 1001)]
    public class Report_ShysController : IDynamicApiController
    {
 
        public readonly static Dictionary<string, string> ProvinceCodeDic = new Dictionary<string, string> { { "四川省", "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" } };
 
        /// <summary>
        /// 用户日志分析报表
        /// </summary> 
        [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<LoginLogItem>();
            dto.LoginStatisticsList = new List<LoginStatistics>();
            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<LoginLogItem>();
                    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<UserLoginLogReportDto>.SaveObjectXmlFile(filePath, dto);
                LogHelper.Info("保存文件!");
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex.Message);
            }
 
            return dto;
        }
 
 
        /// <summary>
        /// 获取Dictionary中不重复的随机Dictionary
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="dict">原dictionary</param>
        /// <param name="count">返回随机个数(如果dict总个数少于count 则返回dict总个数)</param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> RandomValues<TKey, TValue>(Dictionary<TKey, TValue> dict, int count)
        { 
            Random rand = new Random();
            Dictionary<TKey, TValue> dic = new Dictionary<TKey, TValue>();
            int size = dict.Count;
            count = count > size ? size : count;
            List<TKey> 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;
        }
 
 
 
 
    }
}