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.LqXhmxb;
using NCC.Extend.Entitys.lq_xhmxb;
using NCC.Extend.Interfaces.LqXhmxb;
using NCC.FriendlyException;
using NCC.JsonSerialization;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend.LqXhmxb
{
///
/// 消耗明细表服务
///
[ApiDescriptionSettings(Tag = "Extend", Name = "LqXhmxb", Order = 200)]
[Route("api/Extend/[controller]")]
public class LqXhmxbService : ILqXhmxbService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _lqXhmxbRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public LqXhmxbService(ISqlSugarRepository lqXhmxbRepository, IUserManager userManager)
{
_lqXhmxbRepository = lqXhmxbRepository;
_db = _lqXhmxbRepository.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] LqXhmxbListQueryInput 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.bmbh), p => p.Bmbh.Contains(input.bmbh))
.WhereIF(!string.IsNullOrEmpty(input.bmmc), p => p.Bmmc.Contains(input.bmmc))
.WhereIF(!string.IsNullOrEmpty(input.ygbh), p => p.Ygbh.Contains(input.ygbh))
.WhereIF(!string.IsNullOrEmpty(input.ygmc), p => p.Ygmc.Contains(input.ygmc))
.WhereIF(!string.IsNullOrEmpty(input.xms), p => p.Xms.Contains(input.xms))
.WhereIF(!string.IsNullOrEmpty(input.xh), p => p.Xh.Contains(input.xh))
.WhereIF(!string.IsNullOrEmpty(input.sg), p => p.Sg.Contains(input.sg))
.WhereIF(!string.IsNullOrEmpty(input.qtsg1), p => p.Qtsg1.Contains(input.qtsg1))
.WhereIF(!string.IsNullOrEmpty(input.qtsg2), p => p.Qtsg2.Contains(input.qtsg2))
.WhereIF(!string.IsNullOrEmpty(input.qtsg3), p => p.Qtsg3.Contains(input.qtsg3))
.WhereIF(!string.IsNullOrEmpty(input.sghj), p => p.Sghj.Contains(input.sghj))
.Select(it => new LqXhmxbListOutput
{
id = it.Id,
mdbh = it.Mdbh,
mdmc = it.Mdmc,
bmbh = it.Bmbh,
bmmc = it.Bmmc,
ygbh = it.Ygbh,
ygmc = it.Ygmc,
xms = it.Xms,
xh = it.Xh,
sg = it.Sg,
qtsg1 = it.Qtsg1,
qtsg2 = it.Qtsg2,
qtsg3 = it.Qtsg3,
sghj = it.Sghj,
})
.MergeTable()
.OrderBy(sidx + " " + input.sort)
.ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建消耗明细表
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] LqXhmxbCrInput 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] LqXhmxbListQueryInput 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.bmbh), p => p.Bmbh.Contains(input.bmbh))
.WhereIF(!string.IsNullOrEmpty(input.bmmc), p => p.Bmmc.Contains(input.bmmc))
.WhereIF(!string.IsNullOrEmpty(input.ygbh), p => p.Ygbh.Contains(input.ygbh))
.WhereIF(!string.IsNullOrEmpty(input.ygmc), p => p.Ygmc.Contains(input.ygmc))
.WhereIF(!string.IsNullOrEmpty(input.xms), p => p.Xms.Contains(input.xms))
.WhereIF(!string.IsNullOrEmpty(input.xh), p => p.Xh.Contains(input.xh))
.WhereIF(!string.IsNullOrEmpty(input.sg), p => p.Sg.Contains(input.sg))
.WhereIF(!string.IsNullOrEmpty(input.qtsg1), p => p.Qtsg1.Contains(input.qtsg1))
.WhereIF(!string.IsNullOrEmpty(input.qtsg2), p => p.Qtsg2.Contains(input.qtsg2))
.WhereIF(!string.IsNullOrEmpty(input.qtsg3), p => p.Qtsg3.Contains(input.qtsg3))
.WhereIF(!string.IsNullOrEmpty(input.sghj), p => p.Sghj.Contains(input.sghj))
.Select(it => new LqXhmxbListOutput
{
id = it.Id,
mdbh = it.Mdbh,
mdmc = it.Mdmc,
bmbh = it.Bmbh,
bmmc = it.Bmmc,
ygbh = it.Ygbh,
ygmc = it.Ygmc,
xms = it.Xms,
xh = it.Xh,
sg = it.Sg,
qtsg1 = it.Qtsg1,
qtsg2 = it.Qtsg2,
qtsg3 = it.Qtsg3,
sghj = it.Sghj,
})
.MergeTable()
.OrderBy(sidx + " " + input.sort)
.ToListAsync();
return data;
}
///
/// 导出消耗明细表
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] LqXhmxbListQueryInput 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\":\"bmbh\"},{\"value\":\"部门名称\",\"field\":\"bmmc\"},{\"value\":\"员工编号\",\"field\":\"ygbh\"},{\"value\":\"员工名称\",\"field\":\"ygmc\"},{\"value\":\"项目数\",\"field\":\"xms\"},{\"value\":\"消耗\",\"field\":\"xh\"},{\"value\":\"手工\",\"field\":\"sg\"},{\"value\":\"其它手工1\",\"field\":\"qtsg1\"},{\"value\":\"其它手工2\",\"field\":\"qtsg2\"},{\"value\":\"其它手工3\",\"field\":\"qtsg3\"},{\"value\":\"手工合计\",\"field\":\"sghj\"},]".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] LqXhmxbUpInput 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);
}
}
}