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.Application.Entity; using Microsoft.Extensions.Logging; using Furion.FriendlyException; using Myshipping.Application.Enum; using System.ComponentModel; using System.Collections.Generic; using System.IO; using MiniExcelLibs; using NPOI.HSSF.UserModel; using Myshipping.Core.Helper; using NPOI.SS.UserModel; using Furion; using System; using System.Web; using System.Text; using Myshipping.Application.ConfigOption; using Myshipping.Core.Service; namespace Myshipping.Application { /// /// 船司与场站、订舱代理等对应关系 /// [ApiDescriptionSettings("Application", Name = "BookingCarrierRelation", Order = 1)] public class BookingCarrierRelationService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _repCarrierYard; private readonly SqlSugarRepository _repCarrierAgent; private readonly ILogger _logger; private readonly ISysCacheService _cache; public BookingCarrierRelationService(SqlSugarRepository repCarrierYard, SqlSugarRepository repCarrierAgent, ILogger logger, ISysCacheService cache) { _repCarrierYard = repCarrierYard; _repCarrierAgent = repCarrierAgent; _logger = logger; _cache = cache; } #region 船司场站对应关系 /// /// 分页查询船司场站对应关系 /// /// /// [HttpGet("/BookingCarrierRelation/pageYard")] public async Task> PageYard([FromQuery] CarrierYardRelationQueryInput input) { var entities = await _repCarrierYard.AsQueryable() .WhereIF(!string.IsNullOrEmpty(input.CarrierCode), u => u.CarrierCode.Contains(input.CarrierCode)) .WhereIF(!string.IsNullOrEmpty(input.Carrier), u => u.Carrier.Contains(input.Carrier)) .WhereIF(!string.IsNullOrEmpty(input.YardCode), u => u.YardCode.Contains(input.YardCode)) .WhereIF(!string.IsNullOrEmpty(input.Yard), u => u.Yard.Contains(input.Yard)) .ToPagedListAsync(input.PageNo, input.PageSize); var rtn = entities.Adapt>(); return rtn; } /// /// 保存船司场站对应关系 /// /// /// [HttpPost("/BookingCarrierRelation/saveYard")] public async Task SaveYard(CarrierYardRelationSaveInput input) { BookingCarrierYardRelation model = null; if (input.Id > 0) { if (_repCarrierYard.Count(x => x.Carrier == input.Carrier && x.Yard == input.Yard && x.Id != input.Id) > 0) { throw Oops.Bah($"已存在相同代码:{input.Carrier} {input.Yard}"); } model = _repCarrierYard.FirstOrDefault(x => x.Id == input.Id); input.Adapt(model); await _repCarrierYard.UpdateAsync(model); } else { if (_repCarrierYard.Count(x => x.Carrier == input.Carrier && x.Yard == input.Yard && x.Id != input.Id) > 0) { throw Oops.Bah($"已存在相同代码:{input.Carrier} {input.Yard}"); } model = input.Adapt(); await _repCarrierYard.InsertAsync(model); } return model.Id; } /// /// 删除船司场站对应关系 /// /// /// [HttpPost("/BookingCarrierRelation/deleteYard")] public async Task DeleteYard(long id) { var entity = await _repCarrierYard.FirstOrDefaultAsync(u => u.Id == id); entity.IsDeleted = true; await _repCarrierYard.UpdateAsync(entity); } /// /// 根据船司代码获取可以选择的场站 /// /// [HttpGet("/BookingCarrierRelation/ListYardByCarrier")] public async Task ListYardByCarrier(string carrierCode) { return await _repCarrierYard.AsQueryable() .Where(u => u.CarrierCode == carrierCode) .Select(x => new { x.Yard, x.YardCode }) .ToListAsync(); } #endregion #region 船司订舱代理对应关系 /// /// 分页查询船司订舱代理对应关系 /// /// /// [HttpGet("/BookingCarrierRelation/pageAgent")] public async Task> PageAgent([FromQuery] CarrierAgentRelationQueryInput input) { var entities = await _repCarrierAgent.AsQueryable() .WhereIF(!string.IsNullOrEmpty(input.CarrierCode), u => u.CarrierCode.Contains(input.CarrierCode)) .WhereIF(!string.IsNullOrEmpty(input.Carrier), u => u.Carrier.Contains(input.Carrier)) .WhereIF(!string.IsNullOrEmpty(input.AgentCode), u => u.AgentCode.Contains(input.AgentCode)) .WhereIF(!string.IsNullOrEmpty(input.Agent), u => u.Agent.Contains(input.Agent)) .ToPagedListAsync(input.PageNo, input.PageSize); var rtn = entities.Adapt>(); return rtn; } /// /// 保存船司订舱代理对应关系 /// /// /// [HttpPost("/BookingCarrierRelation/saveAgent")] public async Task SaveAgent(CarrierAgentRelationSaveInput input) { BookingCarrierAgentRelation model = null; if (input.Id > 0) { if (_repCarrierAgent.Count(x => x.Carrier == input.Carrier && x.Agent == input.Agent && x.Id != input.Id) > 0) { throw Oops.Bah($"已存在相同代码:{input.Carrier} {input.Agent}"); } model = _repCarrierAgent.FirstOrDefault(x => x.Id == input.Id); input.Adapt(model); await _repCarrierAgent.UpdateAsync(model); } else { if (_repCarrierAgent.Count(x => x.Carrier == input.Carrier && x.Agent == input.Agent && x.Id != input.Id) > 0) { throw Oops.Bah($"已存在相同代码:{input.Carrier} {input.Agent}"); } model = input.Adapt(); await _repCarrierAgent.InsertAsync(model); } return model.Id; } /// /// 删除船司订舱代理对应关系 /// /// /// [HttpPost("/BookingCarrierRelation/deleteAgent")] public async Task DeleteAgent(long id) { var entity = await _repCarrierAgent.FirstOrDefaultAsync(u => u.Id == id); entity.IsDeleted = true; await _repCarrierAgent.UpdateAsync(entity); } /// /// 根据船司代码获取可以选择的订舱代理 /// /// [HttpGet("/BookingCarrierRelation/ListAgentByCarrier")] public async Task ListAgentByCarrier(string carrierCode) { return await _repCarrierAgent.AsQueryable() .Where(u => u.CarrierCode == carrierCode) .Select(x => new { x.Agent, x.AgentCode }) .ToListAsync(); } #endregion } }