using Furion.DependencyInjection; using Furion.DynamicApiController; using Furion.EventBus; using Furion.FriendlyException; using Mapster; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Myshipping.Application.Entity; using Myshipping.Application.Service.Fee.Dto; using Myshipping.Core; using Myshipping.Core.Entity; using Myshipping.Core.Service; using SqlSugar; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Myshipping.Application { /// /// 费用录入 /// [ApiDescriptionSettings("Application", Name = "FeeRecord", Order = 1)] public class FeeRecordService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _repRecord; private readonly ILogger _logger; private readonly ISysCacheService _cache; private readonly ISysUserRoleService _sysUserRoleService; private readonly IDjyCustomerService _customerService; private readonly IEventPublisher _publisher; public FeeRecordService(ILogger logger, ISysCacheService cache, IEventPublisher publisher, SqlSugarRepository repRecord, ISysUserRoleService sysUserRoleService, IDjyCustomerService customerService) { _logger = logger; _cache = cache; _sysUserRoleService = sysUserRoleService; _customerService = customerService; _publisher = publisher; _repRecord = repRecord; } /// /// 费用记录查询 /// [HttpGet("/FeeRecord/Query")] public async Task> Query([FromQuery] FeeRecordInputDto input) { List feeRecords = await _repRecord.AsQueryable() .Where(r => r.BussId == input.BussId) .OrderBy(r => new { r.FeeType, r.Sort, r.CreatedTime }) .ToListAsync(); // 如果费用中含有机密费用,再判断是否有查询的权限 if (feeRecords.Any(r => !r.IsOpen)) { bool canReviewSecretFee = await _sysUserRoleService.IsUserInRole(UserManager.UserId, CommonConst.SECRET_FEE_REVIEW); if (!canReviewSecretFee) { feeRecords.RemoveAll(r => !r.IsOpen && r.CreatedUserId != UserManager.UserId); } } var result = feeRecords.Adapt>(); return result; } /// /// 费用录入保存并返回完整的费用列表 /// [HttpPost("/FeeRecord/Save")] [SqlSugarUnitOfWork] public async Task> Save(FeeRecordSaveDto input) { input.SaveList ??= new List(); input.DeleteList ??= new List(); var waitInsertList = input.SaveList.Where(r => r.Id == 0).ToList(); var waitUpdateList = input.SaveList.Where(r => r.Id != 0 && r.MarkModify).ToList(); #region 校验 foreach (var item in waitInsertList.Concat(waitUpdateList)) { // 人民币汇率必须为1,其它外币汇率不能为空 if (item.ExchangeRate == 0) throw Oops.Bah("汇率不能为空或0,请调整"); if (string.IsNullOrEmpty(item.Currency)) throw Oops.Bah("币别不能为空"); if (item.Currency?.ToUpper() == "RMB") { if (item.ExchangeRate != 1) throw Oops.Bah("币别为人民币时,汇率需为1,请调整"); } } #endregion // 删除 if (input.DeleteList.Any()) { var waitDeleteListTemp = input.DeleteList.Select(r => r.Id).ToList(); await _repRecord.UpdateAsync(r => waitDeleteListTemp.Contains(r.Id), r => new FeeRecord() { IsDeleted = true }); } // 新增 if (waitInsertList.Any()) { var waitInsertListTemp = waitInsertList.Adapt>(); await _repRecord.InsertAsync(waitInsertListTemp); } // 更新 if (waitUpdateList.Any()) { await _repRecord.AsUpdateable(waitUpdateList).UpdateColumns(r => new { r.UpdatedTime, r.UpdatedUserId, r.UpdatedUserName, r.FeeCode, r.FeeName, r.CustomerType, r.CustomerCode, r.CustomerName, r.FeeDescription, r.Currency, r.ExchangeRate, r.TaxRate, r.Tax, r.Unit, r.UnitPrice, r.Quantity, r.Amount, r.NoTaxAmount, r.Remark, r.IsOpen, r.IsAdvancedPay, r.Sort, r.IsInvoice }).ExecuteCommandAsync(); } // 返回完整的费用列表 return await Query(new FeeRecordInputDto() { BussId = input.BussId }); } /// /// 根据类型获取结算对象列表 /// /// [HttpGet("/FeeRecord/GetFeeCustomerList")] public async Task> GetFeeCustomerList(string code) { List result = new(); switch (code) { // 船司 case "carrier": { List list = await _cache.GetAllCodeCarrier(); return list.Select(x => new FeeCustomerDto() { Code = x.Code, Name = x.CnName }).ToList(); } // 船代 case "shipagency": { List list = await _cache.GetAllCodeForwarder(); return list.Select(x => new FeeCustomerDto() { Code = x.Code, Name = x.Name }).ToList(); } // 场站 case "yard": { List list = await _cache.GetAllCodeYard(); return list.Select(x => new FeeCustomerDto() { Code = x.Code, Name = x.Name }).ToList(); } // 往来单位 default: { List<(string codeName, string shortName)> list = await _customerService.QueryDjyCustomerByProp(code); return list.Select(x => new FeeCustomerDto() { Code = x.codeName, Name = x.shortName }).ToList(); } } } /// /// 获取费用单位列表 /// [HttpGet("/FeeRecord/GetFeeUnitList")] public async Task> GetFeeUnitList() { var codeCtnList = (await _cache.GetAllCodeCtn()).Select(x => new FeeUnitDto(x.Code, x.Name)).ToList(); var feeUnitList = (await _cache.GetAllDictData()).Where(d => d.TypeCode == "FeeUnit") .Select(x => new FeeUnitDto(x.Code, x.Value)) .ToList(); var result = codeCtnList.Concat(feeUnitList).ToList(); return result; } } }