Exemple #1
0
        public CustomJsonResult PayCompleted(string operater, string orderSn, DateTime completedTime)
        {
            CustomJsonResult result = new CustomJsonResult();

            using (TransactionScope ts = new TransactionScope())
            {
                var order = CurrentDb.Order.Where(m => m.Sn == orderSn).FirstOrDefault();

                if (order == null)
                {
                    return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, string.Format("找不到该订单号({0})", orderSn)));
                }

                if (order.Status == Enumeration.OrderStatus.Payed || order.Status == Enumeration.OrderStatus.Completed)
                {
                    return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, string.Format("订单号({0})已经支付通知成功", orderSn)));
                }

                if (order.Status != Enumeration.OrderStatus.WaitPay)
                {
                    return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, string.Format("找不到该订单号({0})", orderSn)));
                }

                order.Status       = Enumeration.OrderStatus.Payed;
                order.PayTime      = this.DateTime;
                order.MendTime     = this.DateTime;
                order.Mender       = operater;
                order.IsInVisiable = false;

                var orderDetails = CurrentDb.OrderDetails.Where(m => m.OrderId == order.Id).ToList();

                foreach (var orderDetail in orderDetails)
                {
                    orderDetail.Status = Enumeration.OrderDetailsStatus.Payed;

                    #region  活动处理
                    if (!string.IsNullOrEmpty(orderDetail.PromoteId))
                    {
                        var promoteSku = CurrentDb.PromoteSku.Where(m => m.PromoteId == order.PromoteId && m.SkuId == orderDetail.SkuId).FirstOrDefault();

                        if (promoteSku != null)
                        {
                            if (promoteSku.StockQuantity > 0)
                            {
                                promoteSku.LockQuantity  -= orderDetail.Quantity;
                                promoteSku.StockQuantity -= orderDetail.Quantity;
                            }

                            promoteSku.SaleQuantity += orderDetail.Quantity;;

                            if (promoteSku.IsCoupon)
                            {
                                #region 优惠券处理
                                var clientCoupon = CurrentDb.ClientCoupon.Where(m => m.ClientId == order.ClientId && m.PromoteId == order.PromoteId && m.SkuId == promoteSku.SkuId).FirstOrDefault();
                                if (clientCoupon == null)
                                {
                                    clientCoupon            = new ClientCoupon();
                                    clientCoupon.Id         = GuidUtil.New();
                                    clientCoupon.ClientId   = order.ClientId;
                                    clientCoupon.PromoteId  = order.PromoteId;
                                    clientCoupon.SkuId      = promoteSku.SkuId;
                                    clientCoupon.WxCouponId = promoteSku.WxCouponId;
                                    clientCoupon.IsBuy      = true;
                                    clientCoupon.BuyTime    = this.DateTime;
                                    clientCoupon.IsGet      = false;
                                    clientCoupon.IsConsume  = false;
                                    clientCoupon.Creator    = operater;
                                    clientCoupon.CreateTime = this.DateTime;
                                    clientCoupon.RefereerId = order.RefereerId;
                                    clientCoupon.OrderId    = order.Id;
                                    clientCoupon.OrderSn    = order.Sn;

                                    if (!string.IsNullOrEmpty(promoteSku.ExtAtrrs))
                                    {
                                        CouponModel couponModel = null;
                                        try
                                        {
                                            couponModel = Newtonsoft.Json.JsonConvert.DeserializeObject <CouponModel>(promoteSku.ExtAtrrs);
                                        }
                                        catch
                                        {
                                            couponModel = null;
                                        }

                                        if (couponModel != null)
                                        {
                                            clientCoupon.Name           = couponModel.Name;
                                            clientCoupon.Number         = couponModel.Number;
                                            clientCoupon.NumberType     = couponModel.NumberType;
                                            clientCoupon.NumberUnit     = couponModel.NumberUnit;
                                            clientCoupon.ValidStartTime = couponModel.ValidStartTime;
                                            clientCoupon.ValidEndTime   = couponModel.ValidEndTime;
                                            clientCoupon.Description    = couponModel.Description;
                                            clientCoupon.Discounttip    = couponModel.Discounttip;
                                        }
                                    }

                                    CurrentDb.ClientCoupon.Add(clientCoupon);
                                    CurrentDb.SaveChanges();
                                }
                                #endregion
                            }

                            if (promoteSku.IsGiftGive)
                            {
                                #region 奖品处理
                                var giftGive = CurrentDb.GiftGive.Where(m => m.ClientId == order.ClientId && m.SkuId == orderDetail.SkuId).FirstOrDefault();
                                if (giftGive == null)
                                {
                                    giftGive                   = new GiftGive();
                                    giftGive.Id                = GuidUtil.New();
                                    giftGive.ClientId          = order.ClientId;
                                    giftGive.CurrentQuantity   = orderDetail.Quantity;
                                    giftGive.AvailableQuantity = orderDetail.Quantity;
                                    giftGive.LockQuantity      = 0;
                                    giftGive.SkuId             = orderDetail.SkuId;
                                    giftGive.Creator           = operater;
                                    giftGive.CreateTime        = this.DateTime;
                                    CurrentDb.GiftGive.Add(giftGive);
                                    CurrentDb.SaveChanges();
                                }
                                else
                                {
                                    giftGive.CurrentQuantity   += orderDetail.Quantity;
                                    giftGive.AvailableQuantity += orderDetail.Quantity;
                                    giftGive.Mender             = operater;
                                    giftGive.MendTime           = this.DateTime;
                                }

                                var giftGiveTrans = new GiftGiveTrans();
                                giftGiveTrans.Id                = GuidUtil.New();
                                giftGiveTrans.Sn                = SnUtil.Build(Enumeration.BizSnType.GiftGiveTrans, order.ClientId);
                                giftGiveTrans.ClientId          = order.ClientId;
                                giftGiveTrans.SkuId             = giftGive.SkuId;
                                giftGiveTrans.ChangeType        = Enumeration.GiftGiveTransType.SignupGift;
                                giftGiveTrans.ChangeQuantity    = orderDetail.Quantity;
                                giftGiveTrans.AvailableQuantity = giftGive.AvailableQuantity;
                                giftGiveTrans.CurrentQuantity   = giftGive.CurrentQuantity;
                                giftGiveTrans.LockQuantity      = giftGive.LockQuantity;
                                giftGiveTrans.Description       = "参与报名成功,赠送";
                                giftGiveTrans.Creator           = operater;
                                giftGiveTrans.CreateTime        = this.DateTime;
                                CurrentDb.GiftGiveTrans.Add(giftGiveTrans);
                                CurrentDb.SaveChanges();
                                #endregion
                            }
                        }
                    }
                    #endregion
                }


                var refereerRefereeCount = CurrentDb.PromoteUser.Where(m => m.PromoteId == order.PromoteId && m.RefereerId == order.RefereerId && m.ClientId != m.RefereerId).Count();


                CurrentDb.SaveChanges();
                ts.Complete();

                var handlePms = new RedisMqHandlePms4PromoteRefereerRewardByBuyerBuy();
                handlePms.OrderId              = order.Id;
                handlePms.BuyerId              = order.ClientId;
                handlePms.PromoteId            = order.PromoteId;
                handlePms.RefereerId           = order.RefereerId;
                handlePms.RefereerRefereeCount = refereerRefereeCount;

                ReidsMqFactory.Global.Push(RedisMqHandleType.PromoteRefereerRewardByBuyerBuy, handlePms);

                result = new CustomJsonResult(ResultType.Success, ResultCode.Success, string.Format("支付完成通知:订单号({0})通知成功", orderSn));
            }

            return(result);
        }
        private void PromoteRefereerRewardByBuyerBuy()
        {
            LogUtil.Info(string.Format("正在处理消息类型为:{0},具体的参数未:{1}", this.Type.GetCnName(), Newtonsoft.Json.JsonConvert.SerializeObject(this.Pms)));

            using (LumosDbContext CurrentDb = new LumosDbContext())
            {
                using (TransactionScope ts = new TransactionScope())
                {
                    #region 购买前奖励
                    var model = ((JObject)this.Pms).ToObject <RedisMqHandlePms4PromoteRefereerRewardByBuyerBuy>();

                    var promote = CurrentDb.Promote.Where(m => m.Id == model.PromoteId).FirstOrDefault();

                    if (promote == null)
                    {
                        LogUtil.Info("活动:" + model.PromoteId + ",为空");
                        return;
                    }

                    if (model.BuyerId == null)
                    {
                        LogUtil.Info("购买人,为空");
                        return;
                    }

                    if (model.RefereerId == null)
                    {
                        LogUtil.Info("推荐人,为空");
                        return;
                    }

                    if (model.BuyerId == model.RefereerId)
                    {
                        LogUtil.Info("购买人和推荐人是同一个人");
                        return;
                    }



                    var promoteRefereerRewardSets = CurrentDb.PromoteRefereerRewardSet.Where(m => m.PromoteId == model.PromoteId && m.Channel == Enumeration.PromoteRefereerRewardSetChannel.BuyerBuyProductSku).ToList();

                    if (promoteRefereerRewardSets.Count > 0)
                    {
                        foreach (var reward in promoteRefereerRewardSets)
                        {
                            var rewardModel = Newtonsoft.Json.JsonConvert.DeserializeObject <RewardModel>(reward.Reward);

                            var promoteRefereerRewardFactor = CurrentDb.PromoteRefereerRewardFactor.Where(m => m.RefereerId == model.RefereerId && m.PromoteId == reward.PromoteId && m.PromoteRefereerRewardSetId == reward.Id).FirstOrDefault();
                            if (promoteRefereerRewardFactor == null)
                            {
                                promoteRefereerRewardFactor    = new PromoteRefereerRewardFactor();
                                promoteRefereerRewardFactor.Id = GuidUtil.New();
                                promoteRefereerRewardFactor.PromoteRefereerRewardSetId = reward.Id;
                                promoteRefereerRewardFactor.RefereerId = model.RefereerId;
                                promoteRefereerRewardFactor.PromoteId  = model.PromoteId;
                                promoteRefereerRewardFactor.Factor     = reward.IncreaseFactor;
                                CurrentDb.PromoteRefereerRewardFactor.Add(promoteRefereerRewardFactor);
                                CurrentDb.SaveChanges();
                            }

                            if (model.RefereerRefereeCount == promoteRefereerRewardFactor.Factor)
                            {
                                promoteRefereerRewardFactor.Factor += reward.IncreaseFactor;

                                foreach (var gift in rewardModel.Gifts)
                                {
                                    var giftGive = CurrentDb.GiftGive.Where(m => m.ClientId == model.RefereerId && m.SkuId == gift.SkuId).FirstOrDefault();
                                    if (giftGive == null)
                                    {
                                        giftGive                   = new GiftGive();
                                        giftGive.Id                = GuidUtil.New();
                                        giftGive.ClientId          = model.RefereerId;
                                        giftGive.CurrentQuantity   = gift.Quantity;
                                        giftGive.AvailableQuantity = gift.Quantity;
                                        giftGive.LockQuantity      = 0;
                                        giftGive.SkuId             = gift.SkuId;
                                        giftGive.Creator           = GuidUtil.New();
                                        giftGive.CreateTime        = DateTime.Now;
                                        CurrentDb.GiftGive.Add(giftGive);
                                        CurrentDb.SaveChanges();
                                    }
                                    else
                                    {
                                        giftGive.CurrentQuantity   += gift.Quantity;
                                        giftGive.AvailableQuantity += gift.Quantity;
                                        giftGive.Mender             = GuidUtil.New();
                                        giftGive.MendTime           = DateTime.Now;
                                    }


                                    var giftGiveTrans = new GiftGiveTrans();
                                    giftGiveTrans.Id                = GuidUtil.New();
                                    giftGiveTrans.Sn                = SnUtil.Build(Enumeration.BizSnType.GiftGiveTrans, model.RefereerId);
                                    giftGiveTrans.ClientId          = model.RefereerId;
                                    giftGiveTrans.SkuId             = giftGive.SkuId;
                                    giftGiveTrans.ChangeType        = Enumeration.GiftGiveTransType.SignupGift;
                                    giftGiveTrans.ChangeQuantity    = gift.Quantity;
                                    giftGiveTrans.AvailableQuantity = giftGive.AvailableQuantity;
                                    giftGiveTrans.CurrentQuantity   = giftGive.CurrentQuantity;
                                    giftGiveTrans.LockQuantity      = giftGive.LockQuantity;
                                    giftGiveTrans.Description       = "您推荐的用户参与报名成功,得到奖品";
                                    giftGiveTrans.Creator           = GuidUtil.New();
                                    giftGiveTrans.CreateTime        = DateTime.Now;
                                    CurrentDb.GiftGiveTrans.Add(giftGiveTrans);
                                    CurrentDb.SaveChanges();
                                }
                            }
                        }
                    }



                    //var fund = CurrentDb.Fund.Where(m => m.ClientId == model.RefereerId).FirstOrDefault();

                    //if (fund == null)
                    //{
                    //    LogUtil.Info("用户:" + model.ClientId + ",找不到钱包");
                    //    return;
                    //}

                    //var wxUserInfo = CurrentDb.WxUserInfo.Where(m => m.ClientId == model.ClientId).FirstOrDefault();

                    //if (wxUserInfo == null)
                    //{
                    //    LogUtil.Info("用户:" + model.ClientId + ",找不到信息");
                    //    return;
                    //}

                    //var order = CurrentDb.Order.Where(m => m.Id == model.OrderId).FirstOrDefault();

                    //if (order == null)
                    //{
                    //    LogUtil.Info("订单:" + model.OrderId + ",找不到信息");
                    //    return;
                    //}

                    //if (order.BuyProfitIsSettled)
                    //{
                    //    LogUtil.Info("订单:" + model.OrderId + ",已经结算佣金");
                    //    return;
                    //}

                    //order.BuyProfitIsSettled = true;
                    //order.BuyProfitSettledTime = DateTime.Now;

                    //string nickname = "";
                    //string headImgUrl = IconUtil.BuyCoupon;
                    //if (wxUserInfo != null)
                    //{
                    //    nickname = wxUserInfo.Nickname;

                    //    if (!string.IsNullOrEmpty(wxUserInfo.HeadImgUrl))
                    //    {
                    //        headImgUrl = wxUserInfo.HeadImgUrl;
                    //    }
                    //}

                    //decimal profit = 0;

                    //fund.CurrentBalance += profit;
                    //fund.AvailableBalance += profit;
                    //fund.MendTime = DateTime.Now;
                    //fund.Mender = GuidUtil.Empty();

                    //var fundTrans = new FundTrans();
                    //fundTrans.Id = GuidUtil.New();
                    //fundTrans.Sn = SnUtil.Build(Enumeration.BizSnType.FundTrans, fund.ClientId);
                    //fundTrans.ClientId = fund.ClientId;
                    //fundTrans.ChangeType = Enumeration.FundTransChangeType.BuyCoupon;
                    //fundTrans.ChangeAmount = profit;
                    //fundTrans.CurrentBalance = fund.CurrentBalance;
                    //fundTrans.AvailableBalance = fund.AvailableBalance;
                    //fundTrans.LockBalance = fund.LockBalance;
                    //fundTrans.CreateTime = DateTime.Now;
                    //fundTrans.Creator = GuidUtil.Empty();
                    //fundTrans.Description = string.Format("分享给用户({0})购买入场券", nickname);
                    //fundTrans.TipsIcon = headImgUrl;
                    //fundTrans.IsNoDisplay = false;
                    //CurrentDb.FundTrans.Add(fundTrans);
                    CurrentDb.SaveChanges();
                    ts.Complete();

                    #endregion
                }
            }
        }