using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Extend.Interfaces.LqRyzl;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys.lq_ryzl;
using NCC.Extend.Entitys.Dto.LqRyzl;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.Common.Model.NPOI;
using NCC.Common.Configuration;
using NCC.DataEncryption;
using NCC.ClayObject;
namespace NCC.Extend.LqRyzl
{
///
/// 人员资料服务
///
[ApiDescriptionSettings(Tag = "Extend",Name = "LqRyzl", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqRyzlService : ILqRyzlService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqRyzlRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqRyzlService(
ISqlSugarRepository lqRyzlRepository,
IUserManager userManager)
{
_lqRyzlRepository = lqRyzlRepository;
_db = _lqRyzlRepository.Context;
_userManager = userManager;
}
///
/// 获取人员资料
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
return output;
}
///
/// 获取人员资料列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] LqRyzlListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryRzsj = input.rzsj != null ? input.rzsj.Split(',').ToObeject>() : null;
DateTime? startRzsj = queryRzsj != null ? Ext.GetDateTime(queryRzsj.First()) : null;
DateTime? endRzsj = queryRzsj != null ? Ext.GetDateTime(queryRzsj.Last()) : null;
List queryLzsj = input.lzsj != null ? input.lzsj.Split(',').ToObeject>() : null;
DateTime? startLzsj = queryLzsj != null ? Ext.GetDateTime(queryLzsj.First()) : null;
DateTime? endLzsj = queryLzsj != null ? Ext.GetDateTime(queryLzsj.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.dm), p => p.Dm.Contains(input.dm))
.WhereIF(!string.IsNullOrEmpty(input.zw), p => p.Zw.Contains(input.zw))
.WhereIF(!string.IsNullOrEmpty(input.fyft), p => p.Fyft.Equals(input.fyft))
.WhereIF(!string.IsNullOrEmpty(input.gwfl1), p => p.Gwfl1.Equals(input.gwfl1))
.WhereIF(!string.IsNullOrEmpty(input.xm), p => p.Xm.Contains(input.xm))
.WhereIF(!string.IsNullOrEmpty(input.zzqk), p => p.Zzqk.Equals(input.zzqk))
.WhereIF(!string.IsNullOrEmpty(input.gwfl2), p => p.Gwfl2.Equals(input.gwfl2))
.WhereIF(queryRzsj != null, p => p.Rzsj >= new DateTime(startRzsj.ToDate().Year, startRzsj.ToDate().Month, startRzsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryRzsj != null, p => p.Rzsj <= new DateTime(endRzsj.ToDate().Year, endRzsj.ToDate().Month, endRzsj.ToDate().Day, 23, 59, 59))
.WhereIF(queryLzsj != null, p => p.Lzsj >= new DateTime(startLzsj.ToDate().Year, startLzsj.ToDate().Month, startLzsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryLzsj != null, p => p.Lzsj <= new DateTime(endLzsj.ToDate().Year, endLzsj.ToDate().Month, endLzsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.zd), p => p.Zd.Contains(input.zd))
.WhereIF(!string.IsNullOrEmpty(input.yl1), p => p.Yl1.Contains(input.yl1))
.WhereIF(!string.IsNullOrEmpty(input.yl2), p => p.Yl2.Contains(input.yl2))
.WhereIF(!string.IsNullOrEmpty(input.yl3), p => p.Yl3.Contains(input.yl3))
.WhereIF(!string.IsNullOrEmpty(input.sjh), p => p.Sjh.Contains(input.sjh))
.Select(it=> new LqRyzlListOutput
{
id = it.Id,
dm=it.Dm,
zw=it.Zw,
fyft=it.Fyft,
gwfl1=it.Gwfl1,
xm=it.Xm,
zzqk=it.Zzqk,
gwfl2=it.Gwfl2,
rzsj=it.Rzsj,
lzsj=it.Lzsj,
zd=it.Zd,
yl1=it.Yl1,
yl2=it.Yl2,
yl3=it.Yl3,
sjh=it.Sjh,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建人员资料
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqRyzlCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
///
/// 获取人员资料无分页列表
///
/// 请求参数
///
[NonAction]
public async Task GetNoPagingList([FromQuery] LqRyzlListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryRzsj = input.rzsj != null ? input.rzsj.Split(',').ToObeject>() : null;
DateTime? startRzsj = queryRzsj != null ? Ext.GetDateTime(queryRzsj.First()) : null;
DateTime? endRzsj = queryRzsj != null ? Ext.GetDateTime(queryRzsj.Last()) : null;
List queryLzsj = input.lzsj != null ? input.lzsj.Split(',').ToObeject>() : null;
DateTime? startLzsj = queryLzsj != null ? Ext.GetDateTime(queryLzsj.First()) : null;
DateTime? endLzsj = queryLzsj != null ? Ext.GetDateTime(queryLzsj.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.dm), p => p.Dm.Contains(input.dm))
.WhereIF(!string.IsNullOrEmpty(input.zw), p => p.Zw.Contains(input.zw))
.WhereIF(!string.IsNullOrEmpty(input.fyft), p => p.Fyft.Equals(input.fyft))
.WhereIF(!string.IsNullOrEmpty(input.gwfl1), p => p.Gwfl1.Equals(input.gwfl1))
.WhereIF(!string.IsNullOrEmpty(input.xm), p => p.Xm.Contains(input.xm))
.WhereIF(!string.IsNullOrEmpty(input.zzqk), p => p.Zzqk.Equals(input.zzqk))
.WhereIF(!string.IsNullOrEmpty(input.gwfl2), p => p.Gwfl2.Equals(input.gwfl2))
.WhereIF(queryRzsj != null, p => p.Rzsj >= new DateTime(startRzsj.ToDate().Year, startRzsj.ToDate().Month, startRzsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryRzsj != null, p => p.Rzsj <= new DateTime(endRzsj.ToDate().Year, endRzsj.ToDate().Month, endRzsj.ToDate().Day, 23, 59, 59))
.WhereIF(queryLzsj != null, p => p.Lzsj >= new DateTime(startLzsj.ToDate().Year, startLzsj.ToDate().Month, startLzsj.ToDate().Day, 0, 0, 0))
.WhereIF(queryLzsj != null, p => p.Lzsj <= new DateTime(endLzsj.ToDate().Year, endLzsj.ToDate().Month, endLzsj.ToDate().Day, 23, 59, 59))
.WhereIF(!string.IsNullOrEmpty(input.zd), p => p.Zd.Contains(input.zd))
.WhereIF(!string.IsNullOrEmpty(input.yl1), p => p.Yl1.Contains(input.yl1))
.WhereIF(!string.IsNullOrEmpty(input.yl2), p => p.Yl2.Contains(input.yl2))
.WhereIF(!string.IsNullOrEmpty(input.yl3), p => p.Yl3.Contains(input.yl3))
.WhereIF(!string.IsNullOrEmpty(input.sjh), p => p.Sjh.Contains(input.sjh))
.Select(it=> new LqRyzlListOutput
{
id = it.Id,
dm=it.Dm,
zw=it.Zw,
fyft=it.Fyft,
gwfl1=it.Gwfl1,
xm=it.Xm,
zzqk=it.Zzqk,
gwfl2=it.Gwfl2,
rzsj=it.Rzsj,
lzsj=it.Lzsj,
zd=it.Zd,
yl1=it.Yl1,
yl2=it.Yl2,
yl3=it.Yl3,
sjh=it.Sjh,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToListAsync();
return data;
}
///
/// 导出人员资料
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqRyzlListQueryInput 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\":\"dm\"},{\"value\":\"职位\",\"field\":\"zw\"},{\"value\":\"费用分摊\",\"field\":\"fyft\"},{\"value\":\"岗位分类1\",\"field\":\"gwfl1\"},{\"value\":\"姓名\",\"field\":\"xm\"},{\"value\":\"在职情况\",\"field\":\"zzqk\"},{\"value\":\"岗位分类2\",\"field\":\"gwfl2\"},{\"value\":\"入职时间\",\"field\":\"rzsj\"},{\"value\":\"离职时间\",\"field\":\"lzsj\"},{\"value\":\"战队\",\"field\":\"zd\"},{\"value\":\"预留1\",\"field\":\"yl1\"},{\"value\":\"预留2\",\"field\":\"yl2\"},{\"value\":\"预留3\",\"field\":\"yl3\"},{\"value\":\"手机号\",\"field\":\"sjh\"},]".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;
}
///
/// 批量删除人员资料
///
/// 主键数组
///
[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);
}
}
}
///
/// 更新人员资料
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] LqRyzlUpInput input)
{
var entity = input.Adapt();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
///
/// 删除人员资料
///
///
[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);
}
}
}