/// <summary>
 /// 将GoodsCategoryLevel1转换CategoryLevelModel 采用链式
 /// </summary>
 /// <param name="item"></param>
 /// <returns></returns>
 public CategoryLevelModel ToCategoryLevel1Model(GoodsCategoryLevel1 item)
 {
     CategoryId   = item.CategoryLevel1Id;
     CategoryName = item.CategoryName;
     Description  = item.Description;
     ImageUrl     = item.CategoryLevel1ImageUrl;
     LevelNumber  = (int)CategoryLevel.Level1;
     return(this);
 }
        public GoodsCategoryLevel1 AsGoodsCategoryLevel1()
        {
            GoodsCategoryLevel1 categoryLevel = GoodsCategoryLevel1.New();

            categoryLevel.CategoryName           = CategoryName;
            categoryLevel.Description            = Description;
            categoryLevel.CategoryLevel1ImageUrl = ImageUrl;
            return(categoryLevel);
        }
        public async Task <IActionResult> EditCategoryLevelAsync([FromServices] IHostingEnvironment env, IFormFile files, CategoryLevelModel model, bool isUpdate)
        {
            var file = files;

            if (file != null)
            {
                var filePath      = string.Format("/Uploads/Images/{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd"));
                var qiniuFilePath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd"));
                if (!Directory.Exists(env.WebRootPath + filePath))
                {
                    Directory.CreateDirectory(env.WebRootPath + filePath);
                }
                long         size          = file.Length;
                var          fileExtension = Path.GetExtension(file.FileName);
                const string fileFilt      = ".gif|.jpg|.jpeg|.png|.webp|";
                const string saveSuffix    = ".jpeg";
                if (fileExtension == null)
                {
                    return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件没有后缀")));
                }
                if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1)
                {
                    return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不是图片")));
                }
                if (size > 1024 * 1024 * 2)
                {
                    return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不能大于2M")));
                }
                var strDateTime = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                var strRan      = Convert.ToString(new Random().Next(100, 999));
                var saveName    = strDateTime + strRan;
                var useQiNiu    = BuilderContext.Configuration["UseQiNiu"];
                var qiniuHost   = BuilderContext.Configuration["QiNiuHost"];
                var pathName    = string.Empty;

                string hostPath      = WebSiteUtility.MapPhysicsToUrl($"{filePath}{saveName}");
                string qiniuHostPath = WebSiteUtility.MapPhysicsToUrl($"{qiniuHost}{qiniuFilePath}{saveName}");

                using (var stream = new FileStream($"{env.WebRootPath}{filePath}{saveName}{saveSuffix}", FileMode.Create))
                {
                    await file.CopyToAsync(stream);

                    stream.Flush();
                    pathName       = stream.Name;
                    model.ImageUrl = $"{Request.Scheme}://{Request.Host}{hostPath}{saveSuffix}";
                }
                if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase))
                {
                    await Task.Run(() =>
                    {
                        QiniuManager qiniu = new QiniuManager();
                        string savekey     = $"{qiniuFilePath}{saveName}";
                        byte[] data        = System.IO.File.ReadAllBytes(pathName);
                        qiniu.ByteUpload(data, savekey);
                    });

                    model.ImageUrl = qiniuHostPath;
                }
            }
            if (isUpdate)
            {
                if (model.LevelEnum == CategoryLevel.Level1)
                {
                    GoodsCategoryLevel1 category = goodsCategoryLevelService.GetCategoryLevel1(model.CategoryId);
                    category.CategoryName           = model.CategoryName;
                    category.Description            = model.Description ?? string.Empty;
                    category.CategoryLevel1ImageUrl = model.ImageUrl ?? string.Empty;
                    goodsCategoryLevelService.UpdataCategoryLevel <GoodsCategoryLevel1>(category);
                    return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功")));
                }
                else if (model.LevelEnum == CategoryLevel.Level2)
                {
                    GoodsCategoryLevel2 category = goodsCategoryLevelService.GetCategoryLevel2(model.CategoryId);
                    var oldCategoryLevel2Id      = category.CategoryLevel1Id;
                    category.CategoryName           = model.CategoryName;
                    category.CategoryLevel1Id       = model.ParentCategoryId.Value;
                    category.Description            = model.Description ?? string.Empty;
                    category.CategoryLevel2ImageUrl = model.ImageUrl ?? string.Empty;
                    goodsCategoryLevelService.UpdataCategoryLevel <GoodsCategoryLevel2>(category, oldCategoryLevel2Id);
                    return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功")));
                }
            }
            else
            {
                if (model.LevelEnum == CategoryLevel.Level1)
                {
                    GoodsCategoryLevel1 category = GoodsCategoryLevel1.New();
                    category.CategoryName           = model.CategoryName;
                    category.Description            = model.Description ?? string.Empty;
                    category.CategoryLevel1ImageUrl = model.ImageUrl ?? string.Empty;
                    goodsCategoryLevelService.CreateCategoryLevel <GoodsCategoryLevel1>(category);
                    return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功")));
                }
                else if (model.LevelEnum == CategoryLevel.Level2)
                {
                    GoodsCategoryLevel2 category = GoodsCategoryLevel2.New();
                    category.CategoryName           = model.CategoryName;
                    category.CategoryLevel1Id       = model.ParentCategoryId.Value;
                    category.Description            = model.Description ?? string.Empty;
                    category.CategoryLevel2ImageUrl = model.ImageUrl ?? string.Empty;
                    goodsCategoryLevelService.CreateCategoryLevel <GoodsCategoryLevel2>(category);
                    return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功")));
                }
            }

            return(Json(new StatusMessageData(StatusMessageType.Error, "保存失败")));
        }