using AngleSharp.Dom; using DS.Module.Core; using DS.Module.Core.Extensions; using DS.Module.SqlSugar; using DS.Module.UserModule; using DS.WMS.Core.Fee.Dtos; using DS.WMS.Core.Fee.Entity; using DS.WMS.Core.Fee.Interface; using Mapster; using Microsoft.Extensions.DependencyInjection; using SqlSugar; namespace DS.WMS.Core.Fee.Method { public class FeeRecordService : IFeeRecordService { readonly string DefaultCurrency = "RMB"; private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly ISaasDbService saasService; public FeeRecordService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); } /// /// 列表 /// /// /// public DataResult> GetListByPage(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; } /// /// 提交 /// /// 业务ID /// 要提交的费用记录 /// public DataResult InsertOrUpdate(long bid, IEnumerable items) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); try { tenantDb.Ado.BeginTran(); foreach (var item in items) { item.BusinessId = bid; item.FeeStatus = FeeStatusEnum.Entering; if (item.Id == 0) { tenantDb.Insertable(item).ExecuteCommand(); } else { tenantDb.Updateable(item).ExecuteCommand(); } } tenantDb.Ado.CommitTran(); var list = items.Select(x => x.Adapt()).ToList(); return DataResult.Successed("保存成功", list, MultiLanguageConst.DataUpdateSuccess); } catch { tenantDb.Ado.RollbackTran(); //throw; return DataResult.Failed("保存失败", MultiLanguageConst.DataUpdateFailed); } } /// /// 根据模板ID创建 /// /// 业务ID /// 模板ID(支持多个) /// public DataResult CreateByTemplate(long bid, params long[] tidArray) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); bool hasExists = tenantDb.Queryable().LeftJoin((x, y) => x.FeeId == y.FeeId && x.FeeType == y.FeeType).Any((x, y) => x.BusinessId == bid && tidArray.Contains(y.TemplateId) && !y.Deleted); if (hasExists) return DataResult.Failed("费用记录已存在", MultiLanguageConst.FeeRecordExist); var details = tenantDb.Queryable().Where(x => tidArray.Contains(x.TemplateId) && !x.Deleted).Select(x => new { x.FeeType, x.FeeId, x.FeeCode, x.FeeName, x.FeeFrt, x.FeeGroup, x.CustomerName, x.CustomerType, x.CustomerId, x.Unit, x.UnitPrice, x.Currency, x.ExchangeRate, x.Tax, x.TaxRate, x.AccTaxRate, x.IsAdvancedPay, x.IsInvoice, x.SaleOrgId, x.Note }).ToList(); List records = new List(details.Count); foreach (var item in details) { var record = item.Adapt(); record.BusinessId = bid; record.SubmitDate = DateTime.Now; records.Add(record); } //若计价货币单位不等于默认货币则尝试获取最新汇率 var exDetails = records.FindAll(x => !x.ExchangeRate.HasValue && x.Currency != DefaultCurrency); if (exDetails.Count > 0) { var codes = exDetails.Select(x => x.Currency).Distinct().ToList(); var currencies = tenantDb.Queryable().Where(x => codes.Contains(x.CodeName)).Includes(x => x.Exchanges).ToList(); DateTime dtNow = DateTime.Now; foreach (var item in exDetails) { var currency = currencies.Find(x => x.CodeName == item.Currency); if (currency != null) { item.ExchangeRate = currency.DefaultRate; if (currency.Exchanges != null) { //取当前时间范围内的最新一条 var exchange = currency.Exchanges.FindAll(x => x.Status == StatusEnum.Enable && x.StartDate >= dtNow && x.EndDate <= dtNow).OrderByDescending(x => x.UpdateTime).FirstOrDefault(); if (exchange != null) { item.ExchangeRate = item.FeeType == 1 ? exchange.DRValue : exchange.CRValue; } } } } } int result = tenantDb.Insertable(records).ExecuteCommand(); return result > 0 ? DataResult.Successed("保存成功", records, MultiLanguageConst.DataCreateSuccess) : DataResult.Successed("操作失败!", MultiLanguageConst.Operation_Failed); } /// /// 删除(录入状态) /// /// 费用记录ID /// public DataResult Delete(params long[] ids) { var tenantDb = saasService.GetBizDbScopeById(user.TenantId); if (tenantDb.Queryable().Any(x => ids.Contains(x.Id) && x.FeeStatus != FeeStatusEnum.Entering)) return DataResult.Failed("只能删除状态为‘录入’的费用", MultiLanguageConst.FeeRecordDelete); int result = tenantDb.Deleteable(x => ids.Contains(x.Id)).ExecuteCommand(); return result > 0 ? DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess) : DataResult.Successed("删除失败!", MultiLanguageConst.Operation_Failed); } } }