244 lines
10 KiB
C#
244 lines
10 KiB
C#
using Microsoft.AspNetCore.Http;
|
|
using YD_Event.Application.Event.Dtos;
|
|
using YD_Event.Core.Entities;
|
|
using MiniExcelLibs;
|
|
using SqlSugar;
|
|
|
|
namespace YD_Event.Application.Event;
|
|
|
|
/// <summary>
|
|
/// 成绩与证书管理
|
|
/// </summary>
|
|
public class EventScoreService : BaseService<EventResult>, ITransient
|
|
{
|
|
private readonly ISqlSugarRepository<EventRegistrationPerson> _personRepository;
|
|
private readonly ISqlSugarRepository<EventResult> _resultRepository;
|
|
private readonly ISqlSugarRepository<EventCertificate> _certificateRepository;
|
|
private readonly ISqlSugarRepository<EventRegistration> _registrationRepository;
|
|
private readonly IIdGenerator _idGenerator;
|
|
|
|
public EventScoreService(
|
|
ISqlSugarRepository<EventRegistrationPerson> personRepository,
|
|
ISqlSugarRepository<EventResult> resultRepository,
|
|
ISqlSugarRepository<EventCertificate> certificateRepository,
|
|
ISqlSugarRepository<EventRegistration> registrationRepository,
|
|
IIdGenerator idGenerator) : base(resultRepository)
|
|
{
|
|
_personRepository = personRepository;
|
|
_resultRepository = resultRepository;
|
|
_certificateRepository = certificateRepository;
|
|
_registrationRepository = registrationRepository;
|
|
_idGenerator = idGenerator;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 运动员成绩列表
|
|
/// </summary>
|
|
[DisplayName("运动员成绩列表")]
|
|
public async Task<PageResult<ScoreListOutput>> PageAthlete(ScoreQueryInput input)
|
|
{
|
|
return await _personRepository.AsQueryable()
|
|
.LeftJoin<EventRegistration>((p, r) => p.RegistrationId == r.Id)
|
|
.LeftJoin<EventResult>((p, r, res) => p.Id == res.RegistrationPersonId)
|
|
.LeftJoin<EventCertificate>((p, r, res, c) => res.Id == c.ResultId)
|
|
.Where((p, r, res, c) => r.EventId == input.EventId && (p.PersonType == 1 || p.PersonType == 0))
|
|
.WhereIF(input.ProjectId.HasValue, p => p.ProjectId == input.ProjectId)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), p => p.GroupName == input.GroupName)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), p => p.Name.Contains(input.Name))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.UnitName), (p, r) => r.TeamName.Contains(input.UnitName))
|
|
.Select((p, r, res, c) => new ScoreListOutput
|
|
{
|
|
Id = p.Id,
|
|
Name = p.Name,
|
|
UnitName = r.TeamName,
|
|
Gender = p.Gender.ToString(),
|
|
Score = res.Score,
|
|
Rank = res.Rank,
|
|
ResultId = res.Id,
|
|
CertificateId = c.Id
|
|
})
|
|
.ToPagedListAsync(input);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 教练/领队列表
|
|
/// </summary>
|
|
[DisplayName("教练领队列表")]
|
|
public async Task<PageResult<CoachLeaderListOutput>> PageCoachLeader(CoachLeaderQueryInput input)
|
|
{
|
|
return await _personRepository.AsQueryable()
|
|
.LeftJoin<EventCertificate>((p, c) => p.Id == c.ParticipationRecordId)
|
|
.InnerJoin<EventRegistration>((p, c, r) => p.RegistrationId == r.Id)
|
|
.Where((p, c, r) => r.EventId == input.EventId && p.PersonType == input.PersonType)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), p => p.Name.Contains(input.Name))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Phone), p => p.Phone.Contains(input.Phone))
|
|
.Select((p, c) => new CoachLeaderListOutput
|
|
{
|
|
Id = p.Id,
|
|
Name = p.Name,
|
|
Age = p.Age,
|
|
Gender = p.Gender.ToString(),
|
|
IdCard = p.IdCard,
|
|
Phone = p.Phone,
|
|
CertificateId = c.Id
|
|
})
|
|
.ToPagedListAsync(input);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导入成绩
|
|
/// </summary>
|
|
[UnitOfWork]
|
|
[DisplayName("导入成绩")]
|
|
public async Task ImportScore(long eventId, IFormFile file)
|
|
{
|
|
using var stream = file.OpenReadStream();
|
|
var rows = stream.Query<ScoreImportDto>();
|
|
|
|
foreach(var row in rows)
|
|
{
|
|
var person = await _personRepository.AsQueryable()
|
|
.InnerJoin<EventRegistration>((p, r) => p.RegistrationId == r.Id)
|
|
.Where((p, r) => r.EventId == eventId && p.Name == row.Name && p.ProjectName == row.ProjectName && p.GroupName == row.GroupName)
|
|
.FirstAsync();
|
|
|
|
if (person == null) continue;
|
|
|
|
var result = await _resultRepository.GetFirstAsync(x => x.RegistrationPersonId == person.Id);
|
|
if (result == null)
|
|
{
|
|
result = new EventResult
|
|
{
|
|
Id = _idGenerator.NewLong(),
|
|
EventId = eventId,
|
|
RegistrationPersonId = person.Id,
|
|
RegistrationId = person.RegistrationId,
|
|
ProjectId = person.ProjectId,
|
|
ParticipantName = person.Name,
|
|
GroupName = person.GroupName,
|
|
CreatedTime = DateTime.Now
|
|
};
|
|
await _resultRepository.InsertAsync(result);
|
|
}
|
|
|
|
result.Score = row.Score;
|
|
result.Rank = row.Rank;
|
|
result.Remark = row.RankText; // 存储名次文字
|
|
result.UpdatedTime = DateTime.Now;
|
|
|
|
await _resultRepository.UpdateAsync(result);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 发放证书
|
|
/// </summary>
|
|
[UnitOfWork]
|
|
[DisplayName("发放证书")]
|
|
public async Task IssueCertificate(IssueCertificateInput input)
|
|
{
|
|
foreach (var personId in input.PersonIds)
|
|
{
|
|
var person = await _personRepository.GetByIdAsync(personId);
|
|
if (person == null) continue;
|
|
|
|
if (person.PersonType == 1 || person.PersonType == 0)
|
|
{
|
|
var result = await _resultRepository.GetFirstAsync(x => x.RegistrationPersonId == person.Id);
|
|
if (result == null) continue;
|
|
|
|
var cert = await _certificateRepository.GetFirstAsync(x => x.ResultId == result.Id);
|
|
if (cert == null)
|
|
{
|
|
cert = new EventCertificate
|
|
{
|
|
Id = _idGenerator.NewLong(),
|
|
EventId = input.EventId,
|
|
ResultId = result.Id,
|
|
RegistrationId = person.RegistrationId,
|
|
ParticipationRecordId = person.Id,
|
|
ParticipantName = person.Name,
|
|
GroupName = person.GroupName,
|
|
CertificateStatus = 1,
|
|
GeneratedTime = DateTime.Now,
|
|
CertificateNo = $"C{DateTime.Now:yyyyMMdd}{person.Id}",
|
|
CreatedTime = DateTime.Now
|
|
};
|
|
await _certificateRepository.InsertAsync(cert);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var cert = await _certificateRepository.GetFirstAsync(x => x.ParticipationRecordId == person.Id);
|
|
if (cert == null)
|
|
{
|
|
cert = new EventCertificate
|
|
{
|
|
Id = _idGenerator.NewLong(),
|
|
EventId = input.EventId,
|
|
RegistrationId = person.RegistrationId,
|
|
ParticipationRecordId = person.Id,
|
|
ParticipantName = person.Name,
|
|
GroupName = person.GroupName,
|
|
CertificateStatus = 1,
|
|
GeneratedTime = DateTime.Now,
|
|
CertificateNo = $"CC{DateTime.Now:yyyyMMdd}{person.Id}",
|
|
CreatedTime = DateTime.Now
|
|
};
|
|
await _certificateRepository.InsertAsync(cert);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 下载模板
|
|
/// </summary>
|
|
public byte[] DownloadTemplate()
|
|
{
|
|
var list = new List<ScoreImportDto>
|
|
{
|
|
new ScoreImportDto { ProjectName = "30秒单摇", GroupName = "少儿组", Name = "崔一", UnitName = "上海跃动1", Score = "300", Rank = 1, RankText = "第一名" }
|
|
};
|
|
using var stream = new MemoryStream();
|
|
stream.SaveAs(list);
|
|
return stream.ToArray();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出证书
|
|
/// </summary>
|
|
[DisplayName("导出证书")]
|
|
public async Task<byte[]> ExportCertificate(ScoreQueryInput input)
|
|
{
|
|
var list = await _certificateRepository.AsQueryable()
|
|
.LeftJoin<EventResult>((c, r) => c.ResultId == r.Id)
|
|
.LeftJoin<EventRegistration>((c, r, reg) => c.RegistrationId == reg.Id)
|
|
.LeftJoin<YD_Event.Core.Entities.Project>((c, r, reg, p) => c.ProjectId == p.Id)
|
|
.Where((c, r, reg, p) => c.EventId == input.EventId)
|
|
.WhereIF(input.ProjectId.HasValue, (c, r, reg, p) => c.ProjectId == input.ProjectId)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.GroupName), (c, r, reg, p) => c.GroupName == input.GroupName)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), (c, r, reg, p) => c.ParticipantName.Contains(input.Name))
|
|
.Select((c, r, reg, p) => new CertificateExportDto
|
|
{
|
|
CertificateNo = c.CertificateNo,
|
|
ParticipantName = c.ParticipantName,
|
|
ProjectName = p.Name,
|
|
GroupName = c.GroupName,
|
|
UnitName = reg.TeamName,
|
|
Score = r.Score,
|
|
Rank = r.Rank,
|
|
CertificateType = c.CertificateType == 1 ? "冠军证书" :
|
|
c.CertificateType == 2 ? "亚军证书" :
|
|
c.CertificateType == 3 ? "季军证书" :
|
|
c.CertificateType == 4 ? "优胜证书" : "参与证书",
|
|
IssuedTime = c.IssuedTime
|
|
})
|
|
.ToListAsync();
|
|
|
|
using var stream = new MemoryStream();
|
|
stream.SaveAs(list);
|
|
return stream.ToArray();
|
|
}
|
|
}
|