377 lines
12 KiB
C#
377 lines
12 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.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<List<PersonalGoalInfoDto>> GetPersonalGoalInfo(PersonalGoalInfoReqDto req)
|
||
|
{
|
||
|
var pgList = await (
|
||
|
from pg in _userContext.PersonalGoal
|
||
|
join pgr in _userContext.PersonalGoalResult on pg.Id equals pgr.PersonalGoalId
|
||
|
where pg.UserId == req.UserId && pg.GoalDate.Month == req.Month.Month && pg.GoalDate.Year == req.Month.Year
|
||
|
select new PersonalGoalInfoDto()
|
||
|
{
|
||
|
GoalDuration = pg.GoalDuration,
|
||
|
GoalAmount = pg.GoalAmount,
|
||
|
GoalDate = pg.GoalDate,
|
||
|
Amount = pgr.Amount,
|
||
|
IsFinish = pg.GoalAmount > pgr.Amount
|
||
|
}).ToListAsync();
|
||
|
|
||
|
return pgList;
|
||
|
}
|
||
|
|
||
|
/// <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;
|
||
|
}
|
||
|
}
|
||
|
}
|