/// <summary> /// 退佣金计算 /// </summary> /// <param name="model"></param> private static async Task DeductionCommission(CalculateCommissionModel model) { var commissionFlowCount = await DalTuboAlliance.GetCommissionFlowRecordDal("", nameof(CpsEnum.CpsRunningType.IMPAIRMENT), model.OrderItem.OrderListId, "" + model.OrderItem.OrderId); if (commissionFlowCount <= 0) { var outBizNo = model.OrderItem.OrderId + "_" + (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000; var actutalAmount = (model.OrderItem.PayPrice * model.OrderItem.Num) * (model.CommissionRatio / 100); ; var minuteActutalAmount = actutalAmount * 100; //转换为单位分 var requestNo = Guid.NewGuid(); minuteActutalAmount = Math.Ceiling(minuteActutalAmount); var commissionFlowRecordSumAmount = DalTuboAlliance.GetCommissionFlowRecordSumAmountDal(model.OriOrderId); var comissionSumAmount = commissionFlowRecordSumAmount + minuteActutalAmount; if (comissionSumAmount >= 0) { var cpsCommissionFlowRecordModel = new CpsCommissionFlowRecordModel(); cpsCommissionFlowRecordModel.CommissionFlowRecordNo = outBizNo; cpsCommissionFlowRecordModel.OrderId = model.OriOrderId; cpsCommissionFlowRecordModel.OrderItemPKID = model.OrderItem.OrderListId; cpsCommissionFlowRecordModel.RedRushOrderId = "" + model.OrderItem.OrderId; cpsCommissionFlowRecordModel.CpsId = model.CpsId; cpsCommissionFlowRecordModel.DarenID = model.DarenId; cpsCommissionFlowRecordModel.Pid = model.Pid; cpsCommissionFlowRecordModel.PayPrice = model.OrderItem.PayPrice; cpsCommissionFlowRecordModel.Number = model.Number; cpsCommissionFlowRecordModel.CommissionRatio = model.CommissionRatio; cpsCommissionFlowRecordModel.ActutalAmount = minuteActutalAmount; cpsCommissionFlowRecordModel.Type = nameof(CpsEnum.CpsRunningType.IMPAIRMENT); cpsCommissionFlowRecordModel.RequestNo = requestNo; cpsCommissionFlowRecordModel.TransactionNo = model.OriPayInstructionId; cpsCommissionFlowRecordModel.Status = nameof(CpsEnum.CpsRunningStatus.NOTSENT); int cpsCreateRunningCount = await DalTuboAlliance.CpsCommissionFlowRecordDal(cpsCommissionFlowRecordModel); if (cpsCreateRunningCount > 0) { var cpsSendRefundRequest = new CpsSendRefundRequest(); cpsSendRefundRequest.OutBizNo = outBizNo; cpsSendRefundRequest.OriPayInstructionId = model.OriPayInstructionId; cpsSendRefundRequest.RefundType = nameof(CpsEnum.CpsRefundType.NORMAL); cpsSendRefundRequest.RefundRoute = nameof(CpsEnum.CpsRefundRoute.ORIGINAL); cpsSendRefundRequest.RequestNo = "" + requestNo; cpsSendRefundRequest.RefundAmount = Math.Abs(minuteActutalAmount); await CpsSendRefundAsync(cpsSendRefundRequest); } else { Logger.Info($"CommodityDeductionManager -->DeductionCommission:【orderId:{model.OriOrderId}】【RedRushOrderId:{ model.OrderItem.OrderId}】【DarenID:{model.DarenId}】【CPSID:{model.CpsId}】【PID:{model.Pid}】退款流水创建失败"); } } else { Logger.Info($"CommodityDeductionManager -->DeductionCommission:【orderId:{model.OriOrderId}】【RedRushOrderId:{ model.OrderItem.OrderId}】【DarenID:{model.DarenId}】【CPSID:{model.CpsId}】【PID:{model.Pid}】退款金额大于支付金额"); } } else { Logger.Info($"CommodityDeductionManager -->DeductionCommission:【orderId:{model.OriOrderId}】【RedRushOrderId:{ model.OrderItem.OrderId}】【DarenID:{model.DarenId}】【CPSID:{model.CpsId}】【PID:{model.Pid}】退款流水已存在"); } }
/// <summary> /// 查询佣金流水 /// </summary> /// <param name="orderId">订单号</param> /// <param name="cpsID">佣金比例ID</param> /// <param name="darenID">达人ID</param> /// <param name="Pid">商品ID</param> /// <param name="type">佣金类型</param> /// <returns></returns> public static async Task <CpsCommissionFlowRecordModel> GetCommissionFlowRecordDetalDal(int orderId, Guid cpsID, Guid darenID, string pid, string runingType) { var resultModel = new CpsCommissionFlowRecordModel(); try { using (var dbHelper = DbHelper.CreateDbHelper(false)) { string sqlGetRuningListDal = @"SELECT [PKID] , [CommissionFlowRecordNo] , [OrderItemPKID] , [CpsId] , [OrderId] , [RedRushOrderId] , [DarenID] , [Pid] , [Number] , [CommissionRatio] , [ActutalAmount] , [Type] , [RequestNo] , [TransactionNo] , [Status] , [Reason] , [CreateTime] , [CreateBy] , [UpdateTime] , [UpdateBy] , [IsDelete] FROM [Activity].[dbo].[Cps_CommissionFlowRecord] AS A WITH ( NOLOCK ) WHERE IsDelete = 0 AND OrderId = @OrderId AND CpsId = @CpsId AND DarenID = @DarenID AND Pid = @PID"; if (!string.IsNullOrWhiteSpace(runingType)) { sqlGetRuningListDal += " AND [Type] = @Type"; } sqlGetRuningListDal += " ORDER BY CreateTime DESC"; using (var cmd = new SqlCommand(sqlGetRuningListDal)) { cmd.CommandType = CommandType.Text; if (!string.IsNullOrWhiteSpace(runingType)) { cmd.Parameters.AddWithValue("@Type", runingType); } cmd.Parameters.AddWithValue("@OrderId", orderId); cmd.Parameters.AddWithValue("@CpsId", cpsID); cmd.Parameters.AddWithValue("@DarenID", darenID); cmd.Parameters.AddWithValue("@Pid", pid); resultModel = await dbHelper.ExecuteFetchAsync <CpsCommissionFlowRecordModel>(cmd); } } } catch (Exception ex) { Logger.Error($"GetCommissionFlowRecordDetalDal佣金流水查询接口异常:{ex.Message};堆栈信息:{ex.StackTrace}"); } return(resultModel); }
/// <summary> /// 返佣佣金计算 /// </summary> /// <param name="model"></param> /// <param name="orderRecordProductNumber">下单记录的商品数量</param> private static async Task CalculateCommission(CalculateCommissionModel model) { var commissionFlowCount = await DalTuboAlliance.GetCommissionFlowRecordDal("" + model.OrderItem.OrderId, nameof(CpsEnum.CpsRunningType.VALUEADDED), model.OrderItem.OrderListId, ""); //防止重复请求,每个订单下的商品只能存在一条流水记录 if (commissionFlowCount <= 0) { //获取商品的佣金比例 var cpsProductListModel = await DalTuboAlliance.GetCpsProductDal(model.CpsId); if (cpsProductListModel != null) { var outBizNo = model.OrderItem.OrderId + "_" + (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000; var actutalAmount = (model.OrderItem.PayPrice * model.OrderItem.Num) * (cpsProductListModel.CommissionRatio / 100); var minuteActutalAmount = actutalAmount * 100; //转换为单位分 //向下取整,总是舍去小数 minuteActutalAmount = Math.Floor(minuteActutalAmount); var requestNo = Guid.NewGuid(); var cpsCommissionFlowRecordModel = new CpsCommissionFlowRecordModel(); cpsCommissionFlowRecordModel.CommissionFlowRecordNo = outBizNo; cpsCommissionFlowRecordModel.OrderId = "" + model.OrderItem.OrderId; cpsCommissionFlowRecordModel.OrderItemPKID = model.OrderItem.OrderListId; cpsCommissionFlowRecordModel.CpsId = model.CpsId; cpsCommissionFlowRecordModel.DarenID = model.DarenId; cpsCommissionFlowRecordModel.Pid = model.Pid; cpsCommissionFlowRecordModel.PayPrice = model.OrderItem.PayPrice; cpsCommissionFlowRecordModel.Number = model.Number; cpsCommissionFlowRecordModel.CommissionRatio = cpsProductListModel.CommissionRatio; cpsCommissionFlowRecordModel.ActutalAmount = minuteActutalAmount; cpsCommissionFlowRecordModel.Type = nameof(CpsEnum.CpsRunningType.VALUEADDED); cpsCommissionFlowRecordModel.RequestNo = requestNo; cpsCommissionFlowRecordModel.Status = nameof(CpsEnum.CpsRunningStatus.NOTSENT); int cpsCreateRunningCount = await DalTuboAlliance.CpsCommissionFlowRecordDal(cpsCommissionFlowRecordModel); if (cpsCreateRunningCount > 0) { var cpsSendPayRequest = new CpsSendPayRequest(); cpsSendPayRequest.OutBizNo = outBizNo; cpsSendPayRequest.OrderDesc = $"订单返佣:{model.OrderItem.OrderId};" + model.OrderItem.Remark; cpsSendPayRequest.Amount = minuteActutalAmount; cpsSendPayRequest.ProductName = $"订单返佣:{model.OrderItem.OrderId};"; cpsSendPayRequest.RequestNo = "" + requestNo; cpsSendPayRequest.UserId = "" + model.DarenId; await CpsSendPayAsync(cpsSendPayRequest); } else { Logger.Info($"CommodityRebateManager -->CalculateCommission:【outBizNo:{outBizNo}】支付流水创建失败"); } } else { Logger.Info($"CommodityRebateManager -->CalculateCommission:【CpsId:{model.CpsId}】佣金ID不存在"); } } else { Logger.Info($"CommodityRebateManager -->CalculateCommission:【OrderId:{model.OrderItem.OrderId}】" + $"【OrderItemPKID:{model.OrderItem.OrderListId}】支付流水已经存在"); } }
/// <summary> /// CPS支付流水创建接口 /// </summary> /// <param name="request"></param> /// <returns></returns> public static async Task <int> CpsCommissionFlowRecordDal(CpsCommissionFlowRecordModel model) { int resultRow = 0; try { using (var dbHelper = DbHelper.CreateDbHelper()) { var sqlCpsCommissionFlowRecord = @"INSERT INTO [Activity].[dbo].[Cps_CommissionFlowRecord] ([CommissionFlowRecordNo] ,[OrderId] ,[OrderItemPKID] ,[CpsId] ,[RedRushOrderId] ,[DarenID] ,[Pid] ,[PayPrice] ,[Number] ,[CommissionRatio] ,[ActutalAmount] ,[Type] ,[RequestNo] ,[TransactionNo] ,[Status] ,[Reason] ,[CreateTime] ,[IsDelete]) VALUES (@CommissionFlowRecordNo ,@OrderId ,@OrderItemPKID ,@CpsId ,@RedRushOrderId ,@DarenID ,@Pid ,@PayPrice ,@Number ,@CommissionRatio ,@ActutalAmount ,@TYPE ,@RequestNo ,@TransactionNo ,@STATUS ,@Reason ,GETDATE() ,0)"; using (var cmd = new SqlCommand(sqlCpsCommissionFlowRecord)) { cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@CommissionFlowRecordNo", model.CommissionFlowRecordNo); cmd.Parameters.AddWithValue("@OrderId", model.@OrderId); cmd.Parameters.AddWithValue("@OrderItemPKID", model.OrderItemPKID); cmd.Parameters.AddWithValue("@CpsId", model.CpsId); cmd.Parameters.AddWithValue("@RedRushOrderId", model.RedRushOrderId); cmd.Parameters.AddWithValue("@DarenID", model.DarenID); cmd.Parameters.AddWithValue("@Pid", model.Pid); cmd.Parameters.AddWithValue("@PayPrice", model.PayPrice); cmd.Parameters.AddWithValue("@Number", model.Number); cmd.Parameters.AddWithValue("@CommissionRatio", model.CommissionRatio); cmd.Parameters.AddWithValue("@ActutalAmount", model.ActutalAmount); cmd.Parameters.AddWithValue("@TYPE", model.Type); cmd.Parameters.AddWithValue("@RequestNo", model.RequestNo); cmd.Parameters.AddWithValue("@TransactionNo", model.TransactionNo); cmd.Parameters.AddWithValue("@STATUS", model.Status); cmd.Parameters.AddWithValue("@Reason", model.Reason); resultRow = Convert.ToInt32(await dbHelper.ExecuteNonQueryAsync(cmd)); } } } catch (Exception ex) { Logger.Error($"CpsCommissionFlowRecordDal佣金流水创建接口异常:{ex.Message};堆栈信息:{ex.StackTrace}"); } return(resultRow); }