示例#1
0
        public void OrderMarketService(int monthCount, long shopId, MarketType type)
        {
            if (shopId <= 0)
            {
                throw new HimallException("ShopId不能识别");
            }
            if (monthCount <= 0)
            {
                throw new HimallException("购买服务时长必须大于零");
            }
            var shop = Context.ShopInfo.FindById(shopId);

            if (shop == null || shopId <= 0)
            {
                throw new HimallException("ShopId不能识别");
            }
            var market    = Context.ActiveMarketServiceInfo.Where(a => a.ShopId == shopId && a.TypeId == type).FirstOrDefault();
            var price     = Context.MarketSettingInfo.Where(a => a.TypeId == type).Select(a => a.Price).FirstOrDefault();
            var StartTime = DateTime.Now;
            MarketServiceRecordInfo model = new MarketServiceRecordInfo();

            model.StartTime = StartTime;
            model.Price     = price * monthCount;
            var shopAccount = Context.ShopAccountInfo.Where(a => a.ShopId == shopId).FirstOrDefault(); //店铺帐户信息

            if (shopAccount.Balance < model.Price)                                                     //店铺余额不足以支付服务费用
            {
                throw new HimallException("您的店铺余额为:" + shopAccount.Balance + "元,不足以支付此次营销服务购买费用,请先充值。");
            }
            if (market != null)
            {
                var maxTime = market.MarketServiceRecordInfo.Max(a => a.EndTime);
                if (maxTime > DateTime.Now) //如果结束时间大于当前时间,续费从结束时间加上购买月数,否则从当前时间加上购买月数
                {
                    StartTime = maxTime;
                }
                model.StartTime = StartTime;
                model.BuyTime   = DateTime.Now;
                model.EndTime   = StartTime.AddMonths(monthCount);
                // model.MarketServiceId = market.Id;
                model.SettlementFlag = 1;
                market.MarketServiceRecordInfo.Add(model);
            }
            else
            {
                model.StartTime      = StartTime;
                model.EndTime        = StartTime.AddMonths(monthCount);
                model.SettlementFlag = 1;
                model.BuyTime        = DateTime.Now;
                ActiveMarketServiceInfo activeMarketServiceInfo = new Model.ActiveMarketServiceInfo();
                activeMarketServiceInfo.ShopId   = shopId;
                activeMarketServiceInfo.ShopName = shop.ShopName;
                activeMarketServiceInfo.TypeId   = type;
                activeMarketServiceInfo.MarketServiceRecordInfo.Add(model);
                Context.ActiveMarketServiceInfo.Add(activeMarketServiceInfo);
            }
            Context.SaveChanges();
            var ShopAccount          = Context.ShopAccountInfo.FirstOrDefault(a => a.ShopId == shopId);
            ShopAccountItemInfo info = new ShopAccountItemInfo();

            info.IsIncome        = false;
            info.ShopId          = shopId;
            info.DetailId        = model.Id.ToString();
            info.ShopName        = shop.ShopName;
            info.AccountNo       = shopId + info.DetailId + new Random().Next(10000);
            info.ReMark          = "店铺购买" + type.ToDescription() + "服务," + monthCount + "个月";
            info.TradeType       = CommonModel.ShopAccountType.MarketingServices;
            info.CreateTime      = DateTime.Now;
            info.Amount          = price * monthCount;
            info.AccoutID        = ShopAccount.Id;
            ShopAccount.Balance -= info.Amount;         //总余额减钱
            info.Balance         = ShopAccount.Balance; //变动后当前剩余金额
            Context.ShopAccountItemInfo.Add(info);
            var PlatAccount           = Context.PlatAccountInfo.FirstOrDefault();
            PlatAccountItemInfo pinfo = new PlatAccountItemInfo();

            pinfo.IsIncome       = true;
            pinfo.DetailId       = model.Id.ToString();
            pinfo.AccountNo      = info.AccountNo;
            pinfo.ReMark         = "店铺购买" + type.ToDescription() + "服务," + monthCount + "个月";
            pinfo.TradeType      = CommonModel.PlatAccountType.MarketingServices;
            pinfo.CreateTime     = DateTime.Now;
            pinfo.Amount         = price * monthCount;
            pinfo.AccoutID       = PlatAccount.Id;
            PlatAccount.Balance += info.Amount;  //总余额加钱

            pinfo.Balance = PlatAccount.Balance; //变动后当前剩余金额
            Context.PlatAccountItemInfo.Add(pinfo);
            Context.SaveChanges();
        }
示例#2
0
        public void OrderMarketService(int monthCount, long shopId, MarketType type)
        {
            if (shopId <= 0)
            {
                throw new HimallException("ShopId不能识别");
            }
            if (monthCount <= 0)
            {
                throw new HimallException("购买服务时长必须大于零");
            }
            var shop = DbFactory.Default.Get <ShopInfo>().Where(p => p.Id == shopId).FirstOrDefault();

            if (shop == null || shopId <= 0)
            {
                throw new HimallException("ShopId不能识别");
            }

            var price     = DbFactory.Default.Get <MarketSettingInfo>().Where(a => a.TypeId == type).Select(a => a.Price).FirstOrDefault <decimal>();
            var StartTime = DateTime.Now;
            MarketServiceRecordInfo model = new MarketServiceRecordInfo();

            model.StartTime = StartTime;
            model.Price     = price * monthCount;
            var shopAccount = DbFactory.Default.Get <ShopAccountInfo>().Where(a => a.ShopId == shopId).FirstOrDefault();//店铺帐户信息

            #region 它下面会取几次只,如为空会报异常,默认存入0的初始值
            if (shopAccount == null)
            {
                shopAccount                   = new ShopAccountInfo();
                shopAccount.ShopId            = shopId;
                shopAccount.ShopName          = shop.ShopName;
                shopAccount.Balance           = 0;
                shopAccount.PendingSettlement = 0;
                shopAccount.Settled           = 0;
                shopAccount.ReMark            = string.Empty;
                DbFactory.Default.Add(shopAccount);
            }
            #endregion

            if (shopAccount.Balance < model.Price) //店铺余额不足以支付服务费用
            {
                throw new HimallException("您的店铺余额为:" + shopAccount.Balance + "元,不足以支付此次营销服务购买费用,请先充值。");
            }

            DbFactory.Default.InTransaction(() =>
            {
                var market = DbFactory.Default.Get <ActiveMarketServiceInfo>().Where(a => a.ShopId == shopId && a.TypeId == type).FirstOrDefault();
                if (market != null)
                {
                    var maxTime = MarketApplication.GetServiceEndTime(market.Id);
                    if (maxTime > DateTime.Now) //如果结束时间大于当前时间,续费从结束时间加上购买月数,否则从当前时间加上购买月数
                    {
                        StartTime = maxTime;
                    }
                    model.StartTime = StartTime;
                    model.BuyTime   = DateTime.Now;
                    model.EndTime   = StartTime.AddMonths(monthCount);
                    // model.MarketServiceId = market.Id;
                    model.SettlementFlag  = 1;
                    model.MarketServiceId = market.Id;

                    DbFactory.Default.Add(model);
                }
                else
                {
                    model.StartTime      = StartTime;
                    model.EndTime        = StartTime.AddMonths(monthCount);
                    model.SettlementFlag = 1;
                    model.BuyTime        = DateTime.Now;

                    ActiveMarketServiceInfo activeMarketServiceInfo = new ActiveMarketServiceInfo();
                    activeMarketServiceInfo.ShopId   = shopId;
                    activeMarketServiceInfo.ShopName = shop.ShopName;
                    activeMarketServiceInfo.TypeId   = type;
                    DbFactory.Default.Add(activeMarketServiceInfo);

                    model.MarketServiceId = activeMarketServiceInfo.Id;
                    DbFactory.Default.Add(model);
                }

                var ShopAccount          = DbFactory.Default.Get <ShopAccountInfo>().Where(a => a.ShopId == shopId).FirstOrDefault();
                ShopAccountItemInfo info = new ShopAccountItemInfo();
                info.IsIncome            = false;
                info.ShopId          = shopId;
                info.DetailId        = model.Id.ToString();
                info.ShopName        = shop.ShopName;
                info.AccountNo       = shopId + info.DetailId + new Random().Next(10000);
                info.ReMark          = "店铺购买" + type.ToDescription() + "服务," + monthCount + "个月";
                info.TradeType       = ShopAccountType.MarketingServices;
                info.CreateTime      = DateTime.Now;
                info.Amount          = price * monthCount;
                info.AccoutID        = ShopAccount.Id;
                ShopAccount.Balance -= info.Amount;         //总余额减钱
                info.Balance         = ShopAccount.Balance; //变动后当前剩余金额

                DbFactory.Default.Add(info);
                DbFactory.Default.Update(ShopAccount);

                var platAccount = DbFactory.Default.Get <PlatAccountInfo>().FirstOrDefault();
                var pinfo       = new PlatAccountItemInfo
                {
                    IsIncome   = true,
                    DetailId   = model.Id.ToString(),
                    AccountNo  = info.AccountNo,
                    ReMark     = "店铺购买" + type.ToDescription() + "服务," + monthCount + "个月",
                    TradeType  = PlatAccountType.MarketingServices,
                    CreateTime = DateTime.Now,
                    Amount     = price * monthCount,
                    AccoutID   = platAccount.Id
                };
                platAccount.Balance += info.Amount;         //总余额加钱
                pinfo.Balance        = platAccount.Balance; //变动后当前剩余金额
                DbFactory.Default.Add(pinfo);
                DbFactory.Default.Update(platAccount);
            });
        }