示例#1
0
        /// <summary>
        /// Create new user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public BUser CreateNewUser(BUser user)
        {
            if (user == null) {
                throw new UserException("用户实体不能为空引用");
            }

            if (string.IsNullOrEmpty(user.Name)) {
                throw new UserException("用户名不能为空");
            }

            if (this.CurrentUserPermission.ADD_USER == 0)
            {
                throw new UserException("没有权限创建新用户");
            }

            KuanMaiEntities dba = new KuanMaiEntities();
            try
            {
                if (GetUser(user) != null)
                    throw new UserException("用户名已经存在");

                User dbUser = new User();
                dbUser.User_ID = user.ID;
                dbUser.Mall_ID = user.Mall_ID;
                dbUser.Mall_Name = user.Mall_Name;
                dbUser.Name = user.Name;
                dbUser.Mall_Type = user.Type.ID;
                if (user.Parent != null)
                {
                    dbUser.Parent_Mall_ID = user.Parent.Mall_ID;
                    dbUser.Parent_Mall_Name = user.Parent.Mall_Name;
                    dbUser.Parent_User_ID = user.Parent.ID;
                }
                dba.User.Add(dbUser);
                dba.SaveChanges();
                return user;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (dba != null)
                {
                    dba.Dispose();
                }
            }
        }
示例#2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="shop_id"></param>
        /// <returns></returns>
        public bool SyncShopSubUsers(int shop_id)
        {
            bool result = false;

            using (KuanMaiEntities db = new KuanMaiEntities())
            {
                BUser dbUser = (from us in db.User
                                from sp in db.Shop
                                where us.User_ID == sp.User_ID && sp.Shop_ID == shop_id
                                select new BUser
                                {
                                    ID = us.User_ID,
                                    //EmployeeInfo = (from e in db.Employee where e.User_ID == us.User_ID select e).FirstOrDefault<Employee>(),
                                    Mall_ID = us.Mall_ID,
                                    Mall_Name = us.Mall_Name,
                                    Type = (from type in db.Mall_Type where type.Mall_Type_ID == us.Mall_Type
                                            select new BMallType { ID=type.Mall_Type_ID,Name=type.Name,Description=type.Description }).FirstOrDefault<BMallType>(),
                                    Parent_ID = (int)us.Parent_User_ID,
                                    Parent = null,
                                    Name = us.Name,
                                    Password = us.Password
                                }).FirstOrDefault<BUser>();

                if (dbUser == null)
                {
                    throw new KMJXCException("没有找到对应店铺的卖家信息");
                }

                List<BUser> subUsers = this.MallUserManager.GetSubUsers(dbUser);
                List<BUser> existedUsers = (from us in db.User
                                            from sp in db.Shop_User
                                            where us.User_ID == sp.User_ID && sp.Shop_ID == shop_id
                                            select new BUser
                                            {
                                                ID = us.User_ID,
                                                //EmployeeInfo = (from e in db.Employee where e.User_ID == us.User_ID select e).FirstOrDefault<Employee>(),
                                                Mall_ID = us.Mall_ID,
                                                Mall_Name = us.Mall_Name,
                                                Type = (from type in db.Mall_Type where type.Mall_Type_ID == us.Mall_Type
                                                        select new BMallType { ID=type.Mall_Type_ID,Name=type.Name,Description=type.Description }).FirstOrDefault<BMallType>(),
                                                Parent_ID = (int)us.Parent_User_ID,
                                                Parent = null,
                                                Name = us.Name,
                                                Password = us.Password
                                            }).ToList<BUser>();

                foreach (BUser user in subUsers)
                {
                    bool found = false;
                    foreach (BUser eUser in existedUsers)
                    {
                        if (user.Mall_ID == eUser.Mall_ID && user.Mall_Name == eUser.Mall_Name)
                        {
                            //Update user
                            found = true;
                            eUser.EmployeeInfo = user.EmployeeInfo;
                            eUser.Name = user.Name;
                            break;
                        }
                    }

                    if (!found)
                    {
                        //add new sub user
                        User dbUser1 = new User();
                        //dbUser1.User_ID = user.ID;
                        dbUser1.Mall_ID = user.Mall_ID;
                        dbUser1.Mall_Name = user.Mall_Name;
                        dbUser1.Name = user.Name;
                        dbUser1.Mall_Type = user.Type.ID;
                        if (user.Parent != null)
                        {
                            dbUser1.Parent_Mall_ID = user.Parent.Mall_ID;
                            dbUser1.Parent_Mall_Name = user.Parent.Mall_Name;
                            dbUser1.Parent_User_ID = user.Parent.ID;
                        }
                        db.User.Add(dbUser1);
                        db.SaveChanges();

                        if (user.EmployeeInfo != null && dbUser1.User_ID>0)
                        {
                            Employee employee = new Employee();
                            employee.Name = user.EmployeeInfo.Name;
                            employee.IdentityCard = user.EmployeeInfo.IdentityCard;
                            employee.MatureDate = user.EmployeeInfo.MatureDate;
                            employee.Phone = user.EmployeeInfo.Phone;
                            employee.User_ID = user.EmployeeInfo.User_ID;
                            employee.HireDate = user.EmployeeInfo.HireDate;
                            employee.Gendar = user.EmployeeInfo.Gendar;
                            employee.Duty = user.EmployeeInfo.Duty;
                            employee.Email = user.EmployeeInfo.Email;
                            employee.Department = user.EmployeeInfo.Department;
                            employee.BirthDate = user.EmployeeInfo.BirthDate;
                            employee.Address = user.EmployeeInfo.Address;
                            db.Employee.Add(employee);
                        }

                        Shop_User sp = new Shop_User();
                        sp.Shop_ID = shop_id;
                        sp.User_ID = dbUser1.User_ID;
                        db.Shop_User.Add(sp);
                    }
                }

                db.SaveChanges();
            }

            return result;
        }
示例#3
0
        /// <summary>
        /// Calback from Mall Open API Authorization, it will verify if current login user has access to the system
        /// </summary>
        /// <param name="code">returns by Mall Open API Authorization</param>
        /// <returns></returns>
        public Access_Token AuthorizationCallBack(string code)
        {
            Access_Token request_token = null;
            BUser requester = new BUser();
            //must get access token after mall authorization to identify user
            request_token = TokenManager.RequestAccessToken(code);
            if (request_token == null)
            {
                throw new KMJXCException("没有获取到Access token", ExceptionLevel.SYSTEM);
            }

            requester.Type = new BMallType() { ID = this.Mall_Type_ID };
            requester.Mall_ID = request_token.Mall_User_ID;
            requester.Mall_Name = request_token.Mall_User_Name;
            requester.Parent_ID = 0;
            requester.Parent = null;

            KuanMaiEntities db = new KuanMaiEntities();
            try
            {
                var db_user = from u in db.User
                              where u.Mall_ID == requester.Mall_ID && u.Mall_Name == requester.Mall_Name && u.Mall_Type == this.Mall_Type_ID
                              select new BUser
                              {
                                  ID = u.User_ID,
                                  Name = u.Name,
                                  Mall_Name = u.Mall_Name,
                                  Mall_ID = u.Mall_ID,
                                  Password = u.Password,
                                  Parent_ID = (int)u.Parent_User_ID,
                              };
                List<BUser> users = db_user.ToList<BUser>();
                //Create user in local db with mall owner id
                if (users.Count == 0)
                {
                    this.InitializeMallManagers(request_token);

                    if (this.ShopManager == null)
                    {
                        throw new KMJXCException("IShopManager 实例为null", ExceptionLevel.SYSTEM);
                    }

                    //check if current user's shop is ready in system
                    Shop shop = this.ShopManager.GetShop(requester);
                    if (shop == null)
                    {
                        BUser subUser = this.MallUserManager.GetSubUser(requester.Mall_ID, requester.Mall_Name);
                        if (subUser == null)
                        {
                            throw new KMJXCException("用户:" + requester.Mall_Name + " 没有对应的" + ((KM.JXC.BL.Open.OBaseManager)this.ShopManager).MallType.Description + ",并且不属于任何店铺的子账户", ExceptionLevel.ERROR);
                        }
                        else
                        {
                            //
                            if (subUser.Parent == null || string.IsNullOrEmpty(subUser.Parent.Mall_Name))
                            {
                                throw new KMJXCException("用户:" + requester.Mall_Name + " 没有对应的" + ((KM.JXC.BL.Open.OBaseManager)this.ShopManager).MallType.Description + ",并且不属于任何店铺的子账户", ExceptionLevel.ERROR);
                            }

                            BUser mainUser = null;

                            var u = from us in db.User
                                    where us.Mall_ID == subUser.Parent.Mall_ID && us.Mall_Type == requester.Type.ID && us.Mall_Name == subUser.Parent.Mall_Name
                                    select new BUser
                                    {
                                        ID = us.User_ID,
                                        Name = us.Name,
                                        Mall_Name = us.Mall_Name,
                                        Mall_ID = us.Mall_ID,
                                        Password = us.Password,
                                        Parent_ID = (int)us.Parent_User_ID,
                                        Type = new BMallType { ID = us.Mall_Type }
                                    };
                            if (u.ToList<BUser>().Count() == 1)
                            {
                                mainUser = u.ToList<BUser>()[0];
                            }

                            if (mainUser == null)
                            {
                                throw new KMJXCException("主账户:" + subUser.Parent.Mall_Name + " 还没有初始化店铺信息,所有子账户无法登录系统", ExceptionLevel.ERROR);
                            }

                            requester.Parent_ID = mainUser.ID;
                            requester.Parent = mainUser;
                            requester.EmployeeInfo = subUser.EmployeeInfo;

                        }
                    }

                    //create user in local db
                    requester.Name = requester.Mall_Name;
                    requester.Password = Guid.NewGuid().ToString();

                    User dbUser = new User();
                    dbUser.User_ID = requester.ID;
                    dbUser.Mall_ID = requester.Mall_ID;
                    dbUser.Mall_Name = requester.Mall_Name;
                    dbUser.NickName = "";
                    dbUser.Name = requester.Name;
                    dbUser.Mall_Type = requester.Type.ID;
                    dbUser.Parent_Mall_ID = "";
                    dbUser.Parent_Mall_Name = "";
                    dbUser.Parent_User_ID = 0;
                    dbUser.Password = "";
                    dbUser.Name = dbUser.Mall_Name;
                    dbUser.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    dbUser.Modified = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                    dbUser.Modified_By = 0;

                    if (requester.Parent != null)
                    {
                        dbUser.Parent_Mall_ID = requester.Parent.Mall_ID;
                        dbUser.Parent_Mall_Name = requester.Parent.Mall_Name;
                        dbUser.Parent_User_ID = requester.Parent.ID;
                    }

                    db.User.Add(dbUser);
                    db.SaveChanges();

                    //create access token for the new user
                    request_token.User_ID = dbUser.User_ID;
                    requester.ID = dbUser.User_ID;
                    db.Access_Token.Add(request_token);

                    //save employee
                    if (requester.Parent_ID > 0 && requester.EmployeeInfo != null)
                    {
                        requester.EmployeeInfo.User_ID = requester.ID;
                        Employee employee = new Employee();
                        employee.Name = requester.EmployeeInfo.Name;
                        employee.IdentityCard = requester.EmployeeInfo.IdentityCard;
                        employee.MatureDate = requester.EmployeeInfo.MatureDate;
                        employee.Phone = requester.EmployeeInfo.Phone;
                        employee.User_ID=requester.EmployeeInfo.User_ID;
                        employee.HireDate = requester.EmployeeInfo.HireDate;
                        employee.Gendar = requester.EmployeeInfo.Gendar;
                        employee.Duty = requester.EmployeeInfo.Duty;
                        employee.Email = requester.EmployeeInfo.Email;
                        employee.Department = requester.EmployeeInfo.Department;
                        employee.BirthDate = requester.EmployeeInfo.BirthDate;
                        employee.Address = requester.EmployeeInfo.Address;
                        db.Employee.Add(employee);
                    }

                    if (shop != null)
                    {
                        //create local shop information for the new main user
                        shop.User_ID = requester.ID;
                        shop.Parent_Shop_ID = 0;
                        shop.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        shop.Synced = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        db.Shop.Add(shop);
                        db.SaveChanges();

                        //save shop user
                        Shop_User shop_User = new Shop_User();
                        shop_User.User_ID = requester.ID;
                        shop_User.Shop_ID = shop.Shop_ID;
                        db.Shop_User.Add(shop_User);

                        //update dbuser
                        dbUser.Shop_ID = shop.Shop_ID;
                        db.SaveChanges();
                        //create default stock house
                        Store_House shouse = new Store_House();
                        shouse.Shop_ID = shop.Shop_ID;
                        shouse.Title = "默认仓库";
                        shouse.User_ID = requester.ID;
                        shouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        db.Store_House.Add(shouse);
                        db.SaveChanges();
                    }

                    if (shop != null && requester.Parent_ID == 0)
                    {
                        //sync mall sub users to system
                        //List<BUser> subUsers = this.MallUserManager.GetSubUsers(requester);
                        //if (subUsers != null && subUsers.Count > 0 && shop.Shop_ID > 0)
                        //{
                        //    foreach (BUser user in subUsers)
                        //    {
                        //        User db1User = new User();
                        //        db1User.Parent_Mall_ID = requester.Mall_ID;
                        //        db1User.Parent_Mall_Name = requester.Mall_Name;
                        //        db1User.Parent_User_ID = (int)requester.ID;
                        //        db1User.Mall_Name = user.Mall_Name;
                        //        db1User.Mall_ID = user.Mall_ID;
                        //        db1User.Mall_Type = user.Type.Mall_Type_ID;
                        //        db1User.Name = user.Name;
                        //        db1User.Password = "";
                        //        db.User.Add(db1User);

                        //        db.SaveChanges();

                        //        if (db1User.User_ID > 0)
                        //        {
                        //            //add shop user
                        //            Shop_User shop_User1 = new Shop_User();
                        //            shop_User1.User_ID = requester.ID;
                        //            shop_User1.Shop_ID = shop.Shop_ID;
                        //            db.Shop_User.Add(shop_User1);

                        //            if (user.EmployeeInfo != null)
                        //            {
                        //                user.EmployeeInfo.User_ID = db1User.User_ID;
                        //                db.Employee.Add(user.EmployeeInfo);
                        //                //db.SaveChanges();
                        //            }
                        //        }
                        //    }

                        //    db.SaveChanges();
                        //}
                    }
                }
                else
                {
                    //Verify if local db has non expried accesstoken
                    requester = users[0];

                    Access_Token local_token = GetLocalToken(requester.ID, this.Mall_Type_ID);
                    if (local_token != null)
                    {
                        long timeNow = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);

                        //last access token is expried
                        if (timeNow >= local_token.Expirse_In + local_token.Request_Time)
                        {
                            request_token = TokenManager.RequestAccessToken(code);
                            request_token.User_ID = requester.ID;
                            UpdateLocalAccessToken(request_token);
                        }
                        else
                        {
                            request_token = local_token;
                        }
                    }
                }
            }
            catch (DbEntityValidationException dbex)
            {
                throw new KMJXCException("登录失败,请联系管理员");
            }
            catch (Exception ex)
            {
                throw new KMJXCException(ex.Message, ExceptionLevel.SYSTEM);
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }

            return request_token;
        }