public IActionResult Catg(int id = 50013794) { //using (var file = System.IO.File.OpenRead("F://project/vscode/cache/catg." + id + ".dat")) //{ // var data = Serializer.Deserialize<Ayatta.Model.Catg>(file); // return Json(data); // } var data = DefaultStorage.CatgGet(id); return(Json(data)); }
public IActionResult Item(int id, int catgId, int spuId, Prod.Item item) { var userId = 1; var result = new Result <string>(); if (id < 0 || id < 0) { result.Message = "参数错误"; return(Json(result)); } var form = Request.Form; var time = DateTime.Now; var propPrefix = "prop."; var itemPrefix = "item."; if (item.Name.IsNullOrEmpty() || item.Name.Length > 30) { result.Data = "name"; return(Json(result)); } if (!item.Title.IsNullOrEmpty() && item.Title.Length > 30) { result.Data = "title"; return(Json(result)); } if (item.Stock < 1 || item.Stock > 99999999) { result.Data = "stock"; return(Json(result)); } if (item.RetailPrice < 1 || item.RetailPrice > 99999999) { result.Data = "retailPrice"; return(Json(result)); } if (item.Price < 1 || item.Price > 99999999) { result.Data = "price"; return(Json(result)); } if (item.AppPrice < 1 || item.AppPrice > 99999999) { result.Data = "appPrice"; return(Json(result)); } var summary = form[itemPrefix + "summary"].ToString(); if (summary.IsNullOrEmpty() || (summary.Length < 5 || summary.Length > 200)) { result.Data = "summary"; return(Json(result)); } var subStock = form[itemPrefix + "subStock"].ToString().As <byte>(0); var isTiming = false; var onlineTime = time; var itemStatus = Prod.Status.Online; var onlineTimeField = form[itemPrefix + "online"]; if (onlineTimeField == "0" || onlineTimeField == "1") { if (onlineTimeField == "1") { itemStatus = Prod.Status.Offline; } } else if (!DateTime.TryParse(onlineTimeField, out onlineTime)) { //var error = new { name = prefix + "onlineTime", message = "请选择日期及时间!" }; //errors.Add(error); result.Data = "onlinetime"; result.Message = "请选择日期及时间!"; return(Json(result)); } else { isTiming = true; } var data = DefaultStorage.CatgGet(catgId); if (data == null || !data.Props.Any()) { result.Message = "参数错误!"; return(Json(result)); } var props = new List <Prod.Prop>(); var propIdBuilder = new StringBuilder(); var propStrBuilder = new StringBuilder(); foreach (var p in data.Props) { if (p.IsEnumProp && p.Values != null) { var val = form[propPrefix + p.Id].ToArray().Where(x => !string.IsNullOrEmpty(x)).ToArray(); var array = Array.ConvertAll(val, Convert.ToInt32); foreach (var o in array) { foreach (var v in p.Values) { if (o > 0 && o == v.Id) { var prop = new Prod.Prop(); prop.PId = p.Id; prop.VId = v.Id; prop.PName = p.Name; prop.VName = v.Name; prop.Extra = string.Empty; if (p.IsSaleProp) { prop.Extra = "sale"; } if (p.IsColorProp) { prop.Extra += " color"; } if (p.Id == 20000) { item.BrandId = v.Id; item.BrandName = v.Name; } props.Add(prop); if (!p.IsSaleProp) { propIdBuilder.Append(p.Id + ":" + v.Id + ";"); propStrBuilder.Append(p.Id + ":" + v.Id + ":" + p.Name + ":" + v.Name + ";"); } } } } } } var skus = new List <Prod.Sku>(); var saleProps = props.Where(x => x.Extra.Contains("sale")).ToList(); var saleKeys = new List <string[]>(); foreach (var g in saleProps.OrderBy(x => x.PId).GroupBy(g => g.PId)) { var keys = g.Select(x => g.Key + ":" + x.VId).ToArray(); saleKeys.Add(keys); } var skuKeys = GetAllSkuKeys(saleKeys); if (skuKeys != null && skuKeys.Any()) { foreach (var skuKey in skuKeys) { if (skuKey.IsNullOrEmpty()) { continue; } var codeParam = "sku.code." + skuKey; var stockParam = "sku.stock." + skuKey; var priceParam = "sku.price." + skuKey; var appPriceParam = "sku.appPrice." + skuKey; var barcodeParam = "sku.barcode." + skuKey; int stock; decimal price, appPrice; var codeVal = form[codeParam].ToString().Trim(); var stockVal = form[stockParam].ToString().Trim(); var priceVal = form[priceParam].ToString().Trim(); var appPriceVal = form[appPriceParam].ToString().Trim(); var barcodeVal = form[barcodeParam].ToString().Trim(); if (stockVal.IsNullOrEmpty() || !int.TryParse(stockVal, out stock) || priceVal.IsNullOrEmpty() || !decimal.TryParse(priceVal, out price) || appPriceVal.IsNullOrEmpty() || !decimal.TryParse(appPriceVal, out appPrice)) { continue; } var nvs = skuKey.Split(';'); var propstr = string.Empty; foreach (var nv in nvs) { var pid = nv.Split(':')[0].AsInt(); var vid = nv.Split(':')[1].AsInt(); var pname = saleProps.First(o => o.PId == pid).PName; var vname = saleProps.First(o => o.VId == vid).VName; propstr += string.Format("{0}:{1}:{2}:{3};", pid, vid, pname, vname); } var sku = new Prod.Sku(); sku.SpuId = spuId; sku.UserId = userId; sku.CatgId = catgId; sku.CatgRId = 0; sku.CatgMId = ""; sku.Code = codeVal; sku.Barcode = barcodeVal; sku.BrandId = item.BrandId; sku.Stock = stock; sku.Price = price; sku.AppPrice = appPrice; sku.PropId = skuKey; sku.PropStr = propstr.TrimEnd(';'); sku.SaleCount = 0; sku.Status = itemStatus; sku.CreatedOn = time; sku.ModifiedBy = ""; sku.ModifiedOn = time; skus.Add(sku); } } var colorImgDic = new Dictionary <string, string>(); var colorProps = props.Where(x => x.Extra.Contains("color")).ToList(); foreach (var p in colorProps) { var key = p.PId + ":" + p.VId; var colorParam = "color.img." + key; var val = form[colorParam].ToString(); if (!val.IsNullOrEmpty()) { colorImgDic.Add(key, val); } } var itemImgs = form["item.img"].ToArray().Where(x => !string.IsNullOrEmpty(x)).Take(5); var picture = itemImgs.FirstOrDefault(); var itemImgStr = string.Join(";", itemImgs); item.SpuId = spuId; item.UserId = userId; item.CatgId = catgId; item.CatgRId = 0; item.CatgMId = ""; item.Code = item.Code; item.Name = item.Name.StripHtml(); item.Title = item.Title.StripHtml(); item.Barcode = item.Barcode; item.Keyword = ""; item.PropId = propIdBuilder.ToString().TrimEnd(';'); item.PropStr = propStrBuilder.ToString().TrimEnd(';'); item.PropAlias = string.Empty; item.InputId = ""; item.InputStr = ""; item.Summary = summary; item.Picture = picture; item.ItemImgStr = itemImgStr; item.PropImgStr = JsonConvert.SerializeObject(colorImgDic); item.Width = 0; item.Depth = 0; item.Height = 0; item.Weight = 0; item.Location = ""; item.IsBonded = false; item.IsOversea = false; item.IsTiming = isTiming; item.IsVirtual = false; item.IsAutoFill = false; item.SupportCod = false; item.FreePostage = false; item.PostageTplId = 1; item.SubStock = subStock; item.Showcase = 1; item.OnlineOn = time; item.OfflineOn = time.AddDays(15); item.RewardRate = 0.5m; item.HasInvoice = false; item.HasWarranty = false; item.HasGuarantee = false; item.SaleCount = 0; item.CollectCount = 0; item.ConsultCount = 0; item.CommentCount = 0; item.Status = itemStatus; item.CreatedOn = time; item.ModifiedBy = ""; item.ModifiedOn = time; item.Skus = skus; item.Desc.CreatedOn = time; item.Desc.ModifiedBy = ""; item.Desc.ModifiedOn = time; if (id > 0) { item.Id = id; var propIds = item.Skus.Select(x => x.PropId); var oldSkus = DefaultStorage.ProdSkuList(id); var oldSkuPropIds = oldSkus.Select(x => x.PropId); var dic = oldSkus.ToDictionary(x => x.PropId, x => x.Id); foreach (var sku in item.Skus) { if (dic.ContainsKey(sku.PropId)) { sku.Id = dic[sku.PropId];//更新 } else { sku.Id = 0;//新增 } } var deletedPropIds = oldSkuPropIds.Except(propIds);//删除的 var deletedIds = oldSkus.Where(x => deletedPropIds.Contains(x.PropId)).Select(x => x.Id); result.Status = DefaultStorage.ProdItemUpdate(item, deletedIds.ToArray()); return(Json(result)); } var newId = DefaultStorage.ProdItemCreate(item); result.Data = newId.ToString(); result.Status = newId > 0; return(Json(result)); }