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.

220 lines
10 KiB
C#

using DS.Module.Core;
using DS.Module.Core.Enums;
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.Flow.Entity;
using DS.WMS.Core.Flow.Interface;
using DS.WMS.Core.Op.Entity;
using DS.WMS.Core.Sys.Entity;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
namespace DS.WMS.Core.Fee.Method
{
/// <summary>
/// 付费申请实现
/// </summary>
public class FeeApplicationService : IFeeApplicationService
{
const string DefaultCurrency = "CNY";//默认本币
readonly ISqlSugarClient db;
readonly IUser user;
readonly ISaasDbService saasService;
readonly IClientFlowInstanceService flowService;
/// <summary>
/// 初始化
/// </summary>
/// <param name="serviceProvider"></param>
public FeeApplicationService(IServiceProvider serviceProvider)
{
db = serviceProvider.GetRequiredService<ISqlSugarClient>();
user = serviceProvider.GetRequiredService<IUser>();
saasService = serviceProvider.GetRequiredService<ISaasDbService>();
flowService = serviceProvider.GetRequiredService<IClientFlowInstanceService>();
}
/// <summary>
/// 获取待付费的业务列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DataResult<List<FeeApplicationBiz>>> GetBizListAsync(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var queryList = CreateBizQuery(tenantDb, null);
if (!request.QueryCondition.IsNullOrEmpty())
{
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
queryList = queryList.Where(whereList);
}
var result = await queryList.ToQueryPageAsync(request.PageCondition);
if (result.Data.Count > 0)
{
//关联用户名称
var userIds = result.Data.Where(x => x.OperatorId.HasValue).Select(x => x.OperatorId.Value)
.Union(result.Data.Select(x => x.CreateBy))
.Distinct();
var users = await db.Queryable<SysUser>().Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.UserName }).ToListAsync();
foreach (var item in result.Data)
{
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
if (item.OperatorId.HasValue)
item.Operator = users.Find(x => x.Id == item.OperatorId.Value)?.UserName;
}
}
return result;
}
//创建各项业务数据的查询并集
internal static ISugarQueryable<FeeApplicationBiz> CreateBizQuery(SqlSugarScopeProvider tenantDb, IEnumerable<FlowInstance>? additions)
{
//海运出口
var ids1 = additions?.Where(x => x.BusinessType == BusinessType.OceanShippingExport).Select(x => x.BusinessId).ToList();
if (ids1?.Count == 0)
ids1.Add(0);
var query1 = tenantDb.Queryable<SeaExport>()
.Where(s => ids1.Contains(s.Id))
.Select(s => new FeeApplicationBiz
{
Id = s.Id,
AccountDate = SqlFunc.ToDate(s.AccountDate),
BusinessType = BusinessType.OceanShippingExport,
CntrTotal = s.CntrTotal,
CreateBy = s.CreateBy,
CustomerId = s.CustomerId,
CustomerName = s.CustomerName,//委托单位
DischargePort = s.DischargePort,
ETD = s.ETD,
HBLNO = s.HBLNO,
LoadPort = s.LoadPort,
MBLNO = s.MBLNO,
OperatorId = s.OperatorId,
SaleDeptId = s.SaleDeptId,
//SaleDeptName //所属部门
SaleId = s.SaleId,
SaleName = s.Sale,//揽货人
Vessel = s.Vessel,//船名
Voyage = s.Voyno,//航次
BookingNO = s.BookingNo,
StlName = s.StlName,
UnpaidRMB = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Payable && f.Currency == "CNY").Select(x => SqlFunc.AggregateSumNoNull(x.Amount.Value)),
UnpaidUSD = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Payable && f.Currency == "USD").Select(x => SqlFunc.AggregateSumNoNull(x.Amount.Value)),
UnpaidOther = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Payable && (f.Currency != "USD" && f.Currency != "CNY")).Select(x => SqlFunc.AggregateSumNoNull(x.Amount.Value)),
UnreceivedRMB = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Receivable && f.Currency == "CNY").Select(x => SqlFunc.AggregateSumNoNull(x.Amount.Value)),
UnreceivedUSD = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Receivable && f.Currency == "USD").Select(x => SqlFunc.AggregateSumNoNull(x.Amount.Value)),
UnreceivedOther = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Receivable && (f.Currency != "USD" && f.Currency != "CNY")).Select(x => SqlFunc.AggregateSumNoNull(x.Amount.Value)),
UnpaidRMBInv = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Payable && f.Currency == "CNY").Select(x => SqlFunc.AggregateSumNoNull(x.InvoiceAmount.Value)),
UnpaidUSDInv = SqlFunc.Subqueryable<FeeRecord>().Where(f => f.BusinessId == s.Id && f.FeeStatus == FeeStatus.AuditPassed &&
f.FeeType == FeeType.Payable && f.Currency == "USD").Select(x => SqlFunc.AggregateSumNoNull(x.InvoiceAmount.Value))
});
//海运进口
return tenantDb.UnionAll(new List<ISugarQueryable<FeeApplicationBiz>> { query1 });
}
/// <summary>
/// 获取分页列表
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public async Task<DataResult<List<FeeApplicationDto>>> GetListAsync(PageRequest request)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
var whereList = db.ConfigQuery.Context.Utilities.JsonToConditionalModels(request.QueryCondition);
var data = await tenantDb.Queryable<FeeApplication>().Where(whereList).Select<FeeApplicationDto>().ToQueryPageAsync(request.PageCondition);
//关联用户名称
var userIds = data.Data.Select(x => x.CreateBy).Distinct(); //.Union(data.Data.Select(x => x.CreateBy))
var users = await db.Queryable<SysUser>().Where(x => userIds.Contains(x.Id)).Select(x => new { x.Id, x.UserName }).ToListAsync();
foreach (var item in data.Data)
{
item.CreateByName = users.Find(x => x.Id == item.CreateBy)?.UserName;
}
return data;
}
/// <summary>
/// 提交保存申请单
/// </summary>
/// <param name="application">申请单</param>
/// <returns></returns>
public async Task<DataResult> SaveAsync(FeeApplication application)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (application.Id > 0)
{
//修改需检查申请单状态
if (await tenantDb.Queryable<FeeApplication>().AnyAsync(x =>
x.Id == application.Id && (x.Status == ApplicationStatus.AuditSubmittd || x.Status == ApplicationStatus.AuditPassed)))
return DataResult.Failed("只能修改状态为:未提交/审核驳回的申请单");
}
try
{
await tenantDb.Ado.BeginTranAsync();
}
catch (Exception ex)
{
}
throw new NotImplementedException();
}
/// <summary>
/// 删除申请单
/// </summary>
/// <param name="ids">申请单ID</param>
/// <returns></returns>
public async Task<DataResult> DeleteAsync(params long[] ids)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
if (await tenantDb.Queryable<FeeApplication>().AnyAsync(x => ids.Contains(x.Id) && (x.Status != ApplicationStatus.Pending && x.Status != ApplicationStatus.AuditRejected)))
return DataResult.Failed("只能删除状态为‘待审核’或‘驳回’的申请单", MultiLanguageConst.FeeRecordDelete);
int result = await tenantDb.Deleteable<FeeApplication>(x => ids.Contains(x.Id)).ExecuteCommandAsync();
return result > 0 ? DataResult.Successed("删除成功!") : DataResult.Failed("删除失败!");
}
public async Task<DataResult> SubmitForApprovalAsync(string remark, params long[] idArray)
{
throw new NotImplementedException();
}
public async Task<DataResult> WithdrawAsync(params long[] ids)
{
throw new NotImplementedException();
}
}
}