diff --git a/EntrustSettle.Api/Controllers/OrderController.cs b/EntrustSettle.Api/Controllers/OrderController.cs index e7dbfed..ce2f7fb 100644 --- a/EntrustSettle.Api/Controllers/OrderController.cs +++ b/EntrustSettle.Api/Controllers/OrderController.cs @@ -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,39 +312,140 @@ namespace EntrustSettle.Api.Controllers } order.Status = (int)changeStatusDto.Status; - unitOfWorkManage.BeginTran(); + decimal sumAmount = 0; + bool isBeginTran = false; + try + { - // 清除原有费用信息 - await orderFeeService.Delete(x => x.OrderId == changeStatusDto.Id); - order.Amount = order.Amount == null ? null : 0; + // 执行扣费 + if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0) + { + string logMsg = $"订单Id:{changeStatusDto.Id},提单号:{order.Mblno}"; + try + { + sumAmount = changeStatusDto.FeeList.Select(x => x.Amount).Sum(); - // 添加新的费用信息 - if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0) - { - var orderFeeModelList = changeStatusDto.FeeList.Select(x => new OrderFee() + logger.LogInformation($"{logMsg}, 手动更新状态时,需要扣费,总金额:{sumAmount}"); + + var custBalanceService = App.GetService>(); + 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>(); + + 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},添加扣费记录成功"); + } + + // 更新余额 + 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) { - Name = x.Name, - Amount = x.Amount, - OrderId = changeStatusDto.Id - }).ToList(); - await orderFeeService.Add(orderFeeModelList); + unitOfWorkManage.BeginTran(); + } - order.Amount = changeStatusDto.FeeList.Select(x => x.Amount).Sum(); - } + // 添加新的费用信息 + if (changeStatusDto.FeeList != null && changeStatusDto.FeeList.Count > 0) + { + // 清除原有费用信息 + //await orderFeeService.Delete(x => x.OrderId == changeStatusDto.Id); + //order.Amount = order.Amount == null ? null : 0; - await orderService.Update(order, x => new { x.Status, x.Amount }); + var orderFeeModelList = changeStatusDto.FeeList.Select(x => new OrderFee() + { + Name = x.Name, + Amount = x.Amount, + OrderId = changeStatusDto.Id + }).ToList(); + await orderFeeService.Add(orderFeeModelList); + + // 查出来现有的费用记录,更新 + var sumAllAmount = await orderFeeService.AsQueryable() + .Where(x => x.OrderId == changeStatusDto.Id) + .SumAsync(x => x.Amount); + //order.Amount = sumAllAmount; + } - var orderHistory = new OrderHistory - { - Pid = order.Id, - Status = (int)changeStatusDto.Status, - Remark = changeStatusDto.Remark, - Amount = order.Amount, - StatusTime = DateTime.Now, - }; - await orderHistoryService.Add(orderHistory); - unitOfWorkManage.CommitTran(); + await orderService.Update(order, x => new { x.Status, x.Amount }); + var orderHistory = new OrderHistory + { + Pid = order.Id, + Status = (int)changeStatusDto.Status, + Remark = changeStatusDto.Remark, + Amount = sumAmount, + StatusTime = DateTime.Now, + }; + await orderHistoryService.Add(orderHistory); + unitOfWorkManage.CommitTran(); + } + catch (Exception) + { + unitOfWorkManage.RollbackTran(); + throw; + } // 将更新后的状态及费用推送到消息队列 if (AppSettings.app("RabbitMQ", "Enabled").ObjToBool()) { diff --git a/EntrustSettle.Common/Const/CommonConst.cs b/EntrustSettle.Common/Const/CommonConst.cs index b19e2c2..637076f 100644 --- a/EntrustSettle.Common/Const/CommonConst.cs +++ b/EntrustSettle.Common/Const/CommonConst.cs @@ -6,8 +6,8 @@ public const string API_USER_HEADER_SECRET = "USER_SECRET"; /// - /// AFR申报代码;名称为“AFR申报” + /// 委托结算扣费类型代码 /// - public const int AFR_REPORT = 27; + public const int WEITUOJIESUAN = 30; } } diff --git a/EntrustSettle.Model/Dtos/Expend/ExpendDto.cs b/EntrustSettle.Model/Dtos/Expend/ExpendDto.cs new file mode 100644 index 0000000..d9ac402 --- /dev/null +++ b/EntrustSettle.Model/Dtos/Expend/ExpendDto.cs @@ -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; } + } +} diff --git a/EntrustSettle.Model/Models/DJY/CustBalance.cs b/EntrustSettle.Model/Models/DJY/CustBalance.cs index 77bcaf0..feeba0e 100644 --- a/EntrustSettle.Model/Models/DJY/CustBalance.cs +++ b/EntrustSettle.Model/Models/DJY/CustBalance.cs @@ -3,6 +3,7 @@ using SqlSugar; namespace EntrustSettle.Model.Models.DJY { [Tenant(DBConst.PingTai)] + [SugarTable(tableName: "Cust_Balance")] public class CustBalance { [SugarColumn(IsPrimaryKey = true)] diff --git a/EntrustSettle.Model/Models/DJY/CustFee.cs b/EntrustSettle.Model/Models/DJY/CustFee.cs index cd7501e..f5b6c8a 100644 --- a/EntrustSettle.Model/Models/DJY/CustFee.cs +++ b/EntrustSettle.Model/Models/DJY/CustFee.cs @@ -4,6 +4,7 @@ using System; namespace EntrustSettle.Model.Models.DJY { [Tenant(DBConst.PingTai)] + [SugarTable(tableName: "Cust_Fee")] public class CustFee { [SugarColumn(IsPrimaryKey = true)] diff --git a/EntrustSettle.Model/Models/DJY/CustPrice.cs b/EntrustSettle.Model/Models/DJY/CustPrice.cs index f13cd11..804619a 100644 --- a/EntrustSettle.Model/Models/DJY/CustPrice.cs +++ b/EntrustSettle.Model/Models/DJY/CustPrice.cs @@ -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; } - /// - /// - /// - public decimal? GiftBalance { get; set; } + // /// + // /// + // /// + // public decimal? GiftBalance { get; set; } - /// - /// ۣ - /// - public decimal? PRICE { get; set; } + // /// + // /// ۣ + // /// + // public decimal? PRICE { get; set; } - /// - /// ֵۣ - /// - public decimal PRICEF { get; set; } = 0M; + // /// + // /// ֵۣ + // /// + // public decimal PRICEF { get; set; } = 0M; - /// - /// ۣ - /// - public decimal PRICEFQG { get; set; } = 0M; + // /// + // /// ۣ + // /// + // public decimal PRICEFQG { get; set; } = 0M; - /// - /// ۣ۷ֵ - /// - public decimal PRICEQG { get; set; } = 0M; + // /// + // /// ۣ۷ֵ + // /// + // 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; } + //} } \ No newline at end of file diff --git a/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs b/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs index 5eebdd4..e655442 100644 --- a/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs +++ b/EntrustSettle.Tasks/QuartzNet/Jobs/JobHydStatusQuartz.cs @@ -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()