341 lines
12 KiB
C#
341 lines
12 KiB
C#
using AutoMapper;
|
|
using EFCore.BulkExtensions;
|
|
using Google.Protobuf;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VOL.Business.IRepositories;
|
|
using VOL.Business.IRepositories.Stadium;
|
|
using VOL.Business.IServices;
|
|
using VOL.Business.Repositories;
|
|
using VOL.Core.CacheManager;
|
|
using VOL.Core.Extensions;
|
|
using VOL.Core.Extensions.AutofacManager;
|
|
using VOL.Core.Utilities;
|
|
using VOL.Entity.DomainModels;
|
|
using VOL.Entity.DomainModels.YD;
|
|
using VOL.Model;
|
|
using VOL.Model.Ai;
|
|
using VOL.Model.Stadium;
|
|
using VOL.System.Repositories;
|
|
using static Dapper.SqlMapper;
|
|
|
|
namespace VOL.Business.Services.Stadium
|
|
{
|
|
/// <summary>
|
|
/// 场馆
|
|
/// </summary>
|
|
public class StadiumService : IStadiumService, IDependency
|
|
{
|
|
#region 初始化
|
|
private readonly IMapper _mapper;
|
|
private readonly ICacheService _cacheService;
|
|
private readonly IStadiumRepository _stadiumRepository;
|
|
private readonly IStadiumResourceRepository _stadiumResourceRepository;
|
|
|
|
[ActivatorUtilitiesConstructor]
|
|
public StadiumService(IMapper mapper,
|
|
ICacheService cacheService,
|
|
IStadiumRepository stadiumRepository,
|
|
IStadiumResourceRepository stadiumResourceRepository)
|
|
{
|
|
_mapper = mapper;
|
|
_cacheService = cacheService;
|
|
_stadiumRepository = stadiumRepository;
|
|
_stadiumResourceRepository = stadiumResourceRepository;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 场馆列表
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public async Task<PageDataDto<StadiumListDto>> StadiumList(StadiumListVo dto)
|
|
{
|
|
var res = new PageDataDto<StadiumListDto>();
|
|
|
|
var query = from s in _stadiumRepository.FindAsIQueryable(x => true)
|
|
select new StadiumListDto()
|
|
{
|
|
Id = s.Id,
|
|
StadiumCode = s.StadiumCode,
|
|
UnitType = s.UnitType,
|
|
Status = s.Status,
|
|
Address = s.Address,
|
|
StadiumName = s.StadiumName,
|
|
PhoneNo = s.PhoneNo,
|
|
CreateDate = s.CreateDate
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(dto.StadiumName))
|
|
{
|
|
query = query.Where(x => x.StadiumName.Contains(dto.StadiumName));
|
|
}
|
|
|
|
res.Total = await query.CountAsync();
|
|
|
|
var list = await query
|
|
.Skip((dto.PageIndex - 1) * dto.PageSize)
|
|
.Take(dto.PageSize)
|
|
.OrderBy(x => x.Id)
|
|
.ToListAsync();
|
|
res.Datas = list;
|
|
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 场馆详情
|
|
/// </summary>
|
|
/// <param name="stadiumId"></param>
|
|
/// <returns></returns>
|
|
public async Task<StadiumDetailsDto> StadiumDetails(int stadiumId)
|
|
{
|
|
var res = await _stadiumRepository.FindAsIQueryable(x => x.Id == stadiumId).Select(x => new StadiumDetailsDto()
|
|
{
|
|
Id = x.Id,
|
|
Address = x.Address,
|
|
CoverImage = x.CoverImage,
|
|
Intro = x.Intro,
|
|
Latitude = x.Latitude,
|
|
Longitude = x.Longitude,
|
|
PhoneNo = x.PhoneNo,
|
|
StadiumCode = x.StadiumCode,
|
|
UnitType = x.UnitType,
|
|
Status = x.Status,
|
|
StadiumName = x.StadiumName,
|
|
CreateDate = x.CreateDate,
|
|
Province = x.Province,
|
|
City = x.City,
|
|
Area = x.Area,
|
|
StadiumResourceList = x.StadiumResourceList.Select(x => new StadiumResourceDto()
|
|
{
|
|
Id = x.Id,
|
|
Type = x.Type,
|
|
Url = x.Url
|
|
}).ToList()
|
|
}).FirstOrDefaultAsync();
|
|
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增场馆
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public async Task Add(StadiumDetailsDto dto)
|
|
{
|
|
var entity = _mapper.Map<Y_Stadium>(dto);
|
|
|
|
//var maxId = await _stadiumRepository.FindAsIQueryable(x => true).Select(x => x.StadiumCode).MaxAsync();
|
|
//string nextSchoolCode = Tool.IncrementId(maxId);
|
|
|
|
entity.StadiumCode = Guid.NewGuid().ToString();
|
|
entity.Status = 1;
|
|
|
|
//var stadList = _mapper.Map<List<Y_StadiumResource>>(dto.StadiumResourceList);
|
|
//entity.StadiumResourceList = null;
|
|
|
|
await _stadiumRepository.AddAsync(entity);
|
|
await _stadiumRepository.SaveChangesAsync();
|
|
|
|
//if (stadList != null)
|
|
//{
|
|
// foreach (var item in stadList)
|
|
// {
|
|
// item.StadiumId = entity.Id;
|
|
// // 添加资源项而不是更新主实体
|
|
// _stadiumRepository.Update<Y_StadiumResource>(item);
|
|
// }
|
|
// await _stadiumRepository.SaveChangesAsync();
|
|
//}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新场馆
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public async Task Update(StadiumDetailsDto dto)
|
|
{
|
|
var entity = await _stadiumRepository
|
|
.Include(x => x.StadiumResourceList)
|
|
.FirstOrDefaultAsync(x => x.Id == dto.Id);
|
|
|
|
if (entity == null)
|
|
throw new Exception("未找到需要更新的场馆信息");
|
|
|
|
// 更新场馆基本信息
|
|
entity.StadiumName = dto.StadiumName;
|
|
entity.Province = dto.Province;
|
|
entity.City = dto.City;
|
|
entity.Area = dto.Area;
|
|
entity.PhoneNo = dto.PhoneNo;
|
|
entity.CoverImage = dto.CoverImage;
|
|
entity.Intro = dto.Intro;
|
|
entity.Address = dto.Address;
|
|
entity.UnitType = dto.UnitType;
|
|
entity.Latitude = dto.Latitude;
|
|
entity.Longitude = dto.Longitude;
|
|
|
|
var existingResources = entity.StadiumResourceList.ToList();
|
|
var newResources = _mapper.Map<List<Y_StadiumResource>>(dto.StadiumResourceList);
|
|
|
|
// 处理需要删除的资源
|
|
var toDelete = existingResources.Where(er =>
|
|
!newResources.Any(nr => nr.Id == er.Id)).ToList();
|
|
|
|
foreach (var resource in toDelete)
|
|
{
|
|
if (!string.IsNullOrEmpty(resource.Url))
|
|
{
|
|
ALiYunOss.DeleteFilesByPrefix(resource.Url);
|
|
}
|
|
_stadiumResourceRepository.Delete(resource);
|
|
}
|
|
|
|
// 批量处理新增和更新
|
|
var resourcesToAdd = new List<Y_StadiumResource>();
|
|
|
|
foreach (var newResource in newResources)
|
|
{
|
|
var existing = existingResources.FirstOrDefault(er => er.Id == newResource.Id);
|
|
newResource.StadiumId = dto.Id;
|
|
if (existing != null)
|
|
{
|
|
_mapper.Map(newResource, existing);
|
|
_stadiumResourceRepository.Update(existing);
|
|
}
|
|
else
|
|
{
|
|
resourcesToAdd.Add(newResource);
|
|
}
|
|
}
|
|
|
|
// 批量添加新资源
|
|
if (resourcesToAdd.Any())
|
|
{
|
|
await _stadiumResourceRepository.AddRangeAsync(resourcesToAdd);
|
|
}
|
|
|
|
_stadiumRepository.Update(entity);
|
|
// 一次性保存所有变更
|
|
await _stadiumRepository.SaveChangesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新场馆状态
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
|
|
public async Task UpdateStatus(StadiumDetailsDto dto)
|
|
{
|
|
var entity = await _stadiumRepository.FindAsyncFirst(x => x.Id == dto.Id);
|
|
|
|
if (entity == null)
|
|
throw new Exception("未找到需要更新的场馆信息");
|
|
|
|
entity.Status = dto.Status;
|
|
_stadiumRepository.Update(entity);
|
|
await _stadiumRepository.SaveChangesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除场馆
|
|
/// </summary>
|
|
/// <param name="stadiumId"></param>
|
|
/// <returns></returns>
|
|
public async Task Delete(int stadiumId)
|
|
{
|
|
var entity = await _stadiumRepository.FindAsyncFirst(x => x.Id == stadiumId);
|
|
if (entity == null)
|
|
throw new Exception("未找到需要删除的场馆信息");
|
|
|
|
_stadiumRepository.Delete(entity);
|
|
await _stadiumRepository.SaveChangesAsync();
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 场馆预约列表
|
|
/// </summary>
|
|
/// <param name="dto"></param>
|
|
/// <returns></returns>
|
|
public async Task<PageDataDto<StadiumVisitingDto>> StadiumVisitingList(StadiumVisitingVo dto)
|
|
{
|
|
var res = new PageDataDto<StadiumVisitingDto>();
|
|
|
|
var query = from s in _stadiumRepository.DbContext.Set<Y_StadiumVisiting>().Where(x => x.StadiumId == dto.StadiumId)
|
|
select new StadiumVisitingDto()
|
|
{
|
|
Id = s.Id,
|
|
StadiumId = s.StadiumId,
|
|
PhoneNo = s.PhoneNo,
|
|
Remarks = s.Remarks,
|
|
Status = s.Status,
|
|
UpdateTime = s.UpdateTime,
|
|
UserName = s.UserName,
|
|
VisitingTime = s.VisitingTime
|
|
};
|
|
|
|
if (!string.IsNullOrWhiteSpace(dto.PhoneNo))
|
|
{
|
|
query = query.Where(x => x.PhoneNo.Contains(dto.PhoneNo));
|
|
}
|
|
|
|
res.Total = await query.CountAsync();
|
|
|
|
var list = await query
|
|
.Skip((dto.PageIndex - 1) * dto.PageSize)
|
|
.Take(dto.PageSize)
|
|
.OrderBy(x => x.Id)
|
|
.ToListAsync();
|
|
res.Datas = list;
|
|
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 上传图片/视频
|
|
/// </summary>
|
|
public Y_StadiumResource UploadImage(IFormFile file)
|
|
{
|
|
var timeStampString = DateTime.Now.ToUnixTimeStamp();
|
|
var prefix = $"Upload/Stadium/";
|
|
// 删除文件
|
|
//ALiYunOss.DeleteFilesByPrefix(prefix);
|
|
// 上传文件
|
|
var url = ALiYunOss.Upload(file, prefix, timeStampString);
|
|
var extension = Path.GetExtension(file.FileName).ToLower();
|
|
|
|
return new Y_StadiumResource()
|
|
{
|
|
Type = new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }.Contains(extension) ? 1 :
|
|
new[] { ".mp4", ".avi", ".mov", ".mkv", ".wmv" }.Contains(extension) ? 2 :
|
|
0,
|
|
Url = url
|
|
};
|
|
|
|
//var entity = new Y_StadiumResource();
|
|
//entity.Url = url; var extension = Path.GetExtension(file.FileName).ToLower();
|
|
//entity.Type =
|
|
// new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" }.Contains(extension) ? 1 :
|
|
// new[] { ".mp4", ".avi", ".mov", ".mkv", ".wmv" }.Contains(extension) ? 2 :
|
|
// 0;
|
|
//var stadiumId = await _stadiumRepository.FindAsIQueryable(c => c.Id == c.Id).Select(c => c.Id).FirstOrDefaultAsync();
|
|
//entity.StadiumId = stadiumId;
|
|
//_stadiumRepository.Add<Y_StadiumResource>(entity);
|
|
//_stadiumRepository.SaveChanges();
|
|
//return entity;
|
|
}
|
|
}
|
|
}
|