using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.SqlSugar; using DS.Module.UserModule; using DS.WMS.ContainerManagement.Info.Dtos; using DS.WMS.ContainerManagement.Info.Entity; using DS.WMS.ContainerManagement.Info.Interface; using DS.WMS.Core.Application.Dtos; using DS.WMS.Core.Application.Entity; using DS.WMS.Core; using DS.WMS.Core.Info.Dtos; using DS.WMS.Core.Info.Entity; using DS.WMS.Core.Info.Interface; using DS.WMS.Core.Invoice.Dtos; using DS.WMS.Core.Op.View; using DS.WMS.Core.Sys.Entity; using Mapster; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using SqlSugar; using DS.WMS.Core.Fee.Entity; using DS.WMS.Core.Op.Entity; using AngleSharp.Dom; using NPOI.Util; using DS.WMS.Core.Sys.Method; using DS.WMS.Core.Sys.Interface; using DS.WMS.Core.Fee.Interface; using DS.Module.Core.Enums; using Masuit.Tools.Systems; using DS.WMS.Core.TaskPlat.Dtos; using DS.WMS.Core.Fee.Dtos; using DS.WMS.Core.Fee.Method; namespace DS.WMS.ContainerManagement.Info.Method; // : FeeServiceBase, ISettlementService where TEntity : SettlementBase, new () public class CM_BuyCtnService : CMServiceBase, ICM_BuyCtnService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly ISaasDbService saasService; private readonly ICommonService commonService; readonly IFeeRecordService _feeService; readonly ICM_State_ChangeService _Changeservice; private readonly IFeeCurrencyExchangeService feeCurrencyExchangeService; /// /// /// /// public CM_BuyCtnService(IServiceProvider serviceProvider) : base(serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); commonService = _serviceProvider.GetRequiredService(); _feeService = _serviceProvider.GetRequiredService(); _Changeservice = _serviceProvider.GetRequiredService(); feeCurrencyExchangeService = _serviceProvider.GetRequiredService(); } /// /// 列表 /// /// /// public async Task>> GetListByPage(PageRequest request) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var data = tenantDb.Queryable() .InnerJoin((a, b) => a.Id == b.BusinessId) //.LeftJoin((a, b, c) => a.SaleOrgId == c.Id, "shippingweb8_dev.sys_org") .LeftJoin((a, b, c) => a.OrgId == c.Id, "shippingweb8_dev.sys_org") .Select((a, b, c) => new CM_BuyCtnRes() { OrgName = c.OrgName, }, true) .Where(whereList) //.Select() .ToQueryPage(request.PageCondition); return data; } /// /// 编辑 /// /// /// public async Task EditCM_BuyCtn(CM_BuyCtnReq req) { if (req.BodyList.Count > 0) { if (req.BodyList.Exists(x => string.IsNullOrWhiteSpace(x.Cntrno))) { return DataResult.Failed("保存失败!", MultiLanguageConst.CM_CntrnoNotNull); } //判断是否有重复的 如有则报错 bool hasDuplicates = req.BodyList.GroupBy(item => item.Cntrno).Any(group => group.Count() > 1); if (hasDuplicates) return DataResult.Failed("保存失败!", MultiLanguageConst.CM_CntrnoRepeat); } //if (req.OldContainerOwnerId != null && req.BodyList.Exists(x => x.OldContainerOwnerId == null)) //{ // foreach (var ctn in req.BodyList.Where(x => x.OldContainerOwnerId == null)) { // ctn.OldContainerOwnerId = req.OldContainerOwnerId; // } //} //if (req.Bsdate != null && req.BodyList.Exists(x => x.Bsdate == null)) //{ // foreach (var ctn in req.BodyList.Where(x => x.Bsdate == null)) // { // ctn.Bsdate = req.Bsdate; // } //} 填充明细空白字段(ref req); var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (req.Id == 0) { var data = req.Adapt(); var sequence = commonService.GetSequenceNext(); if (!sequence.Succeeded) { return await Task.FromResult(DataResult.Failed(sequence.Message, MultiLanguageConst.SequenceSetNotExist)); } data.Billno = sequence.Data; //var entity = tenantDb.Insertable(data).ExecuteReturnEntity(); var entity = await tenantDb.Insertable(data).ExecuteReturnEntityAsync(); //(typeof(BusinessType)).GetDescriptionAndValue().Select(x => new { Id = x.Value, Name = x.Key }).Where(x=>x.Id).ToList(); InitBusiness(entity.Id); if (req.BodyList.Count > 0) { //var ctnList = await tenantDb.Queryable().Where(x => x.pid == req.Id.ToString()).ToListAsync(); foreach (var item in req.BodyList) { item.Id = 0; item.Pid = entity.Id; var newdetail = item.Adapt(); //newdetail.RentDirectId = data.RentDirectId; //newdetail.RentTypeId = data.RentTypeId; //newdetail.BillState = data.BillState; await tenantDb.Insertable(newdetail).ExecuteCommandAsync(); } } //return DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess); return await Task.FromResult(DataResult.Successed("添加成功!", entity.Id, MultiLanguageConst.DataCreateSuccess)); } else { var info = await tenantDb.Queryable().Where(x => x.Id == req.Id).FirstAsync(); info = req.Adapt(info); await tenantDb.Updateable(info).ExecuteCommandAsync(); if (req.BodyList.Count > 0) { var _BodyList = await tenantDb.Queryable().Where(x => x.Pid == req.Id).ToListAsync(); foreach (var item in req.BodyList) { if (item.Id == 0) { //item.Id = 0; item.Pid = info.Id; //item.Billno= info.Billno; //item.RentDirectId = info.RentDirectId; //item.RentTypeId = info.RentTypeId; //item.BillState = item.BillState; var newdetail = item.Adapt(); await tenantDb.Insertable(newdetail).ExecuteCommandAsync(); } else { if (!_BodyList.Exists(x => x.Id == item.Id)) { continue; } var updrec = _BodyList.First(x => x.Id == item.Id); //item.Billno = info.Billno; //item.RentDirectId = info.RentDirectId; //item.RentTypeId = info.RentTypeId; item.Pid = info.Id; //item.BillState = item.BillState; updrec = item.Adapt(updrec); await tenantDb.Updateable(updrec).ExecuteCommandAsync(); } } } return await Task.FromResult(DataResult.Successed("更新成功!", MultiLanguageConst.DataUpdateSuccess)); } } private void 填充明细空白字段(ref CM_BuyCtnReq req) { if (req.BodyList.Count > 0) { foreach (var item in req.BodyList) { item.Pid = req.Id; if (req.Bsdate != null) { if (item.Bsdate == null) { item.Bsdate = req.Bsdate; } } if (req.Bsdate != null) { if (item.FeeStartDate == null) { item.FeeStartDate = req.Bsdate; } } if (req.OldContainerOwnerId != null) { if (item.OldContainerOwnerId == null || item.OldContainerOwnerId == 0) { item.OldContainerOwnerId = req.OldContainerOwnerId; item.OldContainerOwner = req.OldContainerOwner; } } item.RentDirectId = req.RentDirectId; item.RentTypeId = req.RentTypeId; if (string.IsNullOrWhiteSpace(item.Currency)) { item.Currency = "USD"; } if (item.DailyRate == null) { item.DailyRate = 0M; } if (item.PickupFee == null) { item.PickupFee = 0M; } if (item.DropoffFee == null) { item.DropoffFee = 0M; } } } var ch = new CtnTotalHelper2>(req.BodyList); req.CntrnoAll = string.Join(",", req.BodyList.Select(s => s.Cntrno).Distinct().ToList()); req.Ctntotal = ch.GetCtnTotalStr(); req.PickupCtntotal = ch.Get已提箱Str(); req.RemainCtntotal = ch.Get未提箱Str(); } /// /// 详情 /// /// /// public DataResult GetCM_BuyCtn(string id) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var data = tenantDb.Queryable() .Where(a => a.Id == long.Parse(id)) .Select() .First(); return DataResult.Success(data, MultiLanguageConst.DataQuerySuccess); } #region 删除 /// /// 删除 /// /// 费用记录ID /// public async Task DeleteCM_BuyCtnAsync(params long[] ids) { foreach (var _id in ids) { if (await IsLockedAsync(_id, BusinessType.CM_RentOneWay)) return DataResult.FailedWithDesc(nameof(MultiLanguageConst.FeeLocked)); } //如果包含费用 不允许删除 var feeCount = await TenantDb.Queryable().Where(x => ids.Contains(x.BusinessId)).CountAsync(); if (feeCount > 0) { return DataResult.FailedWithDesc(nameof(MultiLanguageConst.CM_FeeExist)); } int result = await TenantDb.Deleteable(x => ids.Contains(x.Id)).ExecuteCommandAsync(); if (result > 0) { //删除所有明细 await TenantDb.Deleteable(x => ids.Contains(x.Pid)).ExecuteCommandAsync(); } //await WriteBackStatusAsync(model.BusinessId, model.BusinessType); return result > 0 ? DataResult.Success : DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } #endregion /// /// 买箱_确认 /// /// /// public async Task CM_BuyCtn_Confirm(string id, params long[] ids) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var data = tenantDb.Queryable() .Where(a => a.Id == long.Parse(id)) //.Select() .First(); var body = tenantDb.Queryable().Where(a => a.Pid == data.Id).ToList(); //20240808 首先判断该箱号能否执行确认 //if (!string.IsNullOrWhiteSpace(data.BillState)) //{ // return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); //} data.BillState = "Confirm"; //data.IsBusinessLocking = true; var ch = new CtnTotalHelper2>(body); data.Ctntotal = ch.GetCtnTotalStr(); data.PickupCtntotal = ch.Get已提箱Str(); data.RemainCtntotal= ch.Get未提箱Str(); var entity = await tenantDb.Updateable(data).ExecuteCommandAsync(); //20240826 生成箱状态变动 foreach (var detail in body) { if (detail.PickupDate == null) { continue; } var changerec = new CM_State_ChangeReq(); changerec.Cntrno = detail.Cntrno; changerec.CtnCode = detail.CtnCode; changerec.Ctnall= detail.Ctnall; changerec.UsedState = "USED"; changerec.CtnOwnerId = data.OldContainerOwnerId; changerec.CtnOwner= data.OldContainerOwner; changerec.CtnSourceId = CM_CtnSourceEnum.自有箱; changerec.CtnBizStateId= CM_CtnBizStateEnum.买箱; changerec.ChangeSourceId = CMChangeSourceEnum.买箱; changerec.ChangeSourceDetailId = data.Id; changerec.CtnFlowStateId= null; changerec.CtnStateId = CMCtnStateEnum.AV; changerec.CtnBreakStateId = CMCtnBreakStateEnum.AV; changerec.IsHeavy = false; changerec.Billno = detail.Mblno; changerec.IsOnlineId = CM_IsOnlineEnum.待上线; if (detail.Portid != null) { changerec.Portid = detail.Portid; changerec.PortCode = detail.PortCode; changerec.Port = detail.Port; } changerec.ChangeTime = data.Bsdate; changerec.CtnFlowStateId = null; //如果没有填写提箱日期 则变动来源id设为租箱主单 变动日期设为业务日期 //如果填写了提箱日期 则变动来源id设为租箱主单 变动日期设为提箱日期 if (detail.PickupDate != null) { changerec.CtnStateId = CMCtnStateEnum.AV; changerec.ChangeSourceDetailId = detail.Id; changerec.CtnFlowStateId = CMCtnFlowStateEnum.已提箱; changerec.ChangeTime = detail.PickupDate; changerec.IsOnlineId = CM_IsOnlineEnum.上线; } if(changerec.ChangeTime==null) changerec.ChangeTime=DateTime.Now; changerec.ProductionDate = detail.ProductionDate; changerec.CtnWeight = detail.CtnWeight; changerec.CtnValue_Base = detail.CtnValue_Base; changerec.CtnValue_BuyingPrice = detail.CtnValue_BuyingPrice; await _Changeservice.AddCM_State_ChangeAsync(changerec); } return await Task.FromResult(DataResult.Successed("添加成功!", id, MultiLanguageConst.DataCreateSuccess)); } public async Task CM_BuyCtn_Confirm( params long[] ids) { var _r = DataResult.Successed("添加成功!", null, MultiLanguageConst.DataCreateSuccess); foreach (var id in ids) { _r=await CM_BuyCtn_Confirm(id.ToString(), null); } return _r; } /// /// 买箱_取消 /// /// /// public async Task CM_BuyCtn_Cancel(string id,params long[] ids) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var data = tenantDb.Queryable() .Where(a => a.Id == long.Parse(id)) //.Select() .First(); if (data.BillState!= "Confirm") { return DataResult.FailedWithDesc(nameof(MultiLanguageConst.Operation_Failed)); } data.BillState = ""; //data.IsBusinessLocking = false; var entity = await tenantDb.Updateable(data).ExecuteCommandAsync(); return await Task.FromResult(DataResult.Successed("取消成功!", id, MultiLanguageConst.DataCreateSuccess)); } public async Task CM_BuyCtn_Cancel(params long[] ids) { var _r = DataResult.Successed("取消成功!", null, MultiLanguageConst.DataCreateSuccess); foreach (var id in ids) { _r = await CM_BuyCtn_Cancel(id.ToString(), null); } return _r; } /// /// 买箱_生成费用 /// /// /// public async Task CM_BuyCtn_MakeFee(string id) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); var data = tenantDb.Queryable() .Where(a => a.Id == long.Parse(id)) .Select() .First(); if (data.BillState != "Confirm") { return await Task.FromResult(DataResult.Failed("添加失败!", MultiLanguageConst.CM_CanNotMakeFee)); } else { //根据明细生成费用 //var head = TenantDb.Queryable().Where(x => x.Pid == data.Id).FirstAsync(); var feecode = await tenantDb.Queryable().FirstAsync(x => x.Name == "购箱费"); if (feecode == null) { return await Task.FromResult(DataResult.Failed(MultiLanguageConst.NoFeeName)); } var body = await TenantDb.Queryable().Where(x => x.Pid == data.Id).ToListAsync(); //按箱型和币别和单价分组 var groupList = new List(); foreach (var item in body) { if (groupList.Exists(x => x.Ctnall == item.Ctnall && x.Currency == item.Currency && x.CtnValue_BuyingPrice == item.CtnValue_BuyingPrice)) { groupList.First(x => x.Ctnall == item.Ctnall && x.Currency == item.Currency ).PickupFee += item.CtnValue_BuyingPrice; } else { item.PickupFee = item.CtnValue_BuyingPrice;//用PickupFee承载总金额 groupList.Add(item); } } var savefeeList = new List(); if (groupList != null && groupList.Count > 0) { foreach (var groupdetail in groupList) { if (groupdetail.PickupFee <= 0) continue; var newFee = new FeeRecord(); newFee.Id = 0; //newFee.FeeName = "提箱费"; newFee.FeeId= feecode.Id; newFee.FeeName = feecode.Name; newFee.TaxRate = feecode.TaxRate==null?0: (decimal)feecode.TaxRate; newFee.BusinessType = BusinessType.CM_BuyCtn; newFee.BusinessId = data.Id; newFee.CustomerId= (long)data.OldContainerOwnerId; newFee.CustomerName = data.OldContainerOwner; //newFee.Amount = (decimal)groupdetail.FeeType == FeeType.Receivable ? x.Amount : -x.Amount); newFee.FeeType = FeeType.Payable; newFee.Amount = (decimal)groupdetail.PickupFee; newFee.Currency = groupdetail.Currency; newFee.TaxUnitPrice = (decimal)groupdetail.CtnValue_BuyingPrice; newFee.Quantity = (decimal)groupdetail.PickupFee / (decimal)groupdetail.CtnValue_BuyingPrice; newFee.Unit = groupdetail.CtnCode; var feecurrencyinfo = await feeCurrencyExchangeService.GetExchangeRateAsync(new ExchangeRate { CurrencyFrom = newFee.Currency, CurrencyTo = "", FeeType = newFee.FeeType }); newFee.ExchangeRate = feecurrencyinfo.Data.Rate; newFee.SetTax(); savefeeList.Add(newFee); } } if (savefeeList != null && savefeeList.Count > 0) { await TenantDb.Insertable(savefeeList).ExecuteCommandAsync(); } } return await Task.FromResult(DataResult.Successed("添加成功!", data.Id, MultiLanguageConst.DataCreateSuccess)); } /// /// 买箱明细视图_列表 /// /// /// public async Task>> CM_BuyCtn_Detail_View(PageRequest request) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); //序列化查询条件 var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition); var data = tenantDb.Queryable() .Where(whereList) .Select().ToQueryPage(request.PageCondition); return data; } }