Exemplo n.º 1
0
        public GoodsController(ChestDatabaseContext context,
                               IHostingEnvironment environment,
                               GoodsCounter goodsCounter,
                               EmailService emailService)
        {
            _databaseContext    = context;
            _hostingEnvironment = environment;
            _goodsCounter       = goodsCounter;
            _emailService       = emailService;

            if (_databaseContext.Categories.Any() == false & _databaseContext.Manufacturers.Any() == false & _databaseContext.Goods.Any() == false)
            {
                Category smartphone = new Category {
                    Name = "Smartphone"
                };
                Category notebook = new Category {
                    Name = "Notebook"
                };
                _databaseContext.Categories.AddRange(smartphone, notebook);

                Manufacturer apple = new Manufacturer {
                    Name = "Apple"
                };
                Manufacturer xiaomi = new Manufacturer {
                    Name = "Xiaomi"
                };

                _databaseContext.Manufacturers.AddRange(apple, xiaomi);

                _databaseContext.Goods.AddRange(
                    new Goods {
                    Name = "Mi A2 Lite", Price = 5000, Category = smartphone, Manufacturer = xiaomi
                },
                    new Goods {
                    Name = "Mi Notebook Pro", Price = 25000, Category = notebook, Manufacturer = xiaomi
                },
                    new Goods {
                    Name = "IPhone X", Price = 20000, Category = smartphone, Manufacturer = apple
                },
                    new Goods {
                    Name = "MacBook Pro", Price = 25000, Category = notebook, Manufacturer = apple
                });
                _databaseContext.SaveChanges();
            }
        }
Exemplo n.º 2
0
Arquivo: Goods.cs Projeto: qdjx/C5
        //更新信息
        public static GoodsInfo GoodsInfoUpload(GoodsInfo goodsInfo, GoodsBrand goodsBrand, List <GoodsKind> goodsKinds, List <GoodsTag> goodsTags
                                                , GoodsPhoto goodsPhoto, GoodsDesc goodsDesc, List <GoodsSpecFull> goodsSpecsFull, GoodsCounter goodsCounter, out AlertMessage alertMessage)
        {
            //权限检查
            if (!AUTH.PermissionCheck(ModuleInfo, ActionInfos.Where(i => i.Name == (goodsInfo.ID == 0 ? "Create" : "Modify")).FirstOrDefault(), out string Message))
            {
                alertMessage = new AlertMessage {
                    Message = Message, Type = AlertType.warning
                };
                return(null);
            }

            //表单检查
            if (string.IsNullOrWhiteSpace(goodsInfo.Name))
            {
                alertMessage = new AlertMessage {
                    Message = "商品代码不能为空。", Type = AlertType.warning
                };
                return(null);
            }
            if (string.IsNullOrWhiteSpace(goodsInfo.Title))
            {
                alertMessage = new AlertMessage {
                    Message = "商品名称不能为空。", Type = AlertType.warning
                };
                return(null);
            }

            using (var EF = new EF())
            {
                //修改是否存在?商品代码唯一?
                GoodsInfo goods_exist      = null;
                GoodsInfo goods_name_exist = null;
                if (goodsInfo.ID == 0)
                {
                    goods_name_exist = EF.GoodsInfos.Where(i => i.Name == goodsInfo.Name).FirstOrDefault();
                }
                else
                {
                    goods_exist = EF.GoodsInfos.Where(i => i.ID == goodsInfo.ID).FirstOrDefault();
                    if (goods_exist == null)
                    {
                        alertMessage = new AlertMessage {
                            Message = string.Format("商品编号[{0}]不存在。", goodsInfo.ID), Type = AlertType.warning
                        };
                        return(null);
                    }
                    goods_name_exist = EF.GoodsInfos.Where(i => i.ID != goodsInfo.ID && i.Name == goodsInfo.Name).FirstOrDefault();
                }
                if (goods_name_exist != null && goods_name_exist.ID > 0)
                {
                    alertMessage = new AlertMessage {
                        Message = string.Format("商品代码[{0}]已被ID[{1}]使用。", goodsInfo.Name, goods_name_exist.ID), Type = AlertType.warning
                    };
                    return(null);
                }

                //数据保存
                using (TransactionScope TS = new TransactionScope())
                {
                    if (goodsInfo.ID == 0)
                    {
                        goods_exist = EF.GoodsInfos.Add(new GoodsInfo {
                            Enabled = true, StateID = (int)GoodsState.架,
                        });
                    }
                    goods_exist.Name  = goodsInfo.Name;
                    goods_exist.Title = goodsInfo.Title;
                    EF.SaveChanges();

                    //品牌
                    var brand_exist = EF.GoodsBrands.Where(i => i.GoodsID == goods_exist.ID).FirstOrDefault();
                    if (brand_exist == null)
                    {
                        if (goodsBrand != null)
                        {
                            EF.GoodsBrands.Add(brand_exist = new GoodsBrand
                            {
                                GoodsID = goods_exist.ID,
                                BrandID = goodsBrand.BrandID,
                            });
                        }
                    }
                    else
                    {
                        if (goodsBrand == null)
                        {
                            EF.GoodsBrands.Remove(brand_exist);
                        }
                        else
                        {
                            brand_exist.BrandID = goodsBrand.BrandID;
                        }
                    }
                    //品类
                    var kinds_exist = EF.GoodsKinds.Where(i => i.GoodsID == goods_exist.ID);
                    foreach (var kind_exist in kinds_exist)
                    {
                        var exist_find = false;
                        foreach (var goodsKind in goodsKinds)
                        {
                            if (goodsKind.KindLevel == kind_exist.KindLevel && goodsKind.KindID == kind_exist.KindID)
                            {
                                exist_find = true;
                                goodsKinds.Remove(goodsKind);
                                break;
                            }
                        }
                        if (!exist_find)
                        {
                            EF.GoodsKinds.Remove(kind_exist);
                        }
                    }
                    foreach (var goodsKind in goodsKinds)
                    {
                        goodsKinds.ForEach(i => i.GoodsID = goods_exist.ID);
                        EF.GoodsKinds.Add(new GoodsKind
                        {
                            GoodsID   = goods_exist.ID,
                            KindLevel = goodsKind.KindLevel,
                            KindID    = goodsKind.KindID,
                        });
                    }
                    //标签
                    var tags_exist = EF.GoodsTags.Where(i => i.GoodsID == goods_exist.ID).ToList();
                    if (tags_exist != null)
                    {
                        EF.GoodsTags.RemoveRange(tags_exist);
                    }
                    if (goodsTags != null)
                    {
                        goodsTags.ForEach(i => i.GoodsID = goods_exist.ID);
                        EF.GoodsTags.AddRange(goodsTags);
                    }
                    //图文
                    var photo_exist = EF.GoodsPhotos.Where(i => i.GoodsID == goods_exist.ID).FirstOrDefault();
                    if (photo_exist == null)
                    {
                        EF.GoodsPhotos.Add(photo_exist = new GoodsPhoto {
                            GoodsID = goods_exist.ID,
                        });
                    }
                    photo_exist.FileIDs = goodsPhoto.FileIDs;
                    var desc_exist = EF.GoodsDescs.Where(i => i.GoodsID == goods_exist.ID).FirstOrDefault();
                    if (desc_exist == null)
                    {
                        EF.GoodsDescs.Add(desc_exist = new GoodsDesc {
                            GoodsID = goods_exist.ID,
                        });
                    }
                    desc_exist.Description = goodsDesc.Description;
                    //规格
                    var specs_exist = EF.GoodsSpecs.Where(i => i.GoodsID == goods_exist.ID).ToList();
                    if (specs_exist != null)
                    {
                        specs_exist.ForEach(i => i.Enabled = false);
                        EF.SaveChanges();
                    }
                    foreach (var goodsSpecFill in goodsSpecsFull)
                    {
                        var spec_exist = EF.GoodsSpecs.Where(i => i.GoodsID == goods_exist.ID && i.SpecValueIDs == goodsSpecFill.SpecValueIDs).FirstOrDefault();
                        if (spec_exist == null)
                        {
                            EF.GoodsSpecs.Add(spec_exist = new GoodsSpec
                            {
                                GoodsID      = goods_exist.ID,
                                SpecValueIDs = goodsSpecFill.SpecValueIDs,
                            });
                        }
                        spec_exist.SpecValues = goodsSpecFill.SpecValues;
                        spec_exist.Enabled    = goodsSpecFill.Enabled;
                        EF.SaveChanges();
                        var specCounter = EF.GoodsCounter.Where(i => i.GoodsID == goods_exist.ID && i.GoodsSpecID == spec_exist.ID).FirstOrDefault();
                        if (specCounter == null)
                        {
                            EF.GoodsCounter.Add(specCounter = new GoodsCounter
                            {
                                GoodsID     = goods_exist.ID,
                                GoodsSpecID = spec_exist.ID,
                            });
                        }
                        specCounter.SKU      = goodsSpecFill.SKU;
                        specCounter.UPC      = goodsSpecFill.UPC;
                        specCounter.EAN      = goodsSpecFill.EAN;
                        specCounter.JAN      = goodsSpecFill.JAN;
                        specCounter.ISBN     = goodsSpecFill.ISBN;
                        specCounter.Price    = goodsSpecFill.Price ?? 0;
                        specCounter.Quantity = goodsSpecFill.Quantity ?? 0;
                    }
                    //柜台
                    var counter_exist = EF.GoodsCounter.Where(i => i.GoodsSpecID == 0 && i.GoodsID == goods_exist.ID).FirstOrDefault();
                    if (counter_exist == null)
                    {
                        EF.GoodsCounter.Add(counter_exist = new GoodsCounter {
                            GoodsSpecID = 0,
                        });
                    }
                    counter_exist.GoodsID  = goods_exist.ID;
                    counter_exist.SKU      = goodsCounter.SKU;
                    counter_exist.UPC      = goodsCounter.UPC;
                    counter_exist.EAN      = goodsCounter.EAN;
                    counter_exist.JAN      = goodsCounter.JAN;
                    counter_exist.ISBN     = goodsCounter.ISBN;
                    counter_exist.Price    = goodsCounter.Price;
                    counter_exist.Quantity = goodsCounter.Quantity;
                    //保存
                    EF.SaveChanges();
                    TS.Complete();
                }

                //更新完成
                alertMessage = null;
                return(goods_exist);
            }
        }
Exemplo n.º 3
0
        //信息表单保存按钮
        public void InfoFormSubmit_Click(object sender, EventArgs e)
        {
            //品名
            var goodsInfo = new Ziri.MDL.GoodsInfo
            {
                ID    = long.Parse(hidInfoFormGoodsID.Value),
                Name  = inpInfoFormGoodsName.Text,
                Title = inpInfoFormGoodsTitle.Text,
            };
            //品牌
            GoodsBrand goodsBrand = null;

            if (!string.IsNullOrWhiteSpace(hidInfoFormBrandID.Value))
            {
                goodsBrand = new GoodsBrand
                {
                    BrandID = long.Parse(hidInfoFormBrandID.Value),
                };
            }
            //品类
            List <GoodsKind> goodsKinds = new List <GoodsKind>();
            long             kindRootID = long.Parse(drpInfoFormKindRoot.SelectedValue);
            long             kindL1ID   = 0;

            try { kindL1ID = long.Parse(drpInfoFormKindL1.SelectedValue); } catch { }
            if (kindRootID > 0)
            {
                goodsKinds.Add(new GoodsKind {
                    KindLevel = 0, KindID = kindRootID
                });
            }
            if (kindL1ID > 0)
            {
                goodsKinds.Add(new GoodsKind {
                    KindLevel = 1, KindID = kindL1ID
                });
            }
            //图文
            var PhotoUploadInfos = JsonConvert.DeserializeObject <List <FileUploadInfo> >(hidInfoFormGoodsPhoto.Value);
            var goodsPhoto       = new GoodsPhoto
            {
                FileIDs = string.Join(",", PhotoUploadInfos.Select(i => i.FileInfo.ID)),
            };
            var goodsDesc = new GoodsDesc
            {
                Description = HttpUtility.UrlDecode(hidInfoFormGoodsDesc.Value)
            };
            //规格
            var goodsSpecsFull = new List <GoodsSpecFull>();

            foreach (var specInfo in lvInfoFormSpec.Items)
            {
                var valueIDs = (HiddenField)specInfo.FindControl("hidValueIDs");
                if (!string.IsNullOrWhiteSpace(valueIDs.Value))
                {
                    var inpSpecPrice = (TextBox)specInfo.FindControl("inpSpecPrice");
                    var inpSpecQty   = (TextBox)specInfo.FindControl("inpSpecQty");
                    goodsSpecsFull.Add(new GoodsSpecFull
                    {
                        SpecValueIDs = valueIDs.Value,
                        SpecValues   = ((HiddenField)specInfo.FindControl("hidValues")).Value,
                        Enabled      = ((HtmlInputCheckBox)specInfo.FindControl("inpSpecEnabled")).Checked,
                        SKU          = ((TextBox)specInfo.FindControl("inpInfoFormSKU")).Text,
                        UPC          = ((TextBox)specInfo.FindControl("inpInfoFormUPC")).Text,
                        EAN          = ((TextBox)specInfo.FindControl("inpInfoFormEAN")).Text,
                        JAN          = ((TextBox)specInfo.FindControl("inpInfoFormJAN")).Text,
                        ISBN         = ((TextBox)specInfo.FindControl("inpInfoFormISBN")).Text,
                        Price        = string.IsNullOrWhiteSpace(inpSpecPrice.Text) ? 0 : decimal.Parse(inpSpecPrice.Text),
                        Quantity     = string.IsNullOrWhiteSpace(inpSpecQty.Text) ? 0 : decimal.Parse(inpSpecQty.Text),
                    });
                }
            }
            //单价
            var goodsCounter = new GoodsCounter
            {
                SKU      = inpInfoFormSKU.Text,
                UPC      = inpInfoFormUPC.Text,
                EAN      = inpInfoFormEAN.Text,
                JAN      = inpInfoFormJAN.Text,
                ISBN     = inpInfoFormISBN.Text,
                Price    = decimal.Parse(inpInfoFormPrice.Text),
                Quantity = decimal.Parse(inpInfoFormQuantity.Text),
            };

            //保存
            goodsInfo = Ziri.BLL.ITEM.Goods.GoodsInfoUpload(goodsInfo, goodsBrand, goodsKinds, GetGoodsTags(), goodsPhoto, goodsDesc, goodsSpecsFull, goodsCounter, out AlertMessage alertMessage);
            if (alertMessage == null)
            {
                ListBind();
            }
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "InfoFormMessage", alertMessage == null
                ? string.Format("<script> swal('保存完成,商品编号[{0}]。', '', '{1}'); </script>", goodsInfo.ID, AlertType.success)
                : string.Format("<script> document.getElementById('btnListInfoFormModal').click(); swal('{0}', '', '{1}'); </script>", alertMessage.Message, alertMessage.Type));
        }