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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using DocumentServiceAPI.Application.UserAndLogin.Services.Interfaces;
using DocumentServiceAPI.Application.UserAndLogin.ViewMode;
using DocumentServiceAPI.Model;
using DocumentServiceAPI.Model.cyDocumentModel;
using DocumentServiceAPI.Model.UserInfoModel;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
 
namespace DocumentServiceAPI.Application.UserAndLogin.Services
{
    /// <summary>
    /// 登录验证服务
    /// </summary>
    public class UserService : ILoginVerifyService, IScoped
    {
        private ISqlSugarClient _db { get; set; }
        public UserService(ISqlSugarClient db)
        {
            _db = db;
        }
        /// <summary>
        /// 员工详情
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="TenantID"></param>
        /// <returns></returns>
        public async Task<EmployeeTenantInfo> GetEmployeeInfo(int ID, int TenantID)
        {
            var ret = await _db.Queryable<EmployeeInfo, EmployeeAtTenant, TenantInfo>((ei, eiat, ti) =>
              new JoinQueryInfos(
      JoinType.Right, ei.Id == eiat.EmployeeID, //左连接 左链接 左联 
      JoinType.Right, ti.Id == eiat.TenantID
                              )).Where((ei, eiat, ti) => ei.Id == ID && TenantID == ti.Id)
                              .Select(expression: (ei, eiat, ti) => new EmployeeTenantInfo
                              {
                                  _employeeInfo = ei,
                                  _tenantInfo = ti
 
                              }).SingleAsync();
 
            return ret;
        }
 
        /// <summary>
        /// 获取Jwt相关信息
        /// </summary>
        public JwtInfo GetJwtInfo()
        {
            JwtInfo jwtInfo = new JwtInfo();
            jwtInfo.LogInSource = (LogInFrom)(App.User?.FindFirstValue("loginfrom")?.ObjToInt() ?? 0);
            jwtInfo.EID = App.User?.FindFirstValue("eid")?.ObjToInt();
            jwtInfo.UID = App.User?.FindFirstValue("uid")?.ObjToInt();
            jwtInfo.TID = App.User?.FindFirstValue("tid")?.ObjToInt();
            var guid = App.User?.FindFirstValue("jid");
            jwtInfo.JID = string.IsNullOrEmpty(guid) ? new Guid(guid) : null;
            return jwtInfo;
        }
        /// <summary>
        /// 获取单位列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<UnitVM>> GetUserUnitInfo(int  tid )
        {
 
            var list = await _db.Queryable<DocumentServiceAPI.Model.cyDocumentModel.Document_TenderUnit>()
                   .Where((tu) => tu.TenantId == tid && tu.IsEn == true && tu.IsDeled != true)
                   .Select((tu) => new UnitVM
                   {
                       UnitID = tu.UnitId,
                       UnitName = tu.UnitName,
                       Remark = tu.Remark,
                       UnitLogo = tu.UnitLogo,
                   })
                   .ToListAsync();
            return list;
        }
 
        /// <summary>
        /// 根据账号获取 Tender 列表
        /// </summary>
        /// <param name="ItCode"></param>
        /// <returns></returns>
        public async Task<List<TenderVM>> GetUserTenderList(string ItCode)
            
        {
          
            var tentlist= await _db.Queryable<TenantInfo, EmployeeAtTenant, Document_EmployeeInfo>((ti, et, ei) =>
 
              new JoinQueryInfos
              (
                 JoinType.Right, ti.Id == et.TenantID, //左连接 左链接 左联 
             JoinType.Right, ei.EmployeeId == et.EmployeeID
                  )
             )
                .Where((ti, et, ei) =>
              ( ei.UserName == ItCode) && (ti.IsEn == true) && (ti.IsDel != true) && (ei.IsWork == null || ei.IsWork == 1))
 
             .Select((ti, et, ei) => new TenderVM
             {
                 EmployeeID = ei.EmployeeId,
                 Description = ti.Description,
                 ItCode = ItCode,
                 Name = ti.Name,
                 TenderId = ti.Id
                 
             }
              ).ToListAsync();
 
            var ten = await _db.Queryable<TenantInfo>().Where(x => x.IsEn == true && x.IsDel != true && x.ItCode == ItCode)
                   .Select(x => new TenderVM
                   {
 
                       Description = x.Description,
                       ItCode = ItCode,
                       Name = x.Name,
                       TenderId = x.Id,
                       IsTender = true
 
                   }
              ).ToListAsync()  ;
            ten.AddRange(tentlist);
            return ten;
 
        }
 
        /// <summary>
        /// 检查密码是否登录
        /// </summary>
        /// <param name="TenantID"> 租户ID</param>
        /// <param name="employeeID">员工ID</param>
        /// <param name="PsW">密码</param>
        /// <returns></returns>
        public async Task<bool> CheckPsw(int TenantID, int? employeeID, string PsW)
        {
            if (employeeID == null)
            {
                var psw = await _db.Queryable<TenantInfo>().Where(x => x.Id == TenantID).Select(x => x.PsW).SingleAsync();
                if (PsW == psw)
                    return true;
            }
            else
            {
                var psw = await _db.Queryable<Document_EmployeeInfo, EmployeeAtTenant, TenantInfo>((ei, et, ti) =>
 
             new JoinQueryInfos
             (
                JoinType.Left, ei.EmployeeId == et.EmployeeID, //左连接 左链接 左联 
            JoinType.Left, ti.Id == et.TenantID
                 )
            ).Where((ei, et, ti) =>
 
                ei.EmployeeId == employeeID && ti.Id == TenantID
            )
            .Select((ei, et, ti) => ei.UserPassWord)
            .SingleAsync();
                if (PsW == psw)
                    return true;
            }
            return false;
 
        }
 
 
       
    }
 
 
}