/// <summary> /// 订单申请退款,积分退回,积分余额不足则清零 /// </summary> /// <param name="db"></param> /// <param name="orderId"></param> /// <param name="sourceType"></param> /// <param name="_logger"></param> /// <returns></returns> public async static Task <bool> IntegralReturn(ShopDbContext db, int orderId, SourceType sourceType, ILogger _logger) { try { int returnCount = 0; var shopIntegralRechargeParameter = await GetShopIntegralRechargeParameterAsync(db, sourceType, orderId, _logger); if (shopIntegralRechargeParameter.ShopId == 0) { return(false); } var shopMembers = await db.ShopMembers.FirstOrDefaultAsync(m => !m.IsDel && m.ShopId.Equals(shopIntegralRechargeParameter.ShopId) && m.MemberId.Equals(shopIntegralRechargeParameter.MemberId)); if (shopMembers == null) { return(false); } var shopMemberSet = await db.ShopMemberSet.FirstOrDefaultAsync(m => m.ShopId.Equals(shopIntegralRechargeParameter.ShopId)); if (shopMemberSet == null) { return(false); } var shopIntegralRecharge = await db.ShopIntegralRecharge.FirstOrDefaultAsync(m => !m.IsDel && m.CodeStatut.Equals(1) && m.SourceOrderId.Equals(orderId) && m.SourceType.Equals(sourceType)); if (shopIntegralRecharge == null) //没有获取过积分 { return(true); } var membershopIntegral = await GetMemberIntegral(db, shopIntegralRechargeParameter.MemberId, shopIntegralRechargeParameter.ShopId, _logger); if (membershopIntegral < shopIntegralRecharge.Count) { returnCount = membershopIntegral; } else { returnCount = shopIntegralRecharge.Count; } ShopIntegralRecharge addshopIntegralRecharge = new ShopIntegralRecharge(); addshopIntegralRecharge.AddIp = ""; addshopIntegralRecharge.AddTime = DateTime.Now; addshopIntegralRecharge.CodeStatut = -1; addshopIntegralRecharge.Count = returnCount; addshopIntegralRecharge.ShopMemberId = shopIntegralRecharge.ShopMemberId; addshopIntegralRecharge.MemberId = shopIntegralRecharge.MemberId; addshopIntegralRecharge.ShopId = shopIntegralRecharge.ShopId; addshopIntegralRecharge.SourceOrderId = orderId; addshopIntegralRecharge.SourceRemark = $"{sourceType.ToString()}订单积分退还"; addshopIntegralRecharge.SourceType = sourceType; await db.ShopIntegralRecharge.AddAsync(addshopIntegralRecharge); var isOk = await db.SaveChangesAsync(); await CalculateMemberIntegral(db, shopIntegralRechargeParameter.MemberId, shopIntegralRechargeParameter.ShopId, _logger); await ShopMemberLevelServer.UpdateMemberLevel(db, shopMemberSet, shopIntegralRechargeParameter.MemberId, shopIntegralRechargeParameter.ShopId, _logger); return(isOk > 0); } catch (Exception e) { _logger.LogInformation($"退还积分错误,错误信息:{e}"); return(false); } }
/// <summary> /// 获取消费积分(根据订单Id) /// </summary> /// <param name="db"></param> /// <param name="conglomerationOrder"></param> /// <returns></returns> public async static Task <bool> GetOrderIntegral(ShopDbContext db, int orderId, SourceType sourceType, ILogger _logger) { _logger.LogInformation($"-----获取消费积分积分GetOrderIntegral 开始---"); try { var shopIntegralRechargeParameter = await GetShopIntegralRechargeParameterAsync(db, sourceType, orderId, _logger); if (shopIntegralRechargeParameter.ShopId == 0) { return(false); } var shopMembers = await db.ShopMembers.FirstOrDefaultAsync(m => !m.IsDel && m.ShopId.Equals(shopIntegralRechargeParameter.ShopId) && m.MemberId.Equals(shopIntegralRechargeParameter.MemberId)); if (shopMembers == null) { return(false); } var shopMemberSet = await db.ShopMemberSet.FirstOrDefaultAsync(m => m.ShopId.Equals(shopIntegralRechargeParameter.ShopId)); if (shopMemberSet == null) { return(false); } if (!shopMemberSet.IsConsumeIntegral) //消费积分判断 { return(false); } var rate = await GetShopMemberSet(db, shopMemberSet, shopIntegralRechargeParameter.ShopId, _logger); _logger.LogInformation($"获取积分比例rate:{rate}"); var shopIntegralRecharge = await db.ShopIntegralRecharge.FirstOrDefaultAsync(m => !m.SourceType.Equals(sourceType) && m.SourceOrderId.Equals(orderId) && m.CodeStatut.Equals(1)); if (shopIntegralRecharge != null) { throw new Exception("记录已存在"); } var count = (int)Math.Floor((shopIntegralRechargeParameter.Count * rate)); _logger.LogInformation($"添加的积分shopIntegralRechargeParameter.Count:{shopIntegralRechargeParameter.Count} count:{count}"); if (count == 0) { return(false); } ShopIntegralRecharge addshopIntegralRecharge = new ShopIntegralRecharge(); addshopIntegralRecharge.AddIp = ""; addshopIntegralRecharge.AddTime = DateTime.Now; addshopIntegralRecharge.CodeStatut = 1; addshopIntegralRecharge.Count = count; addshopIntegralRecharge.ShopMemberId = shopMembers.Id; addshopIntegralRecharge.MemberId = shopIntegralRechargeParameter.MemberId; addshopIntegralRecharge.ShopId = shopIntegralRechargeParameter.ShopId; addshopIntegralRecharge.SourceOrderId = orderId; addshopIntegralRecharge.SourceRemark = $"{sourceType.ToString()}订单积分"; addshopIntegralRecharge.SourceType = sourceType; await db.ShopIntegralRecharge.AddAsync(addshopIntegralRecharge); var isOk = await db.SaveChangesAsync(); await CalculateMemberIntegral(db, shopIntegralRechargeParameter.MemberId, shopIntegralRechargeParameter.ShopId, _logger); await ShopMemberLevelServer.UpdateMemberLevel(db, shopMemberSet, shopIntegralRechargeParameter.MemberId, shopIntegralRechargeParameter.ShopId, _logger); _logger.LogInformation($"-----获取消费积分积分GetOrderIntegral 结束---"); return(isOk > 0); } catch (Exception e) { throw e; } }