BookingHeChuan/Myshipping.Core/Service/DjyTenantLine/DjyTenantLineService.cs

114 lines
3.9 KiB
C#

using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Core.Entity;
using Microsoft.Extensions.Logging;
using Furion.FriendlyException;
using System;
namespace Myshipping.Core.Service
{
/// <summary>
/// 租户航线维护
/// </summary>
[ApiDescriptionSettings(Name = "DjyTenantLine", Order = 1)]
public class DjyTenantLineService : IDjyTenantLineService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<DjyTenantLine> _rep;
private readonly ISysCacheService _cache;
private readonly ILogger<DjyTenantLine> _logger;
public DjyTenantLineService(SqlSugarRepository<DjyTenantLine> rep, ISysCacheService cache, ILogger<DjyTenantLine> logger)
{
_rep = rep;
_cache = cache;
_logger = logger;
}
/// <summary>
/// 分页查询网站账号维护
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyTenantLine/page")]
public async Task<dynamic> Page([FromQuery] QueryDjyTenantLineInput input)
{
var entities = await _rep.AsQueryable()
.Where(m => m.TenantId == UserManager.TENANT_ID)
.WhereIF(!string.IsNullOrWhiteSpace(input.LineCode), u => u.LineCode.Contains(input.LineCode.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.LineName), u => u.LineName.Contains(input.LineName.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 保存租户航线
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyTenantLine/save")]
public async Task<long> Save(SaveDjyTenantLineInput input)
{
var lineLine = await _cache.GetAllCodeLane();
var cc = _rep.Count(x => x.LineName == input.LineName);
if (lineLine.Count(x => x.CnName == input.LineName) > 0 || cc > 0)
{
throw Oops.Bah($"已存在同名航线:{input.LineName}");
}
DjyTenantLine model = null;
if (input.Id > 0)
{
model = _rep.FirstOrDefault(x => x.Id == input.Id);
input.Adapt(model);
await _rep.UpdateAsync(model);
}
else
{
model = input.Adapt<DjyTenantLine>();
await _rep.InsertAsync(model);
}
return model.Id;
}
/// <summary>
/// 删除租户航线
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("/DjyTenantLine/delete")]
public async Task Delete(long id)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == id);
await _rep.DeleteAsync(entity);
}
/// <summary>
/// 查询当前租户航线列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyTenantLine/list")]
public async Task<dynamic> List([FromQuery] ListDjyTenantLineInput input)
{
var sysLines = await _cache.GetAllCodeLane();
var entities = await _rep.AsQueryable()
.Where(m => m.TenantId == UserManager.TENANT_ID)
.WhereIF(!string.IsNullOrWhiteSpace(input.LineName), u => u.LineName.Contains(input.LineName.Trim()))
.Select(x => x.LineName)
.ToListAsync();
var result = sysLines.Select(x => x.CnName).ToList();
result.AddRange(entities);
return result;
}
}
}