Example #1
0
        public APIResult BalanceConsume([FromBody] BalanceConsumeArgsModel args)
        {
            int       memberId  = GetMemberId();
            ShopOrder shopOrder = shopDb.GetSingle <ShopOrder>(args.OrderId);

            if (shopOrder == null)
            {
                throw new Exception("订单不存在");
            }
            var shopMemberServer = new ShopMemberServer(shopDb, shopOrder.ShopId, memberId);

            //if (!shopMemberServer.CheckPassword(args.Password)) throw new Exception("支付密码不正确");
            if (!shopMemberServer.BalanceConsume(shopOrder))
            {
                throw new Exception("当前余额不足");
            }
            var checkThirdResult = CheckThird(shopOrder, shopOrder.ShopId);

            if (!checkThirdResult.Success)
            {
                throw new Exception(checkThirdResult.Message);
            }
            shopDb.SaveChanges();
            Utils.ShopOrderUtil.SetShopOrderFinishInBackgroup(shopOrder.Id);
            return(Success());
        }
Example #2
0
        public async Task <APIResult> GetCommoditiesForMember([FromBody] GetShopCategoryAndCommoditiesArgsModel args)
        {
            if (string.IsNullOrEmpty(args.ShopFlag))
            {
                throw new Exception("ShopFlag不能为空");
            }
            var res = await Task.Run(() =>
            {
                var shop = db.Query <Shop>()
                           .Where(m => !m.IsDel)
                           .Where(m => m.Flag == args.ShopFlag)
                           .FirstOrDefault();
                var memberId   = GetMemberId();
                var shopMember = ShopMemberServer.GetShopMember(db, shop.Id, memberId);
                ShopMemberLevel shopMemberLevel = null;
                if (shopMember != null)
                {
                    shopMemberLevel = db.GetSingle <ShopMemberLevel>(shopMember.ShopMemberLevelId);
                }
                ShopBrandCommodityServer server = new ShopBrandCommodityServer(db, shop, args.DiningWay);
                GetShopCommoditiesAndCombos rtn = new GetShopCommoditiesAndCombos()
                {
                    CategoryAndCommodity = server.GetCategoryAndCommodity(shopMemberLevel?.Discount),
                    Commodity            = server.GetComboList()
                };
                return(rtn);
            });

            return(Success(res));
        }
Example #3
0
        public APIResult ChangePassword([FromBody] ChangePasswordArgsModel args)
        {
            if (!args.shopId.HasValue)
            {
                throw new Exception("ShopId不能为空");
            }
            var server = new ShopMemberServer(db, args.shopId.Value, GetMemberId());

            server.ChangePassword(args.OldPWD, args.NewPWD);
            db.SaveChanges();
            return(Success());
        }
Example #4
0
        public APIResult GetMemberBalance([FromBody] GetSingleArgsModel args)
        {
            int memberId   = GetMemberId();
            var shopMember = ShopMemberServer.GetShopMember(db, args.ShopId, memberId);

            if (shopMember == null)
            {
                return(Success(0));
            }
            else
            {
                return(Success(shopMember.Balance));
            }
        }
Example #5
0
        public APIResult Register([FromBody] RegisterArgsModel args)
        {
            if (!CheckVerificationCode(args.Phone, args.Code))
            {
                return(Error("验证码错误"));
            }
            int memberId = GetMemberId();
            var model    = db.Query <ShopMember>()
                           .FirstOrDefault(m => !m.IsDel && m.MemberId == memberId && m.ShopId == args.ShopId.Value);

            if (model != null)
            {
                throw new Exception("该会员已注册过");
            }
            var members    = memberDb.Members.Find(memberId);
            var shopMember = new ShopMember()
            {
                ShopId   = args.ShopId.Value,
                AddIp    = GetIp(),
                AddTime  = DateTime.Now,
                MemberId = memberId,
                Sex      = args.Sex,
                Phone    = args.Phone,
                Credits  = 0,
                Balance  = 0,
                BirthDay = args.BirthDay.Value,
                Name     = members.NickName
            };

            db.Add(shopMember);
            var shopMemberServer = new ShopMemberServer(db, shopMember);

            if (!shopMemberServer.CheckPhoneNumCanUse(args.Phone, args.ShopId.Value))
            {
                return(Error("该手机号码已注册"));
            }
            //shopMemberServer.SetPassword(args.Password);
            db.SaveChanges();
            //更新会员等级
            ShopMemberLevelServer.UpdateMemberLevel(db, db.ShopMemberSet.FirstOrDefault(m => m.ShopId.Equals(args.ShopId) && !m.IsDel), memberId, args.ShopId.Value, _logger);

            return(Success(new
            {
                shopMember.Id
            }));
        }
Example #6
0
        public APIResult GetMemberDiscount([FromBody] GetMemberDiscountArgsModel args)
        {
            if (!args.ShopId.HasValue)
            {
                throw new Exception("ShopId不能为空");
            }
            int memberId = GetMemberId();
            ShopMemberServer memberServer;

            try
            {
                memberServer = new ShopMemberServer(db, args.ShopId.Value, memberId);
            }
            catch (Exception)
            {
                return(Success(new GetMemberDiscountModel()
                {
                    Balance = 0,
                    Discount = 0
                }));
            }
            var dic = new Dictionary <int, IQueryable <ShopCommodityStock> >();

            foreach (var item in args.Items)
            {
                var stock = db.Query <ShopCommodityStock>()
                            .Where(m => !m.IsDel)
                            .Where(m => m.ShopId == args.ShopId.Value)
                            .Where(m => m.Sku.Flag == item.SkuFlag);
                dic.Add(item.Count, stock);
            }
            int rtn = memberServer.ComputeMemberDiscount(dic);

            return(Success(new GetMemberDiscountModel()
            {
                Discount = rtn / 100d,
                Balance = memberServer.GetBalance() / 100d
            }));
        }
Example #7
0
        public APIResult Add([FromBody] AddArgsModel args)
        {
            //TODO:这里还需要进行锁操作,保证每个店铺的库存不是负数
            if (args.Items == null || args.Items.Count <= 0)
            {
                throw new Exception("商品内容不能为空");
            }
            bool isTakeOut = args.IsTakeOut == null ? false : args.IsTakeOut.Value;
            bool isThird   = false;
            var  memberId  = GetMemberId();
            var  shop      = db.Query <Shop>()
                             .Where(m => !m.IsDel)
                             .Where(m => m.Id == args.ShopId)
                             .FirstOrDefault();

            if (shop == null)
            {
                throw new Exception("商铺纪录不存在");
            }

            ShopMember shopMember      = ShopMemberServer.GetShopMember(db, shop.Id, memberId);
            var        shopMemberSet   = ShopMemberServer.GetShopMemberSet(db, shop.Id);
            bool       IsTopUpDiscount = shopMemberSet == null ? false : shopMemberSet.IsTopUpDiscount;
            double     memberDiscount  = 1;

            if (shopMember != null)
            {
                var shopMemberLevel = db.GetSingle <ShopMemberLevel>(shopMember.ShopMemberLevelId);
                if (shopMemberLevel != null)
                {
                    memberDiscount = shopMemberLevel.Discount;
                }
            }

            var shoptakeoutinfo = db.ShopTakeOutInfo.FirstOrDefault(r => r.ShopId == args.ShopId && !r.IsDel);

            if (shoptakeoutinfo != null && shoptakeoutinfo.TakeDistributionType == TakeDistributionType.达达配送 && isTakeOut && args.TakeWay == TakeWay.送货上门)
            {
                isThird = true;
            }

            var model = new ShopOrder()
            {
                AddTime              = DateTime.Now,
                MemberId             = memberId,
                ShopId               = args.ShopId,
                Remark               = args.Remark,
                AddUser              = $"member{memberId}",
                AddIp                = GetIp(),
                Flag                 = Guid.NewGuid().ToString(),
                IsTakeOut            = isTakeOut,
                Status               = ShopOrderStatus.待支付,
                takeDistributionType = isThird == true ? TakeDistributionType.达达配送 : TakeDistributionType.商家配送
            };
            ShopOrderServer shopOrderServer = new ShopOrderServer(model);

            db.AddTo(model);
            if (isTakeOut)
            {
                shopOrderServer.RecordTakeout(db, shop, memberId, args.TakeWay.Value, args.PickupTime);
            }
            else if (args.IsSelfHelp.HasValue && args.IsSelfHelp.Value)
            {
                shopOrderServer.RecordShopOrderSelfHelp(db, shop.Id, args.IsSelfHelpTakeOut ?? false);
            }
            else if (!string.IsNullOrEmpty(args.ShopPartFlag))
            {
                shopOrderServer.RecordScancode(db, args.ShopPartFlag);
            }
            else
            {
                throw new Exception("请先扫描桌上二唯码");
            }
            int cartCount = 0;

            args.Items.ForEach(m => cartCount += m.Count);
            shopOrderServer.RecordOtherFee(db, args.TakeWay, cartCount);
            var commodityIdAndCounts = new Dictionary <int, int>();

            //商铺库存判断
            foreach (var item in args.Items)
            {
                var stock = db.Query <ShopCommodityStock>()
                            .Where(m => !m.IsDel)
                            .Where(m => m.ShopId == shop.Id)
                            .Where(m => m.Sku.Flag == item.SkuFlag)
                            .Select(m => new
                {
                    m.Id,
                    m.Stock,
                    IsCombo       = m.Sku.Commodity.CategoryId == 0, //是否套餐
                    CommodityName = m.Sku.Commodity.Name,
                    m.Sku.CommodityId,
                    m.CostPrice,
                    m.SalePrice,
                    m.MarketPrice,
                    HasVipPrice = m.Sku.Commodity.UseMemberPrice,
                    SkuSummary  = m.Sku.Summary
                })
                            .FirstOrDefault();
                if (stock == null)
                {
                    throw new Exception("商品不存在或者商铺商品未上架");
                }
                if (stock.Stock <= 0 || stock.Stock < item.Count)
                {
                    throw new Exception("库存不足");
                }

                db.AddStock(stock.Id, -item.Count);

                //纪录下commodity的count
                if (commodityIdAndCounts.ContainsKey(stock.CommodityId))
                {
                    commodityIdAndCounts[stock.CommodityId] += item.Count;
                }
                else
                {
                    commodityIdAndCounts.Add(stock.CommodityId, item.Count);
                }


                var orderItem = new ShopOrderItem()
                {
                    CommodityName    = stock.CommodityName,
                    CommodityStockId = stock.Id,
                    AddIp            = model.AddIp,
                    AddTime          = model.AddTime,
                    AddUser          = model.AddUser,
                    CostPrice        = stock.CostPrice,
                    SalePrice        = (stock.HasVipPrice && !stock.IsCombo && (!IsTopUpDiscount || args.IsBalance)) ? ShopMemberServer.GetMemberPrice(stock.SalePrice, memberDiscount) : stock.SalePrice,
                    PrimePrice       = stock.SalePrice,
                    ShopOrder        = model,
                    Count            = item.Count,
                    SkuSummary       = stock.SkuSummary
                };
                db.AddTo(orderItem);

                model.Amount += orderItem.SalePrice * orderItem.Count;
            }

            if (args.ComboPrice.HasValue)
            {
                model.Amount += args.ComboPrice.Value;
            }

            //这里尝试扣钱
            //var memberAmount = db.GetMemberAmountList(memberId);
            //var availAmount = memberAmount.GetAvailAmount();
            //if (availAmount >= model.Amount)
            //{
            //    memberAmount.DecreaseAvailAmount(model.Amount, 0, $"{shop.Name}消费", Newtonsoft.Json.JsonConvert.SerializeObject(new { shopId = shop.Id, orderFlag = model.Flag }), FinaceType.商品购买支出);
            //    memberAmount.UpdateMemberAmountCache();
            //    model.PayTime = DateTime.Now;

            //    //更新商品的销售量
            //    //注意,这里如果有一个品牌,多个店铺的情况,会出现销售额共享的情况
            //    var commodityIds = commodityIdAndCounts.Select(m => m.Key).ToList();
            //    var commoditys = db.Query<ShopBrandCommodity>()
            //            .Where(m => commodityIds.Contains(m.Id))
            //            .ToList();
            //    foreach (var item in commoditys)
            //    {
            //        item.SalesForMonth += commodityIdAndCounts[item.Id];
            //    }
            //    db.SaveChanges();
            //    AfterOrderPlacing(model, shop.Name);
            //}
            //else
            //{
            //    db.SaveChanges();
            //}
            model.OrderNumber = shopOrderServer.GenerateOrderNumber();

            shopOrderServer.ComputePayment(db);
            db.SaveChanges();
            model.Shop = null;  //不返回商店信息
            return(Success(model));
        }
Example #8
0
        //[Authorize]
        public async Task <APIResult> ManagerRefund([FromBody] ToAmountArgsModel args)
        {
            string exmsg = string.Empty;

            try
            {
                var shoporder = db.Set <ShopOrder>().FirstOrDefault(r => r.Id == args.id);
                //if (shoporder == null) throw new Exception("未找到此订单");
                //CheckShopActor(shoporder.ShopId, ShopActorType.超级管理员);//检测用户权限
                var shopMemberConsume = db.GetSingle <ShopMemberConsume>(shoporder.ShopMemberConsumeId ?? 0);
                if (shopMemberConsume != null)
                {
                    var shopMemberRufund = db.Query <ShopMemberRufund>()
                                           .Where(m => !m.IsDel && m.ShopOrderId == shoporder.Id)
                                           .FirstOrDefault();
                    if (shopMemberRufund != null)
                    {
                        throw new Exception("该订单已退款");
                    }
                    ShopMemberServer shopMemberServer = new ShopMemberServer(db, shoporder.ShopId, shoporder.MemberId);
                    shopMemberServer.RefundToBalance(shoporder);
                    shoporder.Status = ShopOrderStatus.已退款;
                    db.SaveChanges();
                    var orderType = await ShopIntegralRechargeServer.GetOrderSourceType(db, args.id, false, _logger); //积分回滚

                    await ShopIntegralRechargeServer.IntegralReturn(db, args.id, orderType, _logger);

                    return(Success("退款成功"));
                }
                else
                {
                    var shoppayinfo = db.Set <ShopPayInfo>().FirstOrDefault(r => r.ShopId == shoporder.ShopId && r.IsEnable == true);
                    if (shoppayinfo == null)
                    {
                        throw new Exception("未找到商户退款配置");
                    }
                    var membertradeforrechange = financeDb.Set <MemberTradeForRechange>().Where(r => r.OrderId == shoporder.Id && r.OrderType == OrderType.普通订单 && r.Status == MemberTradeForRechangeStatus.成功);

                    if (membertradeforrechange != null && membertradeforrechange.Count() > 0) //代表有支付成功的数据
                    {
                        Refunds refunds      = new Refunds(proxyFactory);
                        var     refundresult = refunds.RefundAction(new RefundArgsModel()
                        {
                            ShopFlag = shoppayinfo.ShopFlag, TradeNo = membertradeforrechange.First().TradeNo, OrderType = OrderType.普通订单, OrderId = shoporder.Id
                        });
                        if (refundresult.Status == MemberTradeForRefundStatus.成功)
                        {
                            shoporder.Status = ShopOrderStatus.已退款;
                            DecreaseCommodity(shoporder);
                            db.SaveChanges();
                            return(Success());
                        }
                        else if (refundresult.Status == MemberTradeForRefundStatus.退款中)
                        {
                            shoporder.Status = ShopOrderStatus.退款中;
                            db.SaveChanges();
                        }
                        else
                        {
                            shoporder.Status = ShopOrderStatus.退款中;
                            db.SaveChanges();
                        }
                        var orderType = await ShopIntegralRechargeServer.GetOrderSourceType(db, args.id, false, _logger); //积分回滚

                        await ShopIntegralRechargeServer.IntegralReturn(db, args.id, orderType, _logger);

                        return(Success("您的退款申请已经提交,银行处理退款中"));
                    }
                    else
                    {
                        throw new Exception($"找不到订单{shoporder.OrderNumber}支付成功的记录");
                    }
                }
            }
            catch (Exception ex)
            {
                exmsg = ex.Message;
                throw new Exception(ex.Message);
            }
        }