public Control CallCreateUserControl(Action<User> createCallback=null)
        {
            var formControl = FormControlHelper.CreateFormControl();
            formControl.RenderForm(new User(),false);
            formControl.ConfirmButton.Content = "创建用户";
            formControl.SubmitCallback = (d) =>
            {
                using (var dbcontext = new WarehouseContext())
                {
                    try
                    {
                        dbcontext.Users.Add((User)d);
                        dbcontext.SaveChanges();
                        if (createCallback != null)
                            createCallback((User)d);
                    }
                    catch (Exception ex)
                    {
                        //TODO: handle exception
                        formControl.SetErrorMsgManually("Username", "此用户名已存在,请更换");
                    }
                }
            };

            return formControl;
        }
        public Control CallAddProductControl(string productScanCode, WarehouseContext dbcontext, Action<Product> createCallback = null)
        {
            var formControl = FormControlHelper.CreateFormControl();
            var product = new Product();
            product.ScanCode = productScanCode;
            formControl.RenderForm(product, false);
            formControl.ConfirmButton.Content = "录入产品";
            formControl.SubmitCallback = (d) =>
            {
                try
                {
                    dbcontext.Products.Add((Product)d);
                    dbcontext.SaveChanges();
                    if (createCallback != null)
                        createCallback((Product)d);
                }
                catch (Exception ex)
                {
                    //TODO: handle exception
                    formControl.SetErrorMsgManually("ScanCode", "此产品扫码已存在");
                }
            };

            return formControl;
        }
 private void ReloadUserGrid()
 {
     using (var dbcontext = new WarehouseContext())
     {
         usersdg.ItemsSource = dbcontext.Users.ToList();
     }
 }
 private void DeleteUser_Clicked(object sender, RoutedEventArgs e)
 {
     var data = ((Button)sender).DataContext as User;
     if(data!=null){
         var result = MessageBox.Show(
             string.Format("你是否决定要删除用户'{0}'?", data.Name),
             "操作警告",
             MessageBoxButton.OKCancel,
             MessageBoxImage.Warning);
         if (result == MessageBoxResult.OK)
         {
             try
             {
                 using (var dbcontext = new WarehouseContext())
                 {
                     var deluser = dbcontext.Users.Find(((User)data).UserId);
                     dbcontext.Users.Remove(deluser);
                     dbcontext.SaveChanges();
                     edit_br.Child = null;
                     ReloadUserGrid();
                 }
             }
             catch (Exception ex)
             {
                 WindowMgr.SendNotification("删除操作失败。", NotificationLevel.Error);
             }
         }
     }
 }
 void SelectUserControl_Loaded(object sender, RoutedEventArgs e)
 {
     using (var dbcontext = new WarehouseContext())
     {
         userlist = dbcontext.Users.ToList();
     }
 }
        public Goods CallRegisterGoodsPopup(Product product, WarehouseContext dbcontext)
        {
            Goods newGoods = null;
            var window = new NotifyWindow();
            window.Width = 400;
            window.Height = 600;
            window.Title = "添加货物";

            var formControl = FormControlHelper.CreateFormControl();
            var goods = new Goods();
            goods.Product = product;
            goods.Name = product.Name;
            goods.State = GoodsState.Inbound;
            goods.GoodsCode = DateTime.Now.ToString("yyyyMMddHHmmss")+"001";

            formControl.CreateControlCallback = (cx, ctl) =>
            {
                if (cx.ControlType == ControlType.Editable)
                {
                    switch (cx.PropertyInfo.Name)
                    {
                        case "Product":
                            var tb = new TextBox();
                            tb.Text = product.Name;
                            tb.Style = Application.Current.Resources["editctl_TextBox"] as Style;
                            tb.IsEnabled = false;
                            CustomValidation.SetValidationOptOut(tb);
                            return tb;
                        case "GoodsCode":
                            ctl.IsEnabled = false;
                            return ctl;
                    }
                }
                return ctl;
            };

            formControl.RenderForm(goods, false);
            formControl.ConfirmButton.Content = "添加";
            formControl.SubmitCallback = d =>
            {
                try
                {
                    newGoods = d as Goods;
                    newGoods.InboundDate=DateTime.Now;
                    dbcontext.Goods.Add(newGoods);
                    window.Close();
                }
                catch (Exception ex)
                {
                    window.ShowNotificationMessage("添加货物失败,请重试。");
                }
            };
            window.MyContent = formControl;
            window.ShowDialog();
            return newGoods;
        }
 public static JObject LoadMetadata(string name)
 {
     using (var dbcontext = new WarehouseContext())
     {
         var data = dbcontext.Metadatas.FirstOrDefault(
             m => string.Equals(m.Name, name));
         if (data != null)
             return JObject.Parse(data.Content);
         else return null;
     }
 }
 public void CallAddProductPage()
 {
     var dbcontext = new WarehouseContext();
     var tab = WindowMgr.CreateTabPage("添加产品信息", "添加产品信息", false);
     var form = CallAddProductControl(null, dbcontext, (u) =>
     {
         WindowMgr.SendNotification("添加产品信息成功",NotificationLevel.Information);
         WindowMgr.CloseTabPage(tab);
         dbcontext.Dispose();
     });
     tab.Content = form;
 }
        public Dashboard()
        {
            InitializeComponent();
            EnvironmentInitializer.CheckAndInitializeSQLiteDB();
            SampleData.AddSampleData();

            using (var dbcontext = new WarehouseContext())
            {
                var query = dbcontext.Database.SqlQuery<User>("select * from Users where Name like @p0","%二%");
                var list = query.ToList();
            }
        }
 static void Initialize()
 {
     using (var dbcontext = new WarehouseContext())
     {
         var product = new Product
         {
             Name = "水果机7S",
             ScanCode = "111",
             Memo = "我们要做地球人都买得起的水果机"
         };
     }
 }
        public Product CallAddProductPopup(string productScanCode, string notificationMsg,WarehouseContext dbcontext)
        {
            var window = new NotifyWindow();
            window.Width = 400;
            window.Height = 600;
            window.Title = "录入产品信息";

            Product newp = null;
            window.MyContent = CallAddProductControl(productScanCode, dbcontext, (p) =>
            {
                window.Close();
                newp = p;
            });

            if (!string.IsNullOrEmpty(notificationMsg))
                window.ShowNotificationMessage(notificationMsg);

            // block until dialog close
            window.ShowDialog();
            return newp;
        }
 public static void SaveMetadata(string name, JObject jo)
 {
     using (var dbcontext = new WarehouseContext())
     {
         var data = dbcontext.Metadatas.FirstOrDefault(
             m => string.Equals(m.Name, name));
         if (data != null)
         {
             data.Content = jo.ToString();
             dbcontext.SaveChanges();
         }
         else
         {
             var newmeta = new Warehouse.DataModel.Entities.Metadata
             {
                 Name = name,
                 ContentType = "text/json",
                 Content = jo.ToString()
             };
             dbcontext.Metadatas.Add(newmeta);
             dbcontext.SaveChanges();
         }
     }
 }
 public GoodsList(WarehouseContext dbcontext)
     : this()
 {
     this.DBContext = dbcontext;
 }
 void ResetPassword()
 {
     using (var dbcontext = new WarehouseContext())
     {
         var updateuser = dbcontext.Users.Find(UpdateUser.UserId);
         if (updateuser != null)
         {
             updateuser.Password = pwdbox1.Password;
         }
         dbcontext.SaveChanges();
     }
 }
        private void query_click(object sender, RoutedEventArgs e)
        {
            var filter = searchctl.BuildQueryString();
            if (string.IsNullOrEmpty(filter))
                return;

            using (var dbcontext = new WarehouseContext())
            {
                var querystr = "select * from Goods where " + filter;
                var data = dbcontext.Database.SqlQuery<Goods>(querystr);
                goodsgrid.ItemsSource = null;
                goodsgrid.ItemsSource = data.ToList();
            }
        }
        public Control CallUpdateUserControl(User user, Action<User> updateCallback=null)
        {
            var formControl = FormControlHelper.CreateFormControl();
            // remove password
            formControl.DetermineFieldCreationCallback = (cx, s) =>
            {
                switch (cx.PropertyInfo.Name)
                {
                    case "Username":
                        return true;
                    default:
                        return s;
                }
            };

            formControl.CreateControlCallback = (cx, c) =>
            {
                if (cx.ControlType == ControlType.Editable)
                {
                    if (cx.PropertyInfo.Name == "Password")
                    {
                        return CreateResetPwdButton(user);
                    }
                    else if (cx.PropertyInfo.Name == "Username")
                    {
                        c.IsEnabled = false;
                    }
                }
                return c;
            };

            formControl.SubmitCallback = (d) =>
            {
                using (var dbcontext = new WarehouseContext())
                {
                    try
                    {
                        var selectuser = d as User;
                        var updateuser = dbcontext.Users.Find(selectuser.UserId);
                        updateuser.Name = selectuser.Name;
                        updateuser.PhoneNumber = selectuser.PhoneNumber;
                        updateuser.Memo = selectuser.Memo;
                        updateuser.IdentificationNumber = selectuser.IdentificationNumber;
                        updateuser.PermissionGroup = selectuser.PermissionGroup;
                        updateuser.Department = selectuser.Department;
                        dbcontext.SaveChanges();
                        WindowMgr.SendNotification("更新用户成功", NotificationLevel.Information);
                        if (updateCallback != null)
                            updateCallback(selectuser);
                    }
                    catch (Exception ex)
                    {
                        // TODO
                        WindowMgr.SendNotification("更新用户失败",NotificationLevel.Error);
                    }
                }
            };

            formControl.RenderForm(user, false);
            formControl.ConfirmButton.Content = "保存设置";

            return formControl;
        }
 public ManageTags(WarehouseContext dbcontext)
     : this()
 {
     DBContext = dbcontext;
 }
        internal FrameworkElement RenderSelectUserControl(FormItemContext context, WarehouseContext dbcontext,
			User selecteduser = null)
        {
            var options = (from u in dbcontext.Users
                select new NameValuePair
                {
                    Name = u.Name,
                    Description = u.IdentificationNumber,
                    Value = u
                }).ToList();
            var ctl = new ComboBoxSink().CreateControlForLookup(context, options);

            if (selecteduser != null)
            {
                var selectedop = options.FirstOrDefault(o => ((User) o.Value).UserId == selecteduser.UserId);
                ((ComboBox) ctl).SelectedValue = selectedop.Value;
            }
            return ctl;
        }
 public WarehouseMap(WarehouseContext dbcontext)
     : this()
 {
     _dbcontext = dbcontext;
 }
        public static TabItem CreateTabPageWithDBContext(string name, object title, bool isUnique, out WarehouseContext dbContext, FrameworkElement content = null)
        {
            var tab = CreateTabPage(name, title, isUnique, content);
            if (tab == null)
            {
                dbContext = null;
                return tab;
            }

            dbContext=new WarehouseContext();
            tab.Tag = dbContext;
            return tab;
        }