Пример #1
0
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="userModel"></param>
        /// <param name="productId"></param>
        /// <param name="fullPath"></param>
        /// <param name="path"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateImage(UserModel userModel, Guid productId, string fullPath, out string path, out string msg)
        {
            path = null;

            if (!File.Exists(fullPath))
            {
                msg = "上传文件不存在";
                return(false);
            }

            var product = Da.Get <Product>(productId);

            if (product == null)
            {
                msg = "产品不存在";
                return(false);
            }

            //给图片添加编码
            var pathText     = $"/Files/{DateTime.Today:yyMMdd}/{Guid.NewGuid()}.jpg";
            var pathLogo     = $"/Files/{DateTime.Today:yyMMdd}/{Guid.NewGuid()}.jpg";
            var fullPathText = $"{Config.Root}{pathText}";
            var fullPathLogo = $"{Config.Root}{pathLogo}";

            fullPathText.CreateDirByFilePath();

            var b1 = ImageHelper.AddImageText(fullPath, fullPathText, product.ProductSku, ImagePosition.BottomRigth, 0.5);

            if (!b1)
            {
                msg = "给图片打编码失败";
                return(false);
            }

            if (!File.Exists(fullPathText))
            {
                msg = "上传失败";
                return(false);
            }
            if (!File.Exists(fullPathLogo))
            {
                pathLogo = null;
            }

            var m = new ProductImage()
            {
                ProductImageId = SeqGuid.NewGuid(),
                ProductId      = productId,
                PathImage      = pathText,
                PathLogoImage  = pathLogo,
                Sort           = 999,
                CreateDate     = DateTime.Now,
            };

            //添加并更新数量
            var b3 = Da.AddProductImage(m);

            if (!b3)
            {
                msg = "更新失败";
                return(false);
            }

            //排序
            var list = Da.GetList <ProductImage>(new
            {
                ProductId = productId,
            }).OrderBy(p => p.Sort).ThenBy(p => p.CreateDate).ToList();

            for (var i = 0; i < list.Count; i++)
            {
                list[i].Sort = i;
            }
            Da.BulkUpdate(list, p => p.Sort, p => p.ProductImageId);

            msg  = null;
            path = pathText;
            return(true);
        }