/// <summary> /// /// </summary> /// <param name="categoryId"></param> /// <param name="propName"></param> /// <param name="propValues"></param> /// <returns></returns> public BProperty CreateProperty(int categoryId,string propName,List<string> propValues,int shop_id=0) { BProperty bproperty = null; if (string.IsNullOrEmpty(propName)) { throw new KMJXCException("属性名称不能为空"); } KuanMaiEntities db = new KuanMaiEntities(); try { var existed = from props in db.Product_Spec where props.Name==propName select props; if (categoryId > 0) { //existed = existed.Where(a=>a.Product_Class_ID==categoryId); } if (this.Shop.Parent_Shop_ID > 0) { var pexisted = existed.Where(a=>a.Shop_ID==this.Main_Shop.Shop_ID); if (pexisted.FirstOrDefault<Product_Spec>() != null) { throw new KMJXCException("主店铺已经有此属性,不能重复创建,请使用现有主店铺的属性"); } } existed = existed.Where(a => a.Shop_ID == this.Shop.Shop_ID); if (existed.FirstOrDefault<Product_Spec>() != null) { throw new KMJXCException("此属性已经存在,不能重复创建"); } Product_Spec property = new Product_Spec(); property.Product_Class_ID = categoryId; property.Name = propName; property.User_ID = this.CurrentUser.ID; property.Shop_ID = this.Shop.Shop_ID; if (shop_id > 0) { property.Shop_ID = shop_id; } property.Mall_PID = ""; property.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec.Add(property); db.SaveChanges(); if (property.Product_Spec_ID <= 0) { throw new KMJXCException("属性创建失败"); } bproperty = new BProperty(); bproperty.ID = property.Product_Spec_ID; bproperty.Created_By = this.CurrentUser; bproperty.Created = (int)property.Created; bproperty.CategoryId = categoryId; bproperty.MID = ""; bproperty.Name = propName; if (propValues != null) { if (bproperty.Values == null) { bproperty.Values = new List<Product_Spec_Value>(); } foreach (string v in propValues) { Product_Spec_Value psv = new Product_Spec_Value(); psv.Mall_PVID = ""; psv.Name = v; psv.Product_Spec_ID = property.Product_Spec_ID; psv.Product_Spec_Value_ID = 0; psv.User_ID = this.CurrentUser.ID; psv.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec_Value.Add(psv); } db.SaveChanges(); bproperty.Values=(from pv in db.Product_Spec_Value where pv.Product_Spec_ID==property.Product_Spec_ID select pv).ToList<Product_Spec_Value>(); } } catch(KMJXCException ex) { throw ex; } finally { if (db != null) { db.Dispose(); } } return bproperty; }
/// <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; }
/// <summary> /// /// </summary> /// <param name="products"></param> /// <param name="mapProduct"></param> private void CreateProductsByMallProducts(List<BMallProduct> products,bool mapProduct=false) { if (products == null || products.Count == 0) { return; } List<BProperty> properties = new List<BProperty>(); int shop_id = products[0].Shop.ID; using (KuanMaiEntities db = new KuanMaiEntities()) { string[] dbMallPdtIds=(from p in products select p.ID).ToArray<string>(); List<Mall_Product> dbMallProcuts = (from p in db.Mall_Product where dbMallPdtIds.Contains(p.Mall_ID) select p).ToList<Mall_Product>(); List<Product> dbProducts = new List<Product>(); List<Product_Spec> existedProperties = null; var tmpProperties = from p in db.Product_Spec select p; var tmpDbProducts = from p in db.Product where p.Parent_ID == 0 select p; if (shop_id == this.Main_Shop.Shop_ID) { int[] child_shop_ids = (from c in this.ChildShops select c.ID).ToArray<int>(); tmpProperties = tmpProperties.Where(p => (child_shop_ids.Contains(p.Shop_ID) || p.Shop_ID == shop_id)); tmpDbProducts = tmpDbProducts.Where(p => (child_shop_ids.Contains(p.Shop_ID) || p.Shop_ID == shop_id)); } else { tmpProperties = tmpProperties.Where(p =>(p.Shop_ID == shop_id || p.Shop_ID==this.Main_Shop.Shop_ID)); tmpDbProducts = tmpDbProducts.Where(p => (p.Shop_ID == shop_id || p.Shop_ID == this.Main_Shop.Shop_ID)); } dbProducts = tmpDbProducts.ToList<Product>(); existedProperties = tmpProperties.ToList<Product_Spec>(); int[] prop_ids=(from prop in existedProperties select prop.Product_Spec_ID).ToArray<int>(); List<Product_Spec_Value> existedPropValues=(from pv in db.Product_Spec_Value where prop_ids.Contains(pv.Product_Spec_ID) select pv).ToList<Product_Spec_Value>(); Store_House defaultStoreHouse = null; List<Store_House> storeHouses=(from h in db.Store_House where h.Shop_ID==shop_id select h).ToList<Store_House>(); if (storeHouses.Count == 0) { defaultStoreHouse = new Store_House(); defaultStoreHouse.Shop_ID = shop_id; defaultStoreHouse.Title = "默认仓库"; defaultStoreHouse.User_ID = this.CurrentUser.ID; defaultStoreHouse.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); defaultStoreHouse.Default = true; 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; } } foreach (BMallProduct product in products) { Mall_Product dbMallProduct=(from p in dbMallProcuts where p.Mall_ID==product.ID select p).FirstOrDefault<Mall_Product>(); Product dbProduct = (from p in dbProducts where p.MallProduct == product.ID select p).FirstOrDefault<Product>(); if (dbProduct != null) { if (!string.IsNullOrEmpty(dbMallProduct.Title) && dbMallProduct.Title!=dbProduct.Name) { dbProduct.Name = dbMallProduct.Title; } dbMallProduct.CreatedProduct = true; db.SaveChanges(); continue; } dbProduct = new Product(); dbProduct.Code = product.Code; dbProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbProduct.Description = dbProduct.Description; dbProduct.MallProduct = product.ID; dbProduct.Name = product.Title; dbProduct.Parent_ID = 0; dbProduct.Price = product.Price; dbProduct.Product_Class_ID = 0; //dbProduct.Quantity = (int)product.Quantity; dbProduct.Shop_ID = product.Shop.ID; dbProduct.Update_Time = dbProduct.Create_Time; dbProduct.Update_User_ID = this.CurrentUser.ID; dbProduct.User_ID = this.CurrentUser.ID; dbProduct.Wastage = 0; db.Product.Add(dbProduct); db.SaveChanges(); if (dbProduct.Product_ID <= 0) { continue; } Stock_Batch 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(); Stock_Pile stockPile = new Stock_Pile(); stockPile.LastLeave_Time = 0; stockPile.Price = 0; stockPile.Product_ID = dbProduct.Product_ID; stockPile.Quantity = 0; stockPile.Shop_ID = product.Shop.ID; stockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; stockPile.StockPile_ID = 0; stockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(stockPile); if (product.Skus != null) { foreach (BMallSku sku in product.Skus) { Product dbChildProduct = new Product(); dbChildProduct.Code = product.Code; dbChildProduct.Create_Time = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); dbChildProduct.Description = dbProduct.Description; dbChildProduct.MallProduct = sku.SkuID; dbChildProduct.Name = product.Title; dbChildProduct.Parent_ID = dbProduct.Product_ID; dbChildProduct.Price = product.Price; dbChildProduct.Product_Class_ID = 0; //dbChildProduct.Quantity = (int)sku.Quantity; dbChildProduct.Shop_ID = product.Shop.ID; dbChildProduct.Update_Time = dbChildProduct.Create_Time; dbChildProduct.Update_User_ID = this.CurrentUser.ID; dbChildProduct.User_ID = this.CurrentUser.ID; dbChildProduct.Wastage = 0; db.Product.Add(dbChildProduct); db.SaveChanges(); if (dbChildProduct.Product_ID <= 0) { continue; } if (string.IsNullOrEmpty(sku.PropertiesName)) { continue; } Stock_Pile skustockPile = new Stock_Pile(); skustockPile.LastLeave_Time = 0; skustockPile.Price = 0; skustockPile.Product_ID = dbChildProduct.Product_ID; skustockPile.Quantity = 0; skustockPile.Shop_ID = product.Shop.ID; skustockPile.StockHouse_ID = defaultStoreHouse.StoreHouse_ID; skustockPile.StockPile_ID = 0; skustockPile.Batch_ID = batch.ID; db.Stock_Pile.Add(skustockPile); string[] props = sku.PropertiesName.Split(';'); foreach (string prop in props) { string[] values = prop.Split(':'); Product_Spec property = (from p in existedProperties where (p.Mall_PID == values[0] || p.Name==values[2]) select p).FirstOrDefault<Product_Spec>(); if (property == null) { property=new Product_Spec(); property.Name = values[2]; property.Mall_PID = values[0]; property.Shop_ID = shop_id; property.User_ID = this.CurrentUser.ID; property.Product_Class_ID = 0; property.Created = DateTimeUtil.ConvertDateTimeToInt(DateTime.Now); db.Product_Spec.Add(property); db.SaveChanges(); if (property.Product_Spec_ID > 0) { existedProperties.Add(property); } } Product_Spec_Value propValue=(from v in existedPropValues where (v.Mall_PVID==values[2] || v.Name==values[3]) select v ).FirstOrDefault<Product_Spec_Value>(); if(propValue==null) { propValue=new Product_Spec_Value(){ Product_Spec_ID=property.Product_Spec_ID, Mall_PVID=values[1], Name=values[3], Created=DateTimeUtil.ConvertDateTimeToInt(DateTime.Now), User_ID=this.CurrentUser.ID}; db.Product_Spec_Value.Add(propValue); db.SaveChanges(); if (propValue.Product_Spec_Value_ID > 0) { existedPropValues.Add(propValue); } } if (property.Product_Spec_ID > 0 && propValue.Product_Spec_Value_ID > 0) { Product_Specifications ps = new Product_Specifications(); ps.Product_ID = dbChildProduct.Product_ID; ps.Product_Spec_ID = property.Product_Spec_ID; ps.Product_Spec_Value_ID = propValue.Product_Spec_Value_ID; db.Product_Specifications.Add(ps); } } } } dbMallProduct.CreatedProduct = true; } db.SaveChanges(); } }