|
|
|
@ -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())
|
|
|
|
|
{
|
|
|
|
|