yanjinhui
5 天以前 0bc66c82a07c1b46a98ba7007b0a672fec02176e
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using SqlSugar;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseRepository;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_DTO.Telescopic;
using WIDESEAWCS_ITelescopicService;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_TelescopicService
{
    public class LoginhsyService : ServiceBase<Dt_Loginhsy, IRepository<Dt_Loginhsy>>, ILoginhsyService
    {
        public IRepository<Dt_Loginhsy> Repository => BaseDal;
        private readonly IRepository<Sys_User> _user;
 
        public LoginhsyService(IRepository<Sys_User> user, IRepository<Dt_Loginhsy> BaseDal) : base(BaseDal)
        {
            _user = user;
        }
 
        public WebResponseContent LoginRecord(PaginationDTO pagination)
        {
            try
            {
                int totalCount = 0;
 
                var sys = _user.Db.Queryable<Sys_User>();
                var main = Db.Queryable<Dt_Loginhsy>();
 
                //模糊查询
                var query = sys.InnerJoin<Dt_Loginhsy>((a, b) => a.UserName == b.UserName);
                if (!string.IsNullOrEmpty(pagination.searchKeyword))
                {
                    query = query.Where((a, b) =>
                        a.UserTrueName.Contains(pagination.searchKeyword) ||
                        a.Userteam.Contains(pagination.searchKeyword) ||
                        b.OpCenten.Contains(pagination.searchKeyword));
                }
                
                //时间查询
                if (pagination.startDate.HasValue && pagination.endDate.HasValue)
                {
                    query = query.Where((a, b) => b.LoginTiem >= pagination.startDate.Value && b.LoginTiem <= pagination.endDate.Value);
                }
 
                // 排序处理
                if (!string.IsNullOrEmpty(pagination.sortField))
                {
                    //isAsc:这是一个布尔值,判断排序是升序 (true)"asc" ,还是降序 (false) "desc"
                    var isAsc = pagination.sortOrder?.ToLower() == "asc";//pagination.sortOrder 不为空,则调用 ToLower() 方法将其转为小写字母
 
                    query = pagination.sortField.ToLower() switch
                    {
                        //如果isAsc 为ture就执行query.OrderBy((a, b) => b.LoginTiem, OrderByType.Asc)升序排序,
                        //如果为fales就执行query.OrderBy((a, b) => b.LoginTiem, OrderByType.Desc)降序排序
                        "logintiem" => isAsc ? query.OrderBy((a, b) => b.LoginTiem, OrderByType.Asc)
                                             : query.OrderBy((a, b) => b.LoginTiem, OrderByType.Desc),
 
                        "outtiem" => isAsc ? query.OrderBy((a, b) => b.OutTiem, OrderByType.Asc)
                                           : query.OrderBy((a, b) => b.OutTiem, OrderByType.Desc),
 
                        //"usertruename" => isAsc ? query.OrderBy((a, b) => a.UserTrueName, OrderByType.Asc)
                        //: query.OrderBy((a, b) => a.UserTrueName, OrderByType.Desc),
                        _ => query.OrderBy((a, b) => b.LoginTiem, OrderByType.Desc) // 默认按登入时间降序
                    };
                }
                else
                {
                    // 默认按 LoginTiem 降序
                    query = query.OrderBy((a, b) => b.LoginTiem, OrderByType.Desc);
                }
 
 
                //返回结果
                var result = query.Select((a, b) => new
                {
                    a.User_Id,
                    a.UserTrueName,
                    a.Userteam,
                    a.HeadImageUrl,
                    b.ID,
                    b.LoginTiem,
                    b.OutTiem,
                    b.OpCenten,
                }).ToPageList(pagination.pageIndex, pagination.pageSize, ref totalCount);
 
                return new WebResponseContent
                {
                    Status = true,
                    Data = new
                    {
                        TotalCount = totalCount, // 总数
                        PageIndex = pagination.pageIndex, // 页数
                        PageSize = pagination.pageSize, // 一页多少个数据
                        Items = result
                    }
                };
            }
            catch (Exception ex)
            {
                return new WebResponseContent { Status = false, Message = "错误: " + ex.Message };
            }
        }
 
 
 
 
        public WebResponseContent OutLoginTime(string account)
        {
            try
            {
                var log = BaseDal.QueryData(x=>x.UserName== account).OrderByDescending(x=>x.UserName).FirstOrDefault();
                if (log==null)
                {
                    return new WebResponseContent { Status = false, Message = "失败" };
                }
                log.OutTiem = DateTime.Now;
                BaseDal.UpdateData(log);
                return new WebResponseContent { Status = true, Data = log.OutTiem };
            }
            catch (Exception ex)
            {
 
                return new WebResponseContent { Status = false, Message = "错误信息"+ex };
            }
        }
    }
   
}