2025-07-21 10:20:26 +08:00

273 lines
9.4 KiB
C#

using Autofac.Core;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Drawing;
using YD_AllHeartRates.Api.Context;
using YD_AllHeartRates.Api.Services.Interface;
using YD_AllHeartRates.Api.SmartSportsEntitys;
using YD_AllHeartRates.Api.Utilities;
using YD_AllHeartRates.Commons.Dto;
using YD_AllHeartRates.Commons.Dto.Device;
using YD_AllHeartRates.Commons.Dto.LargeScreen;
namespace YD_AllHeartRates.Api.Services.Impl
{
/// <summary>
/// 服务实现
/// </summary>
public class DeviceService : IDeviceService
{
public SmartSportsContext _sportsContext;
public UserContext _userContext;
private readonly LoginContext _loginContext;
private string schoolCode;
/// <summary>
/// 构造
/// </summary>
public DeviceService(SmartSportsContext sportsContext, UserContext userContext, LoginContext loginContext)
{
_sportsContext = sportsContext;
_userContext = userContext;
_loginContext = loginContext;
schoolCode = _loginContext.SchoolCode;
}
/// <summary>
/// 获取设备列表
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<PageDataDto<DevicePageListDto>> DevicePageList([FromQuery] DevicePageDto dto)
{
var res = new PageDataDto<DevicePageListDto>();
var query = from d in _sportsContext.Device
where d.SchoolCode == schoolCode
join s in _sportsContext.Student on d.StudentNo equals s.StudentNo into ds
from s in ds.DefaultIfEmpty()
select new DevicePageListDto
{
Id = d.Id,
Code = d.Code,
DeviceType = d.DeviceType,
StudentNo = d.StudentNo,
StudentName = s != null ? s.StudentName : null,
IsBind = !string.IsNullOrWhiteSpace(d.StudentNo)
};
if (!string.IsNullOrWhiteSpace(dto.Code))
{
query = query.Where(x => x.Code.Contains(dto.Code));
}
if (dto.DeviceType > 0)
{
query = query.Where(x => x.DeviceType == dto.DeviceType);
}
if (dto.BindStatus > 0)
{
query = dto.BindStatus == 1 ? query.Where(x => !string.IsNullOrWhiteSpace(x.StudentNo)) : query.Where(x => string.IsNullOrWhiteSpace(x.StudentNo));
}
if (!string.IsNullOrWhiteSpace(dto.StudentNo))
{
query = query.Where(x => x.StudentNo.Contains(dto.StudentNo));
}
if (!string.IsNullOrWhiteSpace(dto.StudentName))
{
query = query.Where(x => x.StudentName.Contains(dto.StudentName));
}
res.Total = await query.CountAsync();
var list = await query.OrderByDescending(x => x.Id)
.Skip((dto.PageIndex - 1) * dto.PageSize)
.Take(dto.PageSize)
.ToListAsync();
res.Datas = list;
return res;
}
/// <summary>
/// 设备数量
/// </summary>
/// <returns></returns>
public async Task<Dictionary<string, DeviceNumberDto>> DeviceNumber()
{
// 查询当前学校的设备列表
var deviceList = await _sportsContext.Device
.Where(x => x.SchoolCode == schoolCode)
.Select(x => new { x.StudentNo, x.DeviceType })
.ToListAsync();
// 设备类型名称映射,可扩展更多设备类型
var deviceTypeNames = new Dictionary<int, string>
{
{ 1, "手环设备" },
{ 2, "跳绳设备" }
};
// 分组统计每种设备类型的在线/离线数量
var grouped = deviceList
.GroupBy(x => x.DeviceType)
.ToDictionary(
g => g.Key,
g => new DeviceNumberDto
{
OnLineCount = g.Count(x => !string.IsNullOrWhiteSpace(x.StudentNo)),
OffLineCount = g.Count(x => string.IsNullOrWhiteSpace(x.StudentNo))
});
// 构造返回结果字典
var result = new Dictionary<string, DeviceNumberDto>();
foreach (var kvp in deviceTypeNames)
{
result[kvp.Value] = grouped.GetValueOrDefault(kvp.Key, new DeviceNumberDto());
}
return result;
}
/// <summary>
/// 新增设备
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<bool> AddDevice([FromBody] AddDeviceDto dto)
{
if (!string.IsNullOrWhiteSpace(dto.StudentNo))
{
var student = await _sportsContext.Student.Where(x => x.StudentNo == dto.StudentNo).FirstOrDefaultAsync();
if (student != null)
throw new Exception($"学号:{dto.StudentNo}的学生已存在!");
}
var device = await _sportsContext.Device.Where(x => x.Code == dto.Code).FirstOrDefaultAsync();
if (device != null)
throw new Exception($"设备编号:{dto.Code}的已存在!");
var entity = new S_Device()
{
Code = dto.Code,
SchoolCode = schoolCode,
DeviceType = dto.DeviceType,
StudentNo = dto.StudentNo,
Name = dto.DeviceType == 1 ? "心率" : "跳绳"
};
await _sportsContext.AddAsync(entity);
return await _sportsContext.SaveChangesAsync() > 0;
}
/// <summary>
/// 编辑设备
/// </summary>
/// <param name="dto"></param>
/// <returns></returns>
public async Task<bool> UpdateDevice([FromBody] AddDeviceDto dto)
{
var device = await _sportsContext.Device.Where(x => x.Code == dto.Code).FirstOrDefaultAsync();
if (device == null)
throw new Exception("更新的设备不存在");
if (!string.IsNullOrWhiteSpace(dto.StudentNo))
{
var student = await _sportsContext.Student.Where(x => x.StudentNo == dto.StudentNo).FirstOrDefaultAsync();
if (student == null)
throw new Exception($"学号:{dto.StudentNo}的学生不存在!");
}
device.StudentNo = dto.StudentNo;
device.DeviceType = dto.DeviceType;
device.Name = dto.DeviceType == 1 ? "心率设备" : "跳绳设备";
_sportsContext.Update(device);
return await _sportsContext.SaveChangesAsync() > 0;
}
/// <summary>
/// 删除设备
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public async Task<bool> DeleteDevice(string code)
{
var device = await _sportsContext.Device.Where(x => x.Code == code).FirstOrDefaultAsync();
if (device == null)
throw new Exception("更新的设备不存在");
_sportsContext.Device.Remove(device);
return await _sportsContext.SaveChangesAsync() > 0;
}
/// <summary>
/// 批量导入
/// </summary>
/// <param name="file"></param>
/// <returns></returns>
public async Task<bool> ImportDevice(IFormFile file)
{
if (file == null || file.Length <= 0)
throw new Exception("操作失败");
var extension = Path.GetExtension(file.FileName);
if (string.IsNullOrEmpty(extension) || !extension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
throw new Exception("文件格式不正确,仅支持 .xlsx 格式");
var dataObjects = new List<ImportDeviceDto>();
var deviceList = new List<S_Device>();
using (var fileStream = file.OpenReadStream())
{
dataObjects = Tool.ConvertExcelToList<ImportDeviceDto>(fileStream);
}
foreach (var data in dataObjects)
{
if (string.IsNullOrWhiteSpace(data.Code))
{
throw new Exception("设备编号不能为空");
}
if (data.DeviceType == null || data.DeviceType == 0)
{
throw new Exception("设备类型不能为空");
}
if (string.IsNullOrWhiteSpace(data.StudentNo))
{
throw new Exception("学生学号不能为空");
}
deviceList.Add(new S_Device()
{
SchoolCode = schoolCode,
Code = data.Code,
StudentNo = data.StudentNo,
DeviceType = data.DeviceType,
Name = data.DeviceType == 1 ? "心率设备" : "跳绳设备"
});
}
await _sportsContext.AddRangeAsync(deviceList);
return await _sportsContext.SaveChangesAsync() > 0;
}
}
}