/*********************************************************************** * Project: baifenBinfa * ProjectName: 百分兵法管理系统 * Web: http://chuanyin.com * Author: * Email: * CreateTime: 202403/02 * Description: 暂无 ***********************************************************************/ using System; using System.ComponentModel; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; using CoreCms.Net.Configuration; using CoreCms.Net.Filter; using CoreCms.Net.IServices; using CoreCms.Net.Loging; 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.Utility.Extensions; using CoreCms.Net.Web.Admin.Infrastructure; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using SqlSugar; namespace CoreCms.Net.Web.Admin.Controllers { /// /// 公告表 /// [Description("公告表")] [Route("api/[controller]/[action]")] [ApiController] [RequiredErrorForAdmin] [Authorize(Permissions.Name)] public class CoreCmsNoticeController : ControllerBase { private readonly ICoreCmsNoticeServices _coreCmsNoticeServices; private readonly IWebHostEnvironment _webHostEnvironment; /// /// 构造函数 /// /// /// public CoreCmsNoticeController(IWebHostEnvironment webHostEnvironment, ICoreCmsNoticeServices coreCmsNoticeServices) { _webHostEnvironment = webHostEnvironment; _coreCmsNoticeServices = coreCmsNoticeServices; } #region 获取列表============================================================ // POST: Api/CoreCmsNotice/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; switch (orderField) { case "id": orderEx = p => p.id; break; case "title": orderEx = p => p.title; break; case "contentBody": orderEx = p => p.contentBody; break; case "type": orderEx = p => p.type; break; case "sort": orderEx = p => p.sort; break; case "isDel": orderEx = p => p.isDel; break; case "createTime": orderEx = p => p.createTime; break; default: orderEx = p => p.id; break; } //设置排序方式 var orderDirection = Request.Form["orderDirection"].FirstOrDefault(); var orderBy = orderDirection switch { "asc" => OrderByType.Asc, "desc" => OrderByType.Desc, _ => OrderByType.Desc }; //查询筛选 //序列 int var id = Request.Form["id"].FirstOrDefault().ObjectToInt(0); if (id > 0) @where = @where.And(p => p.id == id); //公告标题 nvarchar var title = Request.Form["title"].FirstOrDefault(); if (!string.IsNullOrEmpty(title)) @where = @where.And(p => p.title.Contains(title)); //公告内容 nvarchar var contentBody = Request.Form["contentBody"].FirstOrDefault(); if (!string.IsNullOrEmpty(contentBody)) @where = @where.And(p => p.contentBody.Contains(contentBody)); //公告类型 int var type = Request.Form["type"].FirstOrDefault().ObjectToInt(0); if (type > 0) @where = @where.And(p => p.type == type); //排序 int var sort = Request.Form["sort"].FirstOrDefault().ObjectToInt(0); if (sort > 0) @where = @where.And(p => p.sort == sort); //软删除位 有时间代表已删除 bit var isDel = Request.Form["isDel"].FirstOrDefault(); if (!string.IsNullOrEmpty(isDel) && isDel.ToLowerInvariant() == "true") @where = @where.And(p => p.isDel == true); else if (!string.IsNullOrEmpty(isDel) && isDel.ToLowerInvariant() == "false") @where = @where.And(p => p.isDel == false); //创建时间 datetime var createTime = Request.Form["createTime"].FirstOrDefault(); if (!string.IsNullOrEmpty(createTime)) { if (createTime.Contains("到")) { var dts = createTime.Split("到"); var dtStart = dts[0].Trim().ObjectToDate(); where = where.And(p => p.createTime > dtStart); var dtEnd = dts[1].Trim().ObjectToDate(); where = where.And(p => p.createTime < dtEnd); } else { var dt = createTime.ObjectToDate(); where = where.And(p => p.createTime > dt); } } //获取数据 var list = await _coreCmsNoticeServices.QueryPageAsync(where, orderEx, orderBy, pageCurrent, pageSize); //返回数据 jm.data = list; jm.code = 0; jm.count = list.TotalCount; jm.msg = "数据调用成功!"; return jm; } #endregion #region 首页数据============================================================ // POST: Api/CoreCmsNotice/GetIndex /// /// 首页数据 /// /// [HttpPost] [Description("首页数据")] public AdminUiCallBack GetIndex() { //返回数据 var jm = new AdminUiCallBack { code = 0 }; return jm; } #endregion #region 创建数据============================================================ // POST: Api/CoreCmsNotice/GetCreate /// /// 创建数据 /// /// [HttpPost] [Description("创建数据")] public AdminUiCallBack GetCreate() { //返回数据 var jm = new AdminUiCallBack { code = 0 }; return jm; } #endregion #region 创建提交============================================================ // POST: Api/CoreCmsNotice/DoCreate /// /// 创建提交 /// /// /// [HttpPost] [Description("创建提交")] public async Task DoCreate([FromBody] CoreCmsNotice entity) { var jm = new AdminUiCallBack(); entity.createTime = DateTime.Now; entity.isDel = false; var bl = await _coreCmsNoticeServices.InsertAsync(entity) > 0; jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.CreateSuccess : GlobalConstVars.CreateFailure; return jm; } #endregion #region 编辑数据============================================================ // POST: Api/CoreCmsNotice/GetEdit /// /// 编辑数据 /// /// /// [HttpPost] [Description("编辑数据")] public async Task GetEdit([FromBody] FMIntId entity) { var jm = new AdminUiCallBack(); var model = await _coreCmsNoticeServices.QueryByIdAsync(entity.id); if (model == null) { jm.msg = "不存在此信息"; return jm; } jm.code = 0; jm.data = model; return jm; } #endregion #region 编辑提交============================================================ // POST: Admins/CoreCmsNotice/Edit /// /// 编辑提交 /// /// /// [HttpPost] [Description("编辑提交")] public async Task DoEdit([FromBody] CoreCmsNotice entity) { var jm = new AdminUiCallBack(); var oldModel = await _coreCmsNoticeServices.QueryByIdAsync(entity.id); if (oldModel == null) { jm.msg = "不存在此信息"; return jm; } //事物处理过程开始 oldModel.id = entity.id; oldModel.title = entity.title; oldModel.contentBody = entity.contentBody; oldModel.type = entity.type; oldModel.sort = entity.sort; oldModel.isDel = entity.isDel; oldModel.createTime = DateTime.Now; //事物处理过程结束 var bl = await _coreCmsNoticeServices.UpdateAsync(oldModel); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure; return jm; } #endregion #region 删除数据============================================================ // POST: Api/CoreCmsNotice/DoDelete/10 /// /// 单选删除 /// /// /// [HttpPost] [Description("单选删除")] public async Task DoDelete([FromBody] FMIntId entity) { var jm = new AdminUiCallBack(); var model = await _coreCmsNoticeServices.QueryByIdAsync(entity.id); if (model == null) { jm.msg = GlobalConstVars.DataisNo; return jm; } var bl = await _coreCmsNoticeServices.DeleteByIdAsync(entity.id); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure; return jm; } #endregion #region 批量删除============================================================ // POST: Api/CoreCmsNotice/DoBatchDelete/10,11,20 /// /// 批量删除 /// /// /// [HttpPost] [Description("批量删除")] public async Task DoBatchDelete([FromBody] FMArrayIntIds entity) { var jm = new AdminUiCallBack(); var bl = await _coreCmsNoticeServices.DeleteByIdsAsync(entity.id); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure; return jm; } #endregion #region 设置软删除============================================================ // POST: Api/CoreCmsNotice/DoSetisDel/10 /// /// 设置软删除位 有时间代表已删除 /// /// /// [HttpPost] [Description("设置软删除位 有时间代表已删除")] public async Task DoSetisDel([FromBody] FMUpdateBoolDataByIntId entity) { var jm = new AdminUiCallBack(); var oldModel = await _coreCmsNoticeServices.QueryByIdAsync(entity.id); if (oldModel == null) { jm.msg = "不存在此信息"; return jm; } oldModel.isDel = entity.data; var bl = await _coreCmsNoticeServices.UpdateAsync(oldModel); jm.code = bl ? 0 : 1; jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure; return jm; } #endregion } }