233 lines
7.7 KiB
C#
233 lines
7.7 KiB
C#
using Admin.NET.Core.Service;
|
|
using Admin.NET.Application.Const;
|
|
using Admin.NET.Application.Entity;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
using System.Text.RegularExpressions;
|
|
namespace Admin.NET.Application;
|
|
/// <summary>
|
|
/// 单位服务
|
|
/// </summary>
|
|
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
|
|
public class SysUnitService : IDynamicApiController, ITransient
|
|
{
|
|
private readonly SqlSugarRepository<SysUnit> _rep;
|
|
public SysUnitService(SqlSugarRepository<SysUnit> rep)
|
|
{
|
|
_rep = rep;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询单位
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Page")]
|
|
public async Task<SqlSugarPagedList<SysUnitOutput>> Page(SysUnitInput input)
|
|
{
|
|
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
|
|
u.CreateOrgName.Contains(input.SearchKey.Trim())
|
|
|| u.Name.Contains(input.SearchKey.Trim())
|
|
|| u.CodeNum.Contains(input.SearchKey.Trim())
|
|
|| u.Code.Contains(input.SearchKey.Trim())
|
|
|| u.ExternalNumber.Contains(input.SearchKey.Trim())
|
|
)
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.CreateOrgName), u => u.CreateOrgName.Contains(input.CreateOrgName.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code.Trim()))
|
|
.WhereIF(!string.IsNullOrWhiteSpace(input.ExternalNumber), u => u.ExternalNumber.Contains(input.ExternalNumber.Trim()))
|
|
.WhereIF(input.GroupUnitId>0, u => u.GroupUnitId == input.GroupUnitId)
|
|
.Select<SysUnitOutput>();
|
|
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(AddSysUnitInput input)
|
|
{
|
|
var entity = input.Adapt<SysUnit>();
|
|
await _rep.InsertAsync(entity);
|
|
return entity.Id;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除单位
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "Delete")]
|
|
public async Task Delete(DeleteSysUnitInput input)
|
|
{
|
|
var entity = await _rep.GetFirstAsync(u => u.Id == input.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(UpdateSysUnitInput input)
|
|
{
|
|
var entity = input.Adapt<SysUnit>();
|
|
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单位
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "Detail")]
|
|
public async Task<SysUnit> Detail([FromQuery] QueryByIdSysUnitInput input)
|
|
{
|
|
return await _rep.GetFirstAsync(u => u.Id == input.Id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单位列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "List")]
|
|
public async Task<List<SysUnitOutput>> List()
|
|
{
|
|
return await _rep.AsQueryable().Select<SysUnitOutput>().ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取单位列表
|
|
/// </summary>
|
|
/// <param name="unitGroupId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "ListByGroupId")]
|
|
public async Task<List<SysUnitOutput>> ListByGroupId(long? unitGroupId)
|
|
{
|
|
return await _rep.AsQueryable().WhereIF(unitGroupId > 0, u => u.GroupUnitId == unitGroupId && !u.IsDelete).OrderBy(a => a.Rate).Select<SysUnitOutput>().ToListAsync();
|
|
}
|
|
public static string GetUnitScale(List<SysUnitOutput> units)
|
|
{
|
|
string result = string.Empty;
|
|
if (units.Count < 1)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
if (units.Count < 2)
|
|
{
|
|
return $"1{units.FirstOrDefault().Name}";
|
|
}
|
|
units = units.OrderBy(a => a.Rate).ToList();
|
|
units.Reverse();
|
|
var first = units.FirstOrDefault();
|
|
var second = units[1];
|
|
result += $"1{first.Name}:{first.ChildUnitCount}{second.Name}";
|
|
for (int i = 1; i < units.Count - 1; i++)
|
|
{
|
|
var current = units[i];
|
|
var next = units[i + 1];
|
|
result += $":{current.ChildUnitCount}{next.Name}";
|
|
}
|
|
return result;
|
|
}
|
|
public static string GetUnitScale(List<SysUnitOutput> units,string currentUnit)
|
|
{
|
|
string result = string.Empty;
|
|
if (units.Count < 1)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
if (units.Count < 2)
|
|
{
|
|
return $"1{units.FirstOrDefault().Name}";
|
|
}
|
|
units = units.OrderBy(a => a.Rate).ToList();
|
|
bool isStart = false;
|
|
for (int i = 0; i < units.Count - 1; i++)
|
|
{
|
|
var current = units[i];
|
|
var next = units[i + 1];
|
|
if (isStart)
|
|
{
|
|
result += $":{current.ChildUnitCount}{next.Name}";
|
|
}
|
|
if (current.Name == currentUnit)
|
|
{
|
|
result += $"1{current.Name}:{current.ChildUnitCount}{next.Name}";
|
|
isStart = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验基本单位是否唯一
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
[ApiDescriptionSettings(Name = "checkBaseUnit")]
|
|
public async Task<SysUnit> CheckBaseUnit(AddSysUnitInput input)
|
|
{
|
|
var result = await _rep.AsQueryable()
|
|
.Where(a => !a.IsDelete && a.IsBaseUnit==true)
|
|
.WhereIF(input.GroupUnitId > 0, u => u.GroupUnitId == input.GroupUnitId)
|
|
.FirstAsync();
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成编码
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
[ApiDescriptionSettings(Name = "createCodeNum")]
|
|
public async Task<string> CreateCodeNum()
|
|
{
|
|
string dt = DateTime.Now.ToString("yyyyMMdd");
|
|
var unit = await _rep.AsQueryable()
|
|
.Where(a => !a.IsDelete)
|
|
.Where(a => a.CodeNum.Contains(dt))
|
|
.OrderByDescending(t => t.CodeNum)
|
|
.FirstAsync();
|
|
if (unit != null)
|
|
{
|
|
Match match = Regex.Match(unit.CodeNum, @"\d+");
|
|
if (match.Success)
|
|
{
|
|
string numberStr = match.Value; // 获取匹配到的数字字符串
|
|
int number = int.Parse(numberStr); // 将数字字符串转换为整数
|
|
number++; // 对数字进行加1操作
|
|
|
|
// 将结果转换回字符串,并保持与原数字相同的长度
|
|
string paddedNumber = number.ToString().PadLeft(match.Value.Length, '0');
|
|
return paddedNumber;
|
|
}
|
|
else
|
|
{
|
|
return dt + "001";
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return dt + "001";
|
|
}
|
|
}
|
|
}
|
|
|