예제 #1
0
파일: UserManager.cs 프로젝트: 21ki/kuanmai
        /// <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
        public static void SyncUserAction()
        {
            Type action = typeof(UserLogAction);

            FieldInfo[] fields = action.GetFields();
            if (fields == null || fields.Length <= 0)
            {
                return;
            }

            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                List <User_Action> allActions = (from ac in db.User_Action select ac).ToList <User_Action>();
                foreach (FieldInfo field in fields)
                {
                    UserActionAttribute attr = field.GetCustomAttribute <UserActionAttribute>();
                    int         aValue       = (int)field.GetValue(null);
                    User_Action userAc       = (from ac in allActions where ac.Action_ID == aValue && ac.Action_Name == field.Name select ac).FirstOrDefault <User_Action>();
                    if (userAc == null)
                    {
                        userAc             = new User_Action();
                        userAc.Action_ID   = aValue;
                        userAc.Action_Name = field.Name;
                        userAc.Created     = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        if (attr != null)
                        {
                            userAc.Action_Description = attr.Description;
                        }
                        db.User_Action.Add(userAc);
                    }
                    else
                    {
                        if (attr != null)
                        {
                            userAc.Action_Description = attr.Description;
                        }
                    }
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
        }
예제 #3
0
        protected void Initialize()
        {
            if (this.Mall_Type_ID <= 0)
            {
                throw new Exception("Mall Type ID is invalid for open API");
            }
            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                var openKey = db.Open_Key.Where(p => p.Mall_Type_ID == this.Mall_Type_ID);
                if (openKey != null)
                {
                    List <Open_Key> keys = openKey.ToList <Open_Key>();
                    if (keys.Count == 1)
                    {
                        this.Open_Key = keys[0];
                    }
                    else
                    {
                        throw new Exception("More app key and secret pair for Mall Type ID:" + this.Mall_Type_ID);
                    }
                }
                else
                {
                    throw new Exception("Didn't find app key and secret for Mall Type ID:" + this.Mall_Type_ID);
                }

                var t = from tp in db.Mall_Type where tp.Mall_Type_ID == this.Mall_Type_ID select new BMallType {
                    ID = tp.Mall_Type_ID, Name = tp.Name
                };
                if (t.ToList <BMallType>().Count == 1)
                {
                    this.MallType = t.ToList <BMallType>()[0];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
        }
예제 #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="action"></param>
        public void CreateActionLog(BUserActionLog action)
        {
            if (action == null || action.Shop == null)
            {
                return;
            }
            KuanMaiEntities db = null;

            try
            {
                db = new KuanMaiEntities();
                User_Action_Log log = new User_Action_Log();

                log.Action = action.Action.Action_ID;

                log.Description = action.Description;
                if (action.User != null && action.User.ID > 0)
                {
                    log.User_ID = action.User.ID;
                }
                else
                {
                    log.User_ID = this.CurrentUser.ID;
                }

                log.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                if (string.IsNullOrEmpty(log.Description))
                {
                    log.Description = "";
                }
                log.Shop_ID = action.Shop.ID;
                db.User_Action_Log.Add(log);
                db.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
        }
예제 #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="propertyId"></param>
        /// <param name="value"></param>
        public void CreatePropertyValues(int propertyId, List <string> value)
        {
            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                Product_Spec ps = (from prop in db.Product_Spec where prop.Product_Spec_ID == propertyId select prop).FirstOrDefault <Product_Spec>();
                if (ps == null)
                {
                    throw new KMJXCException("属性不存在,不能添加属性值");
                }

                List <Product_Spec_Value> psValues = (from psv in db.Product_Spec_Value where psv.Product_Spec_ID == propertyId select psv).ToList <Product_Spec_Value>();

                if (value != null && value.Count > 0)
                {
                    foreach (string v in value)
                    {
                        Product_Spec_Value propValue = (from propv in psValues where propv.Name == v select propv).FirstOrDefault <Product_Spec_Value>();
                        if (propValue == null)
                        {
                            propValue = new Product_Spec_Value();
                            propValue.Product_Spec_ID = propertyId;
                            propValue.Name            = v;
                            propValue.Mall_PVID       = "";
                            propValue.Created         = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                            db.Product_Spec_Value.Add(propValue);
                        }
                    }

                    db.SaveChanges();
                }
            }
            catch
            {
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
        }
예제 #6
0
파일: UserManager.cs 프로젝트: 21ki/kuanmai
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public bool UpdateEmployeeInfo(Employee employee)
        {
            bool result = false;

            if (this.CurrentUserPermission.UPDATE_EMPLOYEE == 0)
            {
                throw new KMJXCException("没有权限更新员工信息");
            }
            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                User user = (from u in db.User where u.User_ID == employee.User_ID select u).FirstOrDefault <User>();
                if (user != null)
                {
                    Employee existing = (from e in db.Employee where e.User_ID == employee.User_ID select e).FirstOrDefault <Employee>();
                    if (existing == null)
                    {
                        db.Employee.Add(employee);
                    }
                    else
                    {
                        this.UpdateProperties(existing, employee);
                    }
                    result = true;
                }
            }
            catch
            {
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }

            return(result);
        }
예제 #7
0
        /// <summary>
        /// Sync actions with Permission object
        /// </summary>
        public void SyncPermissionWithAction()
        {
            Type permission = typeof(Permission);

            FieldInfo[] fields = permission.GetFields();
            if (fields == null || fields.Length <= 0)
            {
                return;
            }

            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                foreach (FieldInfo field in fields)
                {
                    var action = from a in db.Admin_Action where a.action_name == field.Name select a;
                    if (action == null || action.ToList <Admin_Action>().Count == 0)
                    {
                        Admin_Action new_action = new Admin_Action();
                        new_action.action_name        = field.Name;
                        new_action.action_description = field.Name;
                        new_action.enable             = true;
                        db.Admin_Action.Add(new_action);
                    }
                }
                db.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
            }
            finally
            {
                db.Dispose();
            }
        }
예제 #8
0
        /// <summary>
        /// Sync actions with Permission object
        /// </summary>
        public static void SyncPermissionWithAction()
        {
            Type permission = typeof(Permission);

            FieldInfo[] fields = permission.GetFields();
            if (fields == null || fields.Length <= 0)
            {
                return;
            }

            KuanMaiEntities db = new KuanMaiEntities();

            try
            {
                List <AdminActionAttribute> cates      = new List <AdminActionAttribute>();
                List <Admin_Action>         allActions = (from action in db.Admin_Action select action).ToList <Admin_Action>();
                List <Admin_Category>       allCates   = (from cate in db.Admin_Category select cate).ToList <Admin_Category>();
                foreach (FieldInfo field in fields)
                {
                    AdminActionAttribute attr   = field.GetCustomAttribute <AdminActionAttribute>();
                    Admin_Action         action = (from a in allActions where a.action_name == field.Name select a).FirstOrDefault <Admin_Action>();
                    if (action == null)
                    {
                        action             = new Admin_Action();
                        action.action_name = field.Name;
                        action.enable      = true;
                        db.Admin_Action.Add(action);
                    }

                    if (attr != null)
                    {
                        action.category_id        = attr.ID;
                        action.action_description = attr.ActionDescription;
                        AdminActionAttribute existed = (from pcate in cates where pcate.ID == attr.ID select pcate).FirstOrDefault <AdminActionAttribute>();
                        if (existed == null)
                        {
                            cates.Add(attr);
                        }
                    }
                }
                db.SaveChanges();

                foreach (Admin_Action action in allActions)
                {
                    bool found = false;

                    foreach (FieldInfo field in fields)
                    {
                        if (action.action_name == field.Name)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        action.enable = false;
                        //db.Admin_Action.Remove(action);
                    }
                }

                db.SaveChanges();

                //category
                foreach (AdminActionAttribute pcate in cates)
                {
                    Admin_Category dbCate = (from c in allCates where c.ID == pcate.ID select c).FirstOrDefault <Admin_Category>();
                    if (dbCate == null)
                    {
                        dbCate                 = new Admin_Category();
                        dbCate.ID              = pcate.ID;
                        dbCate.Name            = pcate.CategoryName;
                        dbCate.System_category = pcate.IsSystem;
                        dbCate.Created         = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        db.Admin_Category.Add(dbCate);
                    }
                }
                db.SaveChanges();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                db.Dispose();
            }
        }
예제 #9
0
        /// <summary>
        /// Update supplier basic information
        /// </summary>
        /// <param name="supplier"></param>
        /// <returns></returns>
        public bool UpdateSupplier(Supplier supplier)
        {
            bool result = false;

            if (this.CurrentUserPermission.UPDATE_SUPPLIER == 0)
            {
                throw new KMJXCException("没有权限更新供应商");
            }

            if (string.IsNullOrEmpty(supplier.Name))
            {
                throw new KMJXCException("供应商名称不能为空");
            }
            KuanMaiEntities db = null;

            try
            {
                db = new KuanMaiEntities();
                int[]    child_shops = (from c in this.DBChildShops select c.Shop_ID).ToArray <int>();
                Supplier existed     = (from supp in db.Supplier where supp.Supplier_ID == supplier.Supplier_ID select supp).FirstOrDefault <Supplier>();
                if (existed == null)
                {
                    throw new KMJXCException("要修改的供应商不存在");
                }
                var obj = (from sp in db.Supplier where (child_shops.Contains(sp.Shop_ID) || sp.Shop_ID == this.Shop.Shop_ID || sp.Shop_ID == this.Main_Shop.Shop_ID) && supplier.Name.Contains(sp.Name) && sp.Supplier_ID != existed.Supplier_ID select sp);

                if (obj.ToList <Supplier>().Count > 0)
                {
                    throw new KMJXCException("供应商名称已经存在,请换个名字");
                }

                if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID)
                {
                    if (existed.Shop_ID == this.Main_Shop.Shop_ID)
                    {
                        if (this.CurrentUser.Shop.ID != existed.Shop_ID)
                        {
                            throw new KMJXCException("您不能修改主店铺供应商");
                        }
                    }
                    else if (existed.Shop_ID != this.Shop.Shop_ID)
                    {
                        throw new KMJXCException("您不能其他主店铺供应商");
                    }
                }
                else
                {
                    if (existed.Shop_ID != this.Main_Shop.Shop_ID && !child_shops.Contains(existed.Shop_ID))
                    {
                        throw new KMJXCException("您不能修改其他店铺的供应商,只能修改主店铺或者子店铺供应商");
                    }
                }

                supplier.Modified    = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                supplier.Modified_By = this.CurrentUser.ID;
                this.UpdateProperties(existed, supplier);
                db.SaveChanges();
                result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Dispose();
            }
            return(result);
        }
예제 #10
0
        /// <summary>
        /// Gets full local product information
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public BProduct GetProductFullInfo(int productId, string mall_id = null)
        {
            if (productId == 0 && string.IsNullOrEmpty(mall_id))
            {
                throw new KMJXCException("获取产品详细信息时候,必须输入产品编号或者已关联的商城宝贝编号");
            }

            BProduct        product = null;
            KuanMaiEntities db      = new KuanMaiEntities();

            try
            {
                if (productId == 0 && !string.IsNullOrEmpty(mall_id))
                {
                    productId = (from mp in db.Mall_Product where mp.Mall_ID == mall_id select mp.Outer_ID).FirstOrDefault <int>();
                }

                if (productId == 0)
                {
                    throw new KMJXCException("获取产品详细信息时候,必须输入产品编号或者已关联的商城宝贝编号");
                }

                product = (from pudt in db.Product
                           where pudt.Product_ID == productId
                           select new BProduct
                {
                    Shop = (from sp in db.Shop where sp.Shop_ID == pudt.Shop_ID select sp).FirstOrDefault <Shop>(),
                    ID = pudt.Product_ID,
                    Description = pudt.Description,
                    Title = pudt.Name,
                    Code = pudt.Code,
                    CreateTime = pudt.Create_Time,
                    Category = (from c in db.Product_Class
                                where pudt.Product_Class_ID == c.Product_Class_ID
                                select new BCategory
                    {
                        Name = c.Name,
                        ID = c.Product_Class_ID,
                        ParentID = (int)c.Parent_ID
                    }).FirstOrDefault <BCategory>(),
                    User = (from u in db.User
                            where u.User_ID == pudt.User_ID
                            select new BUser
                    {
                        ID = u.User_ID,
                        Mall_Name = u.Mall_Name,
                        Mall_ID = u.Mall_ID,
                    }).FirstOrDefault <BUser>()
                }).FirstOrDefault <BProduct>();

                if (product != null)
                {
                    product.Images    = (from img in db.Image where img.ProductID == product.ID select img).ToList <Image>();
                    product.Suppliers = (from sp in db.Supplier
                                         join ps in db.Product_Supplier on sp.Supplier_ID equals ps.Supplier_ID
                                         where ps.Product_ID == product.ID && ps.Enabled == true
                                         select sp
                                         ).ToList <Supplier>();

                    //product.Properties = properties;
                    List <Product>          children     = (from p in db.Product where p.Parent_ID == product.ID select p).ToList <Product>();
                    int[]                   children_ids = (from c in children select c.Product_ID).ToArray <int>();
                    List <BProductProperty> properties   = (from pp in db.Product_Specifications
                                                            where children_ids.Contains(pp.Product_ID)
                                                            select new BProductProperty
                    {
                        ProductID = pp.Product_ID,
                        PID = pp.Product_Spec_ID,
                        PName = (from prop in db.Product_Spec where prop.Product_Spec_ID == pp.Product_Spec_ID select prop.Name).FirstOrDefault <string>(),
                        PVID = pp.Product_Spec_Value_ID,
                        PValue = (from propv in db.Product_Spec_Value where propv.Product_Spec_Value_ID == pp.Product_Spec_Value_ID select propv.Name).FirstOrDefault <string>()
                    }).OrderBy(p => p.PID).ToList <BProductProperty>();

                    if (children != null && children.Count > 0)
                    {
                        if (product.Children == null)
                        {
                            product.Children = new List <BProduct>();
                        }
                        foreach (Product pdt in children)
                        {
                            BProduct child = new BProduct()
                            {
                                ID = pdt.Product_ID, Title = product.Title
                            };
                            child.Properties = (from prop in properties where prop.ProductID == child.ID select prop).ToList <BProductProperty>();
                            product.Children.Add(child);
                        }
                    }
                }
            }
            catch (KMJXCException kex)
            {
                throw kex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Dispose();
            }
            return(product);
        }
예제 #11
0
        /// <summary>
        /// Update product information
        /// </summary>
        /// <param name="product"></param>
        /// <returns></returns>
        public bool UpdateProduct(ref BProduct bproduct)
        {
            BProduct product = bproduct;

            if (this.CurrentUserPermission.UPDATE_PRODUCT == 0)
            {
                throw new KMJXCException("没有权限更新产品");
            }

            bool            result = false;
            KuanMaiEntities db     = new KuanMaiEntities();

            try
            {
                Product dbProduct = (from pdt in db.Product where pdt.Product_ID == product.ID select pdt).FirstOrDefault <Product>();
                if (dbProduct == null)
                {
                    throw new KMJXCException("您要修改的产品信息不存在");
                }

                if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID)
                {
                    if (dbProduct.Shop_ID == this.Main_Shop.Shop_ID)
                    {
                        throw new KMJXCException("您不能修改主店铺产品");
                    }

                    if (dbProduct.Shop_ID != this.Shop.Shop_ID)
                    {
                        throw new KMJXCException("您不能其他主店铺产品");
                    }
                }
                else
                {
                    int[] child_shops = (from c in this.DBChildShops select c.Shop_ID).ToArray <int>();
                    if (dbProduct.Shop_ID != this.Main_Shop.Shop_ID && !child_shops.Contains(dbProduct.Shop_ID))
                    {
                        throw new KMJXCException("您无法修改其他店铺的产品,只能修改主店铺或者子店铺产品");
                    }
                }

                dbProduct.Name        = product.Title;
                dbProduct.Description = product.Description;
                if (product.Category != null)
                {
                    dbProduct.Product_Class_ID = product.Category.ID;
                }
                dbProduct.Update_Time    = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                dbProduct.Update_User_ID = this.CurrentUser.ID;

                //update images
                if (product.Images != null && product.Images.Count > 0)
                {
                    List <Image> existedImages = (from img in db.Image where img.ProductID == product.ID select img).ToList <Image>();
                    //Update new uploaded images
                    foreach (Image newimg in product.Images)
                    {
                        Image tmp = (from eted in existedImages where eted.ID == newimg.ID select eted).FirstOrDefault <Image>();
                        if (tmp == null)
                        {
                            Image newone = (from ni in db.Image where ni.ID == newimg.ID select ni).FirstOrDefault <Image>();
                            newone.ProductID = product.ID;
                            db.Image.Add(newone);
                        }
                    }

                    //Remove deleted images
                    string rootPath = product.FileRootPath;
                    foreach (Image oldImg in existedImages)
                    {
                        Image tmp = (from eted in product.Images where eted.ID == oldImg.ID select eted).FirstOrDefault <Image>();
                        if (tmp == null)
                        {
                            db.Image.Remove(oldImg);
                            if (rootPath != null && System.IO.File.Exists(rootPath + oldImg.Path))
                            {
                                System.IO.File.Delete(rootPath + oldImg.Path);
                            }
                        }
                    }
                }

                //update suppliers
                if (product.Suppliers != null)
                {
                    foreach (Supplier s in product.Suppliers)
                    {
                        Product_Supplier ps = new Product_Supplier()
                        {
                            Product_ID = product.ID, Supplier_ID = s.Supplier_ID, Enabled = true, Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), Created_By = this.CurrentUser.ID
                        };
                        db.Product_Supplier.Add(ps);
                    }
                }

                //update children
                List <Product> children = (from p in db.Product where p.Parent_ID == dbProduct.Product_ID select p).ToList <Product>();
                foreach (Product child in children)
                {
                    child.Name             = dbProduct.Name;
                    child.Product_Class_ID = dbProduct.Product_Class_ID;
                    child.Description      = dbProduct.Description;
                }

                db.SaveChanges();

                if (product.Children != null && product.Children.Count > 0)
                {
                    foreach (BProduct child in product.Children)
                    {
                        if (child.ID == 0)
                        {
                            //create new child product with properties
                            child.Parent   = product;
                            child.Children = null;
                            this.CreateProduct(child);
                        }
                        else
                        {
                            //Update properties
                            if (child.Properties != null && child.Properties.Count > 0)
                            {
                                List <Product_Specifications> properties = (from prop in db.Product_Specifications
                                                                            where prop.Product_ID == child.ID
                                                                            select prop).ToList <Product_Specifications>();

                                List <Product_Specifications> newProps = new List <Product_Specifications>();
                                if (properties.Count > 0)
                                {
                                    //current just support edit existed property's value, doesn't support deleting property
                                    foreach (BProductProperty p in child.Properties)
                                    {
                                        Product_Specifications psprop = (from ep in properties where ep.Product_Spec_ID == p.PID select ep).FirstOrDefault <Product_Specifications>();
                                        if (psprop == null)
                                        {
                                            //cretae new property for existed product
                                            psprop = new Product_Specifications()
                                            {
                                                Product_ID = child.ID, Product_Spec_ID = p.PID, Product_Spec_Value_ID = p.PVID, Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), User_ID = this.CurrentUser.ID
                                            };
                                            db.Product_Specifications.Add(psprop);
                                        }
                                        else
                                        {
                                            //update existed property's value
                                            if (psprop.Product_Spec_Value_ID != p.PVID)
                                            {
                                                psprop.Product_Spec_Value_ID = p.PVID;
                                            }
                                        }
                                    }

                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                }


                bproduct = this.GetProductFullInfo(product.ID);

                base.CreateActionLog(new BUserActionLog()
                {
                    Shop = new BShop {
                        ID = bproduct.Shop.Shop_ID
                    }, Action = new BUserAction()
                    {
                        Action_ID = UserLogAction.UPDATE_PRODUCT
                    }, Description = "商品编号:" + bproduct.ID + "\n商品名称:" + bproduct.Title
                });
                result = true;
            }
            catch (KMJXCException kex)
            {
                throw kex;
            }
            catch
            {
            }
            finally
            {
                db.Dispose();
            }
            return(result);
        }
예제 #12
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);
        }
예제 #13
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public Product_Spec CreateNewProperty(int categoryId, string propertyName, List <string> value)
        {
            Product_Spec ps = null;

            if (string.IsNullOrEmpty(propertyName))
            {
                throw new KMJXCException("属性名称不能为空");
            }


            KuanMaiEntities     db         = new KuanMaiEntities();
            List <Product_Spec> properties = new List <Product_Spec>();

            try
            {
                var propes = from props in db.Product_Spec where props.Name == propertyName && props.Shop_ID == this.Shop.Shop_ID select props;
                if (categoryId > 0)
                {
                    propes.Where(c => c.Product_Class_ID == categoryId);
                }

                properties = propes.ToList <Product_Spec>();
                if (properties.Count > 0)
                {
                    throw new KMJXCException("名为" + propertyName + "已经存在");
                }

                ps = new Product_Spec();
                ps.Product_Class_ID = categoryId;
                ps.Shop_ID          = this.Shop.Shop_ID;
                ps.User_ID          = this.CurrentUser.ID;
                ps.Name             = propertyName;
                ps.Created          = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                ps.Mall_PID         = "";

                db.Product_Spec.Add(ps);
                db.SaveChanges();

                int psId = ps.Product_Spec_ID;
                if (psId == 0)
                {
                    throw new KMJXCException("产品属性创建失败");
                }

                if (value != null)
                {
                    foreach (string v in value)
                    {
                        Product_Spec_Value psv = new Product_Spec_Value();
                        psv.Product_Spec_ID = psId;
                        psv.Name            = v;
                        psv.Created         = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now);
                        psv.User_ID         = ps.User_ID;
                        psv.Mall_PVID       = "";
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (db != null)
                {
                    db.Dispose();
                }
            }
            return(ps);
        }