using YD_WeChatApplet.Context; using Microsoft.EntityFrameworkCore; using YD_WeChatApplet.Commons; using YD_WeChatApplet.Api.Utilities; using Microsoft.AspNetCore.Mvc; using TGJ.NetworkFreight.SeckillAggregateServices.Pos.UserService; using YD_WeChatApplet.WeChat.Lib; using YD_WeChatApplet.WeChat; using YD_WeChatApplet.Api.Entitys; using YD_WeChatApplet.Commons.Users; using System.Linq.Expressions; using YD_WeChatApplet.Commons.Dto; using YD_WeChatApplet.Api.Services.Impl; using YD_WeChatApplet.Commons.Dto.User; using Newtonsoft.Json.Linq; using Microsoft.Extensions.Logging; using System.Net; using YD_WeChatApplet.Api.SmartSportsEntitys; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Authorization; using System.Linq; using System.Text.Json; using YD_WeChatApplet.Commons.Dto.School; using VOL.Entity.DomainModels; using YD_WeChatApplet.Commons.Dto.Server; using System.Security.AccessControl; namespace YD_WeChatApplet.Services { public class ServerService : IServerService { public UserContext _userContext; public SmartSportsContext _smartSportsContext; public ServerService(UserContext userContext, SmartSportsContext smartSportsContext) { _userContext = userContext; _smartSportsContext = smartSportsContext; } /// /// 获取打卡详情 /// /// public async Task> GetPersonalGoalInfo(PersonalGoalInfoReqDto req) { var query = from pg in _userContext.PersonalGoal join pgr in _userContext.PersonalGoalResult on pg.Id equals pgr.PersonalGoalId where pg.UserId == req.UserId select new PersonalGoalInfoDto() { GoalDuration = pg.GoalDuration, GoalAmount = pg.GoalAmount, GoalDate = pg.GoalDate, Amount = pgr.Amount, IsFinish = pg.GoalAmount > pgr.Amount }; // 如果提供了月份筛选 if (!string.IsNullOrEmpty(req.Month) && DateTime.TryParse(req.Month, out DateTime monthDate)) { query = query.Where(x => x.GoalDate.Month == monthDate.Month && x.GoalDate.Year == monthDate.Year); } var total = await query.CountAsync(); var pgList = await query .OrderByDescending(x => x.GoalDate) .Skip((req.PageIndex - 1) * req.PageSize) .Take(req.PageSize) .ToListAsync(); return new PageDataDto { Total = total, Datas = pgList }; } /// /// 获取用户训练记录 /// /// /// public async Task> GetUserTrainingRecords(UserTrainingRecordsReqDto req) { // 查询个人目标结果表 var personalQuery = from pgr in _userContext.PersonalGoalResult where pgr.UserId == req.UserId select new UserTrainingRecordsDto { Id = pgr.Id, TrainingType = pgr.PersonalGoalType.ToString(), TrainingMode = pgr.WorkModeTypeName, Duration = pgr.Duration, JumpCount = pgr.Amount, Kcal = pgr.Amount / 10m, // 简单的卡路里计算 TrainingTime = pgr.CreateTime, DataSource = "个人训练" }; // 查询团队任务结果表 var teamQuery = from gtr in _userContext.GroupTaskResult where gtr.UserId == req.UserId select new UserTrainingRecordsDto { Id = gtr.Id, TrainingType = "团队训练", TrainingMode = gtr.WorkModeTypeName, Duration = gtr.Duration, JumpCount = gtr.Amount, Kcal = gtr.Amount / 10m, // 简单的卡路里计算 TrainingTime = gtr.CreateTime, DataSource = "团队训练" }; // 合并查询结果 var combinedQuery = personalQuery.Union(teamQuery); // 应用筛选条件 if (!string.IsNullOrEmpty(req.Type)) { combinedQuery = combinedQuery.Where(x => x.TrainingType.Contains(req.Type)); } if (!string.IsNullOrEmpty(req.Mode)) { combinedQuery = combinedQuery.Where(x => x.TrainingMode.Contains(req.Mode)); } if (!string.IsNullOrEmpty(req.StartTime) && DateTime.TryParse(req.StartTime, out DateTime startTime)) { combinedQuery = combinedQuery.Where(x => x.TrainingTime >= startTime); } if (!string.IsNullOrEmpty(req.EndTime) && DateTime.TryParse(req.EndTime, out DateTime endTime)) { combinedQuery = combinedQuery.Where(x => x.TrainingTime <= endTime); } var total = await combinedQuery.CountAsync(); var records = await combinedQuery .OrderByDescending(x => x.TrainingTime) .Skip((req.PageIndex - 1) * req.PageSize) .Take(req.PageSize) .ToListAsync(); return new PageDataDto { Total = total, Datas = records }; } /// /// 获取资源类型树 /// /// 资源类型树形结构 public async Task> GetResourceTypeTree() { // 获取所有资源类型 var allTypes = await _userContext.ResourceType.ToListAsync(); // 构建树形结构 var rootTypes = allTypes.Where(t => t.ParentId == 0).ToList(); var result = new List(); foreach (var rootType in rootTypes) { var rootDto = new B_ResourceTypeTreeDto { Id = rootType.Id, TypeName = rootType.TypeName, ParentId = rootType.ParentId }; // 递归构建子节点 BuildTypeTree(rootDto, allTypes); result.Add(rootDto); } return result; } /// /// 递归构建资源类型树 /// private void BuildTypeTree(B_ResourceTypeTreeDto parentDto, List allTypes) { var children = allTypes.Where(t => t.ParentId == parentDto.Id).ToList(); foreach (var child in children) { var childDto = new B_ResourceTypeTreeDto { Id = child.Id, TypeName = child.TypeName, ParentId = child.ParentId }; BuildTypeTree(childDto, allTypes); parentDto.Children.Add(childDto); } } /// /// 获取资源列表 /// /// 请求参数 /// 资源列表 public async Task> GetResourceList(ResourceListReqDto req) { var query = _userContext.ResourceDetails.AsQueryable(); // 根据资源类型筛选 if (req.ResourceTypeId.HasValue && req.ResourceTypeId.Value > 0) { query = query.Where(r => r.ResourceTypeId == req.ResourceTypeId.Value); } // 根据资源名称筛选(模糊查询) if (!string.IsNullOrWhiteSpace(req.ResourceName)) { query = query.Where(r => r.ResourceName.Contains(req.ResourceName)); } // 计算总记录数 var total = await query.CountAsync(); // 分页查询 var list = await query .OrderByDescending(r => r.Id) .Skip((req.PageIndex - 1) * req.PageSize) .Take(req.PageSize) .Select(r => new B_ResourceDetailsDto { Id = r.Id, ResourceTypeId = r.ResourceTypeId, ResourceName = r.ResourceName, ImageUrl = r.ImageUrl, ResourceUrl = r.ResourceUrl }) .ToListAsync(); return new PageDataDto { Total = total, Datas = list }; } /// /// 添加或更新资源类型 /// /// 资源类型信息 /// 操作结果 public async Task AddOrUpdateResourceType(ResourceTypeReqDto req) { try { if (req.Id.HasValue && req.Id.Value > 0) { // 更新资源类型 var resourceType = await _userContext.ResourceType.FirstOrDefaultAsync(t => t.Id == req.Id.Value); if (resourceType == null) { return false; } resourceType.TypeName = req.TypeName; resourceType.ParentId = req.ParentId; _userContext.ResourceType.Update(resourceType); } else { // 添加资源类型 var resourceType = new WCA_ResourceType { TypeName = req.TypeName, ParentId = req.ParentId }; await _userContext.ResourceType.AddAsync(resourceType); } await _userContext.SaveChangesAsync(); return true; } catch { return false; } } /// /// 删除资源类型 /// /// 资源类型ID /// 操作结果 public async Task DeleteResourceType(int id) { try { // 检查是否有子类型 var hasChildren = await _userContext.ResourceType.AnyAsync(t => t.ParentId == id); if (hasChildren) { return false; // 有子类型不能删除 } // 检查是否有关联的资源 var hasResources = await _userContext.ResourceDetails.AnyAsync(r => r.ResourceTypeId == id); if (hasResources) { return false; // 有关联资源不能删除 } var resourceType = await _userContext.ResourceType.FirstOrDefaultAsync(t => t.Id == id); if (resourceType == null) { return false; } _userContext.ResourceType.Remove(resourceType); await _userContext.SaveChangesAsync(); return true; } catch { return false; } } /// /// 添加或更新资源 /// /// 资源信息 /// 操作结果 public async Task AddOrUpdateResource(ResourceReqDto req) { try { if (req.Id.HasValue && req.Id.Value > 0) { // 更新资源 var resource = await _userContext.ResourceDetails.FirstOrDefaultAsync(r => r.Id == req.Id.Value); if (resource == null) { return false; } resource.ResourceTypeId = req.ResourceTypeId; resource.ResourceName = req.ResourceName; resource.ImageUrl = req.ImageUrl; resource.ResourceUrl = req.ResourceUrl; _userContext.ResourceDetails.Update(resource); } else { // 添加资源 var resource = new WCA_ResourceDetails { ResourceTypeId = req.ResourceTypeId, ResourceName = req.ResourceName, ImageUrl = req.ImageUrl, ResourceUrl = req.ResourceUrl }; await _userContext.ResourceDetails.AddAsync(resource); } await _userContext.SaveChangesAsync(); return true; } catch { return false; } } /// /// 删除资源 /// /// 资源ID /// 操作结果 public async Task DeleteResource(int id) { try { var resource = await _userContext.ResourceDetails.FirstOrDefaultAsync(r => r.Id == id); if (resource == null) { return false; } _userContext.ResourceDetails.Remove(resource); await _userContext.SaveChangesAsync(); return true; } catch { return false; } } /// /// 上传文件 /// /// /// public async Task UploadResourceFile(ResourceFileDto resourceFileDto) { var userName = UserLoginContext.Current.UserName; var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(resourceFileDto.File.FileName); var url = await Task.Run(() => ALiYunOss.Upload(resourceFileDto.File, $"Upload/Resource/", $"{resourceFileDto.ResourceTypeId}—{fileNameWithoutExtension}")); return url; } /// /// 获取微信用户 /// /// /// public async Task> GetUserPageList(UserPageListParam paramDto) { var res = new PageDataDto(); var query = _userContext.Users.Select(x => new UserPageListDto { BirthDate = x.BirthDate, Address = x.Address, CreateDate = x.CreateDate, Gender = x.Gender, HeadImageUrl = x.HeadImageUrl, Height = x.Height, Weight = x.Weight, Role = "用户" + (x != null ? "/教师" : ""), UserName = x.UserName, User_Id = x.User_Id, UserTrueName = x.UserTrueName }); if (!string.IsNullOrEmpty(paramDto.UserTrueName)) { query = query.Where(x => x.UserTrueName.Contains(paramDto.UserTrueName)); } // 按性别筛选 if (paramDto.Gender.HasValue) { query = query.Where(x => x.Gender == paramDto.Gender); } res.Total = await query.CountAsync(); var list = await query.OrderByDescending(x => x.CreateDate) .Skip((paramDto.PageIndex - 1) * paramDto.PageSize) .Take(paramDto.PageSize) .ToListAsync(); res.Datas = list; return res; } /// /// 获取智慧体育用户管理页面数据(员工数据) /// /// /// public async Task> GetSmartSportsUserPageList(SmartSportsUserParam paramDto) { var res = new PageDataDto(); var query = _userContext.Users.Select(x => new SmartSportsUserDto { User_Id = x.User_Id, UserName = x.UserName ?? "", UserTrueName = x.UserTrueName ?? "", HeadImageUrl = x.HeadImageUrl ?? "", BirthDate = x.BirthDate ?? "", Gender = x.Gender, Address = x.Address ?? "", Height = x.Height.HasValue ? x.Height.Value.ToString() : "", Weight = x.Weight.HasValue ? x.Weight.Value.ToString() : "", Role = "员工", // 微信小程序用户统一标记为员工 CreateDate = x.CreateDate, GradeId = null, // 员工数据没有年级概念 GradeName = "" // 员工数据没有年级概念 }); // 按姓名筛选 if (!string.IsNullOrEmpty(paramDto.UserTrueName)) { query = query.Where(x => x.UserTrueName.Contains(paramDto.UserTrueName)); } // 按性别筛选 if (paramDto.Gender.HasValue) { query = query.Where(x => x.Gender == paramDto.Gender); } // 注意:员工数据没有年级概念,所以忽略 GradeId 筛选 res.Total = await query.CountAsync(); var list = await query.OrderByDescending(x => x.CreateDate) .Skip((paramDto.PageIndex - 1) * paramDto.PageSize) .Take(paramDto.PageSize) .ToListAsync(); res.Datas = list; return res; } } }