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 Furion.Logging; using Microsoft.Extensions.Logging; using Myshipping.Core.Entity; using Furion.FriendlyException; namespace Myshipping.Core.Service { /// /// EDI参数设置服务 /// [ApiDescriptionSettings(Name = "DjyEdiSetting", Order = 1)] public class DjyEdiSettingService : IDjyEdiSettingService, IDynamicApiController, ITransient { private readonly SqlSugarRepository _rep; private readonly ISysCacheService _cacheService; private readonly ILogger _logger; public DjyEdiSettingService(SqlSugarRepository rep, ILogger logger, ISysCacheService cacheService) { _rep = rep; _logger = logger; _cacheService = cacheService; } /// /// 分页查询EDI参数设置 /// /// /// [HttpGet("/DjyEdiSetting/page")] public async Task Page([FromQuery] QueryDjyEdiSettingInput input) { var entities = await _rep.AsQueryable().Filter(null, true) .WhereIF(!string.IsNullOrWhiteSpace(input.EDICODE), u => u.EDICODE == input.EDICODE) .WhereIF(!string.IsNullOrWhiteSpace(input.EDINAME), u => u.EDINAME.Contains(input.EDINAME.Trim())) .WhereIF(!string.IsNullOrWhiteSpace(input.CARRIERID), u => u.CARRIERID.Contains(input.CARRIERID.Trim())) .WhereIF(input.TenantId > 0, u => u.TenantId == input.TenantId) .WhereIF(!string.IsNullOrWhiteSpace(input.TenantName), u => u.TenantName.Contains(input.TenantName.Trim())) .ToPagedListAsync(input.PageNo, input.PageSize); return entities.XnPagedResult(); } /// /// 增加EDI参数设置(准备作废,使用save接口代替) /// /// /// [HttpPost("/DjyEdiSetting/add")] public async Task Add(AddDjyEdiSettingInput input) { var cc = _rep.AsQueryable() .Filter(null, true) .Count(x => x.EDICODE == input.EDICODE && x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID && x.SendType == input.SendType); if (cc > 0) { throw Oops.Bah($"该租户({input.TenantName})已存在相同类型({input.EDICODE})、相同船司({input.CARRIERID})、相同发送类型({input.SendType})的参数设置"); } var entity = input.Adapt(); await _rep.InsertAsync(entity); await CacheData(); return entity.Id; } /// /// 保存EDI参数设置 /// /// /// [HttpPost("/DjyEdiSetting/save")] public async Task Save(UpdateDjyEdiSettingInput input) { DjyEdiSetting entity = null; if (input.Id == 0) //新增 { var cc = _rep.AsQueryable() .Filter(null, true) .Count(x => x.EDICODE == input.EDICODE && x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID && x.SendType == input.SendType); if (cc > 0) { throw Oops.Bah($"该租户({input.TenantName})已存在相同类型({input.EDICODE})、相同船司({input.CARRIERID})、相同发送类型({input.SendType})的参数设置"); } entity = input.Adapt(); await _rep.InsertAsync(entity); //其他同船司、同发送类型的都禁用 var otherList = _rep.Where(x => x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID && x.SendType == input.SendType && x.Id != entity.Id).ToList(); foreach (var item in otherList) { item.EnableFlag = false; await _rep.UpdateAsync(item); } } else { entity = _rep.AsQueryable() .Filter(null, true) .First(x => x.Id == input.Id); if (entity == null) { throw Oops.Bah($"未找到数据"); } var cc = _rep.AsQueryable().Filter(null, true) .Count(x => x.EDICODE == input.EDICODE && x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID && x.SendType == input.SendType && x.Id != input.Id); if (cc > 0) { throw Oops.Bah($"该租户({input.TenantName})已存在相同类型({input.EDICODE})、相同船司({input.CARRIERID})、相同发送类型({input.SendType})的参数设置"); } entity = input.Adapt(entity); await _rep.UpdateAsync(entity); } await CacheData(); return entity.Id; } /// /// 更新EDI参数设置(准备作废,使用save接口代替) /// /// /// [HttpPost("/DjyEdiSetting/edit")] public async Task Update(UpdateDjyEdiSettingInput input) { var entity = _rep.AsQueryable() .Filter(null, true) .First(x => x.Id == input.Id); if (entity == null) { throw Oops.Bah($"未找到数据"); } var cc = _rep.AsQueryable().Filter(null, true) .Count(x => x.EDICODE == input.EDICODE && x.TenantId == input.TenantId && x.CARRIERID == input.CARRIERID && x.SendType == input.SendType && x.Id != input.Id); if (cc > 0) { throw Oops.Bah($"该租户({input.TenantName})已存在相同类型({input.EDICODE})、相同船司({input.CARRIERID})、相同发送类型({input.SendType})的参数设置"); } entity = input.Adapt(entity); await _rep.UpdateAsync(entity); await CacheData(); return entity.Id; } /// /// 设置启用(启用后,同船司、同发送类型的其他EDI通道会被取消启用) /// /// /// [HttpPost("/DjyEdiSetting/SetEnable")] public async Task SetEnable(long id) { var entity = _rep.AsQueryable() .Filter(null, true) .First(x => x.Id == id); if (entity == null) { throw Oops.Bah($"未找到数据"); } entity.EnableFlag = true; await _rep.UpdateAsync(entity); //其他同船司、同发送类型的都禁用 var otherList = _rep.Where(x => x.TenantId == entity.TenantId && x.CARRIERID == entity.CARRIERID && x.SendType == entity.SendType && x.Id != entity.Id).ToList(); foreach (var item in otherList) { item.EnableFlag = false; await _rep.UpdateAsync(item); } await CacheData(); } /// /// 删除EDI参数设置 /// /// /// [HttpPost("/DjyEdiSetting/delete")] public async Task Delete(GetDjyEdiSettingInput input) { var entity = await _rep.AsQueryable().Filter(null, true) .FirstAsync(u => u.Id == input.Id); if (entity == null) { throw Oops.Bah($"未找到数据"); } await _rep.DeleteAsync(entity); await CacheData(); } /// /// 获取EDI参数设置 /// /// /// [HttpGet("/DjyEdiSetting/detail")] public async Task Get([FromQuery] GetDjyEdiSettingInput input) { return await _rep.AsQueryable().Filter(null, true).FirstAsync(u => u.Id == input.Id); } /// /// 缓存数据 /// /// [NonAction] public async Task CacheData(bool flag = false) { if (flag) { if (!_cacheService.Exists(CommonConst.CACHE_KEY_DJY_EDI_SETTING)) { var list = _rep.AsQueryable().Filter(null, true).OrderBy(x => x.EDINAME).ToList(); await _cacheService.SetAllEdiSetting(list); } } else { var list = _rep.AsQueryable().Filter(null, true).OrderBy(x => x.EDINAME).ToList(); await _cacheService.SetAllEdiSetting(list); } } ///// ///// 获取EDI参数设置列表 ///// ///// ///// //[HttpGet("/DjyEdiSetting/list")] //public async Task List([FromQuery] QueryDjyEdiSettingInput input) //{ // return await _rep.ToListAsync(); //} } }