using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NCC.ClayObject;
using NCC.Common.Configuration;
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Common.Helper;
using NCC.Common.Model.NPOI;
using NCC.DataEncryption;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.LqYyjl;
using NCC.Extend.Entitys.lq_mdxx;
using NCC.Extend.Entitys.lq_yyjl;
using NCC.Extend.Interfaces.LqYyjl;
using NCC.FriendlyException;
using NCC.JsonSerialization;
using NCC.System.Entitys.Permission;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend.LqYyjl
{
///
/// 预约记录服务
///
[ApiDescriptionSettings(Tag = "绿纤预约记录服务", Name = "LqYyjl", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqYyjlService : ILqYyjlService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqYyjlRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqYyjlService(ISqlSugarRepository lqYyjlRepository, IUserManager userManager)
{
_lqYyjlRepository = lqYyjlRepository;
_db = _lqYyjlRepository.Context;
_userManager = userManager;
}
#region 预约记录
///
/// 获取预约记录
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
// 获取门店名称
if (!string.IsNullOrEmpty(entity.Djmd))
{
var store = await _db.Queryable().Where(u => u.Id == entity.Djmd).FirstAsync();
output.djmdName = store?.Dm;
}
// 获取预约人姓名
if (!string.IsNullOrEmpty(entity.Yyr))
{
var user = await _db.Queryable().Where(u => u.Id == entity.Yyr).FirstAsync();
output.yyrName = user?.RealName;
}
// 获取预约健康师姓名
if (!string.IsNullOrEmpty(entity.Yyjks))
{
var healthCoach = await _db.Queryable().Where(u => u.Id == entity.Yyjks).FirstAsync();
output.yyjksName = healthCoach?.RealName;
}
return output;
}
#endregion
#region 预约记录列表
///
/// 获取预约记录列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] LqYyjlListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryCzsj = input.czsj != null ? input.czsj.Split(',').ToObeject>() : null;
DateTime? startCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.First()) : null;
DateTime? endCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.Last()) : null;
List queryYysj = input.yysj != null ? input.yysj.Split(',').ToObeject>() : null;
DateTime? startYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.First()) : null;
DateTime? endYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.Last()) : null;
List queryYyjs = input.yyjs != null ? input.yyjs.Split(',').ToObeject>() : null;
DateTime? startYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.First()) : null;
DateTime? endYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.djmd), p => p.Djmd.Equals(input.djmd))
.WhereIF(!string.IsNullOrEmpty(input.yyr), p => p.Yyr.Equals(input.yyr))
.WhereIF(!string.IsNullOrEmpty(input.gklx), p => p.Gklx.Contains(input.gklx))
.WhereIF(!string.IsNullOrEmpty(input.yytyxm), p => p.Yytyxm.Equals(input.yytyxm))
.WhereIF(!string.IsNullOrEmpty(input.czr), p => p.Czr.Equals(input.czr))
.WhereIF(queryCzsj != null, p => p.Czsj >= new DateTime(startCzsj.ToDate().Year, startCzsj.ToDate().Month, startCzsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryCzsj != null, p => p.Czsj <= new DateTime(endCzsj.ToDate().Year, endCzsj.ToDate().Month, endCzsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.gk), p => p.Gk.Equals(input.gk))
.WhereIF(!string.IsNullOrEmpty(input.gkxm), p => p.Gkxm.Contains(input.gkxm))
.WhereIF(!string.IsNullOrEmpty(input.yyjks), p => p.Yyjks.Equals(input.yyjks))
.WhereIF(queryYysj != null, p => p.Yysj >= new DateTime(startYysj.ToDate().Year, startYysj.ToDate().Month, startYysj.ToDate().Day, 0, 0, 0))
.WhereIF(queryYysj != null, p => p.Yysj <= new DateTime(endYysj.ToDate().Year, endYysj.ToDate().Month, endYysj.ToDate().Day, 23, 59, 59))
.WhereIF(queryYyjs != null, p => p.Yyjs >= new DateTime(startYyjs.ToDate().Year, startYyjs.ToDate().Month, startYyjs.ToDate().Day, 0, 0, 0))
.WhereIF(queryYyjs != null, p => p.Yyjs <= new DateTime(endYyjs.ToDate().Year, endYyjs.ToDate().Month, endYyjs.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.F_Status), p => p.F_Status.Equals(input.F_Status))
.Select(it => new LqYyjlListOutput
{
id = it.Id,
djmd = it.Djmd,
yyr = it.Yyr,
gklx = it.Gklx,
yytyxm = it.Yytyxm,
czr = it.Czr,
czsj = it.Czsj,
gk = it.Gk,
gkxm = it.Gkxm,
yyjks = it.Yyjks,
yysj = it.Yysj,
yyjs = it.Yyjs,
F_Status = it.F_Status,
yyrName = SqlFunc.Subqueryable().Where(u => u.Id == it.Yyr).Select(u => u.RealName),
yyjksName = SqlFunc.Subqueryable().Where(u => u.Id == it.Yyjks).Select(u => u.RealName),
djmdName = SqlFunc.Subqueryable().Where(u => u.Id == it.Djmd).Select(u => u.Dm),
})
.MergeTable()
.OrderBy(sidx + " " + input.sort)
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 新建预约记录
///
/// 新建预约记录
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqYyjlCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
entity.Czr = _userManager.UserId;
entity.Czsj = DateTime.Now;
entity.CreateTime = DateTime.Now;
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0))
throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 预约记录无分页列表
///
/// 获取预约记录无分页列表
///
/// 请求参数
///
[NonAction]
public async Task GetNoPagingList([FromQuery] LqYyjlListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryCzsj = input.czsj != null ? input.czsj.Split(',').ToObeject>() : null;
DateTime? startCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.First()) : null;
DateTime? endCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.Last()) : null;
List queryYysj = input.yysj != null ? input.yysj.Split(',').ToObeject>() : null;
DateTime? startYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.First()) : null;
DateTime? endYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.Last()) : null;
List queryYyjs = input.yyjs != null ? input.yyjs.Split(',').ToObeject>() : null;
DateTime? startYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.First()) : null;
DateTime? endYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.djmd), p => p.Djmd.Equals(input.djmd))
.WhereIF(!string.IsNullOrEmpty(input.yyr), p => p.Yyr.Equals(input.yyr))
.WhereIF(!string.IsNullOrEmpty(input.gklx), p => p.Gklx.Contains(input.gklx))
.WhereIF(!string.IsNullOrEmpty(input.yytyxm), p => p.Yytyxm.Equals(input.yytyxm))
.WhereIF(!string.IsNullOrEmpty(input.czr), p => p.Czr.Equals(input.czr))
.WhereIF(queryCzsj != null, p => p.Czsj >= new DateTime(startCzsj.ToDate().Year, startCzsj.ToDate().Month, startCzsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryCzsj != null, p => p.Czsj <= new DateTime(endCzsj.ToDate().Year, endCzsj.ToDate().Month, endCzsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.gk), p => p.Gk.Equals(input.gk))
.WhereIF(!string.IsNullOrEmpty(input.gkxm), p => p.Gkxm.Contains(input.gkxm))
.WhereIF(!string.IsNullOrEmpty(input.yyjks), p => p.Yyjks.Equals(input.yyjks))
.WhereIF(queryYysj != null, p => p.Yysj >= new DateTime(startYysj.ToDate().Year, startYysj.ToDate().Month, startYysj.ToDate().Day, 0, 0, 0))
.WhereIF(queryYysj != null, p => p.Yysj <= new DateTime(endYysj.ToDate().Year, endYysj.ToDate().Month, endYysj.ToDate().Day, 23, 59, 59))
.WhereIF(queryYyjs != null, p => p.Yyjs >= new DateTime(startYyjs.ToDate().Year, startYyjs.ToDate().Month, startYyjs.ToDate().Day, 0, 0, 0))
.WhereIF(queryYyjs != null, p => p.Yyjs <= new DateTime(endYyjs.ToDate().Year, endYyjs.ToDate().Month, endYyjs.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.F_Status), p => p.F_Status.Equals(input.F_Status))
.Select(it => new LqYyjlListOutput
{
id = it.Id,
djmd = it.Djmd,
yyr = it.Yyr,
gklx = it.Gklx,
yytyxm = it.Yytyxm,
czr = it.Czr,
czsj = it.Czsj,
gk = it.Gk,
gkxm = it.Gkxm,
yyjks = it.Yyjks,
yysj = it.Yysj,
yyjs = it.Yyjs,
F_Status = it.F_Status,
yyrName = SqlFunc.Subqueryable().Where(u => u.Id == it.Yyr).Select(u => u.RealName),
yyjksName = SqlFunc.Subqueryable().Where(u => u.Id == it.Yyjks).Select(u => u.RealName),
djmdName = SqlFunc.Subqueryable().Where(u => u.Id == it.Djmd).Select(u => u.Dm),
})
.MergeTable()
.OrderBy(sidx + " " + input.sort)
.ToListAsync();
return data;
}
#endregion
#region 导出预约记录
///
/// 导出预约记录
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqYyjlListQueryInput input)
{
var userInfo = await _userManager.GetUserInfo();
var exportData = new List();
if (input.dataType == 0)
{
var data = Clay.Object(await this.GetList(input));
exportData = data.Solidify>().list;
}
else
{
exportData = await this.GetNoPagingList(input);
}
List paramList =
"[{\"value\":\"预约编号\",\"field\":\"id\"},{\"value\":\"单据门店\",\"field\":\"djmd\"},{\"value\":\"邀约人\",\"field\":\"yyr\"},{\"value\":\"顾客姓名\",\"field\":\"gkxm\"},{\"value\":\"操作人\",\"field\":\"czr\"},{\"value\":\"操作时间\",\"field\":\"czsj\"},{\"value\":\"预约体验项目\",\"field\":\"yytyxm\"},{\"value\":\"顾客类型\",\"field\":\"gklx\"},{\"value\":\"预约健康师\",\"field\":\"yyjks\"},{\"value\":\"预约开始时间\",\"field\":\"yysj\"},{\"value\":\"预约结束时间\",\"field\":\"yyjs\"},{\"value\":\"顾客\",\"field\":\"gk\"},{\"value\":\"预约状态\",\"field\":\"F_Status\"},]".ToList();
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = "预约记录.xls";
excelconfig.HeadFont = "微软雅黑";
excelconfig.HeadPoint = 10;
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnModel = new List();
List selectKeyList = input.selectKey.Split(',').ToList();
foreach (var item in selectKeyList)
{
var isExist = paramList.Find(p => p.field == item);
if (isExist != null)
{
excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value });
}
}
var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName;
ExcelExportHelper.Export(exportData, excelconfig, addPath);
var fileName = _userManager.UserId + "|" + addPath + "|xls";
var output = new { name = excelconfig.FileName, url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC") };
return output;
}
#endregion
#region 批量删除预约记录
///
/// 批量删除预约记录
///
/// 主键数组
///
[HttpPost("batchRemove")]
public async Task BatchRemove([FromBody] List ids)
{
var entitys = await _db.Queryable().In(it => it.Id, ids).ToListAsync();
if (entitys.Count > 0)
{
try
{
//开启事务
_db.BeginTran();
//批量删除预约记录
await _db.Deleteable().In(d => d.Id, ids).ExecuteCommandAsync();
//关闭事务
_db.CommitTran();
}
catch (Exception)
{
//回滚事务
_db.RollbackTran();
throw NCCException.Oh(ErrorCode.COM1002);
}
}
}
#endregion
#region 更新预约记录
///
/// 更新预约记录
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] LqYyjlUpInput input)
{
var entity = input.Adapt();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0))
throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除预约记录
///
/// 删除预约记录
///
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0))
throw NCCException.Oh(ErrorCode.COM1002);
}
#endregion
}
}