入库管理
-
- [7. 商品入库管理](#7. 商品入库管理)
-
- [7.2 入库实现显示名称、图片、单位](#7.2 入库实现显示名称、图片、单位)
- [7.3 界面设计](#7.3 界面设计)
- [7.3 功能实现](#7.3 功能实现)
7. 商品入库管理
- 数据库中StockRecord表需要增加商品出入库Type类型为nvarchar(50)。
- C#中的数据库重新同步StockRecord表
- 在Entity→Model中新建枚举类型StockType
csharp
namespace 超市管理系统.Entity.Model
{
public enum StockType
{
入库,
出库
}
}
7.2 入库实现显示名称、图片、单位
- 由于StockRecord表内未设置商品名称,因此名称需要通过部分类实现。在Entity→Model中新建StockRecord:BaseModel。
csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using 超市管理系统.Entity.Model;
using 超市管理系统.Helper;
using 超市管理系统.ViewModel;
namespace 超市管理系统.Entity
{
public partial class StockRecord:BaseModel
{
public string ProductName
{
get
{
//ProductId为StockRecord表中的ProductId为StockRecord表中的
return ProductViewModel.productProvider.GetAll().FirstOrDefault(t => t.Id == ProductId).Name;
}
}
public BitmapImage BitmapImage
{
get
{
string image = ProductViewModel.productProvider.GetAll().FirstOrDefault(t => t.Id == ProductId).Image;
return ImageHelper.GetBitmapImage(image);
}
}
}
}
7.3 界面设计
- 已有UserControl文件InstorageView.xaml,复用ProductView.xaml内容并修改相应内容。
xml
<UserControl x:Class="超市管理系统.View.InstorageView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:超市管理系统.View" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Background="{Binding AppData.Background}"
DataContext="{Binding Source={StaticResource Locator}, Path=InstorageViewModel}"
d:DesignHeight="450" d:DesignWidth="800">
<i:Interaction.Triggers>
<i:EventTrigger EventName ="Loaded">
<i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="#22304B" BorderThickness="0 0 0 1">
<TextBlock Text="商品管理" VerticalAlignment="center" Margin="5 0 0 0" Foreground="{Binding AppData.Foreground}" FontSize="16"/>
</Border>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid>
<StackPanel Orientation="Horizontal" Margin="0 5 0 5">
<TextBlock Text="入库管理" VerticalAlignment="Center" Margin="5 0 5 0"/>
<ComboBox VerticalContentAlignment="Center" MinWidth="100" MaxWidth="200" Margin="5 0 5 0" Height="25"
ItemsSource="{Binding ProductList }"
SelectedItem="{Binding SelectedProduct}"
DisplayMemberPath="Name"/>
<TextBlock Text="入库数量" VerticalAlignment="Center" Margin="5 0 5 0"/>
<TextBox Text="{Binding Stock.Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Height="25" VerticalAlignment="Center" Width="50" Margin="5 0 5 0"/>
<Button Content="入库" Command="{Binding SaveCommand}" Width="80" Margin="0 0 10 0" Height="25"/>
</StackPanel>
</Grid>
<DataGrid Grid.Row="1" ItemsSource="{Binding StockRecordList}"
SelectedItem="{Binding SelectedStockRecord}"
Style="{StaticResource DataGridStyle}">
<DataGrid.Columns>
<!--普通写法-->
<!--<DataGridTextColumn Width="auto" Binding="{Binding Id}" Header="序号"/>
<DataGridTextColumn Width="auto" Binding="{Binding Name}" Header="姓名"/>
<DataGridTextColumn Width="auto" Binding="{Binding Telephone}" Header="电话"/>
<DataGridTextColumn Width="auto" Binding="{Binding Address}" Header="地址"/>-->
<!--数据模板写法-->
<DataGridTemplateColumn Width="auto" Header="序号">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Id,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="商品编号">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding ProductId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="商品名称">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding ProductName, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="商品图片">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Image Source="{Binding BitmapImage}" >
<Image.ToolTip>
<Grid>
<Image Source="{Binding BitmapImage}"/>
</Grid>
</Image.ToolTip>
</Image>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="数量">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="单位">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Unit, Mode=OneWay}" Style="{StaticResource DataGridTextBoxStyle}"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="类型">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}" HorizontalAlignment="Left"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="auto" Header="入库日期">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding StockDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource DataGridTextBoxStyle}" HorizontalAlignment="Left"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="0 5 5 5" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
<TextBlock Text="当前商品:" Margin="0 0 10 0" Foreground="White" Width="auto" />
<TextBlock Text="{Binding SelectedStockRecord.ProductName}" Foreground="White" Width="auto"/>
</StackPanel>
<StackPanel Grid.Column="1" Margin="0 5 5 5" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="删除" Command="{Binding DeleteCommand}" Margin="0 0 10 0" Width="80" Height="25"/>
</StackPanel>
</Grid>
</Grid>
</Grid>
</UserControl>
7.3 功能实现
- InstorageViewModel,需要设计商品列表和选择项,入库数量绑定StockRecord表中的Quantity。功能有入库和删除两个。
- 由于商品相关的ViewModel中private ProductProvider productProvider = new ProductProvider()为不同的实例,造成在入库后切换页面Load不会触发更新 。因此,将ProductViewModel创建的改为 public static ProductProvider productProvider = new ProductProvider();,其他相关ViewModel删除private ProductProvider productProvider = new ProductProvider(),采用ProductProvider .productProvider 调用。
csharp
using CommonServiceLocator;
using GalaSoft.MvvmLight.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using 超市管理系统.Entity;
using 超市管理系统.Entity.Model;
using 超市管理系统.View;
namespace 超市管理系统.ViewModel
{
public class InstorageViewModel :ViewModelBase2
{
private StockRecordProvider stockRecordProvider = new StockRecordProvider();
private List<Product> productList = new List<Product>();
public List<Product> ProductList
{
get { return productList; }
set
{
productList = value;
RaisePropertyChanged();
}
}
//当前选中的顾客实体
private Product selectedProduct = null;
public Product SelectedProduct
{
get { return selectedProduct; }
set
{
selectedProduct = value;
RaisePropertyChanged();
}
}
//选中删除的实体
private Product deleteProduct = null;
public Product DeleteProduct
{
get { return deleteProduct; }
set
{
deleteProduct = value;
RaisePropertyChanged();
}
}
//新增数量
private StockRecord stock;
public StockRecord Stock
{
get { return stock; }
set { stock = value; RaisePropertyChanged(); }
}
//所有入库记录
private List<StockRecord> stockRecordList = new List<StockRecord>();
public List<StockRecord> StockRecordList
{
get { return stockRecordList; }
set
{
stockRecordList = value;
RaisePropertyChanged();
}
}
//当前选中的入库记录
private StockRecord selectedStockRecord = null;
public StockRecord SelectedStockRecord
{
get { return selectedStockRecord; }
set
{
selectedStockRecord = value;
RaisePropertyChanged();
}
}
#region commands
/// <summary>
/// 加载所有供应商
/// </summary>
public RelayCommand<UserControl> LoadedCommand
{
get
{
return new RelayCommand<UserControl>((view) =>
{
ProductList = ProductViewModel.productProvider.GetAll();
StockRecordList = stockRecordProvider.GetAll();
Stock = new StockRecord() { Type = StockType.入库.ToString() };
});
}
}
public RelayCommand<UserControl> DeleteCommand
{
get
{
return new RelayCommand<UserControl>((view) =>
{
if (SelectedStockRecord != null)
{
var count = stockRecordProvider.Delete(SelectedStockRecord);
if (count > 0)
{
//商品数量同步变化
//DeleteProduct.Id = (int)SelectedStockRecord.ProductId;
//DeleteProduct.Quantity -= (double)SelectedStockRecord.Quantity;
//ProductViewModel.productProvider.Update(DeleteProduct);
//更新到商品属性中
ProductList = ProductViewModel.productProvider.GetAll();
MessageBox.Show("删除成功");
StockRecordList = stockRecordProvider.GetAll();
}
}
else
{ return; }
});
}
}
public RelayCommand<UserControl> SaveCommand
{
get
{
return new RelayCommand<UserControl>((view) =>
{
if (SelectedProduct == null)
{
MessageBox.Show("未选择商品");
return;
}
if (Stock.Quantity <= 0)
{
MessageBox.Show("入库商品应大于0!");
return;
}
Stock.ProductId = SelectedProduct.Id;
Stock.StockDate = DateTime.Now;
int count = stockRecordProvider.Insert(Stock);
if (count > 0)
{
//增加商品数量
SelectedProduct.Quantity += (double)Stock.Quantity;
//更新到商品属性中
ProductViewModel.productProvider.Update(selectedProduct);
//刷新界面
StockRecordList = stockRecordProvider.GetAll();
MessageBox.Show("保存成功");
//
Stock = new StockRecord() { Type = StockType.入库.ToString() };
}
});
}
}
#endregion
}
}

- 备注:当前未实现删除入库数据时同步更新商品管理中的数量,等待后续处理。