UavAgentProfitConfigService.cs
14.6 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Extend.Interfaces.UavAgentProfitConfig;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys;
using NCC.Extend.Entitys.Dto.UavAgentProfitConfig;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.System.Entitys.Permission;
using NCC.DataEncryption;
using NCC.Common.Const;
using NCC.System.Entitys.System;
using Microsoft.AspNetCore.Authorization;
using NCC.System.Entitys.Model.Permission.User;
namespace NCC.Extend.UavAgentProfitConfig
{
/// <summary>
/// 代理商信息服务
/// </summary>
[ApiDescriptionSettings(Tag = "代理商信息服务", Name = "UavAgentProfitConfig", Order = 200)]
[Route("api/Extend/[controller]")]
public class UavAgentProfitConfigService : IUavAgentProfitConfigService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository<UavAgentProfitConfigEntity> _uavAgentProfitConfigRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
/// <summary>
/// 初始化一个<see cref="UavAgentProfitConfigService"/>类型的新实例
/// </summary>
public UavAgentProfitConfigService(
ISqlSugarRepository<UavAgentProfitConfigEntity> uavAgentProfitConfigRepository,
IUserManager userManager)
{
_uavAgentProfitConfigRepository = uavAgentProfitConfigRepository;
_db = _uavAgentProfitConfigRepository.Context;
_userManager = userManager;
}
#region 获取代理商分润设置
/// <summary>
/// 获取代理商分润设置
/// </summary>
/// <param name="id">参数</param>
/// <returns></returns>
[HttpGet("{id}")]
public async Task<dynamic> GetInfo(string id)
{
var entity = await _db.Queryable<UavAgentProfitConfigEntity>().FirstAsync(p => p.Id == id);
var output = entity.Adapt<UavAgentProfitConfigInfoOutput>();
return output;
}
#endregion
#region 获取代理商分润设置列表
/// <summary>
/// 获取代理商分润设置列表
/// </summary>
/// <param name="input">请求参数</param>
/// <returns></returns>
[HttpGet("")]
public async Task<dynamic> GetList([FromQuery] UavAgentProfitConfigListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List<object> queryLevel = input.level != null ? input.level.Split(',').ToObeject<List<object>>() : null;
var startLevel = input.level != null && !string.IsNullOrEmpty(queryLevel.First().ToString()) ? queryLevel.First() : decimal.MinValue;
var endLevel = input.level != null && !string.IsNullOrEmpty(queryLevel.Last().ToString()) ? queryLevel.Last() : decimal.MaxValue;
List<object> queryProfitPercent = input.profitPercent != null ? input.profitPercent.Split(',').ToObeject<List<object>>() : null;
var startProfitPercent = input.profitPercent != null && !string.IsNullOrEmpty(queryProfitPercent.First().ToString()) ? queryProfitPercent.First() : decimal.MinValue;
var endProfitPercent = input.profitPercent != null && !string.IsNullOrEmpty(queryProfitPercent.Last().ToString()) ? queryProfitPercent.Last() : decimal.MaxValue;
var data = await _db.Queryable<UavAgentProfitConfigEntity>()
.WhereIF(queryLevel != null, p => SqlFunc.Between(p.Level, startLevel, endLevel))
.WhereIF(queryProfitPercent != null, p => SqlFunc.Between(p.ProfitPercent, startProfitPercent, endProfitPercent))
.Select(it => new UavAgentProfitConfigListOutput
{
id = it.Id,
agentId = it.AgentId,
level = it.Level,
profitPercent = it.ProfitPercent,
remark = it.Remark,
status = it.Status,
parentAgentId = it.ParendAgentId,
agent_name = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.AgentId && u.DeleteMark == null).Select(u => u.RealName),
parentAgent_name = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.ParendAgentId && u.DeleteMark == null).Select(u => u.RealName),
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult<UavAgentProfitConfigListOutput>.SqlSugarPageResult(data);
}
#endregion
#region 新建代理商信息
/// <summary>
/// 新建代理商信息
/// </summary>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPost("")]
public async Task Create([FromBody] UavAgentProfitConfigCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
//判断账号密码必填
if (string.IsNullOrEmpty(input.agentAccount) || string.IsNullOrEmpty(input.agentPassword))
{
throw NCCException.Oh(ErrorCode.COM1000);
}
var info = await _db.Queryable<UserEntity>().Where(o => o.Account == input.agentAccount && o.ExtensionStr == "代理商" && o.EnabledMark != 0 && o.DeleteMark == null).FirstAsync();
if (info.IsNullOrEmpty())
{
var Secretkey = Guid.NewGuid().ToString();
//如果用户信息是空,就注册创建用户
info = new UserEntity
{
Id = YitIdHelper.NextId().ToString(),
Account = input.agentAccount,
RealName = input.agentName,
MobilePhone = input.agentPhone,
EnabledMark = 1,
Secretkey = Secretkey,
Password = MD5Encryption.Encrypt(MD5Encryption.Encrypt(input.agentPassword) + Secretkey),
ExtensionStr = "代理商",
OrganizeId = "17BEBDCB-248D-4668-B6CD-BF22A446BBD4"
};
await _db.Insertable<UserEntity>(info).ExecuteCommandAsync();
var ParentId = "";
if (input.parentAgentId.IsNotEmptyOrNull())
{
var uavAgentInfo = await _db.Queryable<UavAgentProfitConfigEntity>().FirstAsync(o => o.AgentId == input.parentAgentId);
ParentId = uavAgentInfo.Id;
}
var entity = input.Adapt<UavAgentProfitConfigEntity>();
entity.Id = YitIdHelper.NextId().ToString();
entity.AgentId = info.Id;
entity.ParentId = ParentId;
entity.ParendAgentId = input.parentAgentId;
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
else
{
throw NCCException.Oh("已有代理商,请勿重复添加");
}
}
#endregion
#region 更新代理商分润设置
/// <summary>
/// 更新代理商分润设置
/// </summary>
/// <param name="id">主键</param>
/// <param name="input">参数</param>
/// <returns></returns>
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] UavAgentProfitConfigUpInput input)
{
var entity = input.Adapt<UavAgentProfitConfigEntity>();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除代理商
/// <summary>
/// 删除代理商
/// </summary>
/// <returns></returns>
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var result = await _db.Ado.UseTranAsync(async () =>
{
var entity = await _db.Queryable<UavAgentProfitConfigEntity>().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var userInfo = await _db.Queryable<UserEntity>().Where(o => o.Id == entity.AgentId).FirstAsync();
if (userInfo != null)
{
userInfo.DeleteMark = 1;
var userUpdate = await _db.Updateable(userInfo).ExecuteCommandAsync();
if (userUpdate <= 0)
{
throw NCCException.Oh(ErrorCode.COM1002); // 或自定义错误码
}
}
var isOk = await _db.Deleteable<UavAgentProfitConfigEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
if (isOk <= 0)
{
throw NCCException.Oh(ErrorCode.COM1002);
}
});
if (!result.IsSuccess) { throw NCCException.Oh("删除代理商信息失败"); }
}
#endregion
#region 获取所有代理商
/// <summary>
/// 获取所有代理商
/// </summary>
/// <returns></returns>
[HttpGet("GetAllAgent")]
public async Task<dynamic> GetAllAgent()
{
var data = await _db.Queryable<UserEntity>().Where(p => p.ExtensionStr == "代理商" && p.EnabledMark == 1 && p.DeleteMark == null).Select(p => new
{
p.Id,
p.RealName
}).MergeTable().ToListAsync();
return data;
}
#endregion
#region 获取当前登录人下面的代理
/// <summary>
/// 获取当前登录人下面的代理
/// </summary>
/// <returns></returns>
[HttpGet("GetMyAgentCode")]
public async Task<dynamic> GetMyAgentCode([FromQuery] UavAgentProfitConfigListQueryInput input)
{
var agentList = await _db.Queryable<UavAgentProfitConfigEntity, UserEntity>((agent, user) => new JoinQueryInfos(JoinType.Left, agent.AgentId == user.Id))
.Where(agent => agent.ParendAgentId == _userManager.UserId)
.WhereIF(!string.IsNullOrEmpty(input.agentAccount), (agent, user) => user.Account.Contains(input.agentAccount) || user.RealName.Contains(input.agentAccount) || user.MobilePhone.Contains(input.agentAccount))
.Select((agent, user) => new UavAgentProfitConfigListOutput
{
id = agent.Id,
agentId = agent.AgentId,
level = agent.Level,
profitPercent = agent.ProfitPercent,
remark = agent.Remark,
status = agent.Status,
parentAgentId = agent.ParendAgentId,
deviceNumber = SqlFunc.Subqueryable<UavDeviceEntity>().Where(u => u.BelongUserId == agent.AgentId).Count(),
agent_name = user.RealName,
agent_phone = user.MobilePhone
})
.ToListAsync();
return agentList;
}
#endregion
#region 查看指定用户下面的代理人信息
/// <summary>
/// 查看指定用户下面的代理人信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet("GetAgentInfoByUserId/{userId}")]
public async Task<dynamic> GetAgentInfoByUserId(string userId)
{
var AgentLIst = await _db.Queryable<UavAgentProfitConfigEntity>().Where(p => p.ParendAgentId == userId).Select(it => new UavAgentProfitConfigListOutput
{
id = it.Id,
agentId = it.AgentId,
level = it.Level,
profitPercent = it.ProfitPercent,
remark = it.Remark,
status = it.Status,
parentAgentId = it.ParendAgentId,
deviceNumber = SqlFunc.Subqueryable<UavDeviceEntity>().Where(u => u.BelongUserId == it.AgentId).Count(),
agent_name = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.AgentId && u.DeleteMark == null).Select(u => u.RealName),
agent_phone = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.AgentId && u.DeleteMark == null).Select(u => u.MobilePhone),
}).ToListAsync();
return AgentLIst;
}
#endregion
#region 获取指定用户所有的上级代理(递归)
/// <summary>
/// 获取指定用户所有的上级代理(递归)
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[HttpGet("GetAllAgent/{userId}")]
[AllowAnonymous]
public async Task<dynamic> GetAllAgent(string userId)
{
var agent = await _db.Queryable<UavAgentProfitConfigEntity>().FirstAsync(p => p.AgentId == userId);
var agentLIst = _db.Queryable<UavAgentProfitConfigEntity>().ToParentList(it => it.ParentId, agent.Id);
return agentLIst;
}
#endregion
#region 获取代理商用户信息
/// <summary>
/// 获取代理商用户信息
/// </summary>
/// <returns></returns>
[HttpGet("GetAgentInfoCurrentUser")]
public async Task<dynamic> GetAgentInfoCurrentUser()
{
var userId = _userManager.UserId;
var data = await _db.Queryable<UserEntity>().Where(a => a.Id == userId)
.Select(a => new UserInfo
{
userId = a.Id,
headIcon = SqlFunc.MergeString("/api/File/Image/userAvatar/", a.HeadIcon),
userAccount = a.Account,
userName = a.RealName,
gender = SqlFunc.ToInt32(a.Gender),
organizeId = a.OrganizeId,
managerId = a.ManagerId,
isAdministrator = SqlFunc.IIF(a.IsAdministrator == 1, true, false),
positionId = a.PositionId,
roleId = a.RoleId,
prevLoginTime = a.PrevLogTime,
prevLoginIPAddress = a.PrevLogIP,
openId = a.OpenId
}).FirstAsync();
return data;
}
#endregion
}
}