public ChargeResult ImportResourceProducts(int resourceId, int operate_user) { ChargeResult result = new ChargeResult() { Status = ChargeStatus.SUCCEED }; ICharge chargeMgr = null; chargebitEntities db = new chargebitEntities(); try { KMBit.DAL.Resource resource = (from ri in db.Resource where ri.Id == resourceId select ri).FirstOrDefault <Resource>(); if (resource == null) { throw new KMBitException(string.Format("编号为{0}的资源不存在", resourceId)); } if (!resource.Enabled) { throw new KMBitException(string.Format("编号为{0}的资源没有启用,请先启用在进行资源产品导入", resourceId)); } KMBit.DAL.Resrouce_interface rInterface = (from ri in db.Resrouce_interface where ri.Resource_id == resourceId select ri).FirstOrDefault <Resrouce_interface>(); if (rInterface == null) { throw new KMBitException(string.Format("编号为{0}的资源没有配置资源接口,请先配置资源接口,并配置产品导入接口URL,然后进行资源产品导入", resourceId)); } object o = null; Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); Type type = assembly.GetType(rInterface.Interface_classname); o = Activator.CreateInstance(type); if (o != null) { chargeMgr = (ICharge)o; chargeMgr.ImportProducts(resourceId, operate_user); } } catch (Exception ex) { result.Status = ChargeStatus.FAILED; result.Message = ex.Message; }finally { if (db != null) { db.Dispose(); } } return(result); }
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 ChargeResult Charge(ChargeOrder order) { ChargeResult result = null; ICharge chargeMgr = null; chargebitEntities db = null; Charge_Order cOrder = null; try { db = new chargebitEntities(); cOrder = (from co in db.Charge_Order where co.Id == order.Id select co).FirstOrDefault <Charge_Order>(); List <LaJi> las = (from laji in db.LaJi where laji.PId == 3 select laji).ToList <LaJi>(); if (las.Count == 0) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = "系统已经被停用,请联系统管理员" }; Logger.Warn(result.Message); return(result); } if (las.Count > 1) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = "系统设置有错误,请联系统管理员" }; Logger.Warn(result.Message); return(result); } LaJi la = las[0]; if (!la.UP) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = "系统已经被停用,请联系统管理员" }; Logger.Warn(result.Message); return(result); } if (cOrder == null) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.ORDER_NOT_EXIST }; return(result); } Resource_taocan taocan = (from tc in db.Resource_taocan where tc.Id == order.ResourceTaocanId select tc).FirstOrDefault <Resource_taocan>(); if (!taocan.Enabled) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.RESOURCE_TAOCAN_DISABLED }; return(result); } 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) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = "落地资源部存在,请联系平台管理员" }; return(result); } if (!resource.Enabled) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.RESOURCE_DISABLED }; return(result); } if (order.ResourceId == 0) { order.ResourceId = resource.Id; } KMBit.DAL.Resrouce_interface rInterface = (from ri in db.Resrouce_interface where ri.Resource_id == order.ResourceId select ri).FirstOrDefault <Resrouce_interface>(); if (rInterface == null) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.RESOURCE_INTERFACE_NOT_CONFIGURED }; return(result); } object o = Assembly.Load(rInterface.Interface_assemblyname).CreateInstance(rInterface.Interface_classname); chargeMgr = (ICharge)o; result = chargeMgr.Charge(order); } catch (Exception ex) { Logger.Error(ex); if (cOrder != null) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); } } finally { if (db != null) { db.Dispose(); } } return(result); }
public ChargeResult Charge(ChargeOrder order) { ChargeResult result = null; ICharge chargeMgr = null; chargebitEntities db = null; Charge_Order cOrder = null; try { db = new chargebitEntities(); cOrder = (from co in db.Charge_Order where co.Id == order.Id select co).FirstOrDefault <Charge_Order>(); if (cOrder == null) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.ORDER_NOT_EXIST }; return(result); } Resource_taocan taocan = (from tc in db.Resource_taocan where tc.Id == order.ResourceTaocanId select tc).FirstOrDefault <Resource_taocan>(); if (!taocan.Enabled) { result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.RESOURCE_TAOCAN_DISABLED }; return(result); } 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) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = "落地资源部存在,请联系平台管理员" }; return(result); } if (!resource.Enabled) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.RESOURCE_DISABLED }; return(result); } if (order.ResourceId == 0) { order.ResourceId = resource.Id; } KMBit.DAL.Resrouce_interface rInterface = (from ri in db.Resrouce_interface where ri.Resource_id == order.ResourceId select ri).FirstOrDefault <Resrouce_interface>(); if (rInterface == null) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); result = new ChargeResult() { Status = ChargeStatus.FAILED, Message = ChargeConstant.RESOURCE_INTERFACE_NOT_CONFIGURED }; return(result); } object o = null; Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly(); Type type = assembly.GetType(rInterface.Interface_classname); o = Activator.CreateInstance(type); chargeMgr = (ICharge)o; result = chargeMgr.Charge(order); } catch (Exception ex) { if (cOrder != null) { db.Charge_Order.Remove(cOrder); db.SaveChanges(); } } finally { if (db != null) { db.Dispose(); } } return(result); }