using AutoMapper;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.SQ;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Services;
using CoreCms.Net.Services.SQ;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoreCms.Net.Utility.Extensions;
using ToolGood.Words.internals;
using NPOI.SS.Formula.PTG;
using NPOI.OpenXmlFormats.Dml;
using Humanizer;
using Newtonsoft.Json;
namespace CoreCms.Net.Web.WebApi.Controllers;
///
/// 预约接口
///
[Route("api/[controller]/[action]")]
[ApiController]
public class SQController : ControllerBase
{
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ISQReservationsServices _SQReservationsServices;
private readonly ISQRoomsServices _SQRoomsServices;
private readonly ISysDictionaryDataServices _sysDictionaryDataServices;
private readonly ISysDictionaryServices _sysDictionaryServices;
private readonly ISQReservationParticipantsServices _SQReservationParticipantsServices;
private readonly IMapper _mapper;
private readonly ICoreCmsUserServices _userServices;
private readonly IHttpContextUser _user;
private readonly SqlSugarScope _dbBase;
private readonly ICoreCmsUserBlacklistServices _coreCmsUserBlacklistServices;
private readonly ISQReservationReputationServices _sQReservationReputationServices;
private readonly ISQReservationEvaluateServices _sQReservationEvaluateServices;
private readonly ISQRoomUnavailableTimesServices _sQRoomUnavailableTimesServices;
///
/// 构造函数
///
public SQController(IWebHostEnvironment webHostEnvironment
, ISQReservationsServices SQReservationsServices
, ISQRoomsServices SQRoomsServices
, ISysDictionaryServices sysDictionaryServices
, ISysDictionaryDataServices sysDictionaryDataServices
, ISQReservationParticipantsServices sQReservationParticipantsServices
, IMapper mapper
, ICoreCmsUserServices userServices
, IHttpContextUser user
, IUnitOfWork unitOfWork
, ICoreCmsUserBlacklistServices coreCmsUserBlacklistServices
, ISQReservationEvaluateServices sQReservationEvaluateServices
, ISQReservationReputationServices sQReservationReputationServices
, ISQRoomUnavailableTimesServices sQRoomUnavailableTimesServices
)
{
_webHostEnvironment = webHostEnvironment;
_SQReservationsServices = SQReservationsServices;
_SQRoomsServices = SQRoomsServices;
_sysDictionaryServices = sysDictionaryServices;
_sysDictionaryDataServices = sysDictionaryDataServices;
_SQReservationParticipantsServices = sQReservationParticipantsServices;
_mapper = mapper;
_userServices = userServices;
_user = user;
_dbBase = unitOfWork.GetDbClient();
_coreCmsUserBlacklistServices = coreCmsUserBlacklistServices;
_sQReservationEvaluateServices = sQReservationEvaluateServices;
_sQReservationReputationServices = sQReservationReputationServices;
_sQRoomUnavailableTimesServices = sQRoomUnavailableTimesServices;
}
///
/// 我的预约记录
///
/// 0 参与者,1发起者
/// 起始页
/// 页大小
///
[HttpGet]
[Authorize]
public async Task GetMyReservation([FromQuery] int type = 0, [FromQuery] int index = 1, [FromQuery] int size = 10)
{
var userId = _user.ID;
var list = await _dbBase.Ado.SqlQueryAsync($"select r.*,p.id pid, p.status pstatus,p.join_time, p.quit_time ,p.is_refund from SQReservationParticipants p inner join SQReservations r on p.reservation_id=r.id where p.user_id={userId} and p.role={type} order by p.id desc OFFSET {(index - 1) * size} ROWS FETCH NEXT {size} ROWS ONLY ");
if (list != null && list.Count > 0)
{
var roomList = await _SQRoomsServices.GetRoomList();
// 转为基类列表
var baseList = list.Cast().ToList();
await baseList.LoadSQReservationParticipantsApiDto(userId, _dbBase, _coreCmsUserBlacklistServices, roomList, _mapper);
}
return new WebApiDto()
{
Data = list,
Code = 0,
};
}
///
/// 获取正在进行的预约
///
///
[HttpGet]
[Authorize]
public async Task GetMyUseReservation()
{
var userId = _user.ID;
var list = await _dbBase.Ado.SqlQueryAsync($"SELECT r.*, p.role AS Role, p.is_arrive, CASE WHEN r.status = 1 THEN 0 WHEN r.status = 2 THEN 1 WHEN r.status = 0 THEN 2 WHEN r.status = 3 THEN 3 END AS orderid FROM SQReservations r LEFT JOIN SQReservationParticipants p ON r.id = p.reservation_id WHERE r.status < 4 AND p.user_id = {userId} AND p.status = 0 AND DATEADD(day,2, end_time) > GETDATE() ORDER BY orderid asc, r.start_time Desc ");
if (list != null && list.Count > 0)
{
var roomList = await _SQRoomsServices.GetRoomList();
// 转为基类列表
var baseList = list.Cast().ToList();
await baseList.LoadSQReservationParticipantsApiDto(userId, _dbBase, _coreCmsUserBlacklistServices, roomList, _mapper);
}
return new WebApiDto()
{
Data = list,
Code = 0,
};
}
///
/// 首页预约列表
///
///
///
///
[HttpGet]
public async Task GetReservationList([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20)
{
var userId = _user.ID;
var now = DateTime.Now;
var roomList = await _SQRoomsServices.GetRoomList();
var where = PredicateBuilder.True();
where = where.And(it => it.end_time > now);
where = where.And(it => it.status < 3);
//
List userBlacklist = new List();
if (userId > 0)
{
//_sQRoomUnavailableTimesServices.get
userBlacklist = await _coreCmsUserBlacklistServices.GetUserBlacklists(userId);
if (userBlacklist.Count > 0)
{
string sqlWhere = string.Join(",", userBlacklist);
var participants = await _dbBase.Ado.SqlQueryAsync($"select sq.id from SQReservations sq left join SQReservationParticipants p on sq.id=p.reservation_id and p.role=1 where sq.status<3 and p.user_id in ({sqlWhere})");
if (participants.Count > 0)
{
where = where.And(it => !participants.Contains(it.id));
}
}
}
var list = await _SQReservationsServices.QueryPageAsync(where, it => it.start_time, OrderByType.Asc, pageIndex, pageSize, true);
var pageList = _mapper.Map>(list);
if (pageList != null && pageList.Count > 0)
{
// 转为基类列表
var baseList = pageList.Cast().ToList();
await baseList.LoadSQReservationParticipantsApiDto(userId, _dbBase, _coreCmsUserBlacklistServices, roomList, _mapper);
}
return new WebApiDto()
{
Data = pageList,
Code = 0,
Msg = "ok",
};
}
///
/// 根据预约ID获取详情(结构同首页预约列表单项)
///
/// 预约ID
///
[HttpGet]
public async Task GetReservationDetail([FromQuery] int id)
{
var userId = _user.ID;
var model = await _SQReservationsServices.QueryByIdAsync(id);
if (model == null)
{
return new WebApiDto
{
Code = -1,
Msg = "预约不存在",
};
}
var dto = _mapper.Map(model);
var roomList = await _SQRoomsServices.GetRoomList();
var baseList = new List { dto };
await baseList.LoadSQReservationParticipantsApiDto(userId, _dbBase, _coreCmsUserBlacklistServices, roomList, _mapper);
return new WebApiDto
{
Data = dto,
Code = 0,
Msg = "ok",
};
}
///
/// 获取预约评价
///
///
///
[HttpGet]
[Authorize]
public async Task GetEvaluateServices([FromQuery] int reId)
{
var userId = _user.ID;
var list = await _sQReservationEvaluateServices.QueryListByClauseAsync(it => it.reservation_id == reId && it.user_id == userId);
var participants = await _dbBase.Ado.SqlQueryAsync($"select p.id,p.reservation_id,p.status,p.join_time,p.user_id, p.role,u.nickName UserName,u.avatarImage AvatarImage,ISNULL(u.play_level,4) play_level,ISNULL(u.skills_level,4) skills_level from SQReservationParticipants p inner join CoreCmsUser u on p.user_id=u.id where p.status=0 and p.reservation_id ={reId} and p.is_arrive=1 ");
if (participants == null || participants.Count == 0)
{
return new WebApiDto()
{
Code = 0,
Data = null
};
}
var userBlack = await _coreCmsUserBlacklistServices.GetUserBlacklists(userId);
List