[Route("/[controller]/[action]")]
[ApiController]
public class CabinetController : ControllerBase
{
private static IFreeSql freeSqlInstance = new FreeSqlBuilder().UseConnectionString(DataType.SqlServer, "Data Source=.;Initial Catalog=db_test;Persist Security Info=True;uid=sa;pwd=123456;").UseAutoSyncStructure(true).Build();
public CabinetController()
{
}
#region 获取柜子所有数据
[HttpGet]
public ActionResult<string> GetAllCabinetsByJson()
{
List<Cabinet> cabinetList = new List<Cabinet>();
cabinetList = freeSqlInstance.Select<Cabinet>().OrderByDescending(a => a.Name).ToList();
string json = JsonConvert.SerializeObject(cabinetList);
return json;
}
#endregion
#region 根据柜号、柜名、层数、通道号,查找柜子的信息
[HttpPost]
public ActionResult<int> SelectCabinet(Cabinet cabinet)
{
Cabinet cabinetSelect = freeSqlInstance.Select<Cabinet>().Where(a => a.Num == cabinet.Num && a.Floor == cabinet.Floor && a.Aisle == cabinet.Aisle).ToOne();
if (cabinetSelect == null)
{
return 0;
}
return 1;
}
#endregion
#region 添加一条柜子信息
[HttpPost]
public ActionResult<int> AddCabinet(Cabinet cabinet)
{
Cabinet cabinets = cabinet;
// 当柜名、柜号不存在,或者柜名、柜号一致时
if (this.JudgeNumAndName(cabinets.Num, cabinets.Name) == 0)
{
if (this.Select(cabinets.Num, cabinets.Floor, cabinets.Aisle) > 0)
{
// "该柜号-柜名-层数-通道号被占用!"
return 1;
}
freeSqlInstance.Insert(cabinets).ExecuteAffrows();
return 0;
}
// 柜号-柜名不一致!
return 2;
}
#endregion
#region 数据内部查询
public int Select(int cabinetNum, int cabinetFloor, int cabinetAisle)
{
Cabinet cabinetSelect = freeSqlInstance.Select<Cabinet>().Where(a => a.Num == cabinetNum && a.Floor == cabinetFloor && a.Aisle == cabinetAisle).ToOne();
if (cabinetSelect == null)
{
return 0;
}
return 1;
}
#endregion
#region 根据柜号、层数、通道号删除柜子信息
[HttpPost]
public ActionResult<int> DelCabinet(Cabinet cabinet)
{
var j = freeSqlInstance.Delete<Cabinet>()
.Where(k => k.Num == cabinet.Num && k.Floor == cabinet.Floor && k.Aisle == cabinet.Aisle).ExecuteAffrows();
if (j < 0)
{
return 1;
}
return 0;
}
#endregion
#region 判断柜号与柜名是否存在或者统一
public int JudgeNumAndName(int cabinetNum, string cabinetName)
{
List<Cabinet> cabinetList = new List<Cabinet>();
cabinetList = freeSqlInstance.Select<Cabinet>().Where(a => a.Num == cabinetNum || a.Name == cabinetName).ToList();
if (cabinetList.Count == 0)
{
// 表示不存在柜名和柜号,可直接添加
return 0;
}
else
{
var result1 = (from n in cabinetList
where n.Num == cabinetNum
select n).FirstOrDefault();
var result2 = (from n in cabinetList
where n.Name == cabinetName
select n).FirstOrDefault();
// 如果有一个不存在,则返回false
if (cabinetName.Equals(result1?.Name) || cabinetNum.Equals(result2?.Num))
{
return 0;
}
return 1;
}
//return cabinetList.Count;
}
#endregion
#region 修改数据
[HttpPost]
public ActionResult<int> UpDateCabinet(List<Cabinet> cabinetsList)
{
List<Cabinet> cabinetList = cabinetsList;
Cabinet cabinetOld = cabinetList[0];
Cabinet cabinetNew = cabinetList[1];
int result = freeSqlInstance.Update<Cabinet>(1).Set(a => new Cabinet { Num = cabinetNew.Num, Name = cabinetNew.Name, Floor = cabinetNew.Floor, Aisle = cabinetNew.Aisle })
//.Set(a => a.Num, cabinetNew.Num)
//.Set(a => a.Name, cabinetNew.Name)
//.Set(a => a.Floor, cabinetNew.Floor)
//.Set(a => a.Aisle, cabinetNew.Aisle)
.Where(a => a.Num == cabinetOld.Num && a.Floor == cabinetOld.Floor && a.Aisle == cabinetOld.Aisle).ExecuteAffrows();
return result;
}
#endregion
#region 多条数据删除
[HttpPost]
public int DeleteCabinets(List<Cabinet> cabinetsList)
{
if (cabinetsList == null)
{
return 0;
}
for (int i = 0; i < cabinetsList.Count; i++)
{
this.DelCabinet(cabinetsList[i]);
}
return 1;
}
#endregion
}
标签:WebApi,Aisle,return,Floor,WebApiApplication,Num,操作,cabinetList,public From: https://www.cnblogs.com/wen-chen/p/17520658.html