520 lines
18 KiB
C#

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;
}
/// <summary>
/// 获取打卡详情
/// </summary>
/// <returns></returns>
public async Task<PageDataDto<PersonalGoalInfoDto>> 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<PersonalGoalInfoDto>
{
Total = total,
Datas = pgList
};
}
/// <summary>
/// 获取用户训练记录
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
public async Task<PageDataDto<UserTrainingRecordsDto>> 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<UserTrainingRecordsDto>
{
Total = total,
Datas = records
};
}
/// <summary>
/// 获取资源类型树
/// </summary>
/// <returns>资源类型树形结构</returns>
public async Task<List<B_ResourceTypeTreeDto>> GetResourceTypeTree()
{
// 获取所有资源类型
var allTypes = await _userContext.ResourceType.ToListAsync();
// 构建树形结构
var rootTypes = allTypes.Where(t => t.ParentId == 0).ToList();
var result = new List<B_ResourceTypeTreeDto>();
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;
}
/// <summary>
/// 递归构建资源类型树
/// </summary>
private void BuildTypeTree(B_ResourceTypeTreeDto parentDto, List<WCA_ResourceType> 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);
}
}
/// <summary>
/// 获取资源列表
/// </summary>
/// <param name="req">请求参数</param>
/// <returns>资源列表</returns>
public async Task<PageDataDto<B_ResourceDetailsDto>> 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<B_ResourceDetailsDto>
{
Total = total,
Datas = list
};
}
/// <summary>
/// 添加或更新资源类型
/// </summary>
/// <param name="req">资源类型信息</param>
/// <returns>操作结果</returns>
public async Task<bool> 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;
}
}
/// <summary>
/// 删除资源类型
/// </summary>
/// <param name="id">资源类型ID</param>
/// <returns>操作结果</returns>
public async Task<bool> 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;
}
}
/// <summary>
/// 添加或更新资源
/// </summary>
/// <param name="req">资源信息</param>
/// <returns>操作结果</returns>
public async Task<bool> 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;
}
}
/// <summary>
/// 删除资源
/// </summary>
/// <param name="id">资源ID</param>
/// <returns>操作结果</returns>
public async Task<bool> 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;
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public async Task<string> 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;
}
/// <summary>
/// 获取微信用户
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<PageDataDto<UserPageListDto>> GetUserPageList(UserPageListParam paramDto)
{
var res = new PageDataDto<UserPageListDto>();
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;
}
/// <summary>
/// 获取智慧体育用户管理页面数据(员工数据)
/// </summary>
/// <param name="paramDto"></param>
/// <returns></returns>
public async Task<PageDataDto<SmartSportsUserDto>> GetSmartSportsUserPageList(SmartSportsUserParam paramDto)
{
var res = new PageDataDto<SmartSportsUserDto>();
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;
}
}
}