public ApiMessage Add() { string user_id = User.Identity.Name; UserManager userMgr = new UserManager(int.Parse(user_id), null); BUser user = userMgr.CurrentUser; Shop cShop = userMgr.Shop; ShopCategoryManager cateMgr = new ShopCategoryManager(userMgr.CurrentUser, cShop, userMgr.CurrentUserPermission); HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; HttpRequestBase request = context.Request; ApiMessage message = new ApiMessage(); try { BCategory cate = new BCategory(); cate.Name = request["name"].ToString(); cate.ID = 0; cate.Mall_ID = ""; cate.Mall_PID = ""; cate.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); cate.Parent = new BCategory() { ID = int.Parse(request["parent_id"].ToString()) }; cateMgr.CreateCategory(cate); message.Status = "ok"; message.Message = "分类创建成功"; message.Item = cate; } catch (KM.JXC.Common.KMException.KMJXCException ex) { message.Status = "failed"; message.Message = ex.Message; } catch (Exception ex) { message.Status = "failed"; message.Message = "分类创建失败"; } return message; }
/// <summary> /// /// </summary> /// <param name="category_id"></param> /// <param name="db"></param> /// <returns></returns> private BCategory GetCategory(int category_id,KuanMaiEntities db) { BCategory category = null; try { Product_Class ca = (from pc in db.Product_Class where pc.Product_Class_ID == category_id select pc).FirstOrDefault<Product_Class>(); if (ca != null) { category = new BCategory(); category.ID = ca.Product_Class_ID; category.Mall_ID = ca.Mall_CID; category.Mall_PID = ca.Mall_PCID; category.Name = ca.Name; category.Order = (int)ca.Order; if (ca.Parent_ID <= 0) { category.Parent = null; } category.Enabled = ca.Enabled; category.Created = ca.Create_Time; category.Children = new List<BCategory>(); List<Product_Class> children = (from pc in db.Product_Class where pc.Parent_ID == category_id select pc).ToList<Product_Class>(); if (children.Count > 0) { foreach (Product_Class productclass in children) { BCategory category1 = new BCategory(); category1.ID = ca.Product_Class_ID; category1.Mall_ID = ca.Mall_CID; category1.Mall_PID = ca.Mall_PCID; category1.Name = ca.Name; category1.Order = (int)ca.Order; category1.Enabled = ca.Enabled; category1.Created = ca.Create_Time; category.Children.Add(category1); } } } } catch (Exception ex) { } finally { } return category; }
/// <summary> /// Update single category /// </summary> /// <param name="category">Product_Class object</param> /// <returns></returns> public bool UpdateCategory(BCategory category) { bool result = false; if (category == null) { throw new KMJXCException("输入错误"); } if (string.IsNullOrEmpty(category.Name)) { throw new KMJXCException("类目名不能为空"); } if (this.CurrentUserPermission.UPDATE_PRODUCT_CLASS == 0) { throw new KMJXCException("没有权限更新类目"); } using (KuanMaiEntities db = new KuanMaiEntities()) { int[] child_shops = (from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); Product_Class oldCategory = (from pc in db.Product_Class where pc.Product_Class_ID == category.ID select pc).ToList<Product_Class>()[0]; if (oldCategory == null) { throw new KMJXCException("您要修改的类目信息不存在"); } if (this.Shop.Shop_ID != this.Main_Shop.Shop_ID) { if (oldCategory.Shop_ID == this.Main_Shop.Shop_ID) { throw new KMJXCException("您不能修改主店铺类目"); } if (oldCategory.Shop_ID == this.Shop.Shop_ID) { throw new KMJXCException("您不能其他主店铺类目"); } } else { if (oldCategory.Shop_ID != this.Main_Shop.Shop_ID || !child_shops.Contains(oldCategory.Shop_ID)) { throw new KMJXCException("您无法修改其他店铺的类目,只能修改主店铺或者子店铺类目"); } } oldCategory.Name = category.Name; oldCategory.Enabled = category.Enabled; if (category.Parent != null) { oldCategory.Parent_ID = category.Parent.ID; } db.SaveChanges(); result = true; } return result; }
/// <summary> /// /// </summary> /// <returns></returns> public List<BCategory> GetOnlineCategories() { List<BCategory> categories = null; List<Product_Class> classes= mallShopManager.GetCategories(this.MainUser); if (classes != null && classes.Count > 0) { List<Product_Class> parents = (from p in classes where p.Mall_PCID == "0" select p).ToList<Product_Class>(); if (parents != null && parents.Count > 0) { foreach (Product_Class pc in parents) { BCategory cate = new BCategory(); cate.Name = pc.Name; cate.Mall_ID = pc.Mall_CID; cate.Mall_PID = pc.Mall_PCID; List<Product_Class> children = (from c in classes where c.Mall_PCID == pc.Mall_PCID select c).ToList<Product_Class>(); if (children != null && children.Count > 0) { cate.Children = new List<BCategory>(); foreach (Product_Class childCate in children) { BCategory child = new BCategory(); child.Mall_ID = childCate.Mall_CID; child.Mall_PID = childCate.Mall_PCID; child.Name = childCate.Name; cate.Children.Add(child); } } } } } return categories; }
/// <summary> /// Get all categories /// </summary> /// <returns></returns> public List<BCategory> GetCategories(int? parentId,bool fromMailShop=false) { List<BCategory> categories = new List<BCategory>(); using (KuanMaiEntities db = new KuanMaiEntities()) { var allpcs = (from pc in db.Product_Class select pc); int[] childs=(from c in this.DBChildShops select c.Shop_ID).ToArray<int>(); if (this.Shop.Shop_ID == this.Main_Shop.Shop_ID) { allpcs = allpcs.Where(c => (c.Shop_ID == this.Shop.Shop_ID || childs.Contains(c.Shop_ID))); } else { allpcs = allpcs.Where(c => (c.Shop_ID == this.Shop.Shop_ID || c.Shop_ID==this.Main_Shop.Shop_ID)); } if (parentId != null) { allpcs = allpcs.Where(a => a.Parent_ID == parentId); } List<Product_Class> pcs = allpcs.ToList<Product_Class>(); foreach (Product_Class ca in pcs) { BCategory category = new BCategory(); category.ID = ca.Product_Class_ID; category.Mall_ID = ca.Mall_CID; category.Mall_PID = ca.Mall_PCID; category.Name = ca.Name; category.Order = (int)ca.Order; category.Enabled = ca.Enabled; category.Created = ca.Create_Time; category.Children = (from cate in db.Product_Class where cate.Parent_ID == ca.Product_Class_ID select new BCategory { ID = cate.Product_Class_ID, Mall_ID = cate.Mall_CID, Mall_PID = cate.Mall_PCID, Name = cate.Name, Order = (int)cate.Order, Enabled = cate.Enabled, Created = cate.Create_Time, Created_By = (from u in db.User where u.User_ID == cate.Create_User_ID select new BUser { ID = u.User_ID, Name = u.Name, Mall_ID = u.Mall_ID, Mall_Name = u.Mall_Name, //EmployeeInfo = (from em in db.Employee where em.User_ID == cate.Create_User_ID select em).FirstOrDefault<Employee>() }).FirstOrDefault<BUser>() }).ToList<BCategory>(); if (ca.Parent_ID > 0) { category.Parent = (from cate in db.Product_Class where cate.Product_Class_ID == ca.Parent_ID select new BCategory { Created = cate.Create_Time, Enabled = cate.Enabled, ID = cate.Product_Class_ID, Name = cate.Name, Mall_ID = cate.Mall_CID, Mall_PID = cate.Mall_PCID, }).FirstOrDefault<BCategory>(); } category.Created_By = (from u in db.User where u.User_ID == ca.Create_User_ID select new BUser { ID = u.User_ID, Name = u.Name, Mall_ID = u.Mall_ID, Mall_Name = u.Mall_Name, //EmployeeInfo = (from employee in db.Employee // where employee.User_ID == u.User_ID // select new BEmployee // { // ID = employee.Employee_ID, // Name = employee.Name // }).FirstOrDefault<BEmployee>(), }).FirstOrDefault<BUser>(); category.Shop = (from sp in db.Shop where sp.Shop_ID == ca.Shop_ID select new BShop { ID = sp.Shop_ID, Title = sp.Name, Description = sp.Description, Created = (int)sp.Created }).FirstOrDefault<BShop>(); if(category.Shop.ID==this.Main_Shop.Shop_ID) { category.FromMainShop=true; }else if(childs!=null && childs.Length>0 && childs.Contains(category.Shop.ID)) { category.FromChildShop=true; } categories.Add(category); } } return categories; }
/// <summary> /// Create new category /// </summary> /// <param name="category">Product_Class object</param> /// <returns></returns> public bool CreateCategory(BCategory category) { bool result = false; if (category == null) { throw new KMJXCException("输入错误"); } if (string.IsNullOrEmpty(category.Name)) { throw new KMJXCException("类目名不能为空"); } if (this.CurrentUserPermission.ADD_PRODUCT_CLASS == 0) { throw new KMJXCException("没有权限添加类目"); } using (KuanMaiEntities db = new KuanMaiEntities()) { Product_Class pc = new Product_Class(); pc.Create_Time = category.Created; pc.Create_User_ID = this.CurrentUser.ID; pc.Enabled = true; pc.Mall_CID = category.Mall_ID; pc.Mall_PCID = category.Mall_PID; pc.Name = category.Name; pc.Order = category.Order; if (category.Shop == null || category.Shop.ID == 0) { pc.Shop_ID = this.Shop.Shop_ID; } else { pc.Shop_ID = category.Shop.ID; } if (category.Parent == null || category.Parent.ID <= 0) { pc.Parent_ID = 0; } else { pc.Parent_ID = category.Parent.ID; } Product_Class existed = (from ca in db.Product_Class where ca.Name.ToLower() == category.Name.ToLower() && ca.Shop_ID == pc.Shop_ID && ca.Parent_ID==pc.Parent_ID select ca).FirstOrDefault<Product_Class>(); if (existed != null) { if (pc.Parent_ID > 0) { throw new KMJXCException("名为" + category.Name + "的子类目已经存在"); } else { throw new KMJXCException("名为" + category.Name + "的类目已经存在"); } } db.Product_Class.Add(pc); db.SaveChanges(); category.ID = pc.Product_Class_ID; result = true; base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = this.Shop.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_PRODUCT_CATEGORY }, Description = "" }); } return result; }