Beispiel #1
0
        public CustomJsonResult Edit(string operater, string merchId, RopPrdProductEdit rop)
        {
            CustomJsonResult result = new CustomJsonResult();

            if (string.IsNullOrEmpty(rop.Id))
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "商品Id不能为空"));
            }

            if (string.IsNullOrEmpty(rop.Name))
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "商品名称不能为空"));
            }

            if (rop.KindIds == null || rop.KindIds.Count == 0)
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "商品模块分类不能为空"));
            }

            if (rop.DispalyImgUrls == null || rop.DispalyImgUrls.Count == 0)
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "商品图片不能为空"));
            }

            using (TransactionScope ts = new TransactionScope())
            {
                var prdProduct = CurrentDb.PrdProduct.Where(m => m.Id == rop.Id).FirstOrDefault();

                prdProduct.Name           = rop.Name;
                prdProduct.PinYinName     = Pinyin.ConvertEncoding(prdProduct.Name, Encoding.UTF8, Encoding.GetEncoding("GB2312"));
                prdProduct.PinYinIndex    = Pinyin.GetInitials(prdProduct.PinYinName, Encoding.GetEncoding("GB2312"));
                prdProduct.BriefDes       = rop.BriefDes;
                prdProduct.DetailsDes     = rop.DetailsDes;
                prdProduct.DispalyImgUrls = rop.DispalyImgUrls.ToJsonString();
                prdProduct.Mender         = operater;
                prdProduct.MendTime       = DateTime.Now;

                foreach (var sku in rop.Skus)
                {
                    var prdProductSku = CurrentDb.PrdProductSku.Where(m => m.Id == sku.Id).FirstOrDefault();
                    if (prdProductSku != null)
                    {
                        prdProductSku.SpecDes   = sku.SpecDes;
                        prdProductSku.SalePrice = sku.SalePrice;
                        prdProductSku.Mender    = operater;
                        prdProductSku.MendTime  = DateTime.Now;
                    }
                }

                var prdProductKinds = CurrentDb.PrdProductKind.Where(m => m.PrdProductId == prdProduct.Id).ToList();

                foreach (var prdProductKind in prdProductKinds)
                {
                    CurrentDb.PrdProductKind.Remove(prdProductKind);
                }

                if (rop.KindIds != null)
                {
                    foreach (var kindId in rop.KindIds)
                    {
                        var prdProductKind = new PrdProductKind();
                        prdProductKind.Id           = GuidUtil.New();
                        prdProductKind.PrdKindId    = kindId;
                        prdProductKind.PrdProductId = prdProduct.Id;
                        prdProductKind.Creator      = operater;
                        prdProductKind.CreateTime   = DateTime.Now;
                        CurrentDb.PrdProductKind.Add(prdProductKind);
                    }
                }

                var prdProductSubjects = CurrentDb.PrdProductSubject.Where(m => m.PrdProductId == prdProduct.Id).ToList();

                foreach (var prdProductSubject in prdProductSubjects)
                {
                    CurrentDb.PrdProductSubject.Remove(prdProductSubject);
                }


                if (rop.SubjectIds != null)
                {
                    foreach (var subjectId in rop.SubjectIds)
                    {
                        var prdProductSubject = new PrdProductSubject();
                        prdProductSubject.Id           = GuidUtil.New();
                        prdProductSubject.PrdSubjectId = subjectId;
                        prdProductSubject.PrdProductId = prdProduct.Id;
                        prdProductSubject.Creator      = operater;
                        prdProductSubject.CreateTime   = DateTime.Now;
                        CurrentDb.PrdProductSubject.Add(prdProductSubject);
                    }
                }

                CurrentDb.SaveChanges();
                ts.Complete();

                result = new CustomJsonResult(ResultType.Success, ResultCode.Success, "保存成功");
            }

            return(result);
        }
Beispiel #2
0
        public CustomJsonResult Add(string operater, string merchId, RopPrdProductAdd rop)
        {
            CustomJsonResult result = new CustomJsonResult();


            if (string.IsNullOrEmpty(rop.Name))
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "商品名称不能为空"));
            }

            if (rop.KindIds == null || rop.KindIds.Count == 0)
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "至少选择一个商品模块"));
            }

            if (rop.SubjectIds == null || rop.SubjectIds.Count == 0)
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "至少选择一个商品栏目"));
            }

            if (rop.DispalyImgUrls == null || rop.DispalyImgUrls.Count == 0)
            {
                return(new CustomJsonResult(ResultType.Failure, ResultCode.Failure, "至少上传一张商品图片"));
            }

            using (TransactionScope ts = new TransactionScope())
            {
                var prdProduct = new PrdProduct();
                prdProduct.Id             = GuidUtil.New();
                prdProduct.MerchId        = merchId;
                prdProduct.Name           = rop.Name;
                prdProduct.BarCode        = rop.BarCode;
                prdProduct.PinYinName     = Pinyin.ConvertEncoding(prdProduct.Name, Encoding.UTF8, Encoding.GetEncoding("GB2312"));
                prdProduct.PinYinIndex    = Pinyin.GetInitials(prdProduct.PinYinName, Encoding.GetEncoding("GB2312"));
                prdProduct.DispalyImgUrls = rop.DispalyImgUrls.ToJsonString();
                prdProduct.MainImgUrl     = ImgSet.GetMain(prdProduct.DispalyImgUrls);
                prdProduct.DetailsDes     = rop.DetailsDes;
                prdProduct.BriefDes       = rop.BriefDes;
                prdProduct.Creator        = operater;
                prdProduct.CreateTime     = DateTime.Now;

                foreach (var sku in rop.Skus)
                {
                    var prdProductSku = new PrdProductSku();
                    prdProductSku.Id           = GuidUtil.New();
                    prdProductSku.MerchId      = prdProduct.MerchId;
                    prdProductSku.PrdProductId = prdProduct.Id;
                    prdProductSku.Name         = prdProduct.Name;
                    prdProductSku.SpecDes      = sku.SpecDes;
                    prdProductSku.SalePrice    = sku.SalePrice;
                    prdProductSku.Creator      = operater;
                    prdProductSku.CreateTime   = DateTime.Now;
                    CurrentDb.PrdProductSku.Add(prdProductSku);
                }

                if (rop.KindIds != null)
                {
                    prdProduct.PrdKindIds = string.Join(",", rop.KindIds.ToArray());

                    foreach (var kindId in rop.KindIds)
                    {
                        var productSkuKind = new PrdProductKind();
                        productSkuKind.Id           = GuidUtil.New();
                        productSkuKind.PrdKindId    = kindId;
                        productSkuKind.PrdProductId = prdProduct.Id;
                        productSkuKind.Creator      = operater;
                        productSkuKind.CreateTime   = DateTime.Now;
                        CurrentDb.PrdProductKind.Add(productSkuKind);
                    }
                }

                if (rop.SubjectIds != null)
                {
                    prdProduct.PrdSubjectIds = string.Join(",", rop.SubjectIds.ToArray());

                    foreach (var subjectId in rop.SubjectIds)
                    {
                        var productSkuSubject = new PrdProductSubject();
                        productSkuSubject.Id           = GuidUtil.New();
                        productSkuSubject.PrdSubjectId = subjectId;
                        productSkuSubject.PrdProductId = prdProduct.Id;
                        productSkuSubject.Creator      = operater;
                        productSkuSubject.CreateTime   = DateTime.Now;
                        CurrentDb.PrdProductSubject.Add(productSkuSubject);
                    }
                }

                CurrentDb.PrdProduct.Add(prdProduct);
                CurrentDb.SaveChanges();
                ts.Complete();

                result = new CustomJsonResult(ResultType.Success, ResultCode.Success, "保存成功");
            }

            return(result);
        }