/***********************************************************************
* Project: baifenBinfa
* ProjectName: 百分兵法管理系统
* Web: http://chuanyin.com
* Author:
* Email:
* CreateTime: 202403/02
* 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.Filter;
using CoreCms.Net.Loging;
using CoreCms.Net.IServices;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Web.Admin.Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using SqlSugar;
namespace CoreCms.Net.Web.Admin.Controllers
{
///
/// 用户对智能表单的提交记录
///
[Description("用户对智能表单的提交记录")]
[Route("api/[controller]/[action]")]
[ApiController]
[RequiredErrorForAdmin]
[Authorize(Permissions.Name)]
public class CoreCmsFormSubmitController : ControllerBase
{
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ICoreCmsFormSubmitServices _coreCmsFormSubmitServices;
private readonly ICoreCmsFormSubmitDetailServices _formSubmitDetailServices;
private readonly ICoreCmsFormServices _formServices;
private readonly ICoreCmsUserServices _userServices;
///
/// 构造函数
///
public CoreCmsFormSubmitController(IWebHostEnvironment webHostEnvironment
, ICoreCmsFormSubmitServices coreCmsFormSubmitServices, ICoreCmsFormServices formServices, ICoreCmsFormSubmitDetailServices formSubmitDetailServices, ICoreCmsUserServices userServices)
{
_webHostEnvironment = webHostEnvironment;
_coreCmsFormSubmitServices = coreCmsFormSubmitServices;
_formServices = formServices;
_formSubmitDetailServices = formSubmitDetailServices;
_userServices = userServices;
}
#region 获取列表============================================================
// POST: Api/CoreCmsFormSubmit/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,
"formId" => p => p.formId,
"formName" => p => p.formName,
"userId" => p => p.userId,
"money" => p => p.money,
"payStatus" => p => p.payStatus,
"status" => p => p.status,
"feedback" => p => p.feedback,
"ip" => p => p.ip,
"createTime" => p => p.createTime,
"updateTime" => p => p.updateTime,
_ => p => p.id
};
//设置排序方式
var orderDirection = Request.Form["orderDirection"].FirstOrDefault();
var orderBy = orderDirection switch
{
"asc" => OrderByType.Asc,
"desc" => OrderByType.Desc,
_ => OrderByType.Desc
};
//查询筛选
//表单id int
var formId = Request.Form["formId"].FirstOrDefault().ObjectToInt(0);
if (formId > 0)
{
where = where.And(p => p.formId == formId);
}
//用户昵称 nvarchar
var userName = Request.Form["userName"].FirstOrDefault();
if (!string.IsNullOrEmpty(userName))
{
where = where.And(p => p.userName.Contains(userName));
}
//是否支付 bit
var payStatus = Request.Form["payStatus"].FirstOrDefault();
if (!string.IsNullOrEmpty(payStatus) && payStatus.ToLowerInvariant() == "true")
{
where = where.And(p => p.payStatus == true);
}
else if (!string.IsNullOrEmpty(payStatus) && payStatus.ToLowerInvariant() == "false")
{
where = where.And(p => p.payStatus == false);
}
//是否处理 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 feedback = Request.Form["feedback"].FirstOrDefault();
if (!string.IsNullOrEmpty(feedback))
{
where = where.And(p => p.feedback.Contains(feedback));
}
//提交人ip nvarchar
var ip = Request.Form["ip"].FirstOrDefault();
if (!string.IsNullOrEmpty(ip))
{
where = where.And(p => p.ip.Contains(ip));
}
//创建时间 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);
}
}
//更新时间 datetime
var updateTime = Request.Form["updateTime"].FirstOrDefault();
if (!string.IsNullOrEmpty(updateTime))
{
if (updateTime.Contains("到"))
{
var dts = updateTime.Split("到");
var dtStart = dts[0].Trim().ObjectToDate();
where = where.And(p => p.updateTime > dtStart);
var dtEnd = dts[1].Trim().ObjectToDate();
where = where.And(p => p.updateTime < dtEnd);
}
else
{
var dt = updateTime.ObjectToDate();
where = where.And(p => p.updateTime > dt);
}
}
//获取数据
var list = await _coreCmsFormSubmitServices.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/CoreCmsFormSubmit/GetIndex
///
/// 首页数据
///
///
[HttpPost]
[Description("首页数据")]
public async Task GetIndex()
{
//返回数据
var jm = new AdminUiCallBack { code = 0 };
var forms = await _formServices.QueryAsync();
jm.data = new
{
forms
};
return jm;
}
#endregion
#region 编辑数据============================================================
// POST: Api/CoreCmsFormSubmit/GetEdit
///
/// 编辑数据
///
///
///
[HttpPost]
[Description("编辑数据")]
public async Task GetEdit([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _coreCmsFormSubmitServices.QueryByIdAsync(entity.id, false);
if (model == null)
{
jm.msg = "不存在此信息";
return jm;
}
jm.code = 0;
jm.data = model;
return jm;
}
#endregion
#region 删除数据============================================================
// POST: Api/CoreCmsFormSubmit/DoDelete/10
///
/// 单选删除
///
///
///
[HttpPost]
[Description("单选删除")]
public async Task DoDelete([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _coreCmsFormSubmitServices.ExistsAsync(p => p.id == entity.id, true);
if (!model)
{
jm.msg = GlobalConstVars.DataisNo;
return jm;
}
jm = await _coreCmsFormSubmitServices.DeleteByIdAsync(entity.id);
return jm;
}
#endregion
#region 预览数据============================================================
// POST: Api/CoreCmsFormSubmit/GetDetails/10
///
/// 预览数据
///
///
///
[HttpPost]
[Description("预览数据")]
public async Task GetDetails([FromBody] FMIntId entity)
{
var jm = new AdminUiCallBack();
var model = await _coreCmsFormSubmitServices.QueryByIdAsync(entity.id, false);
if (model == null)
{
jm.msg = "不存在此信息";
return jm;
}
jm.code = 0;
var getResult = await _formServices.GetFormInfo(model.formId, "", true);
var formModel = getResult.data as CoreCmsForm;
if (formModel == null)
{
jm.msg = "表单信息获取失败";
jm.data = getResult;
return jm;
}
if (formModel.Items.Any())
{
var ids = formModel.Items.Select(p => p.id).ToArray();
var submitDetail = await _formSubmitDetailServices.QueryListByClauseAsync(p => ids.Contains(p.formItemId) && p.submitId == model.id);
foreach (var item in formModel.Items)
{
var sub = submitDetail.Find(p => p.formItemId == item.id);
if (sub != null)
{
if (item.type == GlobalEnumVars.FormFieldTypes.goods.ToString())
{
item.value = sub.formItemName + " x 数量" + sub.formItemValue;
}
else
{
item.value = sub.formItemValue;
}
}
else
{
item.value = "";
}
}
}
if (model.userId > 0)
{
var user = await _userServices.QueryByClauseAsync(p => p.id == model.userId);
model.userName = user != null ? user.nickName : "";
}
jm.data = new
{
model,
formModel.Items
};
return jm;
}
#endregion
#region 设置是否支付============================================================
// POST: Api/CoreCmsFormSubmit/DoSetpayStatus/10
///
/// 设置是否支付
///
///
///
[HttpPost]
[Description("设置是否支付")]
public async Task DoSetpayStatus([FromBody] FMUpdateBoolDataByIntId entity)
{
var jm = new AdminUiCallBack();
var oldModel = await _coreCmsFormSubmitServices.QueryByIdAsync(entity.id, false);
if (oldModel == null)
{
jm.msg = "不存在此信息";
return jm;
}
oldModel.payStatus = (bool)entity.data;
//事物处理过程结束
var bl = await _coreCmsFormSubmitServices.UpdateAsync(p => new CoreCmsFormSubmit() { payStatus = oldModel.payStatus }, p => p.id == oldModel.id);
jm.code = bl ? 0 : 1;
jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
return jm;
}
#endregion
#region 设置是否处理============================================================
// POST: Api/CoreCmsFormSubmit/DoSetstatus/10
///
/// 设置是否处理
///
///
///
[HttpPost]
[Description("设置是否处理")]
public async Task DoSetstatus([FromBody] FMUpdateBoolDataByIntId entity)
{
var jm = new AdminUiCallBack();
var oldModel = await _coreCmsFormSubmitServices.QueryByIdAsync(entity.id, false);
if (oldModel == null)
{
jm.msg = "不存在此信息";
return jm;
}
oldModel.status = (bool)entity.data;
//事物处理过程结束
var bl = await _coreCmsFormSubmitServices.UpdateAsync(p => new CoreCmsFormSubmit() { status = oldModel.status }, p => p.id == oldModel.id);
jm.code = bl ? 0 : 1;
jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
return jm;
}
#endregion
#region 更新反馈============================================================
// POST: Api/CoreCmsFormSubmit/DoSetstatus/10
///
/// 更新反馈
///
///
///
[HttpPost]
[Description("更新反馈")]
public async Task DoSetFeedback([FromBody] FMUpdateStringDataByIntId entity)
{
var jm = new AdminUiCallBack();
var oldModel = await _coreCmsFormSubmitServices.QueryByIdAsync(entity.id, false);
if (oldModel == null)
{
jm.msg = "不存在此信息";
return jm;
}
oldModel.feedback = entity.data.ToString();
//事物处理过程结束
var bl = await _coreCmsFormSubmitServices.UpdateAsync(p => new CoreCmsFormSubmit() { feedback = oldModel.feedback, updateTime = DateTime.Now }, p => p.id == oldModel.id);
jm.code = bl ? 0 : 1;
jm.msg = bl ? GlobalConstVars.EditSuccess : GlobalConstVars.EditFailure;
return jm;
}
#endregion
}
}