public bool UpdateActivity(Marketing_Activities activity) { bool result = false; if (activity == null || activity.Id <= 0) { throw new KMBitException("参数错误"); } using (chargebitEntities db = new chargebitEntities()) { Marketing_Activities dbac = (from a in db.Marketing_Activities where a.Id == activity.Id select a).FirstOrDefault <Marketing_Activities>(); if (dbac == null) { throw new KMBitException("参数错误"); } if (!string.IsNullOrEmpty(activity.Name)) { dbac.Name = activity.Name; } if (!string.IsNullOrEmpty(activity.Description)) { dbac.Description = activity.Description; } dbac.Enabled = activity.Enabled; db.SaveChanges(); result = true; } return(result); }
public List <BActivityTaocan> FindActivityTaocans(int actityId, int customerId, int agentId) { List <BActivityTaocan> taocans = new List <BActivityTaocan>(); using (chargebitEntities db = new chargebitEntities()) { Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == actityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { throw new KMBitException(string.Format("编号为{0}的活动不存在", actityId)); } if (agentId == 0) { agentId = CurrentLoginUser.User.Id; } if (agentId != activity.AgentId) { throw new KMBitException(string.Format("编号为{0}的活动不是您客户的活动", actityId)); } if (activity.CustomerId != customerId) { throw new KMBitException(string.Format("编号为{0}的活动不是编号为{1}的客户的", actityId, customerId)); } var query = from at in db.Marketing_Activity_Taocan join route in db.Agent_route on at.RouteId equals route.Id join taocan in db.Resource_taocan on route.Resource_taocan_id equals taocan.Id join taocan2 in db.Taocan on taocan.Taocan_id equals taocan2.Id where at.ActivityId == actityId select new BActivityTaocan { ATaocan = at, Route = new BAgentRoute() { Route = route, Taocan = new BResourceTaocan() { Taocan = taocan, Taocan2 = taocan2 } }, UsedCount = (from ao in db.Marketing_Orders where ao.ActivityId == activity.Id && ao.Used == true && ao.ActivityTaocanId == at.Id select ao.Id).Count(), SentOutCount = (from ao in db.Marketing_Orders where ao.ActivityId == activity.Id && ao.Sent == true && ao.ActivityTaocanId == at.Id select ao.Id).Count(), }; taocans = query.ToList <BActivityTaocan>(); } return(taocans); }
public Marketing_Activities CreateNewActivity(Marketing_Activities activity) { if (activity == null) { throw new KMBitException("参数输入错误"); } if (activity.AgentId != CurrentLoginUser.User.Id) { throw new KMBitException("代理商用户不能为别的代理商用户的客户创建活动"); } using (chargebitEntities db = new chargebitEntities()) { 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}的客户不存在", activity.CustomerId)); } db.Marketing_Activities.Add(activity); db.SaveChanges(); } return(activity); }
public ActionResult CreateCustomerActivity(CustomerActivityModel model) { if (ModelState.IsValid) { ActivityManagement activityMgr = new ActivityManagement(User.Identity.GetUserId <int>()); Marketing_Activities activity = new Marketing_Activities() { AgentId = User.Identity.GetUserId <int>(), CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), CustomerId = model.CustomerId, Description = model.Description, ScanType = model.ScanType, Enabled = model.Enable, Name = model.Name, Id = model.Id }; if (model.Id == 0) { activity = activityMgr.CreateNewActivity(activity); if (activity.Id > 0) { return(Redirect("/Agent/CustomerAcivities?customerId=" + model.CustomerId)); } } else { activityMgr.UpdateActivity(activity); return(Redirect("/Agent/CustomerAcivities?customerId=" + model.CustomerId)); } } List <DictionaryTemplate> scanTypes = StaticDictionary.GetScanTypeList(); ViewBag.ScanTypes = new SelectList(from st in scanTypes select new { Id = st.Id, Name = st.Value }, "Id", "Name"); return(View(model)); }
public ChargeOrder GenerateOrder(ChargeOrder order) { if (order == null) { throw new KMBitException("订单对象不能为NULL"); } if (string.IsNullOrEmpty(order.MobileNumber)) { throw new KMBitException("充值的手机号码不能为空"); } chargebitEntities db = null; try { db = new chargebitEntities(); Marketing_Orders mOrder = null; Marketing_Activity_Taocan mTaocan = null; Marketing_Activities activity = null; if (order.IsMarket && order.MarketOrderId > 0) { //if (string.IsNullOrEmpty(order.MacAddress)) //{ // throw new KMBitException("活动充值时必须获取终端的MAC地址"); //} mOrder = (from o in db.Marketing_Orders where o.Id == order.MarketOrderId select o).FirstOrDefault <Marketing_Orders>(); if (mOrder == null) { throw new KMBitException(string.Format("编号为{0}的活动充值记录不存在", order.MarketOrderId)); } if (mOrder.Used) { throw new KMBitException("此二维码已经只用过,不能重复使用"); } mTaocan = (from m in db.Marketing_Activity_Taocan where m.Id == mOrder.ActivityTaocanId select m).FirstOrDefault <Marketing_Activity_Taocan>(); if (mTaocan == null) { throw new KMBitException(string.Format("编号为{0}的活动充值记录的套餐信息不存在", order.MarketOrderId)); } activity = (from a in db.Marketing_Activities where a.Id == mTaocan.ActivityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { throw new KMBitException(string.Format("编号为{0}的活动不存在", mTaocan.ActivityId)); } //check if the device or the number is already charged if (activity.ScanType == 1) { int[] acos = (from mo in db.Marketing_Orders where mo.ActivityId == activity.Id select mo.Id).ToArray <int>(); if (acos == null || acos.Length <= 0) { throw new KMBitException(string.Format("编号为{0}的活动还没有任何可用的充值套餐", mTaocan.ActivityId)); } //验证电话号码是否在某个直扫活动里已经成功充值过 int count = (from o in db.Charge_Order where o.Phone_number == order.MobileNumber && o.Status != 3 && acos.Contains(o.MarketOrderId) select o.Id).Count(); if (count > 0) { throw new KMBitException("同一次营销活动每个手机每个号码只能扫描一次"); } } order.ResourceId = mTaocan.ResourceId; order.ResourceTaocanId = mTaocan.ResourceTaocanId; order.RouteId = mTaocan.RouteId; if (order.AgencyId == 0) { order.AgencyId = (from a in db.Marketing_Activities where a.Id == mTaocan.ActivityId select a.AgentId ).FirstOrDefault <int>(); } } Resource_taocan taocan = null; if (order.ResourceTaocanId <= 0 && order.RouteId > 0) { var query = from ta in db.Agent_route join tc in db.Resource_taocan on ta.Resource_taocan_id equals tc.Id where ta.Id == order.RouteId select tc; taocan = query.FirstOrDefault <Resource_taocan>(); } else if (order.ResourceTaocanId > 0) { taocan = (from tc in db.Resource_taocan where tc.Id == order.ResourceTaocanId select tc).FirstOrDefault <Resource_taocan>(); } if (!taocan.Enabled) { throw new KMBitException(ChargeConstant.RESOURCE_TAOCAN_DISABLED); } if (taocan == null) { throw new KMBitException("套餐信息不存在,不能充值"); } order.ResourceTaocanId = taocan.Id; KMBit.DAL.Resource resource = (from ri in db.Resource join tr in db.Resource_taocan on ri.Id equals tr.Resource_id where tr.Id == order.ResourceTaocanId select ri).FirstOrDefault <Resource>(); if (resource == null) { throw new KMBitException("落地资源不存在"); } if (!resource.Enabled) { throw new KMBitException(ChargeConstant.RESOURCE_DISABLED); } Agent_route route = null; if (order.AgencyId > 0 && order.RouteId > 0) { route = (from r in db.Agent_route where r.Id == order.RouteId select r).FirstOrDefault <Agent_route>(); if (route == null) { throw new KMBitException("代理商路由不存在"); } if (route.User_id != order.AgencyId) { throw new KMBitException("当前代理商没有此路由"); } if (!route.Enabled) { throw new KMBitException(string.Format("编号为{0}代理商路由已被管理员停用", route.Id)); } } Charge_Order history = new Charge_Order(); history.Agent_Id = order.AgencyId; history.Completed_Time = 0; history.Created_time = order.CreatedTime > 0 ? order.CreatedTime : DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); history.Operate_User = order.OperateUserId; history.Out_Order_Id = ""; history.Payed = order.Payed; history.Phone_number = order.MobileNumber; history.MobileSP = order.MobileSP; history.Province = order.Province; history.City = order.City; history.Resource_id = resource.Id; history.Resource_taocan_id = order.ResourceTaocanId; history.RuoteId = order.RouteId; history.Status = 11; history.CallBackUrl = order.CallbackUrl != null ? order.CallbackUrl : null; history.Platform_Sale_Price = taocan.Sale_price; if (taocan.EnableDiscount) { history.Platform_Cost_Price = taocan.Purchase_price * taocan.Resource_Discount; } else { history.Platform_Cost_Price = taocan.Purchase_price; } if (order.IsMarket && order.MarketOrderId > 0) { //营销充值 history.Charge_type = 1; history.MarketOrderId = order.MarketOrderId; history.Sale_price = mTaocan.Price; history.Purchase_price = history.Platform_Sale_Price * route.Discount; history.Payed = true; } else { if (order.AgencyId > 0) { //代理商充值 history.Charge_type = 1; history.Purchase_price = taocan.Sale_price * route.Discount; history.Sale_price = route.Sale_price; history.Revenue = history.Purchase_price - history.Platform_Cost_Price; } else if (order.OperateUserId > 0) { //管理员后台充值 history.Charge_type = 2; history.Purchase_price = taocan.Sale_price; history.Sale_price = taocan.Sale_price; history.Revenue = history.Platform_Sale_Price - history.Platform_Cost_Price; } else { //前台用户直冲 history.Charge_type = 0; history.Purchase_price = taocan.Sale_price; history.Sale_price = taocan.Sale_price; history.Revenue = history.Platform_Sale_Price - history.Platform_Cost_Price; } } db.Charge_Order.Add(history); db.SaveChanges(); order.Id = history.Id; //Create Payment record for direct charge if (history.Charge_type == 0) { Payment_history payment = new Payment_history() { PaymentAccount = "", Amount = taocan.Sale_price, ChargeOrderId = history.Id, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), PayType = 0, Tranfser_Type = 1 }; db.Payment_history.Add(payment); db.SaveChanges(); order.PaymentId = payment.Id; } }catch (Exception ex) { throw new KMBitException(ex.Message); } finally { if (db != null) { db.Dispose(); } } return(order); }
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 void GenerateQRCodeForMarketingOrders(int activityTaocanId) { AppSettings settings = AppSettings.GetAppSettings(); string qrfolder = System.IO.Path.Combine(settings.RootDirectory, settings.QRFolder); using (chargebitEntities db = new chargebitEntities()) { Marketing_Activities activity = null; Customer customer = null; List <Marketing_Orders> orders = (from o in db.Marketing_Orders where o.ActivityTaocanId == activityTaocanId select o).ToList <Marketing_Orders>(); if (orders.Count > 0) { foreach (Marketing_Orders order in orders) { if (order.Used) { continue; } if (!string.IsNullOrEmpty(order.CodePath)) { string tmpPhysicalPath = Path.Combine(settings.RootDirectory, order.CodePath.Replace('/', '\\')); if (File.Exists(tmpPhysicalPath)) { continue; } } if (activity == null) { activity = (from a in db.Marketing_Activities where a.Id == order.ActivityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { continue; } if (customer == null) { customer = (from c in db.Customer where c.Id == activity.CustomerId select c).FirstOrDefault <Customer>(); } } string midPhysicalPath = string.Format("{0}\\{1}", activity.AgentId, activity.CustomerId); string fileName = Guid.NewGuid().ToString() + ".png"; string urlAbsPath = string.Format("{0}/{1}/{2}", activity.AgentId, activity.CustomerId, fileName); string parameter = string.Empty; //string.Format("agentId={0}&customerId={1}&activityId={2}&activityOrderId={3}", activity.AgentId, activity.CustomerId,activity.Id,order.Id); SortedDictionary <string, string> ps = new SortedDictionary <string, string>(); ps["agentId"] = activity.AgentId.ToString(); ps["customerId"] = activity.CustomerId.ToString(); ps["activityId"] = activity.Id.ToString(); ps["activityOrderId"] = order.Id.ToString(); StringBuilder pstr = new StringBuilder(); int count = 1; foreach (KeyValuePair <string, string> p in ps) { pstr.Append(p.Key); pstr.Append("="); pstr.Append(p.Value); if (count < ps.Count) { pstr.Append("&"); } count++; } parameter = pstr.ToString(); pstr.Append("&key="); pstr.Append(customer.Token); parameter = parameter + "&signature=" + UrlSignUtil.GetMD5(pstr.ToString()); parameter = KMEncoder.Encode(parameter); string codeContent = string.Format("{0}/Product/SaoMa?p={1}", settings.WebURL, parameter); string fullFolder = Path.Combine(qrfolder, midPhysicalPath); QRCodeUtil.CreateQRCode(fullFolder, fileName, codeContent); if (File.Exists(Path.Combine(fullFolder, fileName))) { order.CodePath = urlAbsPath; } } db.SaveChanges(); } } }
public string GetOneRandomMarketOrderQrCodeUrl(string spName, string openId, int agentId, int customerId, int activityId) { logger.Info("OpenId:" + openId); AppSettings settings = AppSettings.GetAppSettings(); if (string.IsNullOrEmpty(openId)) { throw new KMBitException("用户的微信号不能为空,用户必须通过关注微信公众号来获取二维码"); } if (string.IsNullOrEmpty(spName)) { throw new KMBitException("获取客户活动随机二维码时候,必须输入运营商名称"); } if (!spName.Contains("+联通") && !spName.Contains("+移动") && !spName.Contains("+电信")) { throw new KMBitException("运营商名称只能是,+联通,+移动,+电信,+号为英文键盘下的+号"); } if (agentId == 0) { throw new KMBitException("代理商编号不能为空"); } if (customerId == 0) { throw new KMBitException("客户编号不能为空"); } using (chargebitEntities db = new chargebitEntities()) { int sp = 0; if (spName.Contains("联通")) { sp = 3; } else if (spName.Contains("移动")) { sp = 1; } else if (spName.Contains("电信")) { sp = 2; } Marketing_Activities activity = null; if (activityId == 0) { //get the latest activity activity = (from a in db.Marketing_Activities where a.CustomerId == customerId && a.AgentId == agentId && a.Enabled == true && a.ScanType == 2 orderby a.Id descending select a).FirstOrDefault <Marketing_Activities>(); } else { activity = (from a in db.Marketing_Activities where a.Id == activityId select a).FirstOrDefault <Marketing_Activities>(); } if (activity == null) { throw new KMBitException("输入参数不正确,请不要试图偷换URL参数来获取数据"); } if (activity.AgentId != agentId) { throw new KMBitException("输入参数不正确,请不要试图偷换URL参数来获取数据"); } if (activity.CustomerId != customerId) { throw new KMBitException("输入参数不正确,请不要试图偷换URL参数来获取数据"); } if (activity.ScanType != 2) { throw new KMBitException("此活动为直接扫码活动,不能分个推送二维码"); } List <Marketing_Activity_Taocan> rTaocans = (from mt in db.Marketing_Activity_Taocan join t in db.Resource_taocan on mt.ResourceTaocanId equals t.Id where mt.ActivityId == activity.Id && t.Sp_id == sp select mt).ToList <Marketing_Activity_Taocan>(); if (rTaocans.Count == 0) { throw new KMBitException(string.Format("此次活动{0}的手机号码不能扫码充流量,如有疑问请联系活动举办方", spName)); } Marketing_Activity_Taocan mTaocan = rTaocans[0]; Marketing_Orders returnOrder = null; List <Marketing_Orders> existedOrders = (from mo in db.Marketing_Orders where mo.ActivityId == activity.Id && mo.OpenId == openId select mo).ToList <Marketing_Orders>(); if (existedOrders.Count > 0) { returnOrder = existedOrders[0]; if (!returnOrder.Used && returnOrder.Sent) { logger.Info("Already sent but not used, sent again"); return(settings.WebURL + "/" + settings.QRFolder + "/" + returnOrder.CodePath); } else { logger.Info("Already sent and used"); throw new KMBitException(string.Format("此微信号已经获取过二维码,并且已经扫码使用过二维码(一次活动一个微信号只能获取一个二维码)")); } } logger.Info("not sent and not used"); returnOrder = (from mo in db.Marketing_Orders where mo.ActivityTaocanId == mTaocan.Id && mo.ActivityId == activity.Id && mo.Used == false && mo.Sent == false orderby mo.Id descending select mo).FirstOrDefault <Marketing_Orders>(); if (returnOrder == null) { throw new KMBitException(string.Format("本次活动的二维码全部送完,尽情期待下次活动,感谢您的关注")); } returnOrder.OpenId = openId; returnOrder.Sent = true; db.SaveChanges(); return(settings.WebURL + "/" + settings.QRFolder + "/" + returnOrder.CodePath); } }
public ChargeResult MarketingCharge(BMarketOrderCharge orderCharge) { logger.Info("MarketingCharge"); ChargeResult result = new ChargeResult(); if (orderCharge == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (orderCharge.AgentId == 0 || orderCharge.CustomerId == 0 || orderCharge.ActivityId == 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (string.IsNullOrEmpty(orderCharge.SPName)) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (string.IsNullOrEmpty(orderCharge.Phone)) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } int sp = 0; if (orderCharge.SPName.Contains("联通")) { sp = 3; } else if (orderCharge.SPName.Contains("移动")) { sp = 1; } else if (orderCharge.SPName.Contains("电信")) { sp = 2; } chargebitEntities db = new chargebitEntities(); Marketing_Activity_Taocan mtaocan = null; Marketing_Orders mOrder = null; try { Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == orderCharge.ActivityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (activity.CustomerId != orderCharge.CustomerId) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } if (activity.AgentId != orderCharge.AgentId) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } //非直接扫码活动,必须传入特定的marketing order id if (activity.ScanType != 1 && orderCharge.ActivityOrderId == 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数不正确"; return(result); } ChargeOrder order = new ChargeOrder() { AgencyId = orderCharge.AgentId, ChargeType = 1, City = orderCharge.City, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), IsMarket = true, MacAddress = orderCharge.OpenId, MobileNumber = orderCharge.Phone, MobileSP = orderCharge.SPName, Payed = true, Province = orderCharge.Province, }; //direct scan if (activity.ScanType == 1 && orderCharge.ActivityOrderId <= 0) { //判断是否还有可用marketing order List <Marketing_Activity_Taocan> rTaocans = (from mt in db.Marketing_Activity_Taocan join t in db.Resource_taocan on mt.ResourceTaocanId equals t.Id where mt.ActivityId == orderCharge.ActivityId && t.Sp_id == sp select mt).ToList <Marketing_Activity_Taocan>(); if (rTaocans.Count == 0) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动" + orderCharge.SPName + "不能扫码充值"; return(result); } mtaocan = rTaocans[0]; mOrder = (from o in db.Marketing_Orders where o.ActivityId == orderCharge.ActivityId && o.Sent == false && o.Used == false && o.ActivityTaocanId == mtaocan.Id select o).FirstOrDefault <Marketing_Orders>(); if (mOrder == null) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动"; return(result); } if (mOrder.Used) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动"; return(result); } mOrder.Used = true; mOrder.Sent = true; //db.SaveChanges(); order.MarketOrderId = mOrder.Id; order.ResourceTaocanId = mtaocan.ResourceTaocanId; }//weichat push else if (activity.ScanType == 2 && orderCharge.ActivityOrderId > 0) { mOrder = (from o in db.Marketing_Orders where o.Id == orderCharge.ActivityOrderId select o).FirstOrDefault <Marketing_Orders>(); if (mOrder == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } if (mOrder.Used) { result.Status = ChargeStatus.FAILED; result.Message = "本次活动的流量充值额度已经全部被扫完,尽请期待下次活动"; return(result); } mtaocan = (from mt in db.Marketing_Activity_Taocan where mt.Id == mOrder.ActivityTaocanId select mt).FirstOrDefault <Marketing_Activity_Taocan>(); if (mtaocan == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } Resource_taocan rT = (from r in db.Resource_taocan where r.Id == mtaocan.ResourceTaocanId select r).FirstOrDefault <Resource_taocan>(); if (rT == null) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } if (rT.Sp_id != sp) { string tmpSPName = ""; if (rT.Sp_id == 1) { tmpSPName = "中国移动"; } else if (rT.Sp_id == 2) { tmpSPName = "中国电信"; } else if (rT.Sp_id == 3) { tmpSPName = "中国联通"; } result.Status = ChargeStatus.FAILED; result.Message = string.Format("此二维码链接不能充值{0}的手机号码的流量,只能充值{1}的号码的流量", orderCharge.SPName, tmpSPName); return(result); } order.ResourceTaocanId = mtaocan.ResourceTaocanId; order.MarketOrderId = orderCharge.ActivityOrderId; mOrder.Used = true; mOrder.Sent = true; } else if (activity.ScanType == 1 && orderCharge.ActivityOrderId > 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } else if (activity.ScanType == 2 && orderCharge.ActivityOrderId <= 0) { result.Status = ChargeStatus.FAILED; result.Message = "参数有误"; return(result); } OrderManagement orderMgr = new OrderManagement(CurrentLoginUser); order = orderMgr.GenerateOrder(order); ChargeBridge chargeBridge = new ChargeBridge(); if (order.Id > 0) { db.SaveChanges(); result = chargeBridge.Charge(order); if (result.Status == ChargeStatus.FAILED) { //Rollback, the order cannot be used next time mOrder.UsedTime = 0; mOrder.Used = false; if (activity.ScanType == 1) { mOrder.Sent = false; } db.SaveChanges(); } else { mOrder.UsedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); } db.SaveChanges(); } } catch (KMBitException kex) { logger.Warn(kex); result.Status = ChargeStatus.FAILED; result.Message = kex.Message; if (mOrder != null) { mOrder.Used = false; db.SaveChanges(); } } catch (Exception ex) { logger.Fatal(ex); result.Status = ChargeStatus.FAILED; result.Message = "系统错误,稍后再试"; if (mOrder != null) { mOrder.Used = false; db.SaveChanges(); } } finally { if (db != null) { db.Dispose(); } } logger.Info("Finished MarketingCharge"); return(result); }
public string GenerateActivityQRCode(int agendId, int customerId, int activityId) { string codePath = string.Empty; agendId = agendId > 0 ? agendId : CurrentLoginUser.User.Id; AppSettings settings = AppSettings.GetAppSettings(); using (chargebitEntities db = new chargebitEntities()) { Marketing_Activities activity = (from a in db.Marketing_Activities where a.Id == activityId select a).FirstOrDefault <Marketing_Activities>(); if (activity == null) { throw new KMBitException(string.Format("编号为:{0}的活动不存在", activityId)); } if (activity.CustomerId != customerId) { throw new KMBitException(string.Format("编号为{0}的活动不属于编号为{1}的客户", activityId, customerId)); } if (activity.AgentId != agendId) { throw new KMBitException(string.Format("编号为{0}的活动不属于编号为{1}的代理的客户的活动", activityId, agendId)); } Customer customer = (from c in db.Customer where c.Id == customerId select c).FirstOrDefault <Customer>(); codePath = agendId + "\\" + customerId; string fileName = Guid.NewGuid().ToString() + ".png"; string absPath = agendId + "/" + customerId + "/" + fileName; string fullDirectory = Path.Combine(settings.RootDirectory + "\\" + settings.QRFolder, codePath); if (!string.IsNullOrEmpty(activity.CodePath) && File.Exists(Path.Combine(settings.RootDirectory + "\\" + settings.QRFolder, activity.CodePath.Replace('/', '\\')))) { return(activity.CodePath); } string parameter = string.Empty;// string.Format("agentId={0}&customerId={1}&activityId={2}", agendId, customerId, activityId); SortedDictionary <string, string> ps = new SortedDictionary <string, string>(); ps["agentId"] = activity.AgentId.ToString(); ps["customerId"] = activity.CustomerId.ToString(); ps["activityId"] = activity.Id.ToString(); StringBuilder pstr = new StringBuilder(); int count = 1; foreach (KeyValuePair <string, string> p in ps) { pstr.Append(p.Key); pstr.Append("="); pstr.Append(p.Value); if (count < ps.Count) { pstr.Append("&"); } count++; } parameter = pstr.ToString(); pstr.Append("&key="); pstr.Append(customer.Token); parameter = parameter + "&signature=" + UrlSignUtil.GetMD5(pstr.ToString()); parameter = KMEncoder.Encode(parameter); string codeContent = string.Format("{0}/Product/SaoMa?p={1}", settings.WebURL, parameter); QRCodeUtil.CreateQRCode(fullDirectory, fileName, codeContent); if (File.Exists(Path.Combine(fullDirectory, fileName))) { activity.CodePath = absPath; } db.SaveChanges(); return(absPath); } }