public ApiMessage UpdateProduct() { HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; HttpRequestBase request = context.Request; ApiMessage message = new ApiMessage(); string user_id = User.Identity.Name; UserManager userMgr = new UserManager(int.Parse(user_id), null); BUser user = userMgr.CurrentUser; ProductManager pdtManager = new ProductManager(userMgr.CurrentUser, userMgr.Shop, userMgr.CurrentUserPermission); int product_id = 0; int categoryId = 0; string description = ""; string props = ""; string images = ""; string title = ""; int.TryParse(request["cid"], out categoryId); int.TryParse(request["product_id"],out product_id); description = request["desc"]; title = request["title"]; images = request["images"]; props = request["props"]; string suppliers = request["sids"]; try { BProduct product = new BProduct(); product.ID = product_id; product.Parent = null; product.Category = new BCategory() { ID = categoryId }; product.Title = title; product.Description = description; product.Properties = null; product.FileRootPath = request.PhysicalApplicationPath; if (!string.IsNullOrEmpty(images)) { product.Images = new List<Image>(); string[] ims = images.Split(','); foreach (string img in ims) { int image_id = 0; int.TryParse(img,out image_id); if (image_id > 0) { product.Images.Add(new Image() { ID = image_id }); } } } if (!string.IsNullOrEmpty(suppliers)) { product.Suppliers = new List<Supplier>(); string[] sids = suppliers.Split(','); foreach (string sid in sids) { product.Suppliers.Add(new Supplier() { Supplier_ID = int.Parse(sid), Enabled = true }); } } if (!string.IsNullOrEmpty(props)) { if (product.Children == null) { product.Children = new List<BProduct>(); } string[] groups = props.Split(';'); foreach (string group in groups) { string groupp = group.Split('|')[1]; int pdtId = int.Parse(group.Split('|')[0]); BProduct child = new BProduct(); child.ID = pdtId; child.Title = product.Title; child.Description = product.Description; child.Category = product.Category; List<BProductProperty> properties = new List<BProductProperty>(); string[] pops = groupp.Split(','); foreach (string pop in pops) { BProductProperty prop = new BProductProperty(); prop.PID = int.Parse(pop.Split(':')[0]); prop.PVID = int.Parse(pop.Split(':')[1]); properties.Add(prop); } child.Properties = properties; product.Children.Add(child); } } pdtManager.UpdateProduct(ref product); message.Status = "ok"; message.Item = product; } catch (KM.JXC.Common.KMException.KMJXCException kex) { message.Status = "failed"; message.Message = kex.Message; } catch (Exception ex) { message.Status = "failed"; message.Message = ex.Message; } return message; }
/// <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; }
/// <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; }
/// <summary> /// Input product is Parent product which doesn't have any chind products with Properties combination /// </summary> /// <param name="product"></param> /// <param name="props"></param> /// <returns></returns> public bool CreateProperties(BProduct product,List<BProductProperty> props) { return this.CreateProperties(this.GetProduct(product.ID), props); }
/// <summary> /// Create single parent or child product which can contains Properties combination /// </summary> /// <param name="product"></param> /// <returns></returns> public void CreateProduct(BProduct product) { if (this.CurrentUserPermission.ADD_PRODUCT == 0) { throw new KMJXCException("没有权限创建产品"); } Product dbProduct = new Product(); dbProduct.Code = product.Code; dbProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbProduct.Name = product.Title; dbProduct.Parent_ID = product.ParentID; dbProduct.Quantity = 0; dbProduct.Description = product.Description; if (product.Category != null) { dbProduct.Product_Class_ID = product.Category.ID; } dbProduct.Product_ID = 0; dbProduct.Price = product.Price; if (product.Unit != null) { dbProduct.Product_Unit_ID = product.Unit.Product_Unit_ID; } dbProduct.Shop_ID = this.Shop.Shop_ID; dbProduct.User_ID = this.MainUser.ID; if (product.Parent != null && product.Parent.ID > 0) { dbProduct.Parent_ID = product.Parent.ID; } using (KuanMaiEntities db = new KuanMaiEntities()) { db.Product.Add(dbProduct); db.SaveChanges(); if (dbProduct.Product_ID <= 0) { throw new KMJXCException("产品创建失败"); } product.ID = dbProduct.Product_ID; //Update product images if (product.Images != null && product.Images.Count > 0) { List<int> img_ids = new List<int>(); foreach (Image img in product.Images) { img_ids.Add(img.ID); } List<Image> dbImages=(from img in db.Image where img_ids.Contains(img.ID) select img).ToList<Image>(); foreach (Image image in dbImages) { image.ProductID = product.ID; } db.SaveChanges(); } Stock_Batch batch = null; if (dbProduct.Parent_ID == 0) { batch = new Stock_Batch() { Name = "P0", ProductID = dbProduct.Product_ID, Price = 0, ShopID = dbProduct.Shop_ID, Desc = "", Created_By = this.CurrentUser.ID }; batch.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); batch.ParentProductID = dbProduct.Parent_ID; db.Stock_Batch.Add(batch); db.SaveChanges(); } else { batch=(from b in db.Stock_Batch where b.ProductID==dbProduct.Parent_ID select b).FirstOrDefault<Stock_Batch>(); } Store_House defaultStoreHouse = null; List<Store_House> storeHouses = (from h in db.Store_House where (h.Shop_ID == dbProduct.Shop_ID || h.Shop_ID==this.Main_Shop.Shop_ID) select h).ToList<Store_House>(); if (storeHouses.Count == 0) { defaultStoreHouse = new Store_House(); defaultStoreHouse.Shop_ID = dbProduct.Shop_ID; defaultStoreHouse.Title = "默认仓库"; defaultStoreHouse.Address = ""; defaultStoreHouse.Phone = ""; defaultStoreHouse.User_ID = this.CurrentUser.ID; defaultStoreHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); defaultStoreHouse.Default = true; defaultStoreHouse.Guard = 0; db.Store_House.Add(defaultStoreHouse); db.SaveChanges(); } else { defaultStoreHouse = (from h in storeHouses where h.Default == true select h).FirstOrDefault<Store_House>(); if (defaultStoreHouse == null) { defaultStoreHouse = storeHouses[0]; defaultStoreHouse.Default = true; } } Stock_Pile stockPile = new Stock_Pile(); stockPile.LastLeave_Time = 0; stockPile.Price = 0; stockPile.Product_ID = product.ID; stockPile.Quantity = 0; stockPile.Shop_ID = dbProduct.Shop_ID; stockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; stockPile.StockPile_ID = 0; stockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(stockPile); if (product.Properties != null && product.Properties.Count > 0) { foreach (BProductProperty pro in product.Properties) { Product_Specifications ps = new Product_Specifications(); ps.Product_ID = dbProduct.Product_ID; ps.Product_Spec_ID = pro.PID; ps.Product_Spec_Value_ID = pro.PVID; ps.User_ID = this.CurrentUser.ID; ps.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Specifications.Add(ps); } //db.SaveChanges(); } if (product.Suppliers != null) { foreach (Supplier su in product.Suppliers) { Product_Supplier ps = new Product_Supplier(); ps.Product_ID = product.ID; ps.Supplier_ID = su.Supplier_ID; db.Product_Supplier.Add(ps); } //db.SaveChanges(); } if (product.Children != null) { foreach (BProduct p in product.Children) { if (p.Parent == null) { p.ParentID = dbProduct.Product_ID; p.Parent = new BProduct() { ID = product.ID }; } p.Children = null; this.CreateProduct(p); } } else { base.CreateActionLog(new BUserActionLog() { Shop = new BShop { ID = dbProduct.Shop_ID }, Action = new BUserAction() { Action_ID = UserLogAction.CREATE_PRODUCT }, Description = "" }); } db.SaveChanges(); } }
public ApiMessage UpdateProductsWastage() { ApiMessage message = new ApiMessage() { Status = "ok" }; HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"]; HttpRequestBase request = context.Request; string user_id = User.Identity.Name; UserManager userMgr = new UserManager(int.Parse(user_id), null); BUser user = userMgr.CurrentUser; StockManager stockManager = new StockManager(userMgr.CurrentUser, userMgr.Shop, userMgr.CurrentUserPermission); try { string ws=request["wastages"]; if (string.IsNullOrEmpty(ws)) { message.Status = "failed"; message.Message = "输入错误"; return message; } ws = HttpUtility.UrlDecode(ws); JArray ps = (JArray)JArray.Parse(ws); if (ps != null && ps.Count > 0) { List<BProduct> products = new List<BProduct>(); for (int i = 0; i < ps.Count; i++) { BProduct product = new BProduct(); JObject json = (JObject)ps[i]; if (json["product_id"] != null) { product.ID = (int)json["product_id"]; } if (json["quantity"] != null) { product.Quantity = (int)json["quantity"]; } if (json["parent_id"] != null) { product.ParentID = (int)json["parent_id"]; } products.Add(product); } stockManager.UpdateProductsWastage(products); } } catch (KMJXCException kex) { message.Status = "failed"; message.Message = kex.Message; } catch (Exception ex) { message.Status = "failed"; message.Message = "未知错误"; } return message; }