委托结算扣费

master
zhangxiaofeng 8 months ago
parent 93b22cabe6
commit 32d0ba2b19

@ -1,13 +1,18 @@
using EntrustSettle.Common;
using EntrustSettle.Common.Const;
using EntrustSettle.Common.Extensions;
using EntrustSettle.Common.Helper;
using EntrustSettle.Controllers;
using EntrustSettle.IRepository.Base;
using EntrustSettle.IServices;
using EntrustSettle.IServices.Base;
using EntrustSettle.Model;
using EntrustSettle.Model.Dtos;
using EntrustSettle.Model.Models;
using EntrustSettle.Model.Models.DJY;
using EntrustSettle.Model.Validator;
using EntrustSettle.Repository.UnitOfWorks;
using EntrustSettle.Services.Base;
using FluentValidation;
using Mapster;
using Microsoft.AspNetCore.Authorization;
@ -307,15 +312,107 @@ namespace EntrustSettle.Api.Controllers
}
order.Status = (int)changeStatusDto.Status;
decimal sumAmount = 0;
bool isBeginTran = false;
try
{
// 执行扣费
if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0)
{
string logMsg = $"订单Id{changeStatusDto.Id},提单号:{order.Mblno}";
try
{
sumAmount = changeStatusDto.FeeList.Select(x => x.Amount).Sum();
logger.LogInformation($"{logMsg} 手动更新状态时,需要扣费,总金额:{sumAmount}");
var custBalanceService = App.GetService<IBaseServices<CustBalance>>();
var balance = await custBalanceService.AsQueryable().FirstAsync(x => x.COMNAME == order.CompanyName);
if (balance == null)
{
logger.LogInformation($"{logMsg},扣费失败:没有账户钱包!");
throw new Exception("扣费失败:没有账户钱包!");
}
if (balance.Balance < sumAmount)
{
logger.LogInformation($"{logMsg},扣费失败:账户余额不足!{sumAmount}");
throw new Exception($"账户余额不足!{sumAmount} {order.CompanyName}");
}
logger.LogInformation($"{logMsg},开始写入消费记录");
var custFeeService = App.GetService<IBaseServices<CustFee>>();
var beizhu = string.Join('', changeStatusDto.FeeList.Select(x => x.Name + "" + x.Amount));
CustFee custFeeModel = new CustFee()
{
BEIZHU = beizhu,
BSNO = order.Id.ToString(),
COMNAME = order.CompanyName,
MBLNO = order.Mblno,
BSTYPE = CommonConst.WEITUOJIESUAN,
CREATETIME = DateTime.Now,
LURURENID = order.ContactId,
LURUREN = order.ContactName,
COMID = order.CompanyId,
SENDUSERID = order.ContactId,
SENDUSER = order.ContactName,
SENDCOM = order.CompanyName,
SENDTYPE = 0,
SENDTIME = DateTime.Now,
CtnrInfo = string.Empty,
CtnrCount = 0,
GID = Guid.NewGuid().ToString(),
PRICE = sumAmount
};
logger.LogInformation($"{logMsg},扣费记录内容:{{0}}", custFeeModel);
unitOfWorkManage.BeginTran();
isBeginTran = true;
// 添加扣费记录
var num = await custFeeService.AsInsertable(custFeeModel).ExecuteCommandAsync();
if (num <= 0)
{
unitOfWorkManage.RollbackTran();
throw new Exception($"{logMsg},添加扣费记录失败!");
}
else
{
logger.LogInformation($"{logMsg},添加扣费记录成功");
}
// 清除原有费用信息
await orderFeeService.Delete(x => x.OrderId == changeStatusDto.Id);
order.Amount = order.Amount == null ? null : 0;
// 更新余额
num = await custBalanceService.AsUpdateable()
.SetColumns(x => x.Balance == x.Balance - sumAmount)
.Where(x => x.COMNAME == order.CompanyName)
.ExecuteCommandAsync();
if (num <= 0)
{
unitOfWorkManage.RollbackTran();
throw new Exception($"{logMsg},更新余额失败!");
}
else
{
logger.LogInformation($"{logMsg},更新余额成功");
}
}
catch (Exception ex)
{
logger.LogError(ex, $"{logMsg},执行扣费时发生未知异常");
throw;
}
}
if (!isBeginTran)
{
unitOfWorkManage.BeginTran();
}
// 添加新的费用信息
if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0)
{
// 清除原有费用信息
//await orderFeeService.Delete(x => x.OrderId == changeStatusDto.Id);
//order.Amount = order.Amount == null ? null : 0;
var orderFeeModelList = changeStatusDto.FeeList.Select(x => new OrderFee()
{
Name = x.Name,
@ -324,7 +421,11 @@ namespace EntrustSettle.Api.Controllers
}).ToList();
await orderFeeService.Add(orderFeeModelList);
order.Amount = changeStatusDto.FeeList.Select(x => x.Amount).Sum();
// 查出来现有的费用记录,更新
var sumAllAmount = await orderFeeService.AsQueryable()
.Where(x => x.OrderId == changeStatusDto.Id)
.SumAsync(x => x.Amount);
//order.Amount = sumAllAmount;
}
await orderService.Update(order, x => new { x.Status, x.Amount });
@ -334,12 +435,17 @@ namespace EntrustSettle.Api.Controllers
Pid = order.Id,
Status = (int)changeStatusDto.Status,
Remark = changeStatusDto.Remark,
Amount = order.Amount,
Amount = sumAmount,
StatusTime = DateTime.Now,
};
await orderHistoryService.Add(orderHistory);
unitOfWorkManage.CommitTran();
}
catch (Exception)
{
unitOfWorkManage.RollbackTran();
throw;
}
// 将更新后的状态及费用推送到消息队列
if (AppSettings.app("RabbitMQ", "Enabled").ObjToBool())
{

@ -6,8 +6,8 @@
public const string API_USER_HEADER_SECRET = "USER_SECRET";
/// <summary>
/// AFR申报代码名称为“AFR申报”
/// 委托结算扣费类型代码
/// </summary>
public const int AFR_REPORT = 27;
public const int WEITUOJIESUAN = 30;
}
}

@ -0,0 +1,28 @@
namespace EntrustSettle.Model.Dtos.Expend
{
public class ExpendDto
{
public string BsType { get; set; }
public Data Data { get; set; }
public string Md5 { get; set; }
public string Module { get; set; }
public string RunId { get; set; }
public string SendType { get; set; }
public string Timestamp { get; set; }
public string UserId { get; set; }
}
public class Data
{
public string Beizhu { get; set; }
public string Bsno { get; set; }
public string Comid { get; set; }
public string Comname { get; set; }
public string Lururen { get; set; }
public string Lururenid { get; set; }
public string Mblno { get; set; }
public string Sendcom { get; set; }
public string Senduser { get; set; }
public string Senduserid { get; set; }
}
}

@ -3,6 +3,7 @@ using SqlSugar;
namespace EntrustSettle.Model.Models.DJY
{
[Tenant(DBConst.PingTai)]
[SugarTable(tableName: "Cust_Balance")]
public class CustBalance
{
[SugarColumn(IsPrimaryKey = true)]

@ -4,6 +4,7 @@ using System;
namespace EntrustSettle.Model.Models.DJY
{
[Tenant(DBConst.PingTai)]
[SugarTable(tableName: "Cust_Fee")]
public class CustFee
{
[SugarColumn(IsPrimaryKey = true)]

@ -4,46 +4,47 @@ using System;
namespace EntrustSettle.Model.Models.DJY
{
[Tenant(DBConst.PingTai)]
public class CustPrice
{
[SugarColumn(IsPrimaryKey = true)]
public string GID { get; set; }
//[Tenant(DBConst.PingTai)]
//[SugarTable(tableName: "Cust_Price")]
//public class CustPrice
//{
// [SugarColumn(IsPrimaryKey = true)]
// public string GID { get; set; }
public int? BSTYPE { get; set; }
// public int? BSTYPE { get; set; }
public string COMID { get; set; }
// public string COMID { get; set; }
public string COMNAME { get; set; }
// public string COMNAME { get; set; }
/// <summary>
/// 騰箇岱띨
/// </summary>
public decimal? GiftBalance { get; set; }
// /// <summary>
// /// 騰箇岱띨
// /// </summary>
// public decimal? GiftBalance { get; set; }
/// <summary>
/// 데송(寮데)
/// </summary>
public decimal? PRICE { get; set; }
// /// <summary>
// /// 데송(寮데)
// /// </summary>
// public decimal? PRICE { get; set; }
/// <summary>
/// 데송(롸데)
/// </summary>
public decimal PRICEF { get; set; } = 0M;
// /// <summary>
// /// 데송(롸데)
// /// </summary>
// public decimal PRICEF { get; set; } = 0M;
/// <summary>
/// 데송(행멍寮데)
/// </summary>
public decimal PRICEFQG { get; set; } = 0M;
// /// <summary>
// /// 데송(행멍寮데)
// /// </summary>
// public decimal PRICEFQG { get; set; } = 0M;
/// <summary>
/// 데송(행멍롸데)
/// </summary>
public decimal PRICEQG { get; set; } = 0M;
// /// <summary>
// /// 데송(행멍롸데)
// /// </summary>
// public decimal PRICEQG { get; set; } = 0M;
[JsonProperty]
public int? SENDTYPE { get; set; }
// [JsonProperty]
// public int? SENDTYPE { get; set; }
public DateTime? UPDATETIME { get; set; }
}
// public DateTime? UPDATETIME { get; set; }
//}
}

@ -106,7 +106,7 @@ namespace EntrustSettle.Tasks
{
_ = Task.Run(async () =>
{
string msg = $"Id[{order.Id}],提单号:[{order.Mblno}]动更新状态后推送队列";
string msg = $"Id[{order.Id}],提单号:[{order.Mblno}]动更新状态后推送队列";
try
{
StatusPushDto pushDto = new()

Loading…
Cancel
Save