当前位置: 首页 > news >正文

WPFC#超市管理系统(4)入库管理

入库管理

    • 7. 商品入库管理
      • 7.2 入库实现显示名称、图片、单位
      • 7.3 界面设计
      • 7.3 功能实现


7. 商品入库管理

  • 数据库中StockRecord表需要增加商品出入库Type类型为nvarchar(50)
  • C#中的数据库重新同步StockRecord表
  • 在Entity→Model中新建枚举类型StockType
namespace 超市管理系统.Entity.Model
{public enum StockType{入库,出库}
}

7.2 入库实现显示名称、图片、单位

  • 由于StockRecord表内未设置商品名称,因此名称需要通过部分类实现。在Entity→Model中新建StockRecord:BaseModel
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内容并修改相应内容。
<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 调用。
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}
}

在这里插入图片描述

  • 备注:当前未实现删除入库数据时同步更新商品管理中的数量,等待后续处理。
http://www.lryc.cn/news/608012.html

相关文章:

  • STM32——启动过程浅析
  • Shell【脚本 02】离线安装配置Zookeeper及Kafka并添加service服务和开机启动(脚本分析)
  • Kubernetes Gateway API 详解:现代流量路由管理方案
  • Flink2.0学习笔记:Stream API 窗口
  • ubuntu 系统风扇控制软件 CoolerControl
  • 关于项目发布中到后半夜的一些总结
  • Maven - 并行安全无重复打包构建原理揭秘
  • 公网服务器上Nginx或者Openresty如何屏蔽IP直接扫描
  • 译|Netflix 技术博客:一个利用视觉-语言模型和主动学习高效构建视频分类器的框架
  • 初始C语言---第四讲(数组)
  • Python So Easy 大虫小呓三部曲 - 高阶篇
  • 【语音技术】什么是实体
  • appium中urllib3.exceptions.LocationValueError: No host specified. 的错误解决办法
  • cv快速input
  • InfluxDB 与 Node.js 框架:Express 集成方案(二)
  • SpringBoot与TurboGears2跨栈、整合AI服务、智能客服路由系统整合实战
  • 基于Redis自动过期的流处理暂停机制
  • dbt中多源数据的处理
  • 仿真电路:(十七下)DC-DC升压压电路原理简单仿真
  • Git下载及安装保姆级教程
  • 电子电气架构 --- 汽车网络安全概述
  • 深入 Go 底层原理(九):context 包的设计哲学与实现
  • 八股取士-go
  • python爬取豆瓣电影评论通用代码
  • Getedit-得辑SCI论文润色的重要性?
  • 自动驾驶:技术、应用与未来展望——从开创到全面革新交通出行
  • 【Linux系统】详解,进程控制
  • mongo,mongod,mongos指令
  • 【Linux】vim—基操
  • hcip---ospf知识点总结及实验配置