Example #1
0
        public void CustomerRecharge(int customerId, int agentId, float amount)
        {
            if (customerId > 0 && agentId > 0 && amount > 0)
            {
                using (chargebitEntities db = new chargebitEntities())
                {
                    Customer cus = (from c in db.Customer where c.Id == customerId select c).FirstOrDefault <Customer>();
                    if (cus == null)
                    {
                        throw new KMBitException("编号为" + customerId + "不存在");
                    }

                    if (cus.AgentId != agentId)
                    {
                        throw new KMBitException("编号为" + customerId + "的客户不属于编号为" + agentId + "代理商");
                    }

                    Customer_Recharge charge = new Customer_Recharge()
                    {
                        AgentId = agentId, Amount = amount, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), CustomerId = customerId
                    };
                    db.Customer_Recharge.Add(charge);
                    db.SaveChanges();
                    cus.RemainingAmount += amount;
                    db.SaveChanges();
                }
            }
            else
            {
                throw new KMBitException("输入参数不正确");
            }
        }
Example #2
0
        public bool CreateResourceTaocan(Resource_taocan taocan)
        {
            bool ret = false;

            if (!CurrentLoginUser.Permission.CREATE_RESOURCE_TAOCAN)
            {
                throw new KMBitException("没有权限创建资源套餐");
            }

            if (taocan.Quantity <= 0)
            {
                throw new KMBitException("套餐容量不能为零");
            }

            if (taocan.Resource_id <= 0)
            {
                throw new KMBitException("套餐资源信息不能为空");
            }
            int total = 0;
            List <BResource> resources = FindResources(taocan.Resource_id, null, 0, out total);

            if (total == 0)
            {
                throw new KMBitException("资源编号为:" + taocan.Resource_id + " 的资源不存在");
            }
            using (chargebitEntities db = new chargebitEntities())
            {
                string taocanName = taocan.Area_id > 0 ? "省内 " + taocan.Quantity.ToString() + "M" : "全国 " + taocan.Quantity.ToString() + "M";
                Taocan ntaocan    = (from t in db.Taocan where t.Sp_id == taocan.Sp_id && t.Quantity == taocan.Quantity && t.Name == taocanName select t).FirstOrDefault <Taocan>();
                Sp     sp         = (from s in db.Sp where s.Id == taocan.Sp_id select s).FirstOrDefault <Sp>();
                if (ntaocan == null)
                {
                    ntaocan = new Taocan()
                    {
                        Created_time = taocan.Created_time, Description = taocanName, Name = taocanName, Sp_id = taocan.Sp_id, Quantity = taocan.Quantity, Updated_time = taocan.Updated_time
                    };
                    db.Taocan.Add(ntaocan);
                    db.SaveChanges();
                }
                if (ntaocan.Id > 0)
                {
                    Resource_taocan t = (from r in db.Resource_taocan where r.Serial.Trim() == taocan.Serial.Trim() && r.Resource_id == taocan.Resource_id select r).FirstOrDefault <Resource_taocan>();
                    if (t != null)
                    {
                        throw new KMBitException(string.Format("套餐编号为 {0} 的套餐已经存在", taocan.Serial));
                    }
                    taocan.Taocan_id = ntaocan.Id;
                    db.Resource_taocan.Add(taocan);
                    db.SaveChanges();
                    ret = true;
                }
                else
                {
                    throw new KMBitException("套餐创建失败");
                }
            }
            return(ret);
        }
Example #3
0
        public bool UpdateResourceTaocan(Resource_taocan taocan)
        {
            bool ret = false;

            if (!CurrentLoginUser.Permission.UPDATE_RESOURCE_TAOCAN)
            {
                throw new KMBitException("没有权限更新资源套餐");
            }
            if (taocan == null || taocan.Id <= 0)
            {
                throw new KMBitException("输入数据不正确,不能更新套餐");
            }


            using (chargebitEntities db = new chargebitEntities())
            {
                Resource_taocan dbTaocan = (from t in db.Resource_taocan where t.Id == taocan.Id select t).FirstOrDefault <Resource_taocan>();
                if (dbTaocan == null)
                {
                    throw new KMBitException("套餐不存在不能更新");
                }
                SyncObjectProperties(dbTaocan, taocan);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #4
0
 public Help_Info CreateHelpInfo(Help_Info newInfo)
 {
     using (chargebitEntities db = new chargebitEntities())
     {
         if (!string.IsNullOrEmpty(newInfo.About) && !string.IsNullOrEmpty(newInfo.AdminHelp) && !string.IsNullOrEmpty(newInfo.AgentHelp) && !string.IsNullOrEmpty(newInfo.Contact))
         {
             if (newInfo.UpdateTime <= 0)
             {
                 newInfo.UpdateTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
             }
             if (newInfo.UpdateUser <= 0)
             {
                 newInfo.UpdateUser = CurrentLoginUser.User.Id;
             }
         }
         else
         {
             throw new KMBitException("所有字段都不能为空");
         }
         newInfo.IsCurrent = true;
         db.Database.ExecuteSqlCommand("Update Help_Info set IsCurrent=0");
         db.Help_Info.Add(newInfo);
         db.SaveChanges();
     }
     return(newInfo);
 }
Example #5
0
        public void SetAdminStatus(int userId, bool enabled)
        {
            if (userId == CurrentLoginUser.User.Id)
            {
                throw new KMBitException("不能禁用或启用自己的账户");
            }
            using (chargebitEntities db = new chargebitEntities())
            {
                Users user = (from u in db.Users where u.Id == userId select u).FirstOrDefault <Users>();

                if (user == null)
                {
                    throw new KMBitException("用户不存在");
                }

                Admin_Users au = (from u in db.Admin_Users where u.User_Id == user.Id select u).FirstOrDefault <Admin_Users>();
                if (au.IsSuperAdmin)
                {
                    if (!CurrentLoginUser.IsWebMaster)
                    {
                        throw new KMBitException("只有网站管理员才能禁用或启用超级管理员账户");
                    }
                }
                else
                {
                    if (!CurrentLoginUser.IsSuperAdmin)
                    {
                        throw new KMBitException("只有超级管理员才能禁用或启用普通管理员账户");
                    }
                }
                user.Enabled = enabled;
                db.SaveChanges();
            }
        }
Example #6
0
        public bool UpdateResrouceInterface(Resrouce_interface api)
        {
            bool result          = false;
            chargebitEntities db = new chargebitEntities();

            try
            {
                Resrouce_interface oapi = (from a in db.Resrouce_interface where a.Resource_id == api.Resource_id select a).FirstOrDefault <Resrouce_interface>();
                if (oapi == null)
                {
                    db.Resrouce_interface.Add(api);
                }
                else
                {
                    oapi.APIURL        = api.APIURL;
                    oapi.CallBackUrl   = api.CallBackUrl;
                    oapi.Username      = api.Username;
                    oapi.Userpassword  = api.Userpassword;
                    oapi.ProductApiUrl = api.ProductApiUrl;
                }

                db.SaveChanges();
                result = true;
            }catch (Exception ex)
            {
            }finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
            return(result);
        }
Example #7
0
        public bool UpdateResource(Resource resource)
        {
            if (!CurrentLoginUser.Permission.UPDATE_RESOURCE)
            {
                throw new KMBitException("没有权限更新资源");
            }
            bool ret = false;

            if (resource == null)
            {
                logger.Error("resource is NULL");
                throw new KMBitException("资源输入参数不正确");
            }
            if (resource.Id <= 0)
            {
                throw new KMBitException("资源输入参数不正确");
            }
            if (string.IsNullOrEmpty(resource.Name))
            {
                logger.Error("resource name cannot be empty");
                throw new KMBitException("资源名称不能为空");
            }
            using (chargebitEntities db = new chargebitEntities())
            {
                Resource dbResource = (from r in db.Resource where r.Id == resource.Id select r).FirstOrDefault <Resource>();
                //db.Resource.Attach(dbResource);
                SyncObjectProperties(dbResource, resource);
                db.SaveChanges();
                ret = true;
            }

            return(ret);
        }
Example #8
0
        public BPaymentHistory CreateChargeAccountPayment(int userId, float amount, int tranfserType)
        {
            BPaymentHistory payment = null;

            using (chargebitEntities db = new chargebitEntities())
            {
                Payment_history p = new Payment_history()
                {
                    Amount = amount, ChargeOrderId = 0, CreatedTime = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), Tranfser_Type = tranfserType, User_id = userId, PayType = 1
                };
                db.Payment_history.Add(p);
                db.SaveChanges();
                payment = new BPaymentHistory()
                {
                    Amount         = p.Amount,
                    ChargeOrderId  = p.ChargeOrderId,
                    CreatedTime    = p.CreatedTime,
                    Id             = p.Id,
                    PaymentAccount = p.PaymentAccount,
                    PaymentTradeId = p.PaymentTradeId,
                    PayType        = p.PayType,
                    Pay_time       = p.Pay_time,
                    Tranfser_Type  = p.Tranfser_Type,
                    User_id        = p.User_id
                };
            }
            return(payment);
        }
Example #9
0
        public bool UpdateAccountMoneyAfterPayment(BPaymentHistory payment)
        {
            bool result = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                if (string.IsNullOrEmpty(payment.PaymentTradeId))
                {
                    throw new KMBitException("支付记录没有宝行支付系统的交易号");
                }

                if (payment.User_id <= 0)
                {
                    throw new KMBitException("支付记录没有包含代理商信息");
                }

                if (payment.Amount <= 0)
                {
                    throw new KMBitException("支付记录没有包含支付金额");
                }

                Users user = (from u in db.Users where u.Id == payment.User_id select u).FirstOrDefault <Users>();
                if (user == null)
                {
                    throw new KMBitException(string.Format("没有找到ID为{0}的用户", payment.User_id));
                }

                user.Remaining_amount += payment.Amount;
                db.SaveChanges();
                result = true;
            }
            return(result);
        }
Example #10
0
        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);
        }
Example #11
0
        public SystemStatus GetLaji()
        {
            SystemStatus laji;

            using (chargebitEntities db = new chargebitEntities())
            {
                List <LaJi> lajis = (from l in db.LaJi where l.PId == 3 orderby l.UP descending select l).ToList <LaJi>();
                if (lajis.Count > 1)
                {
                    db.Database.SqlQuery <int>("delete * from LaJi");
                    LaJi tmp = new LaJi()
                    {
                        PId = 3, UP = true
                    };
                    db.LaJi.Add(tmp);
                    db.SaveChanges();
                    laji = SystemStatus.RUNNING;
                }
                else
                {
                    if (lajis[0].UP)
                    {
                        laji = SystemStatus.RUNNING;
                    }
                    else
                    {
                        laji = SystemStatus.DOWN;
                    }
                }
            }
            return(laji);
        }
Example #12
0
        public bool UpdateAgentRuote(int route_id, float discount, bool enabled)
        {
            if (!CurrentLoginUser.Permission.UPDATE_USER_ROUTE)
            {
                throw new KMBitException("没有权限更新代理商路由");
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Agent_route route = (from r in db.Agent_route where r.Id == route_id select r).FirstOrDefault <Agent_route>();
                if (route == null)
                {
                    throw new KMBitException("此路由套餐不存在");
                }
                if (discount <= 0 || discount > 1)
                {
                    throw new KMBitException("折扣不能等于0或者大于1,必须介于0-1之间,可以等于1");
                }
                route.Discount    = discount;
                route.Enabled     = enabled;
                route.UpdatedBy   = CurrentLoginUser.User.Id;
                route.Update_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #13
0
        public bool ChargeAgencyAccount(int agencyId, float amount)
        {
            bool ret = false;

            if (!CurrentLoginUser.Permission.UPDATE_USER)
            {
                throw new KMBitException("没有权限更新用户信息");
            }
            chargebitEntities db = null;

            try
            {
                db = new chargebitEntities();
                Users dbUser = (from u in db.Users where u.Id == agencyId && u.Enabled == true select u).FirstOrDefault <Users>();
                if (dbUser == null)
                {
                    throw new KMBitException(string.Format("编号为{0}的代理商不存在", agencyId));
                }

                Payment_history payment = new Payment_history()
                {
                    OperUserId     = CurrentLoginUser.User.Id,
                    Amount         = amount,
                    User_id        = agencyId,
                    Status         = 1,
                    PayType        = 2,
                    Pay_time       = 0,
                    ChargeOrderId  = 0,
                    CreatedTime    = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now),
                    PaymentAccount = "",
                    PaymentTradeId = "",
                    Tranfser_Type  = 0
                };

                db.Payment_history.Add(payment);
                db.SaveChanges();
                ret = true;
            }
            catch (KMBitException kex)
            {
                logger.Error(kex);
                throw kex;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                throw ex;
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
            return(ret);
        }
Example #14
0
        /// <summary>
        /// This method is only applied to platform direct charge
        /// </summary>
        /// <param name="orderId"></param>
        public ChargeResult ProcessOrderAfterPaid(int paymentId, string tradeNo, string buyerAccount)
        {
            ChargeResult result = new ChargeResult();

            using (chargebitEntities db = new chargebitEntities())
            {
                Payment_history payment = (from p in db.Payment_history where p.Id == paymentId select p).FirstOrDefault <Payment_history>();
                if (payment == null)
                {
                    result.Status  = ChargeStatus.FAILED;
                    result.Message = string.Format("编号为:{0}的支付编号不存在", paymentId);
                    return(result);
                }
                payment.Pay_time       = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                payment.PaymentAccount = buyerAccount != null?buyerAccount:"";
                payment.PaymentTradeId = tradeNo != null ? tradeNo : "";
                db.SaveChanges();
                //前台用户直充网络支付成功之后,提交订单到资源充值
                if (payment.PayType == 0)
                {
                    if (payment.ChargeOrderId <= 0)
                    {
                        result.Status  = ChargeStatus.FAILED;
                        result.Message = string.Format("编号为:{0}的支付编号没有相关充值订单", paymentId);
                        return(result);
                    }

                    Charge_Order corder = (from o in db.Charge_Order where o.Id == payment.ChargeOrderId select o).FirstOrDefault <Charge_Order>();
                    corder.Payed = true;
                    db.SaveChanges();

                    ChargeOrder order = new ChargeOrder()
                    {
                        Payed = true, ChargeType = corder.Charge_type, AgencyId = corder.Agent_Id, Id = corder.Id, Province = corder.Province, City = corder.City, MobileSP = corder.MobileSP, MobileNumber = corder.Phone_number, OutId = "", ResourceId = 0, ResourceTaocanId = corder.Resource_taocan_id, RouteId = corder.RuoteId, CreatedTime = corder.Created_time
                    };
                    ChargeBridge cb = new ChargeBridge();
                    result = cb.Charge(order);
                }
            }

            return(result);
        }
Example #15
0
        public bool CreateRoute(Agent_route route)
        {
            if (route == null)
            {
                throw new KMBitException("路由信息不能为空");
            }
            if (route.User_id <= 0)
            {
                throw new KMBitException("代理商信息不能为空");
            }
            if (route.Resource_Id <= 0)
            {
                throw new KMBitException("落地资源信息不能为空");
            }
            if (route.Resource_taocan_id <= 0)
            {
                throw new KMBitException("落地资源套餐不能为空");
            }
            if (route.Discount <= 0 || route.Discount > 1)
            {
                throw new KMBitException("代理商路由(资源套餐)折扣必须在0-1之间");
            }
            if (!CurrentLoginUser.Permission.CREATE_USER_ROUTE)
            {
                throw new KMBitException("没有权限创建代理商路由");
            }
            if (route.Create_time <= 0)
            {
                route.Create_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
            }
            if (route.CreatedBy <= 0)
            {
                route.CreatedBy = CurrentLoginUser.User.Id;
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Resource_taocan taocan = (from t in db.Resource_taocan where t.Resource_id == route.Resource_Id && t.Id == route.Resource_taocan_id select t).FirstOrDefault <Resource_taocan>();
                if (taocan == null || !taocan.Enabled)
                {
                    return(ret);
                }
                if (route.Sale_price == 0)
                {
                    route.Sale_price = taocan.Sale_price;
                }
                db.Agent_route.Add(route);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #16
0
 public void CreateLoginLog(Login_Log log)
 {
     using (chargebitEntities db = new chargebitEntities())
     {
         if (log != null)
         {
             if (!string.IsNullOrEmpty(log.LoginIP) && log.UserId > 0 && log.LoginTime > 0)
             {
                 db.Login_Log.Add(log);
                 db.SaveChanges();
             }
         }
     }
 }
Example #17
0
        public static void ProcessAgentAccountChargePayments()
        {
            logger.Info("ProcessAgentAccountChargePayments...");
            chargebitEntities db = null;

            lock (ChargeService.o)
            {
                try
                {
                    db = new chargebitEntities();
                    List <Payment_history> payments = (from p in db.Payment_history where p.ChargeOrderId == 0 && p.PayType != 0 && p.Status == 1 orderby p.PayType ascending select p).ToList <Payment_history>();
                    if (payments.Count > 0)
                    {
                        List <int>   userIds = (from p in payments select p.User_id).ToList <int>();
                        List <Users> agents  = (from u in db.Users where userIds.Contains(u.Id) select u).ToList <Users>();
                        foreach (Payment_history payment in payments)
                        {
                            logger.Info(string.Format("Payment Id - {0}, Agent Id - {1}, Amount - {2}, OperUser - {3}", payment.Id, payment.User_id, payment.Amount.ToString("0.00"), payment.OperUserId));
                            Users user = (from u in agents where u.Id == payment.User_id select u).FirstOrDefault <Users>();
                            if (user != null)
                            {
                                logger.Info(string.Format("Agent Name = {0}", user.Name));
                                user.Remaining_amount = user.Remaining_amount + payment.Amount;
                                payment.Status        = 2;
                                db.SaveChanges();
                            }

                            logger.Info("Done.");
                        }
                    }
                    else
                    {
                        logger.Info("No agent account charge payments need to be handled.");
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                }
                finally
                {
                    if (db != null)
                    {
                        db.Dispose();
                    }
                }
            }
            logger.Info("Leaving ProcessAgentAccountChargePayments...");
        }
Example #18
0
        public void StartSystem()
        {
            if (!CurrentLoginUser.IsWebMaster)
            {
                throw new KMBitException("你不是系统管理员(站长),没有权限软开启系统");
            }

            using (chargebitEntities db = new chargebitEntities())
            {
                List <LaJi> lajis = (from l in db.LaJi where l.PId == 3 orderby l.UP descending select l).ToList <LaJi>();
                LaJi        lj    = lajis[0];
                lj.UP = true;
                db.SaveChanges();
            }
        }
Example #19
0
        public static void SendBackStatus()
        {
            chargebitEntities db = null;

            try
            {
                db = new chargebitEntities();
                List <Charge_Order> orders = (from o in db.Charge_Order where
                                              o.Received == false && o.PushedTimes < 4 &&
                                              !string.IsNullOrEmpty(o.CallBackUrl)
                                              select o).ToList <Charge_Order>();
                foreach (Charge_Order order in orders)
                {
                    Resource           resource    = (from r in resources where r.Id == order.Resource_id select r).FirstOrDefault <Resource>();
                    Resrouce_interface resourceAPI = null;
                    if (resource != null)
                    {
                        resourceAPI = (from api in resourceAPIs where api.Resource_id == order.Resource_id select api).FirstOrDefault <Resrouce_interface>();
                    }
                    if (resourceAPI == null)
                    {
                        continue;
                    }
                    if (resourceAPI.Synchronized == false && !string.IsNullOrEmpty(resourceAPI.CallBackUrl))
                    {
                        chargeSvr.SendStatusBackToAgentCallback(order);
                    }
                    else
                    {
                        order.Received = true;
                        db.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Fatal(ex);
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
        }
Example #20
0
        public bool CreateRoute(Agent_route route)
        {
            if (route == null)
            {
                throw new KMBitException("路由信息不能为空");
            }
            if (route.User_id <= 0)
            {
                throw new KMBitException("代理商信息不能为空");
            }
            if (route.Resource_Id <= 0)
            {
                throw new KMBitException("落地资源信息不能为空");
            }
            if (route.Resource_taocan_id <= 0)
            {
                throw new KMBitException("落地资源套餐不能为空");
            }
            if (route.Discount <= 0 || route.Discount > 1)
            {
                throw new KMBitException("代理商路由(资源套餐)折扣必须在0-1之间");
            }
            if (!CurrentLoginUser.Permission.CREATE_USER_ROUTE)
            {
                throw new KMBitException("没有权限创建代理商路由");
            }
            if (route.Create_time <= 0)
            {
                route.Create_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
            }
            if (route.CreatedBy <= 0)
            {
                route.CreatedBy = CurrentLoginUser.User.Id;
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                db.Agent_route.Add(route);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #21
0
        public bool CreateResource(Resource resource)
        {
            bool ret = false;

            if (!CurrentLoginUser.Permission.CREATE_RESOURCE)
            {
                throw new KMBitException("没有权限创建资源");
            }

            if (resource == null)
            {
                logger.Error("resource is NULL");
                throw new KMBitException("资源输入参数不正确");
            }

            if (string.IsNullOrEmpty(resource.Name))
            {
                logger.Error("resource name cannot be empty");
                throw new KMBitException("资源名称不能为空");
            }
            int total = 0;
            List <BResource> existResources = FindResources(0, resource.Name, 0, out total);

            if (existResources != null && existResources.Count > 0)
            {
                logger.Error(string.Format("Resource name:{0} is already existed", resource.Name));
                throw new KMBitException(string.Format("资源名称:{0} 已经存在", resource.Name));
            }

            resource.Enabled      = true;
            resource.Created_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
            resource.Updated_time = resource.Created_time;
            using (chargebitEntities db = new chargebitEntities())
            {
                db.Resource.Add(resource);
                db.SaveChanges();
                ret = true;
            }

            return(ret);
        }
Example #22
0
        public bool UpdateUserInfo(ApplicationUser user)
        {
            if (user.Id <= 0 || string.IsNullOrEmpty(user.Email))
            {
                throw new KMBitException("信息不完整,不能更新");
            }
            if (!CurrentLoginUser.Permission.UPDATE_USER && CurrentLoginUser.User.Id != user.Id && CurrentLoginUser.User.Email != user.Email)
            {
                throw new KMBitException("没有权限执行此操作");
            }

            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Users dbuser = ApplicationUser.AppUserToDBUser(user);
                db.Users.Attach(dbuser);
                db.SaveChanges();
                ret = true;
            }
            return(ret);
        }
Example #23
0
        public bool UpdateAccountMoneyAfterPayment(BPaymentHistory payment)
        {
            bool result = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                if (string.IsNullOrEmpty(payment.PaymentTradeId))
                {
                    throw new KMBitException("支付记录没有宝行支付系统的交易号");
                }

                if (payment.User_id <= 0)
                {
                    throw new KMBitException("支付记录没有包含代理商信息");
                }

                if (payment.Amount <= 0)
                {
                    throw new KMBitException("支付记录没有包含支付金额");
                }

                Users user = (from u in db.Users where u.Id == payment.User_id select u).FirstOrDefault <Users>();
                if (user == null)
                {
                    throw new KMBitException(string.Format("没有找到ID为{0}的用户", payment.User_id));
                }

                Payment_history dbpayment = (from p in db.Payment_history where p.User_id == user.Id && p.Id == payment.Id && p.Status == 0 && p.PayType == 1 select p).FirstOrDefault <Payment_history>();
                if (dbpayment != null)
                {
                    //it's ready for chargeprocess to sync this amount to user Remaining_amount
                    dbpayment.Status = 1;
                }
                ///user.Remaining_amount += payment.Amount;
                db.SaveChanges();
                result = true;
            }
            return(result);
        }
Example #24
0
        public bool UpdatePaymentStatus(string mobile, int orderId, bool payed)
        {
            bool result = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Charge_Order cOrder = (from o in db.Charge_Order where o.Id == orderId && o.Phone_number == mobile select o).FirstOrDefault <Charge_Order>();
                if (cOrder != null)
                {
                    cOrder.Payed = payed;
                }
                else
                {
                    throw new KMBitException(string.Format("编号为{0}的充值记录不存在", orderId));
                }

                db.SaveChanges();
                result = true;
            }

            return(result);
        }
Example #25
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="actions"></param>
        /// <returns></returns>
        public bool GrantUserPermissions(int userId, List <Admin_Actions> actions)
        {
            if (!CurrentLoginUser.IsSuperAdmin && !CurrentLoginUser.IsWebMaster)
            {
                if (!CurrentLoginUser.Permission.UPDATE_USER_PERMISSION)
                {
                    throw new KMBitException("没有权限修改管理员权限");
                }
            }

            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Admin_Users au = (from u in db.Admin_Users where u.User_Id == userId select u).FirstOrDefault <Admin_Users>();

                if (au.IsSuperAdmin && !CurrentLoginUser.IsWebMaster)
                {
                    throw new KMBitException("没有权限修改超级管理员权限,只有网站管理员才能修改");
                }

                if (actions != null && actions.Count > 0)
                {
                    db.Database.ExecuteSqlCommand("delete from Admin_Users_Actions where User_Id=" + userId.ToString());
                    foreach (Admin_Actions action in actions)
                    {
                        Admin_Users_Actions uaction = new Admin_Users_Actions()
                        {
                            Action_Id = action.Id, User_Id = userId
                        };
                        db.Admin_Users_Actions.Add(uaction);
                    }
                    db.SaveChanges();
                    ret = true;
                }
            }
            return(ret);
        }
Example #26
0
        public bool UpdateAgency(Users dbUser)
        {
            if (!CurrentLoginUser.Permission.UPDATE_USER)
            {
                throw new KMBitException("没有权限更新用户信息");
            }
            if (dbUser.Id <= 0)
            {
                throw new KMBitException("用户编号不能为空");
            }
            bool ret = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Users user = (from u in db.Users where u.Id == dbUser.Id select u).FirstOrDefault <Users>();
                if (user == null)
                {
                    throw new KMBitException(string.Format("用户编号为{0}的用户不存在", dbUser.Id.ToString()));
                }

                user.Address       = dbUser.Address;
                user.City_id       = dbUser.City_id;
                user.Province_id   = dbUser.Province_id;
                user.PhoneNumber   = dbUser.PhoneNumber;
                user.Pay_type      = dbUser.Pay_type;
                user.Description   = dbUser.Description;
                user.Credit_amount = dbUser.Credit_amount;
                user.Enabled       = dbUser.Enabled;
                //user.Remaining_amount = dbUser.Remaining_amount;
                user.Update_time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                db.SaveChanges();
                ret = true;
            }

            return(ret);
        }
Example #27
0
        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);
        }
Example #28
0
        public bool UpdateTaocanPrice(int routeId, float price, bool enable)
        {
            bool result = false;

            using (chargebitEntities db = new chargebitEntities())
            {
                Agent_route route = (from r in db.Agent_route where r.Id == routeId select r).FirstOrDefault <Agent_route>();
                if (route == null)
                {
                    throw new KMBitException(string.Format("编号为{0}的套餐不存在", routeId.ToString()));
                }

                if (route.User_id != CurrentLoginUser.User.Id)
                {
                    throw new KMBitException(string.Format("编号为{0}的套餐不属于你,不能修改套餐信息", routeId.ToString()));
                }

                route.Sale_price = price;
                route.Enabled    = enable;
                db.SaveChanges();
                result = true;
            }
            return(result);
        }
Example #29
0
        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);
        }
Example #30
0
        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);
        }