public IActionResult Copy([FromBody] CopyProduct copyData, [FromServices] IHostingEnvironment environment) { var productPath = $"{environment.WebRootPath}/{ProductImage._defaultPath}"; var parentPath = $"{productPath}/{copyData.ChainId}"; var formatArr = new[] { "400x300", "200x150", "100x75" }; // 首先获取所有需要复制的文件路径 var imageDic = new Dictionary <string, List <string> >(); foreach (var format in formatArr) { var list = new List <string>(); foreach (var name in copyData.ImageNames) { var filePath = $"{parentPath}/{format}/{name}"; if (!Fs.File.Exists(filePath)) { continue; } list.Add(filePath); } imageDic.Add(format, list); } // 然后给每个商户复制 foreach (var id in copyData.StoreIds) { var dir = $"{productPath}/{id}"; if (!Fs.Directory.Exists(dir)) { Fs.Directory.CreateDirectory(dir); } foreach (var format in formatArr) { var formatDir = $"{dir}/{format}"; if (!Fs.Directory.Exists(formatDir)) { Fs.Directory.CreateDirectory(formatDir); } foreach (var filepath in imageDic[format]) { Fs.File.Copy(filepath, $"{formatDir}/{Fs.Path.GetFileName(filepath)}", true); } } } return(Ok("复制成功")); }
public async Task <IActionResult> Copy([FromBody] CopyProduct copyData) { var result = new JsonData(); copyData.ChainId = Business.ID; if (copyData.StoreIds == null || copyData.StoreIds.Length == 0) { result.Msg = "请选择需要复制商品的商户"; return(Json(result)); } if (copyData.ProductIds == null || copyData.ProductIds.Length == 0) { result.Msg = "请选择需要复制的商品"; return(Json(result)); } var count = await Service.Copy(copyData, AppData.ApiUri + "/Product/Copy"); result.Success = true; result.Msg = "复制成功"; result.Data = count; return(Json(result)); }
public async Task <int> Copy(CopyProduct copyData, string imageUrl) { // 注:复制的商品不包含:折扣、套餐 // 1. 查找需要复制的商品 var now = DateTime.Now; var products = Context.Products.AsNoTracking() .Include(a => a.ProductType) .Include(a => a.Attributes) .Include(a => a.Formats) .Include(a => a.Images) .Where(a => copyData.ProductIds.Contains(a.ID)) .OrderBy(a => a.ProductType.Sort) .ThenBy(a => a.ID) .ToList(); var imageNames = new List <string>(); // 需要复制的图片名称 // 2. 将需要复制的商品属性初始化,Tag1用来保存商品类别名称 products.ForEach(product => { product.ID = 0; product.BusinessId = 0; product.CreateTime = now; product.ModifyTime = null; product.NotSaleTime = null; product.ProductIdSet = null; product.ProductTypeId = null; product.PublishTime = now; product.Code = null; if (product.ProductType != null) { product.Tag1 = product.ProductType.Name; product.ProductType = null; } if (product.Attributes != null) { foreach (var attr in product.Attributes) { attr.CreateTime = now; attr.ID = 0; attr.ProductId = 0; } } if (product.Formats != null) { foreach (var format in product.Formats) { format.CreateTime = now; format.ID = 0; format.ProductId = 0; } } if (product.Images != null) { foreach (var img in product.Images) { img.CreateTime = now; img.ID = 0; img.ProductId = 0; imageNames.Add(img.Name + "." + img.ExtensionName); } } }); // 3. 读取每个门店的商品类型 var types = Context.ProductTypes .Where(a => copyData.StoreIds.Contains(a.BusinessId)) .OrderBy(a => a.BusinessId) .ThenBy(a => a.Sort) .ToList(); // 4. 复制商品 foreach (var id in copyData.StoreIds) { var curTypes = types.Where(a => a.BusinessId == id).ToList(); // 当前的门店所有商品类别 var maxSort = 0; if (curTypes.Count > 0) { maxSort = curTypes.Max(a => a.Sort); } products.ForEach(a => { var product = (Product)a.Clone(); product.BusinessId = id; var typeName = product.Tag1 + ""; if (typeName.Length > 0) { var type = curTypes.FirstOrDefault(b => b.Name == product.Tag1 + ""); if (type == null) { type = new ProductType { BusinessId = id, Description = "总店复制", Name = typeName, Sort = ++maxSort }; product.ProductType = type; curTypes.Add(type); } else { product.ProductTypeId = type.ID; } } Context.Add(product); Context.SaveChanges(); product.Code = product.ID.ToString().PadLeft(6, '0'); Context.SaveChanges(); }); } var count = Context.SaveChanges(); var postData = new CopyProduct { ChainId = copyData.ChainId, StoreIds = copyData.StoreIds, ImageNames = imageNames.ToArray() }; var content = JsonConvert.SerializeObject(postData); try { using (var client = new HttpClient()) using (var body = new StringContent(content)) { body.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var res = await client.PostAsync(imageUrl, body); res.EnsureSuccessStatusCode(); } } catch (Exception e) { Log.Error(e.Message); } return(count); }