Skip to content

Commit

Permalink
设备的增删修查
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbian committed Jul 18, 2016
1 parent 88a16ca commit dc7c2b5
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 57 deletions.
3 changes: 2 additions & 1 deletion AssetSystem/Adaptor/BaseAdaptor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AssetSystem.Models;
using AssetSystem.Library;
using AssetSystem.Models;

namespace AssetSystem.Adaptor
{
Expand Down
99 changes: 98 additions & 1 deletion AssetSystem/Adaptor/EquipmentAdaptor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects.DataClasses;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AssetSystem.Library;
using AssetSystem.Models;
using AssetSystem.Views;

namespace AssetSystem.Adaptor
{
Expand All @@ -12,6 +17,98 @@ public EquipmentAdaptor() :base()
{

}


/// <summary>
/// 添加一个设备
/// </summary>
/// <param name="title">设备的标题</param>
/// <param name="worth">设备的价格</param>
/// <param name="dateTime">设备的购买日期</param>
/// <param name="remark">备注</param>
/// <param name="equipmentTypeId">设备所属小类的Id</param>
/// <param name="adminId">当前管理员的Id</param>
/// <returns>
/// 0 : 输入的设备种类Id不合法,
/// 1 : 操作成功
/// </returns>
public int AddEquipment(string title,double worth,DateTime dateTime,string remark,int equipmentTypeId,int adminId)
{
var equipmentType = DbCtx.EquipmentTypes
.FirstOrDefault(et => et.Id == equipmentTypeId);
if (equipmentType == null || equipmentType.Type == 0)
{
return 0;
}
var admin = DbCtx.Admins
.FirstOrDefault(a => a.Id == adminId); //根据adminId得到admin,不能直接传进来admin给这个DbCtx用!!!
if (admin == null)
{
throw new MyException("此处需要有管理操作",-1);
}
string logicId = Util.GenerateIntId();
DbCtx.Equipments.Add(new Equipment()
{
LogicId = logicId,
Title = title,
Worth = worth,
PurchasingDate = dateTime,
Remark = remark,
State = 1, //默认正常
EquipmentType = equipmentType,
User = null,
Admin = admin
});
DbCtx.SaveChanges();
return 1;
}

/// <summary>
/// 删除一个设备
/// </summary>
/// <param name="Id">设备的Id</param>
/// <returns>
/// 0 : 对应Id的设备不存在
/// 1 : 操作成功
/// </returns>
public int DeleteEquipment(int Id)
{
var equipment = DbCtx.Equipments
.FirstOrDefault(e => e.Id == Id);
if (equipment == null) //此Id不存在
{
return 0; //返回0
}
DbCtx.Entry(equipment).State = EntityState.Deleted;
DbCtx.SaveChanges();
return 1;
}


/// <summary>
/// 根据Id返回对应设备
/// </summary>
/// <param name="Id"></param>
/// <returns>查找到的设备,可为空</returns>
public Equipment GetEquipmentById(int Id)
{
return DbCtx.Equipments
.FirstOrDefault(e => e.Id == Id);
}

public int UpdateEquipment(Equipment equipment)
{
DbCtx.Entry(equipment).State = EntityState.Modified;
DbCtx.SaveChanges();
return 1;
}

/// <summary>
/// 得到数据库中的所有的设备
/// </summary>
/// <returns>List型设备数据</returns>
public List<Equipment> GetAllEquipments()
{
return DbCtx.Equipments.ToList();
}
}
}
19 changes: 18 additions & 1 deletion AssetSystem/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -12,6 +13,18 @@ namespace AssetSystem.Controllers
{
class AdminController : BaseController
{
private static Admin _admin;

public void Login(Admin admin)
{
_admin = admin;
}

public Admin GetCurrentAdmin()
{
return _admin;
}

public AdminController() : base()
{
AdminViews = AdminViews ?? new AdminViews();
Expand All @@ -21,6 +34,8 @@ public AdminController() : base()
public AdminAdaptor AdminAdaptor;
public AdminViews AdminViews;

//todo : 管理员存储放在这里还是崩溃

//get Auth
public void Auth()
{
Expand All @@ -31,6 +46,7 @@ public void Auth()
AdminViews.ShowLoginError();
Auth();
}
Login(admin); //管理登录,上下文保存当前登录的管理员的信息
Choose();
}

Expand All @@ -39,7 +55,7 @@ public void Auth()
/// </summary>
public void Choose()
{
int op = AdminViews.ShowChoose();
int op = AdminViews.ShowChoose(GetCurrentAdmin());
switch (op)
{
#region 调用设备种类Controller对设备种类进行管理
Expand All @@ -49,6 +65,7 @@ public void Choose()
#endregion
#region 调用设备Controller对设备进行管理
case (int)ChooseOptions.EquipmentCtrl:
CtrlCtx.GetEquipmentController().EquipmentCtrl();
break;
#endregion
#region 调用用户Controller对用户进行管理
Expand Down
4 changes: 3 additions & 1 deletion AssetSystem/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using AssetSystem.Library;
using AssetSystem.Models;

namespace AssetSystem.Controllers
{
class BaseController
{
public BaseController()
{
CtrlCtx = CtrlCtx ?? new ControllerCtx(); //防止创建多个上下文对象造成内存泄漏
CtrlCtx = CtrlCtx ?? new ControllerCtx(); //todo 这里对象的潜拷贝有问题,无法复制_admin,而一元运算符=又不能重载
}

protected ControllerCtx CtrlCtx;
Expand Down
15 changes: 15 additions & 0 deletions AssetSystem/Controllers/ControllerCtx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AssetSystem.Models;

namespace AssetSystem.Controllers
{
Expand All @@ -12,8 +13,12 @@ public ControllerCtx()
{
}



private AdminController _adminController;
private EquipmentTypeController _equipmentTypeController;
private EquipmentController _equipmentController;
private UserController _userController;

public AdminController GetAdminController()
{
Expand All @@ -24,6 +29,16 @@ public EquipmentTypeController GetEquipmentTypeController()
{
return _equipmentTypeController ?? (_equipmentTypeController = new EquipmentTypeController());
}

public EquipmentController GetEquipmentController()
{
return _equipmentController ?? (_equipmentController = new EquipmentController());
}

public UserController GetUserController()
{
return _userController ?? (_userController = new UserController());
}
}

}
15 changes: 15 additions & 0 deletions AssetSystem/Controllers/Enum/EnumOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@

namespace AssetSystem.Controllers.Enum
{
/// <summary>
/// 设备种类操作的enum
/// </summary>
public enum EnumEquipmentTypeOperatorOptions
{
Add = 1,
Delete = 2,
PrintAll = 3,
Exit = 4
}

/// <summary>
/// 设备操作的enum
/// </summary>
public enum EnumEquipmentOpOptions
{
Add = 1,
Delete = 2,
Update = 3,
PrintAll = 4,
Exit = 5
}
}
110 changes: 110 additions & 0 deletions AssetSystem/Controllers/EquipmentController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System;
using System.Collections.Generic;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AssetSystem.Adaptor;
using AssetSystem.Controllers.Enum;
using AssetSystem.Library;
using AssetSystem.Models;
using AssetSystem.Views;

namespace AssetSystem.Controllers
Expand All @@ -18,5 +23,110 @@ public EquipmentController() : base()

public EquipmentViews EquipmentViews;
public EquipmentAdaptor EquipmentAdaptor;

/// <summary>
/// 处理用户的对设备的操作并分发
/// </summary>
public void EquipmentCtrl()
{
int op = EquipmentViews.EquipmentCtrl();
switch (op)
{
case (int)EnumEquipmentOpOptions.Add: //添加一个设备
AddEquipment();
break;
case (int)EnumEquipmentOpOptions.Delete: //删除某一设备
DeleteEquipment();
break;
case (int)EnumEquipmentOpOptions.Update: //修改某一设备
UpdateEquipment();
break;
case (int)EnumEquipmentOpOptions.PrintAll: //打印所有设备数据
PrintAllEquipment();
break;
case (int)EnumEquipmentOpOptions.Exit: //退出,函数返回
return;
default:
EquipmentViews.OperatorFail(); //打印操作失败
break;
}
EquipmentCtrl(); //持续调用自身
}

/// <summary>
/// 添加一个设备
/// </summary>
protected void AddEquipment()
{
CtrlCtx
.GetEquipmentTypeController()
.PrintAllEquipmentType(); //调用设备种类Controller的公共方法打印所有的设备种类

Dictionary<int, string> dictionary = EquipmentViews.AddEquipement(); //调用view里面的添加设备方法得到用户输入
string title = dictionary[0]; //设备标题
double worth = Convert.ToDouble(dictionary[1]); //设备价格
DateTime dateTime = Util.StringToDateTime(dictionary[2]); //设备购买日期
string remark = dictionary[3]; //备注
int equipmentTypeId = Convert.ToInt32(dictionary[4]); //设备所属小类的Id
Admin admin = CtrlCtx.GetAdminController().GetCurrentAdmin();
int result = EquipmentAdaptor
.AddEquipment(title, worth, dateTime, remark, equipmentTypeId,
admin.Id); //调用数据库方法添加设备
if (result == 0) //因为设备种类Id不合法而失败
{
EquipmentViews.OperatorFail(); //打印失败
}else if (result == 1) //操作成功
{
EquipmentViews.OperatorSuccess(); //打印成功
}
else
{
throw new MyException("不应有其他返回值",3);
}
}

/// <summary>
/// 删除一个设备
/// </summary>
protected void DeleteEquipment()
{
PrintAllEquipment(); //打印所有的设备列表
int Id = EquipmentViews.DeleteEquipment();
int result = EquipmentAdaptor.DeleteEquipment(Id);
if (result == 0)
{
EquipmentViews.OperatorFail();
}else if (result == 1)
{
EquipmentViews.OperatorSuccess();
}
else
{
throw new MyException("不应有其他的返回结果",0);
}
}

/// <summary>
/// 开始修改一个设备
/// </summary>
protected void UpdateEquipment()
{
PrintAllEquipment();
int Id = EquipmentViews.UpdateEquipment(); //调用视图获取用户要修改的设备的Id
Equipment equipment = EquipmentAdaptor.GetEquipmentById(Id); //通过Id得到设备
equipment = EquipmentViews.UpdateEquipementById(equipment); //调用视图获取用户的修改结果
int result = EquipmentAdaptor.UpdateEquipment(equipment); //更新设备
EquipmentViews.OperatorSuccess(); //显示操作成功
}


/// <summary>
/// 打印所有设备
/// </summary>
protected void PrintAllEquipment()
{
List<Equipment> equipments = EquipmentAdaptor.GetAllEquipments(); //得到所有的设备数据
EquipmentViews.PrintAllEquipments(equipments);
}
}
}
2 changes: 1 addition & 1 deletion AssetSystem/Controllers/EquipmentTypeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void DeleteEquipmentType()
}
}

protected void PrintAllEquipmentType()
public void PrintAllEquipmentType()
{
EquipmentTypeViews
.PrintAllEquipmentType(EquipmentTypeAdaptor.GetAllEquipmentTypes());//打印所有的设备种类
Expand Down
Loading

0 comments on commit dc7c2b5

Please sign in to comment.