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.LqJsfa;
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_jsfa;
using NCC.Extend.Entitys.Dto.LqJsfa;
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.LqJsfa
{
///
/// 总经理经理核算条件方案管理服务
///
[ApiDescriptionSettings(Tag = "Extend",Name = "LqJsfa", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqJsfaService : ILqJsfaService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqJsfaRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个 类型的新实例
///
public LqJsfaService(
ISqlSugarRepository lqJsfaRepository,
IUserManager userManager)
{
_lqJsfaRepository = lqJsfaRepository;
_db = _lqJsfaRepository.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] LqJsfaListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryJd1 = input.jd1 != null ? input.jd1.Split(',').ToObeject>() : null;
var startJd1 = input.jd1 != null && !string.IsNullOrEmpty(queryJd1.First().ToString()) ? queryJd1.First() : decimal.MinValue;
var endJd1 = input.jd1 != null && !string.IsNullOrEmpty(queryJd1.Last().ToString()) ? queryJd1.Last() : decimal.MaxValue;
List queryJd2 = input.jd2 != null ? input.jd2.Split(',').ToObeject>() : null;
var startJd2 = input.jd2 != null && !string.IsNullOrEmpty(queryJd2.First().ToString()) ? queryJd2.First() : decimal.MinValue;
var endJd2 = input.jd2 != null && !string.IsNullOrEmpty(queryJd2.Last().ToString()) ? queryJd2.Last() : decimal.MaxValue;
List queryJd3 = input.jd3 != null ? input.jd3.Split(',').ToObeject>() : null;
var startJd3 = input.jd3 != null && !string.IsNullOrEmpty(queryJd3.First().ToString()) ? queryJd3.First() : decimal.MinValue;
var endJd3 = input.jd3 != null && !string.IsNullOrEmpty(queryJd3.Last().ToString()) ? queryJd3.Last() : decimal.MaxValue;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.famc), p => p.Famc.Contains(input.famc))
.WhereIF(queryJd1 != null, p => SqlFunc.Between(p.Jd1, startJd1, endJd1))
.WhereIF(queryJd2 != null, p => SqlFunc.Between(p.Jd2, startJd2, endJd2))
.WhereIF(queryJd3 != null, p => SqlFunc.Between(p.Jd3, startJd3, endJd3))
.Select(it=> new LqJsfaListOutput
{
id = it.Id,
famc=it.Famc,
jd1=it.Jd1,
jd2=it.Jd2,
jd3=it.Jd3,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建总经理经理核算条件方案管理
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqJsfaCrInput 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] LqJsfaListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
List queryJd1 = input.jd1 != null ? input.jd1.Split(',').ToObeject>() : null;
var startJd1 = input.jd1 != null && !string.IsNullOrEmpty(queryJd1.First().ToString()) ? queryJd1.First() : decimal.MinValue;
var endJd1 = input.jd1 != null && !string.IsNullOrEmpty(queryJd1.Last().ToString()) ? queryJd1.Last() : decimal.MaxValue;
List queryJd2 = input.jd2 != null ? input.jd2.Split(',').ToObeject>() : null;
var startJd2 = input.jd2 != null && !string.IsNullOrEmpty(queryJd2.First().ToString()) ? queryJd2.First() : decimal.MinValue;
var endJd2 = input.jd2 != null && !string.IsNullOrEmpty(queryJd2.Last().ToString()) ? queryJd2.Last() : decimal.MaxValue;
List queryJd3 = input.jd3 != null ? input.jd3.Split(',').ToObeject>() : null;
var startJd3 = input.jd3 != null && !string.IsNullOrEmpty(queryJd3.First().ToString()) ? queryJd3.First() : decimal.MinValue;
var endJd3 = input.jd3 != null && !string.IsNullOrEmpty(queryJd3.Last().ToString()) ? queryJd3.Last() : decimal.MaxValue;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.famc), p => p.Famc.Contains(input.famc))
.WhereIF(queryJd1 != null, p => SqlFunc.Between(p.Jd1, startJd1, endJd1))
.WhereIF(queryJd2 != null, p => SqlFunc.Between(p.Jd2, startJd2, endJd2))
.WhereIF(queryJd3 != null, p => SqlFunc.Between(p.Jd3, startJd3, endJd3))
.Select(it=> new LqJsfaListOutput
{
id = it.Id,
famc=it.Famc,
jd1=it.Jd1,
jd2=it.Jd2,
jd3=it.Jd3,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToListAsync();
return data;
}
///
/// 导出总经理经理核算条件方案管理
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqJsfaListQueryInput 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\":\"famc\"},{\"value\":\"阶段一\",\"field\":\"jd1\"},{\"value\":\"阶段二\",\"field\":\"jd2\"},{\"value\":\"阶段三\",\"field\":\"jd3\"},]".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] LqJsfaUpInput 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);
}
}
}