You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
9.6 KiB
C#

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 DS.WMS.Core.Sys.Entity;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
public class FeeRecordService : IFeeRecordService
{
readonly string DefaultCurrency = "CNY";
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<ISqlSugarClient>();
user = _serviceProvider.GetRequiredService<IUser>();
saasService = _serviceProvider.GetRequiredService<ISaasDbService>();
}
/// <summary>
/// 列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public DataResult<List<FeeRecordRes>> GetListByPage(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = tenantDb.Queryable<FeeRecord>()
.Where(whereList)
.Select<FeeRecordRes>()
.ToQueryPage(request.PageCondition);
//关联用户名称
var userIds = data.Data.Where(x => x.UpdateBy.HasValue).Select(x => x.UpdateBy.Value).Distinct().ToList();
var users = db.Queryable<SysUser>().Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.UserName }).ToList();
foreach (var item in data.Data)
{
if (item.UpdateBy.HasValue)
{
item.UpdateByName = users.Find(x => x.Id == item.UpdateBy.Value)?.UserName;
}
}
return data;
}
/// <summary>
/// 列表
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
public DataResult<List<FeeRecord>> GetList(string query)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var src = tenantDb.Queryable<FeeRecord>();
if (!query.IsNullOrEmpty())
{
//序列化查询条件
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(query);
src = src.Where(whereList);
}
var data = src.ToList();
return new DataResult<List<FeeRecord>>(ResultCode.Success) { Data = data };
}
/// <summary>
/// 提交
/// </summary>
/// <param name="bid">业务ID</param>
/// <param name="items">要提交的费用记录</param>
/// <returns></returns>
public DataResult InsertOrUpdate(long bid, IEnumerable<FeeRecord> items)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
try
{
tenantDb.Ado.BeginTran();
DateTime dtNow = DateTime.Now;
foreach (var item in items)
{
item.BusinessId = bid;
item.FeeStatus = FeeStatusEnum.Entering;
item.SubmitDate = dtNow;
if (item.Id == 0)
{
tenantDb.Insertable(item).ExecuteCommand();
}
else
{
tenantDb.Updateable(item).IgnoreColumns(x => new
{
x.FeeStatus,
x.CreateBy,
x.CreateTime,
x.BusinessId,
x.DeleteBy,
x.Deleted,
x.DeleteTime
}).ExecuteCommand();
}
}
tenantDb.Ado.CommitTran();
var list = items.Select(x => x.Adapt<FeeRecordRes>()).ToList();
return DataResult.Successed("保存成功", list, MultiLanguageConst.DataUpdateSuccess);
}
catch
{
tenantDb.Ado.RollbackTran();
//throw;
return DataResult.Failed("保存失败", MultiLanguageConst.DataUpdateFailed);
}
}
/// <summary>
/// 根据模板ID创建
/// </summary>
/// <param name="bid">业务ID</param>
/// <param name="tidArray">模板ID支持多个</param>
/// <returns></returns>
public DataResult CreateByTemplate(long bid, params long[] tidArray)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
bool hasExists = tenantDb.Queryable<FeeRecord>().LeftJoin<FeeTemplateDetail>((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<FeeTemplateDetail>().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();
6 months ago
List<FeeRecord> records = new List<FeeRecord>(details.Count);
foreach (var item in details)
{
var record = item.Adapt<FeeRecord>();
record.BusinessId = bid;
record.SubmitDate = DateTime.Now;
records.Add(record);
}
//若计价货币单位不等于默认货币则尝试获取最新汇率
var exRecords = records.FindAll(x => !x.ExchangeRate.HasValue && x.Currency != DefaultCurrency);
if (exRecords.Count > 0)
{
var codes = exRecords.Select(x => x.Currency).Distinct().ToList();
var currencies = tenantDb.Queryable<FeeCurrency>().Where(x => codes.Contains(x.CodeName)).Includes(x => x.Exchanges).ToList();
6 months ago
DateTime dtNow = DateTime.Now;
foreach (var item in exRecords)
{
var currency = currencies.Find(x => x.CodeName == item.Currency);
6 months ago
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);
}
/// <summary>
/// 根据费用明细转换为模板明细
/// </summary>
/// <param name="idArray">费用明细ID</param>
/// <returns></returns>
public DataResult<List<FeeTemplateDetailRes>> ReadAsTemplate(params long[] idArray)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var list = tenantDb.Queryable<FeeRecord>().Where(x => idArray.Contains(x.Id)).Select<FeeTemplateDetailRes>().ToList();
return new DataResult<List<FeeTemplateDetailRes>>(ResultCode.Success) { Data = list };
}
/// <summary>
/// 删除(录入状态)
/// </summary>
/// <param name="ids">费用记录ID</param>
/// <returns></returns>
public DataResult Delete(params long[] ids)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (tenantDb.Queryable<FeeRecord>().Any(x => ids.Contains(x.Id) && x.FeeStatus != FeeStatusEnum.Entering))
return DataResult.Failed("只能删除状态为‘录入’的费用", MultiLanguageConst.FeeRecordDelete);
int result = tenantDb.Deleteable<FeeRecord>(x => ids.Contains(x.Id)).ExecuteCommand();
return result > 0 ? DataResult.Successed("删除成功!", MultiLanguageConst.DataDelSuccess) : DataResult.Successed("删除失败!", MultiLanguageConst.Operation_Failed);
}
}
}