三层架构-庖丁解牛-05-餐桌管理

1 厅包管理

  1.1 Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaterModel
{
    /// <summary>
    /// HallInfo:实体类(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public partial class HallInfo
    {
        public HallInfo()
        {}
        #region Model
        private int _hid;
        private string _htitle;
        private bool _hisdelete;
        /// <summary>
        /// 
        /// </summary>
        public int HId
        {
            set{ _hid=value;}
            get{return _hid;}
        }
        /// <summary>
        /// 
        /// </summary>
        public string HTitle
        {
            set{ _htitle=value;}
            get{return _htitle;}
        }
        /// <summary>
        /// 
        /// </summary>
        public bool HIsDelete
        {
            set{ _hisdelete=value;}
            get{return _hisdelete;}
        }
        #endregion Model

    }
}
HallInfo

  1.2 UI

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CaterBLL;
using CaterModel;

namespace CaterUI
{
    public partial class FormHallInfo : Form
    {
        #region 00 设置公共变量
        //00-01 设置model对象
        private HallInfo hallModel = new HallInfo();

        //00-02 设置bll 对象
        private HallInfoBLL hallBll = new HallInfoBLL();

        //00-03 设置选中行的rowindex
        private int selectRowIndex = -1;

        //00-04 定义两个事件
        public event Action evenMakeSelect;

        public event Action<TableInfo> evenMakeGridView;
        //00-05 需要TableInfo的参数,所以定义一个TableInfo对象
        public TableInfo tableModel = new TableInfo();

        #endregion
        public FormHallInfo()
        {
            InitializeComponent();
        }

        #region 02 窗体加载事件
        private void FormHallInfo_Load(object sender, EventArgs e)
        {
            LoadView();
        }

        #endregion

        #region 03 GridView加载事件
        private void LoadView()
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = hallBll.GetHallInfoList();
        }
        #endregion

        #region 04 model对象实例化

        public void MakeHallInstance()
        {
            int hid;
            if (Int32.TryParse(txtId.Text, out hid))
            {
                hallModel.HId = hid;
            }
            hallModel.HTitle = txtTitle.Text;
        }

        #endregion

        #region 05 双击列事件
        private void dgvList_DoubleClick(object sender, EventArgs e)
        {
            //01 设置各种控件值
            btnSave.Text = "修改";
            txtId.Text = dgvList.SelectedRows[0].Cells["HId"].Value.ToString();
            txtTitle.Text = dgvList.SelectedRows[0].Cells["HTitle"].Value.ToString();
            selectRowIndex = dgvList.SelectedRows[0].Index;
        }
        #endregion

        #region 06 取消按钮触发事件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Recovery();
        }

        #endregion

        #region 07 页面复原事件
        private void Recovery()
        {
            txtId.Text = "添加时无编号";
            txtTitle.Text = string.Empty;
            btnSave.Text = "添加";
        }
        #endregion

        #region 08 保存按钮触发事件
        private void btnSave_Click(object sender, EventArgs e)
        {
            //08-01 model实例化
            MakeHallInstance();
            if (btnSave.Text == "添加")
            {
                if (hallBll.AddHallInfo(hallModel))
                {
                    MessageBox.Show("添加成功");
                    LoadView();
                    Recovery();
                }
                else
                {
                    MessageBox.Show("添加失败");
                }
            }
            else
            {
                if (hallBll.ModifyHallInfo(hallModel))
                {
                    MessageBox.Show("修改成功");
                    LoadView();
                    Recovery();
                    if (selectRowIndex > -1)
                    {
                        dgvList.Rows[selectRowIndex].Selected = true;
                    }
                }
                else
                {
                    MessageBox.Show("修改失败");
                }
            }
            //调用事件,通过事件来实现窗体传值
            evenMakeGridView(tableModel);
            evenMakeSelect();
        }
        #endregion

        #region 09 删除按钮触发事件
        private void btnRemove_Click(object sender, EventArgs e)
        {
            hallModel.HId = Convert.ToInt32(dgvList.Rows[0].Cells["HId"].Value.ToString());
            DialogResult result = MessageBox.Show("确认要删除选定行吗?", "提示", MessageBoxButtons.OKCancel);
            if (result == DialogResult.Cancel)
            {
                return;
            }
            if (hallBll.RemoveHallInfo(hallModel))
            {
                MessageBox.Show("删除成功");
                LoadView();
                Recovery();
            }
            else
            {
                MessageBox.Show("删除失败");
            }
            evenMakeGridView(tableModel);
            evenMakeSelect();
        }
        #endregion
    }
}
FormHallInfo

  1.3 BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CaterDAL;
using CaterModel;

namespace CaterBLL
{
   public  partial class HallInfoBLL
    {
        #region 00 定义全局变量
       //00-01 定义hallDal对象
        private HallInfoDAL hallDal = new HallInfoDAL();
        #endregion
        #region 01 获取HallInfo列表
        public List<HallInfo> GetHallInfoList()
        {
            return hallDal.GetHallInfoList();
        } 
        #endregion

        #region 02 添加HallInfo信息
        public bool AddHallInfo(HallInfo hallModel)
        {
            return hallDal.AddHallInfo(hallModel)>0;
        } 
        #endregion

        #region 03 修改HallInfo信息
        public bool ModifyHallInfo(HallInfo hallModel)
        {
            return hallDal.UpdateHallInfo(hallModel)>0;

        } 
        #endregion

        #region 04 删除HallInfo信息
        public bool RemoveHallInfo(HallInfo hallModel)
        {
            return hallDal.DeleteHallInfo(hallModel) > 0;

        } 
        #endregion
    }
}
HallInfoBLL

  1.4 DAL

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CaterModel;

namespace CaterDAL
{
  public partial  class HallInfoDAL
    {
        #region 01 获取厅包的列表信息
        public List<HallInfo> GetHallInfoList()
        {
           //01-01 定义厅包列表
            List<HallInfo> hallList = new List<HallInfo>( );
            //01-02 sql语句
            string strSql = "select * from HallInfo where HIsDelete = 0;";
            //01-03 执行SQL语句
            DataTable dt = SqliteHelper.ExcuteTable(strSql);
            if (dt.Rows.Count>0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    hallList.Add(new HallInfo()
                    {
                        HId = Convert.ToInt32(dr["HId"].ToString()),
                        HTitle =  dr["HTitle"].ToString()
                    });
                }
            }
            return hallList;
        } 
        #endregion

        #region 02 添加HallInfo
        public int AddHallInfo(HallInfo hallModel)
        {
            string strSql = "insert into HallInfo (HTitle,HIsDelete) values (@HTitle,0);";
           
            return SqliteHelper.ExcuteNonQuery(strSql, new SQLiteParameter("@HTitle", hallModel.HTitle));
        } 
        #endregion

        #region 03 修改厅包信息
        public int UpdateHallInfo(HallInfo hallModel)
        {
            string strSql = "Update  HallInfo set HTitle = @HTitle where HId=@HId ;";
            SQLiteParameter[] ps =
            {
                new SQLiteParameter( "@HId",hallModel.HId),
                new SQLiteParameter( "@HTitle",hallModel.HTitle)
            };
            return SqliteHelper.ExcuteNonQuery(strSql, ps);
        } 
        #endregion

        #region 04 更新厅包信息
        public int DeleteHallInfo(HallInfo hallModel)
        {
            string strSql = "Update  HallInfo set HIsDelete = 1 where HId=@HId ;";
           
            return SqliteHelper.ExcuteNonQuery(strSql,  new SQLiteParameter( "@HId",hallModel.HId));
        } 
        #endregion
    }
}
GetHallInfoList

2 餐桌管理

  2.1 Model  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaterModel
{
    /// <summary>
    /// TableInfo:实体类(属性说明自动提取数据库字段的描述信息)
    /// </summary>
    [Serializable]
    public partial class TableInfo
    {
        public TableInfo()
        {}
        #region Model
        private int _tid;
        private string _ttitle;
        private int? _thallid;
        private bool? _tisfree;
        private bool _tisdelete;
        /// <summary>
        /// 
        /// </summary>
        public int TId
        {
            set{ _tid=value;}
            get{return _tid;}
        }
        /// <summary>
        /// 
        /// </summary>
        public string TTitle
        {
            set{ _ttitle=value;}
            get{return _ttitle;}
        }
        /// <summary>
        /// 
        /// </summary>
        public int? THallId
        {
            set{ _thallid=value;}
            get{return _thallid;}
        }
        /// <summary>
        /// 
        /// </summary>
        public bool? TIsFree
        {
            set{ _tisfree=value;}
            get{return _tisfree;}
        }
        /// <summary>
        /// 
        /// </summary>
        public bool TIsDelete
        {
            set{ _tisdelete=value;}
            get{return _tisdelete;}
        }
        #endregion Model

    }
}
TableInfo-01
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaterModel
{
   public partial class MemberInfo{
       public string MTypeTitle { get; set; }
    }
    public partial class DishInfo
    {
        public string DTypeTitle { get; set; }
    }
    public partial class TableInfo
    {
        public string THallTitle { get; set; }
    }
    
}
TableInfo-02

  2.2 UI

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CaterBLL;
using CaterModel;

namespace CaterUI
{
    public partial class FormTableInfo : Form
    {
        #region 00 定义全局变量(注意初始化方式)

        //00-01 定义Model变量
        private TableInfo tableModel;

        //00-01 定义BLL变量
        private TableInfoBLL tableBll;

        //00-02 定义BLL变量
        private HallInfoBLL hallBll;

        #endregion

        #region 01 设置单例模式

        //01-01 构造函数私有化
        private FormTableInfo()
        {
            InitializeComponent();
            //变量初识化
            tableModel = new TableInfo();
            tableBll = new TableInfoBLL();
            hallBll = new HallInfoBLL();
        }

        //01-02 定义对象
        private static FormTableInfo _form;

        //01-03 定义form对象创建方法
        public static FormTableInfo MakeFormTableInfo()
        {
            if (_form == null)
            {
                _form = new FormTableInfo();
            }
            return _form;

        }

        //01-04 记得窗体关闭释放对象
        private void FormTableInfo_FormClosed(object sender, FormClosedEventArgs e)
        {
            _form = null;
        }

        #endregion

        #region 02 窗体加载

        private void FormTableInfo_Load(object sender, EventArgs e)
        {
            MakeSelectEvaluate();
            tableModel.THallId = null;
            tableModel.TIsFree = null;
            LoadView(tableModel);
        }

        #endregion

        #region 03 实例化

        public void MakeTableModelInstance()
        {
            int tId;
            //01 编号
            if (Int32.TryParse(txtId.Text, out tId))
            {
                tableModel.TId = tId;
            }
            //02 名称
            tableModel.TTitle = txtTitle.Text;
            int tHId;
            //03 厅包
            if (Int32.TryParse(Convert.ToString(ddlHallAdd.SelectedValue), out tHId))
            {
                tableModel.THallId = tHId;
            }
            //04 是否使用
            tableModel.TIsFree = rbFree.Checked;
        }

        #endregion

        #region 04 下拉列表实赋值

        public void MakeSelectEvaluate()
        {
            if (ddlHallAdd.DataSource == null)
            {
                ddlHallAdd.DataSource = hallBll.GetHallInfoList();
                ddlHallAdd.DisplayMember = "HTitle";
                ddlHallAdd.ValueMember = "HId";
                ddlHallAdd.SelectedIndex = -1;
            }
            if (ddlHallSearch.DataSource == null)
            {
                ddlHallSearch.DataSource = hallBll.GetHallInfoList();
                ddlHallSearch.DisplayMember = "HTitle";
                ddlHallSearch.ValueMember = "HId";
                ddlHallSearch.SelectedIndex = -1;
            }
        }

        #endregion

        #region 05 加载GridView 

        public void LoadView(TableInfo tableModel)
        {
            //05-01 设置自动添加列为false
            dgvList.AutoGenerateColumns = false;
            //05-02 设置数据源
            dgvList.DataSource = tableBll.GetTableInfoList(tableModel);
            //05-03 数据格式化
        }

        #endregion

        #region 06 当"是否空闲"的下拉列表框发生变化时触发事件

        private void ddlFreeSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlFreeSearch.SelectedIndex.ToString() == "0")
            {
                tableModel.TIsFree = false;
            }
            else if (ddlFreeSearch.SelectedIndex.ToString() == "1")
            {
                tableModel.TIsFree = true;
            }
            LoadView(tableModel);
        }

        #endregion

        #region 07 当"厅包"下拉列表框发生变化时触发事件

        private void ddlHallSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            int THId;
            if (Int32.TryParse(Convert.ToString(ddlHallSearch.SelectedValue), out THId))
            {
                tableModel.THallId = THId;
            }
            LoadView(tableModel);
        }

        #endregion

        #region 08 显示全部

        private void btnSearchAll_Click(object sender, EventArgs e)
        {
            //01 设置页面显示效果
            ddlFreeSearch.SelectedIndex = -1;
            ddlHallSearch.SelectedIndex = -1;
            //02 设置model对象值
            tableModel.THallId = null;
            tableModel.TIsFree = null;
            LoadView(tableModel);
        }

        #endregion

        #region 09 数据格式化
        private void dgvList_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                e.Value = Convert.ToBoolean(e.Value) ? "" : "×";
            }
        } 
        #endregion

        #region 10 保存按钮触发事件
        private void btnSave_Click(object sender, EventArgs e)
        {
            //01 实例化
            MakeTableModelInstance();
            if (btnSave.Text == "添加")
            {
                if (tableBll.AddTableInfoList(tableModel))
                {
                    MessageBox.Show("添加成功!");
                    //加载数据
                    LoadView(tableModel);
                    //复原
                    Recovery();
                }
                else
                {
                    MessageBox.Show("添加失败!");
                }
            }
            else
            {
                if (tableBll.ModifyTableInfoList(tableModel))
                {
                    MessageBox.Show("修改成功!");
                    //加载数据
                    LoadView(tableModel);
                    //复原
                    Recovery();
                }
                else
                {
                    MessageBox.Show("修改失败!");
                }
            }
        } 
        #endregion

        #region 11取消按钮触发事件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Recovery();
        }

        #endregion

        #region 12取消方法
        private void Recovery()
        {
            //01编号
            txtId.Text = "添加是无编号";
            //02 名称
            txtTitle.Text = string.Empty;
            //03 厅包
            ddlHallAdd.SelectedIndex = -1;
            //04 是否使用
            rbFree.Checked = true;
            //05 保存按钮
            btnSave.Text = "添加";
        } 
        #endregion

        #region 13 双加按钮触发事件
        private void dgvList_DoubleClick(object sender, EventArgs e)
        {
            //01编号
            txtId.Text = dgvList.SelectedRows[0].Cells["TId"].Value.ToString();
            //02 名称
            txtTitle.Text = dgvList.SelectedRows[0].Cells["TTitle"].Value.ToString();
            //03 厅包
            ddlHallAdd.Text = dgvList.SelectedRows[0].Cells[2].Value.ToString();
            //04 是否使用
            if (dgvList.SelectedRows[0].Cells[3].Value.ToString() == "True")
            {
                rbFree.Checked = true;
            }
            else
            {
                rbUnFree.Checked = true;
            }

            //05 保存按钮
            btnSave.Text = "编辑";
            MakeTableModelInstance();
        } 
        #endregion

        #region 14删除按钮触发事件
        private void btnRemove_Click(object sender, EventArgs e)
        {
            tableModel.TId = Convert.ToInt32(dgvList.Rows[0].Cells[0].Value.ToString());
            DialogResult result = MessageBox.Show("确定要删除当前选中的行吗?","提示",MessageBoxButtons.OKCancel);
            if (result==DialogResult.Cancel)
            {
                return;
            }
            if (tableBll.RemoveTableInfoList(tableModel))
            {
                MessageBox.Show("删除成功!");
                //01 设置页面显示效果
                ddlFreeSearch.SelectedIndex = -1;
                ddlHallSearch.SelectedIndex = -1;
                //02 设置model对象值
                tableModel.THallId = null;
                tableModel.TIsFree = null;
                LoadView(tableModel);
                //复原
                Recovery();
            }
            else
            {
                MessageBox.Show("删除失败!");
            }
        } 
        #endregion

        #region 15 单击"厅包管理"触发事件
        private void btnAddHall_Click(object sender, EventArgs e)
        {
            FormHallInfo form = new FormHallInfo();
            //执行完厅包管理后需要两个操作
            //由于俩个方法的参数不同,所以需要定义两个事件
            ddlHallAdd.DataSource = null;
            ddlHallSearch.DataSource = null;
            form.evenMakeSelect += MakeSelectEvaluate ; 
            form.evenMakeGridView += LoadView ;
           
            form.ShowDialog();
        } 
        #endregion
    }
}
FormTableInfo

  2.3 BLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CaterDAL;
using CaterModel;
namespace CaterBLL
{
   public  partial class TableInfoBLL
    { 
       #region 00 定义全局变量(注意初始化方式)
        //00-01 定义DAL变量
        private TableInfoDAL tableDal = new TableInfoDAL() ;
        #endregion
        #region 01 加载table列表
        public List<TableInfo> GetTableInfoList(TableInfo tableModel)
        {
           return tableDal.GetTableInfoList(tableModel);
        } 
        #endregion

        #region 02 添加数据
        public bool AddTableInfoList(TableInfo tableModel)
        {
            return tableDal.InsertTableInfoList(tableModel)>0;
        } 
        #endregion

        #region 03 修改数据
        public bool ModifyTableInfoList(TableInfo tableModel)
        {
            return tableDal.UpdateTableInfoList(tableModel) > 0;
        } 
        #endregion

        #region 04 删除数据
        public bool RemoveTableInfoList(TableInfo tableModel)
        {
            return tableDal.DeleteTableInfoList(tableModel) > 0;
        } 
        #endregion
    }
}
TableInfoBLL

  2.4 DAL

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CaterModel;

namespace CaterDAL
{
   public partial class TableInfoDAL
    {
        #region 01 获取TableInfo信息列表
        public List<TableInfo> GetTableInfoList(TableInfo tableModel)
        {
            //01-01 定义信息列表
            List<TableInfo>  tableList = new List<TableInfo>( );
            //01-02 定义参数
            List<SQLiteParameter> psList = new List<SQLiteParameter>( );
            //01-03 定义SQL语句
            string strSql = @"  select ti.*,hi.HTitle as THallTitle 
                                from TableInfo as ti,HallInfo as hi 
                                where ti.THallId = hi.HId 
                                and ti.TIsDelete = 0 ";
            if (tableModel.THallId != null)
            {
                strSql += " and ti.THallId = @THallId";
                psList.Add(new SQLiteParameter("@THallId", tableModel.THallId));
            }
            if (tableModel.TIsFree != null)
            {
                strSql += " and ti.TIsFree = @TIsFree";
                psList.Add(new SQLiteParameter("@TIsFree", tableModel.TIsFree));
            }
            DataTable dt = SqliteHelper.ExcuteTable(strSql,psList.ToArray());
            if (dt.Rows.Count>0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    tableList.Add(new TableInfo()
                    {
                        TId = Convert.ToInt32(dr["TId"].ToString()),
                        TTitle = dr["TTitle"].ToString(),
                        THallId = Convert.ToInt32(dr["THallId"].ToString()),
                        TIsFree = Convert.ToBoolean(dr["TIsFree"].ToString()),
                        THallTitle = dr["THallTitle"].ToString()
                    });
                }
            }
            return tableList;
        } 
        #endregion

        #region 02 添加信息
        public int InsertTableInfoList(TableInfo tableModel)
        {
            string strSql =@"insert into TableInfo (TTitle,THallId,TIsFree,TIsDelete) values (@TTitle,@THallId,@TIsFree,0);";
            List<SQLiteParameter> psList = new List<SQLiteParameter>( );
            psList.Add(new SQLiteParameter("@TTitle", tableModel.TTitle));
            psList.Add(new SQLiteParameter("@THallId", tableModel.THallId));
            psList.Add(new SQLiteParameter("@TIsFree", tableModel.TIsFree));
            return SqliteHelper.ExcuteNonQuery(strSql,psList.ToArray());
        } 
        #endregion

        #region 03 修改信息
        public int UpdateTableInfoList(TableInfo tableModel)
        {
            string strSql = @"update TableInfo set TTitle=@TTitle,THallId=@THallId,TIsFree=@TIsFree where TId=@TId;";
            List<SQLiteParameter> psList = new List<SQLiteParameter>();
            psList.Add(new SQLiteParameter("@TId", tableModel.TId));
            psList.Add(new SQLiteParameter("@TTitle", tableModel.TTitle));
            psList.Add(new SQLiteParameter("@THallId", tableModel.THallId));
            psList.Add(new SQLiteParameter("@TIsFree", tableModel.TIsFree));
            return SqliteHelper.ExcuteNonQuery(strSql, psList.ToArray());
        }
        
        #endregion

        #region 04删除代码
        public int DeleteTableInfoList(TableInfo tableModel)
        {
            string strSql = @"update TableInfo set TIsDelete = 1 where TId=@TId;";
            return SqliteHelper.ExcuteNonQuery(strSql, new SQLiteParameter("@TId", tableModel.TId));
        } 
        #endregion
    }
}
TableInfoDAL

3 运行效果

posted @ 2017-05-22 22:57  逍遥小天狼  阅读(144)  评论(0)    收藏  举报