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 { /// /// 代理商信息服务 /// [ApiDescriptionSettings(Tag = "代理商信息服务", Name = "UavAgentProfitConfig", Order = 200)] [Route("api/Extend/[controller]")] public class UavAgentProfitConfigService : IUavAgentProfitConfigService, IDynamicApiController, ITransient { private readonly ISqlSugarRepository _uavAgentProfitConfigRepository; private readonly SqlSugarScope _db; private readonly IUserManager _userManager; /// /// 初始化一个类型的新实例 /// public UavAgentProfitConfigService( ISqlSugarRepository uavAgentProfitConfigRepository, IUserManager userManager) { _uavAgentProfitConfigRepository = uavAgentProfitConfigRepository; _db = _uavAgentProfitConfigRepository.Context; _userManager = userManager; } #region 获取代理商分润设置 /// /// 获取代理商分润设置 /// /// 参数 /// [HttpGet("{id}")] public async Task GetInfo(string id) { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); var output = entity.Adapt(); return output; } #endregion #region 获取代理商分润设置列表 /// /// 获取代理商分润设置列表 /// /// 请求参数 /// [HttpGet("")] public async Task GetList([FromQuery] UavAgentProfitConfigListQueryInput input) { var sidx = input.sidx == null ? "id" : input.sidx; List queryLevel = input.level != null ? input.level.Split(',').ToObeject>() : 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 queryProfitPercent = input.profitPercent != null ? input.profitPercent.Split(',').ToObeject>() : 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() .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().Where(u => u.Id == it.AgentId && u.DeleteMark == null).Select(u => u.RealName), parentAgent_name = SqlFunc.Subqueryable().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.SqlSugarPageResult(data); } #endregion #region 新建代理商信息 /// /// 新建代理商信息 /// /// 参数 /// [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().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(info).ExecuteCommandAsync(); var ParentId = ""; if (input.parentAgentId.IsNotEmptyOrNull()) { var uavAgentInfo = await _db.Queryable().FirstAsync(o => o.AgentId == input.parentAgentId); ParentId = uavAgentInfo.Id; } var entity = input.Adapt(); 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 更新代理商分润设置 /// /// 更新代理商分润设置 /// /// 主键 /// 参数 /// [HttpPut("{id}")] public async Task Update(string id, [FromBody] UavAgentProfitConfigUpInput input) { var entity = input.Adapt(); var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001); } #endregion #region 删除代理商 /// /// 删除代理商 /// /// [HttpDelete("{id}")] public async Task Delete(string id) { var result = await _db.Ado.UseTranAsync(async () => { var entity = await _db.Queryable().FirstAsync(p => p.Id == id); _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005); var userInfo = await _db.Queryable().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().Where(d => d.Id == id).ExecuteCommandAsync(); if (isOk <= 0) { throw NCCException.Oh(ErrorCode.COM1002); } }); if (!result.IsSuccess) { throw NCCException.Oh("删除代理商信息失败"); } } #endregion #region 获取所有代理商 /// /// 获取所有代理商 /// /// [HttpGet("GetAllAgent")] public async Task GetAllAgent() { var data = await _db.Queryable().Where(p => p.ExtensionStr == "代理商" && p.EnabledMark == 1 && p.DeleteMark == null).Select(p => new { p.Id, p.RealName }).MergeTable().ToListAsync(); return data; } #endregion #region 获取当前登录人下面的代理 /// /// 获取当前登录人下面的代理 /// /// [HttpGet("GetMyAgentCode")] public async Task GetMyAgentCode([FromQuery] UavAgentProfitConfigListQueryInput input) { var agentList = await _db.Queryable((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().Where(u => u.BelongUserId == agent.AgentId).Count(), agent_name = user.RealName, agent_phone = user.MobilePhone }) .ToListAsync(); return agentList; } #endregion #region 查看指定用户下面的代理人信息 /// /// 查看指定用户下面的代理人信息 /// /// /// [HttpGet("GetAgentInfoByUserId/{userId}")] public async Task GetAgentInfoByUserId(string userId) { var AgentLIst = await _db.Queryable().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().Where(u => u.BelongUserId == it.AgentId).Count(), agent_name = SqlFunc.Subqueryable().Where(u => u.Id == it.AgentId && u.DeleteMark == null).Select(u => u.RealName), agent_phone = SqlFunc.Subqueryable().Where(u => u.Id == it.AgentId && u.DeleteMark == null).Select(u => u.MobilePhone), }).ToListAsync(); return AgentLIst; } #endregion #region 获取指定用户所有的上级代理(递归) /// /// 获取指定用户所有的上级代理(递归) /// /// /// [HttpGet("GetAllAgent/{userId}")] [AllowAnonymous] public async Task GetAllAgent(string userId) { var agent = await _db.Queryable().FirstAsync(p => p.AgentId == userId); var agentLIst = _db.Queryable().ToParentList(it => it.ParentId, agent.Id); return agentLIst; } #endregion #region 获取代理商用户信息 /// /// 获取代理商用户信息 /// /// [HttpGet("GetAgentInfoCurrentUser")] public async Task GetAgentInfoCurrentUser() { var userId = _userManager.UserId; var data = await _db.Queryable().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 } }