/***********************************************************************
* Project: CoreCms
* ProjectName: 核心内容管理系统
* Web: https://www.corecms.net
* Author: 大灰灰
* Email: jianweie@163.com
* CreateTime: 2025/9/3 0:53:58
* Description: 暂无
***********************************************************************/
using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;
using CoreCms.Net.Web.Admin.Infrastructure;
using AutoMapper;
using System.Collections.Generic;
using CoreCms.Net.Model.ViewModels.SQ;
using Essensoft.Paylink.Alipay.Domain;
namespace CoreCms.Net.Web.Admin.Controllers
{
///
/// 预约记录表
///
[Description("预约记录表")]
[Route("api/[controller]/[action]")]
[ApiController]
[RequiredErrorForAdmin]
[Authorize]
public class SQReservationParticipantsController : ControllerBase
{
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ISQReservationParticipantsServices _SQReservationParticipantsServices;
private readonly IMapper _mapper;
private readonly ICoreCmsUserServices _userServices;
private readonly ISQReservationsServices _SQReservationsServices;
///
/// 构造函数
///
public SQReservationParticipantsController(IWebHostEnvironment webHostEnvironment
, ISQReservationParticipantsServices SQReservationParticipantsServices
, IMapper mapper
, ICoreCmsUserServices userServices
, ISQReservationsServices SQReservationsServices
)
{
_webHostEnvironment = webHostEnvironment;
_SQReservationParticipantsServices = SQReservationParticipantsServices;
_mapper = mapper;
_userServices = userServices;
_SQReservationsServices = SQReservationsServices;
}
#region 获取列表============================================================
// POST: Api/SQReservationParticipants/GetPageList
///
/// 获取列表
///
///
[HttpPost]
[Description("获取列表")]
public async Task GetPageList()
{
var jm = new AdminUiCallBack();
var pageCurrent = Request.Form["page"].FirstOrDefault().ObjectToInt(1);
var pageSize = Request.Form["limit"].FirstOrDefault().ObjectToInt(30);
var where = PredicateBuilder.True();
//获取排序字段
var orderField = Request.Form["orderField"].FirstOrDefault();
Expression> orderEx = orderField switch
{
"id" => p => p.id,
"reservation_id" => p => p.reservation_id,
"user_id" => p => p.user_id,
"role" => p => p.role,
"join_time" => p => p.join_time,
"quit_time" => p => p.quit_time,
"status" => p => p.status,
_ => p => p.id
};
//设置排序方式
var orderDirection = Request.Form["orderDirection"].FirstOrDefault();
var orderBy = orderDirection switch
{
"asc" => OrderByType.Asc,
"desc" => OrderByType.Desc,
_ => OrderByType.Desc
};
//查询筛选
//参与记录ID int
var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
if (id > 0)
{
where = where.And(p => p.id == id);
}
//预约ID int
var reservation_id = Request.Form["reservation_id"].FirstOrDefault().ObjectToInt(0);
if (reservation_id > 0)
{
where = where.And(p => p.reservation_id == reservation_id);
}
//参与者ID(关联用户表,暂时占位) int
var user_id = Request.Form["user_id"].FirstOrDefault().ObjectToInt(0);
if (user_id > 0)
{
where = where.And(p => p.user_id == user_id);
}
List userids = null;
//参与者ID(关联用户表,暂时占位) int
var user_name = Request.Form["user_name"].FirstOrDefault();
if (!string.IsNullOrEmpty(user_name))
{
var _userIds = await _userServices.QueryListByClauseAsync(it => it.userName.Contains(user_name));
if (_userIds == null || _userIds.Count == 0)
{
userids = new List();
}
else
{
userids = _userIds.Select(it => it.id).ToList();
}
//where = where.And(p => p.user_id == user_id);
}
if (userids != null)
{
where = where.And(p => userids.Contains(p.user_id));
}
//角色:0=参与者,1=发起者 int
var role = Request.Form["role"].FirstOrDefault().ObjectToInt(0);
if (role > 0)
{
where = where.And(p => p.role == role);
}
//user_name
//加入时间 datetime
var join_time = Request.Form["join_time"].FirstOrDefault();
if (!string.IsNullOrEmpty(join_time))
{
if (join_time.Contains("到"))
{
var dts = join_time.Split("到");
var dtStart = dts[0].Trim().ObjectToDate();
where = where.And(p => p.join_time > dtStart);
var dtEnd = dts[1].Trim().ObjectToDate();
where = where.And(p => p.join_time < dtEnd);
}
else
{
var dt = join_time.ObjectToDate();
where = where.And(p => p.join_time > dt);
}
}
//退出时间 datetime
var quit_time = Request.Form["quit_time"].FirstOrDefault();
if (!string.IsNullOrEmpty(quit_time))
{
if (quit_time.Contains("到"))
{
var dts = quit_time.Split("到");
var dtStart = dts[0].Trim().ObjectToDate();
where = where.And(p => p.quit_time > dtStart);
var dtEnd = dts[1].Trim().ObjectToDate();
where = where.And(p => p.quit_time < dtEnd);
}
else
{
var dt = quit_time.ObjectToDate();
where = where.And(p => p.quit_time > dt);
}
}
//状态,0正常,1已退出 int
var status = Request.Form["status"].FirstOrDefault().ObjectToInt(-1);
if (status > -1)
{
where = where.And(p => p.status == status);
}
//获取数据
var list = await _SQReservationParticipantsServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize, true);
var pageList = _mapper.Map>(list);
if (pageList != null && pageList.Count > 0)
{
var reIds = pageList.Select(it => it.reservation_id).ToList();
var userIds = pageList.Select(it => it.user_id).ToList();
var re = await _SQReservationsServices.QueryListByClauseAsync(p => reIds.Contains(p.id));
var users = await _userServices.QueryListByClauseAsync(p => userIds.Contains(p.id));
foreach (var item in pageList)
{
var reservation = re.FirstOrDefault(it => it.id == item.reservation_id);
if (reservation != null)
{
item.ReservationName = reservation.title;
}
var user = users.FirstOrDefault(it => it.id == item.user_id);
if (user != null)
{
item.UserName = user.userName;
item.NickName = user.nickName;
}
}
}
//返回数据
jm.data = pageList;
jm.code = 0;
jm.count = list.TotalCount;
jm.msg = "数据调用成功!";
return jm;
}
#endregion
#region 首页数据============================================================
// POST: Api/SQReservationParticipants/GetIndex
///
/// 首页数据
///
///
[HttpPost]
[Description("首页数据")]
public AdminUiCallBack GetIndex()
{
//返回数据
var jm = new AdminUiCallBack { code = 0 };
return jm;
}
#endregion
#region 创建数据============================================================
// POST: Api/SQReservationParticipants/GetCreate
///
/// 创建数据
///
///
[HttpPost]
[Description("创建数据")]
public AdminUiCallBack GetCreate()
{
//返回数据
var jm = new AdminUiCallBack { code = 0 };
return jm;
}
#endregion
#region 创建提交============================================================
// POST: Api/SQReservationParticipants/DoCreate
///
/// 创建提交
///
///
///
[HttpPost]
[Description("创建提交")]
public async Task DoCreate([FromBody] SQReservationParticipants entity)
{
var jm = await _SQReservationParticipantsServices.InsertAsync(entity);
return jm;
}
#endregion
#region 编辑数据============================================================
// POST: Api/SQReservationParticipants/GetEdit
///
/// 编辑数据
///
///
///
[HttpPost]
[Description("编辑数据")]
public async Task GetEdit([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _SQReservationParticipantsServices.QueryByIdAsync(entity.id, false);
if (model == null)
{
jm.msg = "不存在此信息";
return jm;
}
jm.code = 0;
jm.data = model;
return jm;
}
#endregion
#region 编辑提交============================================================
// POST: Api/SQReservationParticipants/Edit
///
/// 编辑提交
///
///
///
[HttpPost]
[Description("编辑提交")]
public async Task DoEdit([FromBody] SQReservationParticipants entity)
{
var jm = await _SQReservationParticipantsServices.UpdateAsync(entity);
return jm;
}
#endregion
#region 删除数据============================================================
// POST: Api/SQReservationParticipants/DoDelete/10
///
/// 单选删除
///
///
///
[HttpPost]
[Description("单选删除")]
public async Task DoDelete([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _SQReservationParticipantsServices.ExistsAsync(p => p.id == entity.id, true);
if (!model)
{
jm.msg = GlobalConstVars.DataisNo;
return jm;
}
jm = await _SQReservationParticipantsServices.DeleteByIdAsync(entity.id);
return jm;
}
#endregion
#region 批量删除============================================================
// POST: Api/SQReservationParticipants/DoBatchDelete/10,11,20
///
/// 批量删除
///
///
///
[HttpPost]
[Description("批量删除")]
public async Task DoBatchDelete([FromBody] FMArrayIntIds entity)
{
var jm = await _SQReservationParticipantsServices.DeleteByIdsAsync(entity.id);
return jm;
}
#endregion
#region 预览数据============================================================
// POST: Api/SQReservationParticipants/GetDetails/10
///
/// 预览数据
///
///
///
[HttpPost]
[Description("预览数据")]
public async Task GetDetails([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _SQReservationParticipantsServices.QueryByIdAsync(entity.id, false);
if (model == null)
{
jm.msg = "不存在此信息";
return jm;
}
jm.code = 0;
jm.data = model;
return jm;
}
#endregion
#region 选择导出============================================================
// POST: Api/SQReservationParticipants/SelectExportExcel/10
///
/// 选择导出
///
///
///
[HttpPost]
[Description("选择导出")]
public async Task SelectExportExcel([FromBody] FMArrayIntIds entity)
{
var jm = new AdminUiCallBack();
//创建Excel文件的对象
var book = new HSSFWorkbook();
//添加一个sheet
var mySheet = book.CreateSheet("Sheet1");
//获取list数据
var listModel = await _SQReservationParticipantsServices.QueryListByClauseAsync(p => entity.id.Contains(p.id), p => p.id, OrderByType.Asc, true);
//给sheet1添加第一行的头部标题
var headerRow = mySheet.CreateRow(0);
var headerStyle = ExcelHelper.GetHeaderStyle(book);
var cell0 = headerRow.CreateCell(0);
cell0.SetCellValue("参与记录ID");
cell0.CellStyle = headerStyle;
mySheet.SetColumnWidth(0, 10 * 256);
var cell1 = headerRow.CreateCell(1);
cell1.SetCellValue("预约ID");
cell1.CellStyle = headerStyle;
mySheet.SetColumnWidth(1, 10 * 256);
var cell2 = headerRow.CreateCell(2);
cell2.SetCellValue("参与者ID(关联用户表,暂时占位)");
cell2.CellStyle = headerStyle;
mySheet.SetColumnWidth(2, 10 * 256);
var cell3 = headerRow.CreateCell(3);
cell3.SetCellValue("角色:0=参与者,1=发起者");
cell3.CellStyle = headerStyle;
mySheet.SetColumnWidth(3, 10 * 256);
var cell4 = headerRow.CreateCell(4);
cell4.SetCellValue("加入时间");
cell4.CellStyle = headerStyle;
mySheet.SetColumnWidth(4, 10 * 256);
var cell5 = headerRow.CreateCell(5);
cell5.SetCellValue("退出时间");
cell5.CellStyle = headerStyle;
mySheet.SetColumnWidth(5, 10 * 256);
var cell6 = headerRow.CreateCell(6);
cell6.SetCellValue("状态,0正常,1已退出");
cell6.CellStyle = headerStyle;
mySheet.SetColumnWidth(6, 10 * 256);
headerRow.Height = 30 * 20;
var commonCellStyle = ExcelHelper.GetCommonStyle(book);
//将数据逐步写入sheet1各个行
for (var i = 0; i < listModel.Count; i++)
{
var rowTemp = mySheet.CreateRow(i + 1);
var rowTemp0 = rowTemp.CreateCell(0);
rowTemp0.SetCellValue(listModel[i].id.ToString());
rowTemp0.CellStyle = commonCellStyle;
var rowTemp1 = rowTemp.CreateCell(1);
rowTemp1.SetCellValue(listModel[i].reservation_id.ToString());
rowTemp1.CellStyle = commonCellStyle;
var rowTemp2 = rowTemp.CreateCell(2);
rowTemp2.SetCellValue(listModel[i].user_id.ToString());
rowTemp2.CellStyle = commonCellStyle;
var rowTemp3 = rowTemp.CreateCell(3);
rowTemp3.SetCellValue(listModel[i].role.ToString());
rowTemp3.CellStyle = commonCellStyle;
var rowTemp4 = rowTemp.CreateCell(4);
rowTemp4.SetCellValue(listModel[i].join_time.ToString());
rowTemp4.CellStyle = commonCellStyle;
var rowTemp5 = rowTemp.CreateCell(5);
rowTemp5.SetCellValue(listModel[i].quit_time.ToString());
rowTemp5.CellStyle = commonCellStyle;
var rowTemp6 = rowTemp.CreateCell(6);
rowTemp6.SetCellValue(listModel[i].status.ToString());
rowTemp6.CellStyle = commonCellStyle;
}
// 导出excel
string webRootPath = _webHostEnvironment.WebRootPath;
string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-SQReservationParticipants导出(选择结果).xls";
string filePath = webRootPath + tpath;
DirectoryInfo di = new DirectoryInfo(filePath);
if (!di.Exists)
{
di.Create();
}
FileStream fileHssf = new FileStream(filePath + fileName, FileMode.Create);
book.Write(fileHssf);
fileHssf.Close();
jm.code = 0;
jm.msg = GlobalConstVars.ExcelExportSuccess;
jm.data = tpath + fileName;
return jm;
}
#endregion
#region 查询导出============================================================
// POST: Api/SQReservationParticipants/QueryExportExcel/10
///
/// 查询导出
///
///
[HttpPost]
[Description("查询导出")]
public async Task QueryExportExcel()
{
var jm = new AdminUiCallBack();
var where = PredicateBuilder.True();
//查询筛选
//参与记录ID int
var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
if (id > 0)
{
where = where.And(p => p.id == id);
}
//预约ID int
var reservation_id = Request.Form["reservation_id"].FirstOrDefault().ObjectToInt(0);
if (reservation_id > 0)
{
where = where.And(p => p.reservation_id == reservation_id);
}
//参与者ID(关联用户表,暂时占位) int
var user_id = Request.Form["user_id"].FirstOrDefault().ObjectToInt(0);
if (user_id > 0)
{
where = where.And(p => p.user_id == user_id);
}
//角色:0=参与者,1=发起者 int
var role = Request.Form["role"].FirstOrDefault().ObjectToInt(0);
if (role > 0)
{
where = where.And(p => p.role == role);
}
//加入时间 datetime
var join_time = Request.Form["join_time"].FirstOrDefault();
if (!string.IsNullOrEmpty(join_time))
{
var dt = join_time.ObjectToDate();
where = where.And(p => p.join_time > dt);
}
//退出时间 datetime
var quit_time = Request.Form["quit_time"].FirstOrDefault();
if (!string.IsNullOrEmpty(quit_time))
{
var dt = quit_time.ObjectToDate();
where = where.And(p => p.quit_time > dt);
}
//状态,0正常,1已退出 int
var status = Request.Form["status"].FirstOrDefault().ObjectToInt(0);
if (status > 0)
{
where = where.And(p => p.status == status);
}
//获取数据
//创建Excel文件的对象
var book = new HSSFWorkbook();
//添加一个sheet
var mySheet = book.CreateSheet("Sheet1");
//获取list数据
var listModel = await _SQReservationParticipantsServices.QueryListByClauseAsync(where, p => p.id, OrderByType.Asc, true);
//给sheet1添加第一行的头部标题
var headerRow = mySheet.CreateRow(0);
var headerStyle = ExcelHelper.GetHeaderStyle(book);
var cell0 = headerRow.CreateCell(0);
cell0.SetCellValue("参与记录ID");
cell0.CellStyle = headerStyle;
mySheet.SetColumnWidth(0, 10 * 256);
var cell1 = headerRow.CreateCell(1);
cell1.SetCellValue("预约ID");
cell1.CellStyle = headerStyle;
mySheet.SetColumnWidth(1, 10 * 256);
var cell2 = headerRow.CreateCell(2);
cell2.SetCellValue("参与者ID(关联用户表,暂时占位)");
cell2.CellStyle = headerStyle;
mySheet.SetColumnWidth(2, 10 * 256);
var cell3 = headerRow.CreateCell(3);
cell3.SetCellValue("角色:0=参与者,1=发起者");
cell3.CellStyle = headerStyle;
mySheet.SetColumnWidth(3, 10 * 256);
var cell4 = headerRow.CreateCell(4);
cell4.SetCellValue("加入时间");
cell4.CellStyle = headerStyle;
mySheet.SetColumnWidth(4, 10 * 256);
var cell5 = headerRow.CreateCell(5);
cell5.SetCellValue("退出时间");
cell5.CellStyle = headerStyle;
mySheet.SetColumnWidth(5, 10 * 256);
var cell6 = headerRow.CreateCell(6);
cell6.SetCellValue("状态,0正常,1已退出");
cell6.CellStyle = headerStyle;
mySheet.SetColumnWidth(6, 10 * 256);
headerRow.Height = 30 * 20;
var commonCellStyle = ExcelHelper.GetCommonStyle(book);
//将数据逐步写入sheet1各个行
for (var i = 0; i < listModel.Count; i++)
{
var rowTemp = mySheet.CreateRow(i + 1);
var rowTemp0 = rowTemp.CreateCell(0);
rowTemp0.SetCellValue(listModel[i].id.ToString());
rowTemp0.CellStyle = commonCellStyle;
var rowTemp1 = rowTemp.CreateCell(1);
rowTemp1.SetCellValue(listModel[i].reservation_id.ToString());
rowTemp1.CellStyle = commonCellStyle;
var rowTemp2 = rowTemp.CreateCell(2);
rowTemp2.SetCellValue(listModel[i].user_id.ToString());
rowTemp2.CellStyle = commonCellStyle;
var rowTemp3 = rowTemp.CreateCell(3);
rowTemp3.SetCellValue(listModel[i].role.ToString());
rowTemp3.CellStyle = commonCellStyle;
var rowTemp4 = rowTemp.CreateCell(4);
rowTemp4.SetCellValue(listModel[i].join_time.ToString());
rowTemp4.CellStyle = commonCellStyle;
var rowTemp5 = rowTemp.CreateCell(5);
rowTemp5.SetCellValue(listModel[i].quit_time.ToString());
rowTemp5.CellStyle = commonCellStyle;
var rowTemp6 = rowTemp.CreateCell(6);
rowTemp6.SetCellValue(listModel[i].status.ToString());
rowTemp6.CellStyle = commonCellStyle;
}
// 写入到excel
string webRootPath = _webHostEnvironment.WebRootPath;
string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-SQReservationParticipants导出(查询结果).xls";
string filePath = webRootPath + tpath;
DirectoryInfo di = new DirectoryInfo(filePath);
if (!di.Exists)
{
di.Create();
}
FileStream fileHssf = new FileStream(filePath + fileName, FileMode.Create);
book.Write(fileHssf);
fileHssf.Close();
jm.code = 0;
jm.msg = GlobalConstVars.ExcelExportSuccess;
jm.data = tpath + fileName;
return jm;
}
#endregion
}
}