VisitLogDal.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using Cksoft.Data;
  2. using DllUfpEntity;
  3. using DllUfpEntity.Dto;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Data;
  7. using System.Linq;
  8. using DllUfpDal.Extension;
  9. using DllEapEntity.Dtos;
  10. using System.Threading.Tasks;
  11. using Cksoft.Data.Repository;
  12. namespace DllUfpDal
  13. {
  14. public class VisitLogDal
  15. {
  16. public IDatabase CurrDb { get; set; }
  17. public VisitLogDal(IDatabase db)
  18. {
  19. CurrDb = db;
  20. }
  21. public int Add(VisitLog log)
  22. {
  23. return CurrDb.Insert(log);
  24. }
  25. /// <summary>
  26. /// 新增日志
  27. /// </summary>
  28. /// <param name="logDto"></param>
  29. public int Log(VisitLogDto logDto)
  30. {
  31. string errorinfo = string.Empty;
  32. //var staff = CurrDb.FindListForCondition<Staff>($" and a.FCode='{logDto.UserCode}'",
  33. // ref errorinfo).FirstOrDefault();
  34. //if (staff == null || staff.IsSA == 1)
  35. // return -1;
  36. var sql = $"select id from function where url='{logDto.Url}' limit 0,1";
  37. var funcId = Convert.ToInt32(CurrDb.FindObject(sql) ?? "-1");
  38. if (funcId > 0)
  39. {
  40. var log = new VisitLog
  41. {
  42. FunctionId = funcId,
  43. UserCode = logDto.UserCode,
  44. VisitTime = DateTime.Now
  45. };
  46. return Add(log);
  47. }
  48. return -1;
  49. }
  50. #region 页面数据展示
  51. /// <summary>
  52. /// 按日期获取报表总的访问次数
  53. /// </summary>
  54. /// <param name="startTime">开始时间</param>
  55. /// <param name="endTime">结束时间</param>
  56. /// <returns></returns>
  57. public IEnumerable<VisitLogAnalysisDto> GetVisitLogByDate(DateTime startTime, DateTime endTime)
  58. {
  59. endTime = endTime.AddDays(1);
  60. var days = Math.Ceiling((endTime - startTime).TotalDays);
  61. var sql = $@"select date_format(visittime,'%Y-%m-%d') as date,count(1) as count from visitlog a RIGHT JOIN `function` fun on a.FunctionId=fun.ID
  62. where a.visitTime>='{startTime.ToString("yyyy-MM-dd")}' and a.visitTime<='{endTime.ToString("yyyy-MM-dd")}'
  63. group by date_format(visittime, '%Y-%m-%d')";
  64. var dtos = CurrDb.FindList<VisitLogAnalysisDto>(sql);
  65. var list = new List<VisitLogAnalysisDto>();
  66. for (int i = 0; i < days; i++)
  67. {
  68. var currDate = Convert.ToDateTime(startTime.AddDays(i).ToString("yyyy-MM-dd"));
  69. var temp = new VisitLogAnalysisDto
  70. {
  71. Date = currDate,
  72. Count = dtos.FirstOrDefault(c => c.Date == currDate)?.Count ?? 0
  73. };
  74. list.Add(temp);
  75. }
  76. return list;
  77. }
  78. /// <summary>
  79. /// 将List转换为DataTable
  80. /// </summary>
  81. /// <param name="startTime"></param>
  82. /// <param name="endTime"></param>
  83. /// <returns></returns>
  84. public DataTable GetVisitLogTable(DateTime startTime, DateTime endTime)
  85. {
  86. var list = GetVisitLogByDate(startTime, endTime);
  87. return list.TransferToTable();
  88. }
  89. /// <summary>
  90. /// 分页获取人员访问报表次数
  91. /// </summary>
  92. /// <param name="filter">主表过滤条件</param>
  93. /// <param name="subFilter">连接表过滤条件</param>
  94. /// <param name="start">开始记录数</param>
  95. /// <param name="length">返回长度</param>
  96. /// <param name="total">记录总数</param>
  97. /// <returns></returns>
  98. public IEnumerable<VisitLogAnalysisDto> GetStaffVisitLog(string filter, string subFilter, int start, int length,
  99. out int total, string sort = "staffCode", string order = "asc")
  100. {
  101. var countSql = $@"select count(1) from
  102. (select usercode,count(1) as count from visitlog a RIGHT JOIN `function` fun on a.FunctionId=fun.ID
  103. where 1=1 {filter} group by usercode) t
  104. left join staff b on t.usercode=b.fcode
  105. where 1=1 and b.FCode is not null {subFilter}";
  106. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "0");
  107. var sql = $@"select b.FCode as StaffCode,b.FName as StaffName,t.count from
  108. (select usercode,count(1) as count from visitlog a RIGHT JOIN `function` fun on a.FunctionId=fun.ID
  109. where 1=1 {filter} group by usercode) t
  110. left join staff b on t.usercode=b.fcode
  111. where 1=1 and b.FCode is not null {subFilter} order by {sort} {order} limit {start - 1},{length}";
  112. // 获取员工职级信息
  113. var datas = CurrDb.FindList<VisitLogAnalysisDto>(sql);
  114. if (datas != null && datas.Count() > 0)
  115. {
  116. var staffCodes = datas.Select(c => c.StaffCode).Distinct();
  117. var empInfos = this.GetEmployeeInfos(staffCodes);
  118. Parallel.ForEach(datas, item =>
  119. {
  120. item.Level = empInfos.FirstOrDefault(c => c.StaffCode == item.StaffCode)?.FLevel ?? "--";
  121. item.Duty = empInfos.FirstOrDefault(c => c.StaffCode == item.StaffCode)?.Duty ?? "--";
  122. item.Department = empInfos.FirstOrDefault(c => c.StaffCode == item.StaffCode)?.Dept ?? "--";
  123. });
  124. }
  125. return datas;
  126. }
  127. /// <summary>
  128. /// 获取人员访问报表总次数
  129. /// </summary>
  130. /// <param name="filter">主表过滤条件</param>
  131. /// <param name="subFilter">连接表过滤条件</param>
  132. /// <returns></returns>
  133. public VisitLogAnalysisDto GetTotalInfo(string filter, string subFilter)
  134. {
  135. var sql = $@"select sum(count) count from
  136. (select usercode,count(1) as count from visitlog a RIGHT JOIN `function` fun on a.FunctionId=fun.ID
  137. where 1=1 {filter} group by usercode) t
  138. left join staff b on t.usercode=b.fcode
  139. where 1=1 and b.FCode is not null {subFilter} ";
  140. var dto = CurrDb.FindList<VisitLogAnalysisDto>(sql).FirstOrDefault();
  141. if (dto != null)
  142. {
  143. dto.StaffCode = "Total";
  144. dto.StaffName = "总计";
  145. }
  146. return dto;
  147. }
  148. /// <summary>
  149. /// 分页获取报表被访问次数
  150. /// </summary>
  151. /// <param name="filter">主表过滤条件</param>
  152. /// <param name="subFilter">连接表过滤条件</param>
  153. /// <param name="start">开始记录数</param>
  154. /// <param name="length">返回长度</param>
  155. /// <param name="total">记录总数</param>
  156. /// <returns></returns>
  157. public IEnumerable<VisitLogAnalysisDto> GetReportVisitLog(string filter, string subFilter, int start, int length,
  158. out int total, string sort = "count", string order = "asc")
  159. {
  160. var roots = CurrDb.FindList<EapFunction>("select * from function where parentId=0");
  161. var rootIds = roots.Select(c => c.ID);
  162. var children = CurrDb.FindList<EapFunction>($"select * from function where parentid in ({string.Join(",", rootIds)})");
  163. var Ids = rootIds.Concat(children.Select(c => c.ID));
  164. var grandson = CurrDb.FindList<EapFunction>($"select * from function where parentid in ({string.Join(",", Ids)})");
  165. var allIds = Ids.Concat(grandson.Select(c => c.ID));
  166. //var countSql = $@"select count(1) from
  167. // (select count(1) count,FunctionId from visitlog a
  168. // group by FunctionId) t
  169. // inner join function b on t.FunctionId=b.id
  170. // inner join function c on b.parentid=c.id ";
  171. var countSql = $@"select count(1) from(select b.id as reportId,ifnull(concat(c.fname,'/',b.fname),b.fname) reportname,ifnull(count,0) count from
  172. function b
  173. left join function c on b.ParentID=c.id
  174. LEFT JOIN function f on c.ParentID=f.ID
  175. left join
  176. (select t.FunctionId ReportId,count from
  177. (select count(1) count,FunctionId from visitlog a
  178. where 1=1 {filter}
  179. group by FunctionId) t) s
  180. on b.id=s.reportId
  181. where b.id in ({string.Join(",", allIds)}) and b.url!='/' and b.url!='' {subFilter} )tt where tt.reportname !=''";
  182. total = Convert.ToInt32(CurrDb.FindList<string>(countSql).FirstOrDefault() ?? "0");
  183. //var sql = $@"select t.FunctionId ReportId,concat(c.fname,'/',b.fname) as reportName,count from
  184. // (select count(1) count,FunctionId from visitlog a
  185. // where 1=1 {filter}
  186. // group by FunctionId) t
  187. // inner join function b on t.FunctionId=b.id
  188. // inner join function c on b.parentid=c.id
  189. // where 1=1 {subFilter} order by {sort} {order} limit {start - 1},{length}";
  190. // 统计报表访问记录时将所有报表包含进去
  191. var sql = $@"
  192. select * from(select b.id as reportId,IFNULL(CONCAT(f.fname,'/',c.fname,'/',b.fname),ifnull(concat(c.fname,'/',b.fname),b.fname)) reportname,ifnull(count,0) count from
  193. function b
  194. left join function c on b.ParentID=c.id
  195. LEFT JOIN function f on c.ParentID=f.ID
  196. left join
  197. (select t.url ReportId,count from
  198. (select count(1) count,fun.url url from visitlog a RIGHT JOIN `function` fun on a.FunctionId=fun.ID
  199. where 1=1 {filter}
  200. group by fun.url) t) s
  201. on b.Url=s.reportId
  202. where b.id in ({string.Join(",", allIds)}) and b.url!='/' and b.url!='' {subFilter} )tt where tt.reportname !=''
  203. order by {sort} {order} limit {start - 1},{length} ";
  204. return CurrDb.FindList<VisitLogAnalysisDto>(sql);
  205. }
  206. /// <summary>
  207. /// 获取人员访问报表总次数
  208. /// </summary>
  209. /// <param name="filter">主表过滤条件</param>
  210. /// <param name="subFilter">连接表过滤条件</param>
  211. /// <returns></returns>
  212. public VisitLogAnalysisDto GetReportTotalInfo(string filter, string subFilter)
  213. {
  214. var sql = $@"select sum(count) count from
  215. (select count(1) count,fun.Url Url from visitlog a RIGHT JOIN `function` fun on a.FunctionId=fun.ID
  216. where 1=1 {filter}
  217. group by fun.Url) t
  218. inner join function b on t.Url=b.Url
  219. left join function c on b.parentid=c.id
  220. where 1=1 {subFilter}";
  221. var dto = CurrDb.FindList<VisitLogAnalysisDto>(sql).FirstOrDefault();
  222. if (dto != null)
  223. {
  224. dto.StaffCode = "Total";
  225. dto.StaffName = "总计";
  226. }
  227. return dto;
  228. }
  229. public IEnumerable<StaffReportLogDto> GetStaffReportLogs(string filter, string subFilter)
  230. {
  231. var sql = $@" select b.FCode StaffCode,b.FName StaffName,t.date,ifnull(concat(d.fname,'/',c.fname),c.fname) reportName,t.count
  232. from
  233. (select count(1) count,usercode,FunctionId,date_format(visitTime,'%Y-%m-%d') date
  234. from visitlog a
  235. where 1=1 {filter}
  236. group by UserCode,FunctionId,date_format(visitTime,'%Y-%m-%d') ) t
  237. inner join staff b on t.usercode=b.fcode
  238. inner join function c on t.FunctionId=c.id
  239. left join function d on c.parentid=d.id
  240. where 1=1 {subFilter} order by staffcode,date desc,count desc";
  241. var entities = CurrDb.FindList<VisitLogAnalysisDto>(sql);
  242. var list = new List<StaffReportLogDto>();
  243. var no = 0;
  244. if (entities != null && entities.Count() > 0)
  245. {
  246. var staffCodes = entities.Select(c => c.StaffCode).Distinct();
  247. var empInfos = this.GetEmployeeInfos(staffCodes);
  248. var nameGroups = entities.GroupBy(c => new { c.StaffCode, c.StaffName });
  249. if (nameGroups != null && nameGroups.Count() > 0)
  250. {
  251. foreach (var item in nameGroups)
  252. {
  253. var renderName = true;
  254. no++;
  255. var dateGroups = item.GroupBy(c => c.Date);
  256. if (dateGroups != null && dateGroups.Count() > 0)
  257. {
  258. foreach (var dateItem in dateGroups)
  259. {
  260. var renderDate = true;
  261. foreach (var reportItem in dateItem)
  262. {
  263. var temp = new StaffReportLogDto
  264. {
  265. StaffCode = item.Key.StaffCode,
  266. StaffName = item.Key.StaffName,
  267. Department = empInfos.FirstOrDefault(c => c.StaffCode == item.Key.StaffCode)?.Dept ?? "--",
  268. NameRowSpan = item.Count(),
  269. TotalCount = item.Sum(c => c.Count),
  270. Date = dateItem.Key.Value.ToString("yyyy-MM-dd"),
  271. DateCount = dateItem.Sum(c => c.Count),
  272. DateRowSpan = dateItem.Count(),
  273. ReportName = reportItem.ReportName,
  274. Count = reportItem.Count,
  275. RenderName = renderName,
  276. RenderDate = renderDate,
  277. No = no
  278. };
  279. list.Add(temp);
  280. renderName = false;
  281. renderDate = false;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. return list;
  289. }
  290. /// <summary>
  291. /// 获取欧菲光员工职级信息
  292. /// </summary>
  293. /// <param name="staffCodes"></param>
  294. /// <returns></returns>
  295. public IEnumerable<EmployeeInfo> GetEmployeeInfos(IEnumerable<string> staffCodes)
  296. {
  297. var filter = $" and a.EMP_CODE in ({string.Join(",", staffCodes.Select(c => $"'{c}'"))})";
  298. using (IDatabase empDb = DbFactory.Base("OfilmDeptInfo"))
  299. {
  300. var sql = $"select EMP_CODE StaffCode,DEPT_NAME Dept,JOBNAME Duty from EAP_USER_ACCESS_V a where 1=1 {filter}";
  301. return empDb.FindList<EmployeeInfo>(sql);
  302. }
  303. }
  304. #endregion
  305. }
  306. }