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.LqJlmxb;
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_jlmxb;
using NCC.Extend.Entitys.Dto.LqJlmxb;
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.LqJlmxb
{
///
/// 奖励统计表服务
///
[ApiDescriptionSettings(Tag = "Extend",Name = "LqJlmxb", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqJlmxbService : ILqJlmxbService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqJlmxbRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqJlmxbService(
ISqlSugarRepository lqJlmxbRepository,
IUserManager userManager)
{
_lqJlmxbRepository = lqJlmxbRepository;
_db = _lqJlmxbRepository.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] LqJlmxbListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.mdbh), p => p.Mdbh.Contains(input.mdbh))
.WhereIF(!string.IsNullOrEmpty(input.mdmc), p => p.Mdmc.Contains(input.mdmc))
.WhereIF(!string.IsNullOrEmpty(input.gw), p => p.Gw.Contains(input.gw))
.WhereIF(!string.IsNullOrEmpty(input.jks), p => p.Jks.Contains(input.jks))
.WhereIF(!string.IsNullOrEmpty(input.ymmsk), p => p.Ymmsk.Contains(input.ymmsk))
.WhereIF(!string.IsNullOrEmpty(input.kjbmsk), p => p.Kjbmsk.Contains(input.kjbmsk))
.WhereIF(!string.IsNullOrEmpty(input.cjj), p => p.Cjj.Contains(input.cjj))
.WhereIF(!string.IsNullOrEmpty(input.pk), p => p.Pk.Contains(input.pk))
.WhereIF(!string.IsNullOrEmpty(input.ty), p => p.Ty.Contains(input.ty))
.WhereIF(!string.IsNullOrEmpty(input.db), p => p.Db.Contains(input.db))
.WhereIF(!string.IsNullOrEmpty(input.cs), p => p.Cs.Contains(input.cs))
.WhereIF(!string.IsNullOrEmpty(input.jb), p => p.Jb.Contains(input.jb))
.WhereIF(!string.IsNullOrEmpty(input.hj), p => p.Hj.Contains(input.hj))
.WhereIF(!string.IsNullOrEmpty(input.kcmx), p => p.Kcmx.Contains(input.kcmx))
.WhereIF(!string.IsNullOrEmpty(input.bhjb), p => p.Bhjb.Contains(input.bhjb))
.Select(it=> new LqJlmxbListOutput
{
id = it.Id,
mdbh=it.Mdbh,
mdmc=it.Mdmc,
gw=it.Gw,
jks=it.Jks,
ymmsk=it.Ymmsk,
kjbmsk=it.Kjbmsk,
cjj=it.Cjj,
pk=it.Pk,
ty=it.Ty,
db=it.Db,
cs=it.Cs,
jb=it.Jb,
hj=it.Hj,
kcmx=it.Kcmx,
bhjb=it.Bhjb,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建奖励统计表
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqJlmxbCrInput 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] LqJlmxbListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
.WhereIF(!string.IsNullOrEmpty(input.mdbh), p => p.Mdbh.Contains(input.mdbh))
.WhereIF(!string.IsNullOrEmpty(input.mdmc), p => p.Mdmc.Contains(input.mdmc))
.WhereIF(!string.IsNullOrEmpty(input.gw), p => p.Gw.Contains(input.gw))
.WhereIF(!string.IsNullOrEmpty(input.jks), p => p.Jks.Contains(input.jks))
.WhereIF(!string.IsNullOrEmpty(input.ymmsk), p => p.Ymmsk.Contains(input.ymmsk))
.WhereIF(!string.IsNullOrEmpty(input.kjbmsk), p => p.Kjbmsk.Contains(input.kjbmsk))
.WhereIF(!string.IsNullOrEmpty(input.cjj), p => p.Cjj.Contains(input.cjj))
.WhereIF(!string.IsNullOrEmpty(input.pk), p => p.Pk.Contains(input.pk))
.WhereIF(!string.IsNullOrEmpty(input.ty), p => p.Ty.Contains(input.ty))
.WhereIF(!string.IsNullOrEmpty(input.db), p => p.Db.Contains(input.db))
.WhereIF(!string.IsNullOrEmpty(input.cs), p => p.Cs.Contains(input.cs))
.WhereIF(!string.IsNullOrEmpty(input.jb), p => p.Jb.Contains(input.jb))
.WhereIF(!string.IsNullOrEmpty(input.hj), p => p.Hj.Contains(input.hj))
.WhereIF(!string.IsNullOrEmpty(input.kcmx), p => p.Kcmx.Contains(input.kcmx))
.WhereIF(!string.IsNullOrEmpty(input.bhjb), p => p.Bhjb.Contains(input.bhjb))
.Select(it=> new LqJlmxbListOutput
{
id = it.Id,
mdbh=it.Mdbh,
mdmc=it.Mdmc,
gw=it.Gw,
jks=it.Jks,
ymmsk=it.Ymmsk,
kjbmsk=it.Kjbmsk,
cjj=it.Cjj,
pk=it.Pk,
ty=it.Ty,
db=it.Db,
cs=it.Cs,
jb=it.Jb,
hj=it.Hj,
kcmx=it.Kcmx,
bhjb=it.Bhjb,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToListAsync();
return data;
}
///
/// 导出奖励统计表
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqJlmxbListQueryInput 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\":\"mdbh\"},{\"value\":\"门店名称\",\"field\":\"mdmc\"},{\"value\":\"岗位\",\"field\":\"gw\"},{\"value\":\"健康师\",\"field\":\"jks\"},{\"value\":\"医美秒杀卡\",\"field\":\"ymmsk\"},{\"value\":\"科技部秒杀卡\",\"field\":\"kjbmsk\"},{\"value\":\"成交奖\",\"field\":\"cjj\"},{\"value\":\"PK\",\"field\":\"pk\"},{\"value\":\"体验\",\"field\":\"ty\"},{\"value\":\"打版\",\"field\":\"db\"},{\"value\":\"沉睡\",\"field\":\"cs\"},{\"value\":\"嘉宾\",\"field\":\"jb\"},{\"value\":\"合计\",\"field\":\"hj\"},{\"value\":\"扣除明细\",\"field\":\"kcmx\"},{\"value\":\"不含嘉宾\",\"field\":\"bhjb\"},]".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] LqJlmxbUpInput 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);
}
}
}