public bool CreateActivityTaocan(Marketing_Activity_Taocan taocan) { bool result = false; if(taocan==null) { throw new KMBitException("输入不正确"); } if(taocan.ActivityId<=0) { throw new KMBitException("活动信息丢失"); } if(taocan.RouteId<=0) { throw new KMBitException("套餐必须选择"); } if(taocan.Quantity<=0) { throw new KMBitException("数量不能小于0"); } if (taocan.Price <= 0) { throw new KMBitException("价格不能小于0"); } chargebitEntities db = new chargebitEntities(); try { Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == taocan.ActivityId select a).FirstOrDefault<Marketing_Activities>(); if(activity==null) { throw new KMBitException(string.Format("编号为{0}的活动不存在",taocan.ActivityId)); } Customer customer = (from c in db.Customer where c.Id == activity.CustomerId select c).FirstOrDefault<Customer>(); if(customer==null) { throw new KMBitException(string.Format("编号为{0}的活动不属于任何客户", taocan.ActivityId)); } if(customer.AgentId!=CurrentLoginUser.User.Id) { throw new KMBitException(string.Format("编号为{0}的活动不属您的客户", taocan.ActivityId)); } List<BAgentRoute> routes = new List<BAgentRoute>(); var query = from r in db.Agent_route join u in db.Users on r.CreatedBy equals u.Id join uu in db.Users on r.UpdatedBy equals uu.Id into luu from lluu in luu.DefaultIfEmpty() join re in db.Resource on r.Resource_Id equals re.Id join tc in db.Resource_taocan on r.Resource_taocan_id equals tc.Id join city in db.Area on tc.Area_id equals city.Id into lcity from llcity in lcity.DefaultIfEmpty() join sp in db.Sp on tc.Sp_id equals sp.Id into lsp from llsp in lsp.DefaultIfEmpty() join tcc in db.Taocan on tc.Taocan_id equals tcc.Id into ltc from lltc in ltc.DefaultIfEmpty() where r.Id==taocan.RouteId select new BAgentRoute { Route = r, CreatedBy = u, UpdatedBy = lluu, Taocan = new BResourceTaocan { Taocan = tc, Resource = new BResource { Resource = re }, Province = llcity, SP = llsp, Taocan2 = lltc } }; routes = query.ToList<BAgentRoute>(); if(routes.Count==0) { throw new KMBitException(string.Format("编号为{0}的套餐不存在", taocan.RouteId)); } BAgentRoute route = routes[0]; if(!route.Route.Enabled) { throw new KMBitException(string.Format("编号为{0}的套餐已经被管理员停用", taocan.RouteId)); } if(!route.Taocan.Taocan.Enabled) { throw new KMBitException(string.Format("编号为{0}的套餐的落地套餐被管理员停用", taocan.RouteId)); } if(!route.Taocan.Resource.Resource.Enabled) { throw new KMBitException(string.Format("编号为{0}的套餐的落地被管理员停用", taocan.RouteId)); } taocan.CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); taocan.ResourceId = route.Taocan.Resource.Resource.Id; taocan.ResourceTaocanId = route.Taocan.Taocan.Id; taocan.Generated = true; Users agency = (from a in db.Users where activity.AgentId == a.Id select a).FirstOrDefault<Users>(); if (agency.Remaining_amount < taocan.Quantity * (route.Taocan.Taocan.Sale_price * route.Route.Discount)) { throw new KMBitException("代理商账户没有足够的余额"); } if(customer.RemainingAmount<taocan.Quantity * taocan.Price) { throw new KMBitException("客户账户没有足够的余额"); } Marketing_Activity_Taocan dbmTaocan = (from mt in db.Marketing_Activity_Taocan where mt.ResourceTaocanId == taocan.ResourceTaocanId && mt.ActivityId==taocan.ActivityId select mt).FirstOrDefault<Marketing_Activity_Taocan>(); if(dbmTaocan==null) { db.Marketing_Activity_Taocan.Add(taocan); }else { dbmTaocan.Quantity += taocan.Quantity; taocan.Id = dbmTaocan.Id; } db.SaveChanges(); if (taocan.Id>0) { agency.Remaining_amount -= taocan.Quantity * (route.Taocan.Taocan.Sale_price * route.Route.Discount); customer.RemainingAmount-= taocan.Quantity * taocan.Price; db.SaveChanges(); for (int i=0;i<taocan.Quantity;i++) { Marketing_Orders order = new Marketing_Orders() { ActivityId = taocan.ActivityId, ActivityTaocanId = taocan.Id, AgentPurchasePrice = 0, AgentSalePrice = 0, CreatedTime = taocan.CreatedTime, ExpiredTime = activity.ExpiredTime, PlatformPurchasePrice = 0, PlatformSalePrice = route.Taocan.Taocan.Sale_price, Sent = false, Used = false, StartTime = activity.StartedTime, UsedTime = 0 }; if(route.Taocan.Taocan.EnableDiscount) { order.PlatformPurchasePrice = route.Taocan.Taocan.Purchase_price * route.Taocan.Taocan.Resource_Discount; } order.AgentPurchasePrice = order.PlatformSalePrice * route.Route.Discount; order.AgentSalePrice = taocan.Price; db.Marketing_Orders.Add(order); } db.SaveChanges(); if(activity.ScanType==2) { GenerateQRCodeForMarketingOrders(taocan.Id); } result = true; } }catch(Exception ex) { throw ex; } finally { if (db!=null) { db.Dispose(); } } return result; }
public ActionResult CreateActivityTaocan(ActivityTaocanModel model) { ActivityManagement activityMgr = null; if (ModelState.IsValid) { try { activityMgr = new ActivityManagement(User.Identity.GetUserId<int>()); Marketing_Activity_Taocan taocan = new Marketing_Activity_Taocan() { ActivityId=model.ActivityId, Price=model.Price, Quantity=model.Quantity, RouteId=model.RouteId}; activityMgr.CreateActivityTaocan(taocan); return Redirect("/Agent/CustomerAcivities?customerId="+model.CustomerId); } catch (KMBitException ex) { ViewBag.Message = ex.Message; } finally { } } List<BAgentRoute> routes = activityMgr.FindAvailableAgentRoutes(User.Identity.GetUserId<int>(), model.CustomerId, model.ActivityId); ViewBag.Routes = new SelectList((from r in routes select new { Id = r.Route.Id, Name = r.Taocan.Taocan2.Name + " - 代理价格" + (r.Taocan.Taocan.Sale_price*r.Route.Discount).ToString("0.00") + "元" }), "Id", "Name"); return View(model); }