DigitalFactory/Admin.NET/Admin.NET.Application/Service/PrintData/PrintDataService.cs

207 lines
7.1 KiB
C#

using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
using Admin.NET.Application.Utils;
namespace Admin.NET.Application;
/// <summary>
/// 打印信息服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class PrintDataService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<PrintData> _rep;
private readonly CodeElementService _codeElementService;
private readonly CodeElementPropService _codeElementPropService;
private readonly CodePakageConfigurationService _codePakageConfigurationService;
public PrintDataService(SqlSugarRepository<PrintData> rep,
CodeElementService codeElementService,
CodeElementPropService codeElementPropService,
CodePakageConfigurationService codePakageConfigurationService)
{
_rep = rep;
_codeElementService = codeElementService;
_codeElementPropService = codeElementPropService;
_codePakageConfigurationService = codePakageConfigurationService;
}
/// <summary>
/// 分页查询打印信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<PrintData>> Page(PrintDataInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.BarCode.Contains(input.SearchKey.Trim())
|| u.QrCode.Contains(input.SearchKey.Trim())
|| u.Remark.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.BarCode), u => u.BarCode.Contains(input.BarCode.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.QrCode), u => u.QrCode.Contains(input.QrCode.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Remark), u => u.Remark.Contains(input.Remark.Trim()))
.Select<PrintData>();
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
/// <summary>
/// 增加打印信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(PrintData input)
{
var entity = input.Adapt<PrintData>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除打印信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(long id)
{
var entity = await _rep.GetFirstAsync(u => u.Id == id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//await _rep.FakeDeleteAsync(entity); //假删除
await _rep.DeleteAsync(entity); //真删除
}
/// <summary>
/// 更新打印信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(PrintDataInput input)
{
var entity = input.Adapt<PrintData>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取打印信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<PrintData> Detail([FromQuery] long id)
{
return await _rep.GetFirstAsync(u => u.Id == id);
}
/// <summary>
/// 获取打印信息列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "GetPrintDataList")]
public async Task<List<PrintData>> GetPrintDataList(PrintDataMaterialsInput input)
{
return await GetPrintDatas(input.UnitGroupId, input.CodeHead, input.CodeType, input.Count);
}
public async Task<List<PrintData>> GetPrintDatas(long? unitGroupId, string codeName,string codeType,int count)
{
var result = new List<PrintData>();
if (string.IsNullOrEmpty(codeName))
{
return result;
}
var elem = await _codeElementService.GetElementByName(codeName, unitGroupId);
if (elem == null)
{ return result; }
var config = await _codePakageConfigurationService.GetConfigByName(codeName, unitGroupId);
if (config == null)
{ return result; }
var elemProp = await _codeElementPropService.CodePropByElement(new CodeElementOutput() { Id = elem.Id, CodeLength = elem.CodeLength });
if (elemProp.Count > 0)
{
var prefix = (string.IsNullOrEmpty(config.CodePrefix) || codeType == "条形码") ? "" : config.CodePrefix;
elemProp = elemProp.OrderBy(a => a.Index).ToList();
for (int i = 0; i < count; i++)
{
var code = prefix + GetCodeNumByProp(elemProp, i + 1);
if (codeType == "条形码")
{
result.Add(new PrintData() { BarCode = code });
}
else
{
result.Add(new PrintData() { QrCode = code });
}
}
}
return result;
}
private string GetCodeNumByProp(List<CodeElementPropOutput> lists,int index)
{
var result = string.Empty;
for (int i = 0; i < lists.Count; i++)
{
var item = lists[i];
var format = "";
for (int j = 0; j < item.Length; j++)
{
format += "0";
}
switch (item.CodeType)
{
case "序号":
result += (Convert.ToInt32(item.CodeValue) + index).ToString(format);
break;
case "随机数":
result += CodeHelper.GetCodeRandom(item.Length);
break;
case "固定字符串":
result += item.CodeValue;
break;
case "随机字母数字":
result += CodeHelper.GetCodeLetterNum(item.Length);
break;
case "随机大小写字母":
result += CodeHelper.GetCodeLetter(item.Length);
break;
case "随机大写字母":
result += CodeHelper.GetCodeUpLetter(item.Length);
break;
case "随机小写字母":
result += CodeHelper.GetCodeDownLetter(item.Length);
break;
default:
result += (Convert.ToInt32(item.CodeValue) + 1).ToString(format);
break;
}
}
return result;
}
/// <summary>
/// 获取打印信息列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<PrintData>> List()
{
return await _rep.AsQueryable().ToListAsync();
}
}