基础资料是ERP中一个模块。主要功能是相当于一个数据字典。最近在搭建一个简单的ERP的框架。需要将基础资料与单据的模板抽象出来,做成通用模板,以后需要新增基础资料和单据时,只需要继承相应的模板,增删改查,审核、弃审、打印等都不需要再写。
这篇文章主要介绍基础资料的模板搭建。
我们先建一个ViewModelBase,添加一些常用的工具栏命令。
public partial class ViewModelBase : INotifyPropertyChanged
{
public ViewModelBase()
{
CloseCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteClose);
PrintCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecutePrint, CanExecutePrint);
SaveCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteSave, CanExecuteSave);
DeleteCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteDelete, CanExecuteDelete);
DetailCommand = new DelegateCommand(ExecuteDetail);
NavigateCommand = new DelegateCommand<string>(ExecuteNavigate);
ShowHandInputCommand = new DelegateCommand(ExecuteShowHandInput);
AddCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteAdd);
ModifyCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteModify);
ListCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteList);
MouseDoubleClickCommand = new DelegateCommand(ExecuteMouseDoubleClick);
ReferBDCommand = new DelegateCommand(ExecuteReferBD);
ExitCommand = new DelegateCommand(ExecuteExit);
AuditCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteAudit, CanExecuteAudit);
UnAuditCommand = new DelegateCommand<T_DATAFLOWSUBOPER>(ExecuteUnAudit, CanExecuteUnAduti);
} #region 【命令】
public DelegateCommand<T_DATAFLOWSUBOPER> AddCommand { get; set; }
/// <summary>
/// 保存命令
/// </summary>
public DelegateCommand<T_DATAFLOWSUBOPER> SaveCommand { get; set; }
/// <summary>
/// 审核
/// </summary>
public DelegateCommand<T_DATAFLOWSUBOPER> AuditCommand { get; set; }
/// <summary>
/// 反审核
/// </summary>
public DelegateCommand<T_DATAFLOWSUBOPER> UnAuditCommand { get; set; }
/// <summary>
/// 打印命令
/// </summary>
public DelegateCommand<T_DATAFLOWSUBOPER> PrintCommand { get; set; }
/// <summary>
/// 关闭命令
/// </summary>
public DelegateCommand<T_DATAFLOWSUBOPER> CloseCommand { get; set; }
/// <summary>
/// 详情命令
/// </summary>
public DelegateCommand DetailCommand { get; set; }
/// <summary>
/// 删除命令
/// </summary>
public DelegateCommand<T_DATAFLOWSUBOPER> DeleteCommand { get; set; }
/// <summary>
/// 导航命令
/// </summary>
public DelegateCommand<string> NavigateCommand { get; set; }
/// <summary>
/// 手写命令
/// </summary>
public DelegateCommand ShowHandInputCommand { get; set; }
public DelegateCommand<T_DATAFLOWSUBOPER> ModifyCommand { get; set; }
public DelegateCommand<T_DATAFLOWSUBOPER> ListCommand { get; set; }
public DelegateCommand MouseDoubleClickCommand { get; set; }
public DelegateCommand ReferBDCommand { get; set; }
public DelegateCommand ExitCommand { get; set; }
#endregion
#region 【执行命令】
public virtual void ExecuteSave(T_DATAFLOWSUBOPER para)
{
MessageService.Show("Save");
}
public virtual void ExecutePrint(T_DATAFLOWSUBOPER para)
{
MessageService.Show("Print");
}
public virtual void ExecuteClose(T_DATAFLOWSUBOPER para)
{
Messager.Messager.Default.Send("CloseChildWindow");
}
public virtual void ExecuteDetail()
{
throw new NotImplementedException();
}
public virtual void ExecuteDelete(T_DATAFLOWSUBOPER para)
{
MessageService.Show("Delete");
}
public virtual void ExecuteNavigate(string obj)
{
throw new NotImplementedException();
}
public virtual void ExecuteShowHandInput()
{
GlobalMethod.CallHandWriting();
}
public virtual void ExecuteAdd(T_DATAFLOWSUBOPER para)
{
MessageService.Show("Add");
}
public virtual void ExecuteModify(T_DATAFLOWSUBOPER para)
{
}
public virtual void ExecuteList(T_DATAFLOWSUBOPER para)
{
}
public virtual void ExecuteMouseDoubleClick()
{
}
public virtual void ExecuteReferBD()
{
GlobalVariableBusiness.BaseData.ReferBaseData();
}
public virtual void ExecuteExit()
{
GlobalMethod.ApplicationExit();
}
public virtual void ExecuteAudit(T_DATAFLOWSUBOPER para)
{
}
public virtual void ExecuteUnAudit(T_DATAFLOWSUBOPER para)
{
}
#endregion
#region 【命令逻辑】
public virtual bool CanExecuteSave(T_DATAFLOWSUBOPER para)
{
return true;
}
public virtual bool CanExecutePrint(T_DATAFLOWSUBOPER para)
{
return true;
}
protected virtual bool CanExecuteUnAduti(T_DATAFLOWSUBOPER arg)
{
return true;
}
protected virtual bool CanExecuteAudit(T_DATAFLOWSUBOPER arg)
{
return true;
}
protected virtual bool CanExecuteDelete(T_DATAFLOWSUBOPER arg)
{
return true;
}
#endregion
#region 【实现接口】
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
#endregion
}
现在我们增加基础资料的通用ViewModel,继承自ViewModelBase,并重载命令的具体实现。
public class BDViewModelBase<T>:ViewModelBase
{
private T _Master;
public T Master
{
get { return _Master; }
set
{
_Master = value;
RaisePropertyChanged("Master");
if (Master != null)
{
SetAuditImageShowStatus(Master.GetPropValue("FSTATUS"));
}
}
}
private List<T> _Detail;
public List<T> Detail
{
get { return _Detail; }
set { _Detail = value; RaisePropertyChanged("Detail"); }
}
private T _SelectedData;
public T SelectedData
{
get { return _SelectedData; }
set
{
_SelectedData = value;
RaisePropertyChanged("SelectedData");
if (value != null)
{
Master = GlobalMethod.Clone(SelectedData);
}
}
}
#region 重载
public override bool CanExecuteSave(T_DATAFLOWSUBOPER para)
{
if (BillState != NFMES.Core.Type.BillStateType.Complete)
{
return true;
}
else
{
return false;
}
}
public override NFMES.Core.MVVM.ViewModelBase ReferData()
{
ReLoadData();
return base.ReferData();
}
public override void ReLoadData()
{
Master = Activator.CreateInstance<T>();
Detail = BaseData.GetBaseDataList<T>();
}
#region 工具栏操作
public override void ExecuteAdd(T_DATAFLOWSUBOPER para)
{
CanEdit = true;
Master = Activator.CreateInstance<T>();
BillState = NFMES.Core.Type.BillStateType.newbill;
}
public override void ExecuteModify(T_DATAFLOWSUBOPER para)
{
CanEdit = true;
BillState = NFMES.Core.Type.BillStateType.modifybill;
}
public override void ExecuteSave(T_DATAFLOWSUBOPER para)
{
if (GlobalVariableBusiness.VaridateObj.CheckBaseDataNumberOnly(Master.GetTypeName(), Master.GetPropValue("FNUMBER").ToString())>0)
{
MessageService.Show("编号不可重复!");
return;
}
int result = -1;
switch (BillState)
{
case NFMES.Core.Type.BillStateType.newbill:
result = BaseData.SaveBaseData<T>(Master);
MessageService.Show(NFMES.Core.Type.OperateType.save, result);
break;
case NFMES.Core.Type.BillStateType.modifybill:
result = BaseData.UpdateBaseData<T>(Master);
MessageService.Show(NFMES.Core.Type.OperateType.update, result);
break;
}
if (result >= 0)
{
Detail = new List<T>(BaseData.GetBaseDataList<T>());
CanEdit = false;
BillState = NFMES.Core.Type.BillStateType.Complete;
}
}
public override void ExecuteDelete(T_DATAFLOWSUBOPER para)
{
if (GlobalVariableBusiness.VaridateObj.CheckBaseDataUsed(SubClass.FAddInKey, Convert.ToInt32(Master.GetPropValue("FID"))) == -1)
{
MessageService.Show("此条数据已被使用,不可删除!");
return;
}
int result = BaseData.DeleteBaseData<T>(Master);
MessageService.Show(NFMES.Core.Type.OperateType.delete, result);
if (result >= 0)
{
Master = Activator.CreateInstance<T>();
Detail = new List<T>(BaseData.GetBaseDataList<T>());
}
}
#endregion
#endregion
}
可以看到,我们的BDViewModelBase需要传递一个泛型,我们的模板类里,都是基于这个泛型来做增删改查的。
类中有三个属性,Master,Detail,SelectedData。MVVM模式,VM中的数据需要绑定到View中的。所以这三个数据,就是我们基础资料的数据源。后面加的所有基础资料View,绑定的数据源名称都是相同的。
Master是录入部分,Detail是列表部分,SelectedData是选中的列表行数据。
数据的DAL层用的Dapper框架,这个这边就不讨论了。我前面的文章有个对Dapper的封装的分享,也用在这个项目中了。
下面,我们基础资料的模板有了。以后变动的就是基础资料界面。如果基础资料的界面上的字段都一样,也可以做成通用的,不过很少。最常用的就是编码、名称字段。不过有时会关联其它基础资料。比如员工就需要关联角色。
下面我们看看一个标准的基础资料的View的写法。以部门资料为例:
<UserControl
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="MESClient.Unit.Depart.Depart"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<dxlc:LayoutControl VerticalAlignment="Top">
<dxlc:LayoutGroup Orientation="Vertical">
<dxlc:LayoutGroup>
<dxlc:LayoutItem Label="编号" Height="36" >
<dxe:TextEdit IsEnabled="{Binding CanEdit}" Style="{StaticResource TextEditNoBorderStyle}" Text="{Binding Master.FNUMBER,UpdateSourceTrigger=PropertyChanged}" />
</dxlc:LayoutItem>
<dxlc:LayoutItem Label="名称" Height="36" >
<dxe:TextEdit IsEnabled="{Binding CanEdit}" Style="{StaticResource TextEditNoBorderStyle}" Text="{Binding Master.FNAME,UpdateSourceTrigger=PropertyChanged}" />
</dxlc:LayoutItem>
</dxlc:LayoutGroup>
</dxlc:LayoutGroup>
</dxlc:LayoutControl>
<GridSplitter Style="{DynamicResource GridSplitterHStyle}"/>
<dxg:GridControl Style="{DynamicResource GridControlStyle}" Grid.Row="1" ItemsSource="{Binding Detail}" SelectedItem="{Binding SelectedData}">
<dxg:GridControl.Columns>
<dxg:GridColumn FieldName="FNUMBER" Header="编码"/>
<dxg:GridColumn FieldName="FNAME" Header="名称"/>
<dxg:GridColumn FieldName="FSTATUS" Header="单据状态">
<dxg:GridColumn.EditSettings>
<dxe:ComboBoxEditSettings ItemsSource="{Binding StatusTypeData.BillStatusList,Mode=OneWay}" ValueMember="Id" DisplayMember="Description"/>
</dxg:GridColumn.EditSettings>
</dxg:GridColumn>
</dxg:GridControl.Columns>
<dxg:GridControl.View>
<dxg:TableView AllowEditing="False" ShowGroupPanel="False"/>
</dxg:GridControl.View>
</dxg:GridControl>
</Grid>
</UserControl>
现在,我们不需要再为部门单独做ViewModel了,我们直接用通用的BDViewModelBase。
public partial class Depart : UserControl
{
public Depart()
: this(null)
{
}
public Depart(object para)
{
InitializeComponent();
this.DataContext = new BDViewModelBase<T_Department>();
}
}
T_Department是一个Model,与数据表的结构相同。
如果嫌写一个与表结构相同的Model麻烦,可以参考我前面的一篇文章,利用tt模板,自动将表结构生成Model实体。
如果我们需要增加一个职员的档案,界面如果和部门一样的话,那我们只需要改下this.DataContext的那行代码
this.DataContext = new BDViewModelBase<T_Employee>();
只需要换一下实体。
抽象单据的方法与此类似,只是数据源和保存删除,数据刷新会有不同。