mahjong_group/server/CoreCms.Net.Web.Admin/Controllers/SQ/SQRoomsController.cs
2026-01-01 14:35:52 +08:00

691 lines
25 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/***********************************************************************
* Project: CoreCms
* ProjectName: 核心内容管理系统
* Web: https://www.corecms.net
* Author: 大灰灰
* Email: jianweie@163.com
* CreateTime: 2025/9/2 12:47:30
* 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;
namespace CoreCms.Net.Web.Admin.Controllers
{
/// <summary>
/// 房间表
///</summary>
[Description("房间表")]
[Route("api/[controller]/[action]")]
[ApiController]
[RequiredErrorForAdmin]
[Authorize]
public class SQRoomsController : ControllerBase
{
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ISQRoomsServices _SQRoomsServices;
/// <summary>
/// 构造函数
///</summary>
public SQRoomsController(IWebHostEnvironment webHostEnvironment
, ISQRoomsServices SQRoomsServices
)
{
_webHostEnvironment = webHostEnvironment;
_SQRoomsServices = SQRoomsServices;
}
#region ============================================================
// POST: Api/SQRooms/GetPageList
/// <summary>
/// 获取列表
/// </summary>
/// <returns></returns>
[HttpPost]
[Description("获取列表")]
public async Task<AdminUiCallBack> 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<SQRooms>();
//获取排序字段
var orderField = Request.Form["orderField"].FirstOrDefault();
Expression<Func<SQRooms, object>> orderEx = orderField switch
{
"id" => p => p.id,
"name" => p => p.name,
"price_per_hour" => p => p.price_per_hour,
"capacity" => p => p.capacity,
"status" => p => p.status,
"sort_order" => p => p.sort_order,
"room_type_name" => p => p.room_type_name,
_ => p => p.id
};
//设置排序方式
var orderDirection = Request.Form["orderDirection"].FirstOrDefault();
var orderBy = orderDirection switch
{
"asc" => OrderByType.Asc,
"desc" => OrderByType.Desc,
_ => OrderByType.Desc
};
//查询筛选
//房间ID bigint
var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
if (id > 0)
{
where = where.And(p => p.id == id);
}
//房间号304号-大包) nvarchar
var name = Request.Form["name"].FirstOrDefault();
if (!string.IsNullOrEmpty(name))
{
where = where.And(p => p.name.Contains(name));
}
//价格/小时 decimal
var price_per_hour = Request.Form["price_per_hour"].FirstOrDefault().ObjectToDecimal(0);
if (price_per_hour > 0)
{
where = where.And(p => p.price_per_hour == price_per_hour);
}
//可容纳人数 int
var capacity = Request.Form["capacity"].FirstOrDefault().ObjectToInt(0);
if (capacity > 0)
{
where = where.And(p => p.capacity == capacity);
}
//状态1=可用0=停用 bit
var status = Request.Form["status"].FirstOrDefault();
if (!string.IsNullOrEmpty(status) && status.ToLowerInvariant() == "true")
{
where = where.And(p => p.status == true);
}
else if (!string.IsNullOrEmpty(status) && status.ToLowerInvariant() == "false")
{
where = where.And(p => p.status == false);
}
//房间类型名称 nvarchar
var room_type_name = Request.Form["room_type_name"].FirstOrDefault();
if (!string.IsNullOrEmpty(room_type_name))
{
where = where.And(p => p.room_type_name.Contains(room_type_name));
}
//获取数据
var list = await _SQRoomsServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize, true);
//返回数据
jm.data = list;
jm.code = 0;
jm.count = list.TotalCount;
jm.msg = "数据调用成功!";
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/GetIndex
/// <summary>
/// 首页数据
/// </summary>
/// <returns></returns>
[HttpPost]
[Description("首页数据")]
public AdminUiCallBack GetIndex()
{
//返回数据
var jm = new AdminUiCallBack { code = 0 };
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/GetCreate
/// <summary>
/// 创建数据
/// </summary>
/// <returns></returns>
[HttpPost]
[Description("创建数据")]
public AdminUiCallBack GetCreate()
{
//返回数据
var jm = new AdminUiCallBack { code = 0 };
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/DoCreate
/// <summary>
/// 创建提交
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("创建提交")]
public async Task<AdminUiCallBack> DoCreate([FromBody] SQRooms entity)
{
var jm = await _SQRoomsServices.InsertAsync(entity);
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/GetEdit
/// <summary>
/// 编辑数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("编辑数据")]
public async Task<AdminUiCallBack> GetEdit([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _SQRoomsServices.QueryByIdAsync(entity.id, false);
if (model == null)
{
jm.msg = "不存在此信息";
return jm;
}
jm.code = 0;
jm.data = model;
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/Edit
/// <summary>
/// 编辑提交
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("编辑提交")]
public async Task<AdminUiCallBack> DoEdit([FromBody] SQRooms entity)
{
var jm = await _SQRoomsServices.UpdateAsync(entity);
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/DoDelete/10
/// <summary>
/// 单选删除
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("单选删除")]
public async Task<AdminUiCallBack> DoDelete([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _SQRoomsServices.ExistsAsync(p => p.id == entity.id, true);
if (!model)
{
jm.msg = GlobalConstVars.DataisNo;
return jm;
}
jm = await _SQRoomsServices.DeleteByIdAsync(entity.id);
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/DoBatchDelete/10,11,20
/// <summary>
/// 批量删除
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("批量删除")]
public async Task<AdminUiCallBack> DoBatchDelete([FromBody] FMArrayIntIds entity)
{
var jm = await _SQRoomsServices.DeleteByIdsAsync(entity.id);
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/GetDetails/10
/// <summary>
/// 预览数据
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("预览数据")]
public async Task<AdminUiCallBack> GetDetails([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _SQRoomsServices.QueryByIdAsync(entity.id, false);
if (model == null)
{
jm.msg = "不存在此信息";
return jm;
}
jm.code = 0;
jm.data = model;
return jm;
}
#endregion
#region ============================================================
// POST: Api/SQRooms/SelectExportExcel/10
/// <summary>
/// 选择导出
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("选择导出")]
public async Task<AdminUiCallBack> SelectExportExcel([FromBody] FMArrayIntIds entity)
{
var jm = new AdminUiCallBack();
//创建Excel文件的对象
var book = new HSSFWorkbook();
//添加一个sheet
var mySheet = book.CreateSheet("Sheet1");
//获取list数据
var listModel = await _SQRoomsServices.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("房间名称");
cell1.CellStyle = headerStyle;
mySheet.SetColumnWidth(1, 10 * 256);
var cell2 = headerRow.CreateCell(2);
cell2.SetCellValue("价格/小时");
cell2.CellStyle = headerStyle;
mySheet.SetColumnWidth(2, 10 * 256);
var cell3 = headerRow.CreateCell(3);
cell3.SetCellValue("人数");
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, 15 * 256);
var cell6 = headerRow.CreateCell(6);
cell6.SetCellValue("房间类型");
cell6.CellStyle = headerStyle;
mySheet.SetColumnWidth(6, 15 * 256);
var cell7 = headerRow.CreateCell(7);
cell7.SetCellValue("排序权重");
cell7.CellStyle = headerStyle;
mySheet.SetColumnWidth(7, 12 * 256);
var cell8 = headerRow.CreateCell(8);
cell8.SetCellValue("房间图片");
cell8.CellStyle = headerStyle;
mySheet.SetColumnWidth(8, 30 * 256);
var cell9 = headerRow.CreateCell(9);
cell9.SetCellValue("房间描述");
cell9.CellStyle = headerStyle;
mySheet.SetColumnWidth(9, 40 * 256);
var cell10 = headerRow.CreateCell(10);
cell10.SetCellValue("房间设施");
cell10.CellStyle = headerStyle;
mySheet.SetColumnWidth(10, 30 * 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].name.ToString());
rowTemp1.CellStyle = commonCellStyle;
var rowTemp2 = rowTemp.CreateCell(2);
rowTemp2.SetCellValue(listModel[i].price_per_hour.ToString());
rowTemp2.CellStyle = commonCellStyle;
var rowTemp3 = rowTemp.CreateCell(3);
rowTemp3.SetCellValue(listModel[i].capacity.ToString());
rowTemp3.CellStyle = commonCellStyle;
var rowTemp4 = rowTemp.CreateCell(4);
rowTemp4.SetCellValue(listModel[i].status ? "启用" : "停用");
rowTemp4.CellStyle = commonCellStyle;
var rowTemp5 = rowTemp.CreateCell(5);
rowTemp5.SetCellValue(listModel[i].room_type_name ?? "");
rowTemp5.CellStyle = commonCellStyle;
var rowTemp6 = rowTemp.CreateCell(6);
rowTemp6.SetCellValue(listModel[i].room_type ?? "");
rowTemp6.CellStyle = commonCellStyle;
var rowTemp7 = rowTemp.CreateCell(7);
rowTemp7.SetCellValue(listModel[i].sort_order?.ToString() ?? "0");
rowTemp7.CellStyle = commonCellStyle;
var rowTemp8 = rowTemp.CreateCell(8);
rowTemp8.SetCellValue(listModel[i].image_url ?? "");
rowTemp8.CellStyle = commonCellStyle;
var rowTemp9 = rowTemp.CreateCell(9);
rowTemp9.SetCellValue(listModel[i].description ?? "");
rowTemp9.CellStyle = commonCellStyle;
var rowTemp10 = rowTemp.CreateCell(10);
rowTemp10.SetCellValue(listModel[i].amenities ?? "");
rowTemp10.CellStyle = commonCellStyle;
}
// 导出excel
string webRootPath = _webHostEnvironment.WebRootPath;
string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-SQRooms导出(选择结果).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/SQRooms/QueryExportExcel/10
/// <summary>
/// 查询导出
/// </summary>
/// <returns></returns>
[HttpPost]
[Description("查询导出")]
public async Task<AdminUiCallBack> QueryExportExcel()
{
var jm = new AdminUiCallBack();
var where = PredicateBuilder.True<SQRooms>();
//查询筛选
//房间ID bigint
var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0);
if (id > 0)
{
where = where.And(p => p.id == id);
}
//房间号304号-大包) nvarchar
var name = Request.Form["name"].FirstOrDefault();
if (!string.IsNullOrEmpty(name))
{
where = where.And(p => p.name.Contains(name));
}
//价格/小时 decimal
var price_per_hour = Request.Form["price_per_hour"].FirstOrDefault().ObjectToDecimal(0);
if (price_per_hour > 0)
{
where = where.And(p => p.price_per_hour == price_per_hour);
}
//可容纳人数 int
var capacity = Request.Form["capacity"].FirstOrDefault().ObjectToInt(0);
if (capacity > 0)
{
where = where.And(p => p.capacity == capacity);
}
//状态1=可用0=停用 bit
var status = Request.Form["status"].FirstOrDefault();
if (!string.IsNullOrEmpty(status) && status.ToLowerInvariant() == "true")
{
where = where.And(p => p.status == true);
}
else if (!string.IsNullOrEmpty(status) && status.ToLowerInvariant() == "false")
{
where = where.And(p => p.status == false);
}
//房间类型名称 nvarchar
var room_type_name = Request.Form["room_type_name"].FirstOrDefault();
if (!string.IsNullOrEmpty(room_type_name))
{
where = where.And(p => p.room_type_name.Contains(room_type_name));
}
//获取数据
//创建Excel文件的对象
var book = new HSSFWorkbook();
//添加一个sheet
var mySheet = book.CreateSheet("Sheet1");
//获取list数据
var listModel = await _SQRoomsServices.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("房间名称");
cell1.CellStyle = headerStyle;
mySheet.SetColumnWidth(1, 10 * 256);
var cell2 = headerRow.CreateCell(2);
cell2.SetCellValue("价格/小时");
cell2.CellStyle = headerStyle;
mySheet.SetColumnWidth(2, 10 * 256);
var cell3 = headerRow.CreateCell(3);
cell3.SetCellValue("人数");
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, 15 * 256);
var cell6 = headerRow.CreateCell(6);
cell6.SetCellValue("房间类型");
cell6.CellStyle = headerStyle;
mySheet.SetColumnWidth(6, 15 * 256);
var cell7 = headerRow.CreateCell(7);
cell7.SetCellValue("排序权重");
cell7.CellStyle = headerStyle;
mySheet.SetColumnWidth(7, 12 * 256);
var cell8 = headerRow.CreateCell(8);
cell8.SetCellValue("房间图片");
cell8.CellStyle = headerStyle;
mySheet.SetColumnWidth(8, 30 * 256);
var cell9 = headerRow.CreateCell(9);
cell9.SetCellValue("房间描述");
cell9.CellStyle = headerStyle;
mySheet.SetColumnWidth(9, 40 * 256);
var cell10 = headerRow.CreateCell(10);
cell10.SetCellValue("房间设施");
cell10.CellStyle = headerStyle;
mySheet.SetColumnWidth(10, 30 * 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].name.ToString());
rowTemp1.CellStyle = commonCellStyle;
var rowTemp2 = rowTemp.CreateCell(2);
rowTemp2.SetCellValue(listModel[i].price_per_hour.ToString());
rowTemp2.CellStyle = commonCellStyle;
var rowTemp3 = rowTemp.CreateCell(3);
rowTemp3.SetCellValue(listModel[i].capacity.ToString());
rowTemp3.CellStyle = commonCellStyle;
var rowTemp4 = rowTemp.CreateCell(4);
rowTemp4.SetCellValue(listModel[i].status ? "启用" : "停用");
rowTemp4.CellStyle = commonCellStyle;
var rowTemp5 = rowTemp.CreateCell(5);
rowTemp5.SetCellValue(listModel[i].room_type_name ?? "");
rowTemp5.CellStyle = commonCellStyle;
var rowTemp6 = rowTemp.CreateCell(6);
rowTemp6.SetCellValue(listModel[i].room_type ?? "");
rowTemp6.CellStyle = commonCellStyle;
var rowTemp7 = rowTemp.CreateCell(7);
rowTemp7.SetCellValue(listModel[i].sort_order?.ToString() ?? "0");
rowTemp7.CellStyle = commonCellStyle;
var rowTemp8 = rowTemp.CreateCell(8);
rowTemp8.SetCellValue(listModel[i].image_url ?? "");
rowTemp8.CellStyle = commonCellStyle;
var rowTemp9 = rowTemp.CreateCell(9);
rowTemp9.SetCellValue(listModel[i].description ?? "");
rowTemp9.CellStyle = commonCellStyle;
var rowTemp10 = rowTemp.CreateCell(10);
rowTemp10.SetCellValue(listModel[i].amenities ?? "");
rowTemp10.CellStyle = commonCellStyle;
}
// 写入到excel
string webRootPath = _webHostEnvironment.WebRootPath;
string tpath = "/files/" + DateTime.Now.ToString("yyyy-MM-dd") + "/";
string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "-SQRooms导出(查询结果).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 1=0=============================================================
// POST: Api/SQRooms/DoSetstatus/10
/// <summary>
/// 设置状态1=可用0=停用
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[HttpPost]
[Description("设置状态1=可用0=停用")]
public async Task<AdminUiCallBack> DoSetstatus([FromBody] FMUpdateBoolDataByIntId entity)
{
var jm = new AdminUiCallBack();
var oldModel = await _SQRoomsServices.QueryByIdAsync(entity.id, false);
if (oldModel == null)
{
jm.msg = "不存在此信息";
return jm;
}
oldModel.status = (bool)entity.data;
var bl = await _SQRoomsServices.UpdateAsync(p => new SQRooms() { status = oldModel.status }, p => p.id == oldModel.id);
jm.code = bl ? 0 : 1;
jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
return jm;
}
#endregion
}
}