using IWareDataAccess.EF;
|
using IWareDataAccess.Entity.Alert;
|
using IWareDataAccess.Helper;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IWareDataAccess.Alert.ALERT
|
{
|
public static class AlertSqlFunc
|
{
|
/// <summary>
|
/// 搜索表
|
/// </summary>
|
/// <param name="keyValue"></param>
|
/// <param name="page"></param>
|
/// <returns></returns>
|
public static List<ALERT_ALERT> Search(AlertWebEntity webEntity, int page, int onePageNum)
|
{
|
using (Model edm = new Model())
|
{
|
var f = PredicateBuilder.True<ALERT_ALERT>();
|
|
//便利所有属性
|
Type type = webEntity.GetType();
|
foreach (var i in type.GetProperties())
|
{
|
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
|
if (i.PropertyType == typeof(String))
|
{
|
string value;
|
if (v != null)
|
{
|
value = v.ToString();
|
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
|
}
|
}
|
else if (i.PropertyType == typeof(DateTime?))
|
{
|
if (v != null)
|
{
|
DateTime value = DateTime.Parse(v.ToString());
|
if (i.Name == "createTimeStart")
|
{
|
f = f.And(x => x.CREATETIME > value);
|
}
|
if (i.Name == "createTimeEnd")
|
{
|
f = f.And(x => x.CREATETIME < value);
|
}
|
|
if (i.Name == "finishTimeStart")
|
{
|
f = f.And(x => x.FINISHTIME > value);
|
}
|
if (i.Name == "finishTimeEnd")
|
{
|
f = f.And(x => x.FINISHTIME < value);
|
}
|
}
|
}
|
else
|
{
|
if (v != null)
|
{
|
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
|
}
|
}
|
}
|
|
List<ALERT_ALERT> list = edm.ALERT_ALERT.OrderByDescending(x=>x.CREATETIME).Where(f.Compile()).Skip((page - 1) * onePageNum).Take(onePageNum).ToList();
|
|
return list;
|
}
|
}
|
|
/// <summary>
|
/// 搜索(视图)
|
/// </summary>
|
public static List<View_ALERT_ALERT> SearchView(AlertWebEntity webEntity, int page, int onePageNum,out string msg)
|
{
|
msg = "";
|
using (Model edm = new Model())
|
{
|
var f = PredicateBuilder.True<View_ALERT_ALERT>();
|
|
//便利所有属性
|
Type type = webEntity.GetType();
|
foreach (var i in type.GetProperties())
|
{
|
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
|
if (i.PropertyType == typeof(String))
|
{
|
string value;
|
if (v != null)
|
{
|
value = v.ToString();
|
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
|
}
|
}
|
else if (i.PropertyType == typeof(DateTime?))
|
{
|
if (v != null)
|
{
|
DateTime value = DateTime.Parse(v.ToString());
|
if (i.Name == "createTimeStart")
|
{
|
f = f.And(x => x.createTime > value);
|
}
|
if (i.Name == "createTimeEnd")
|
{
|
f = f.And(x => x.createTime < value);
|
}
|
|
if (i.Name == "finishTimeStart")
|
{
|
f = f.And(x => x.finishTime > value);
|
}
|
if (i.Name == "finishTimeEnd")
|
{
|
f = f.And(x => x.finishTime < value);
|
}
|
}
|
}
|
else
|
{
|
if (v != null)
|
{
|
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
|
}
|
}
|
}
|
|
List<View_ALERT_ALERT> list = edm.View_ALERT_ALERT.OrderByDescending(x=>x.createTime).Where(f.Compile()).Skip((page - 1) * onePageNum).Take(onePageNum).ToList();
|
|
return list;
|
}
|
}
|
|
/// <summary>
|
/// 搜索数量(视图)
|
/// </summary>
|
public static int SearchViewNum(AlertWebEntity webEntity)
|
{
|
using (Model edm = new Model())
|
{
|
var f = PredicateBuilder.True<View_ALERT_ALERT>();
|
|
//便利所有属性
|
Type type = webEntity.GetType();
|
foreach (var i in type.GetProperties())
|
{
|
object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
|
if (i.PropertyType == typeof(String))
|
{
|
string value;
|
if (v != null)
|
{
|
value = v.ToString();
|
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
|
}
|
}
|
else if (i.PropertyType == typeof(DateTime?))
|
{
|
if (v != null)
|
{
|
DateTime value = DateTime.Parse(v.ToString());
|
if (i.Name == "createTimeStart")
|
{
|
f = f.And(x => x.createTime > value);
|
}
|
if (i.Name == "createTimeEnd")
|
{
|
f = f.And(x => x.createTime < value);
|
}
|
|
if (i.Name == "finishTimeStart")
|
{
|
f = f.And(x => x.finishTime > value);
|
}
|
if (i.Name == "finishTimeEnd")
|
{
|
f = f.And(x => x.finishTime < value);
|
}
|
}
|
}
|
else
|
{
|
if (v != null)
|
{
|
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
|
}
|
}
|
}
|
|
int num = edm.View_ALERT_ALERT.Where(f.Compile()).Count();
|
|
return num;
|
}
|
}
|
|
/// <summary>
|
/// 添加修改
|
/// </summary>
|
/// <returns></returns>
|
public static bool AddOrUpdate(AlertWebEntity webAlert, out string msg)
|
{
|
msg = "";
|
using (Model edm = new Model())
|
{
|
//查询是否已存在此故障
|
ALERT_ALERT alert = edm.ALERT_ALERT.FirstOrDefault(x => x.ALERTCODE == webAlert.alertCode);
|
if (alert == null)
|
{
|
//无此故障,属于新建
|
alert = new ALERT_ALERT();
|
alert.CREATETIME = DateTime.Now;
|
alert.ALERTCODE = webAlert.alertCode ?? alert.ALERTCODE;
|
}
|
else
|
{
|
alert.FINISHTIME = DateTime.Now;
|
|
System.TimeSpan time = alert.FINISHTIME.Value - alert.CREATETIME.Value; //两个时间相减 。默认得到的是 两个时间之间的天数 得到:365.00:00:00
|
double getMinutes = time.TotalMinutes; //将这个天数转换成小时, 返回值是double类型的
|
alert.TOTALTIME = (int)getMinutes;
|
}
|
alert.ALERTNAME = webAlert.alertName?? alert.ALERTNAME;
|
alert.ALERTDES = webAlert.alertDes ?? alert.ALERTDES;
|
//绑定设备
|
BASE_DEVICE device = edm.BASE_DEVICE.FirstOrDefault(x => x.DEVICENAME == webAlert.deviceName);
|
if (device != null)
|
{
|
alert.BASE_DEVICE = device;
|
}
|
|
edm.ALERT_ALERT.AddOrUpdateExtension(alert);
|
if (edm.SaveChanges() > 0)
|
{
|
return true;
|
}
|
else
|
{
|
msg = "修改失败";
|
return false;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 删除
|
/// </summary>
|
/// <param name="webUser"></param>
|
/// <returns></returns>
|
public static bool Delete(AlertWebEntity webUser, out string msg)
|
{
|
msg = "";
|
using (Model edm = new Model())
|
{
|
//查询是否已存在此用户
|
ALERT_ALERT alert = edm.ALERT_ALERT.FirstOrDefault(x => x.ALERTCODE == webUser.alertCode);
|
if (alert == null)
|
{
|
//无此用户,错误
|
msg = "无此故障";
|
return false;
|
}
|
else
|
{
|
edm.ALERT_ALERT.Remove(alert);
|
if (edm.SaveChanges() > 0)
|
{
|
return true;
|
}
|
else
|
{
|
msg = "保存失败";
|
return false;
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 获取组盘号
|
/// </summary>
|
/// <returns></returns>
|
public static string GetCode()
|
{
|
using (Model edm = new Model())
|
{
|
string month = DateTime.Now.Month.ToString("00");
|
string day = DateTime.Now.Day.ToString("00");
|
string hour = DateTime.Now.Hour.ToString("00");
|
string minute = DateTime.Now.Minute.ToString("00");
|
string second = DateTime.Now.Second.ToString("00");
|
string millSecond = DateTime.Now.Millisecond.ToString("0000");
|
string time = DateTime.Now.Year.ToString() + month + day + hour + minute + second + millSecond;
|
return "AL" + time;
|
}
|
}
|
|
/// <summary>
|
/// 获取所有设备报警信息
|
/// </summary>
|
/// <param name="model"></param>
|
/// <returns></returns>
|
public static List<View_ALERT_ALERT> GetAllAlarmInfo(AlertWebEntity model)
|
{
|
|
using (Model edm = new Model())
|
{
|
var f = PredicateBuilder.True<View_ALERT_ALERT>();
|
|
//便利所有属性
|
Type type = model.GetType();
|
foreach (var i in type.GetProperties())
|
{
|
object v = Helper.Helper.GetFieldValueByName(model, i.Name);
|
if (i.PropertyType == typeof(String))
|
{
|
string value;
|
if (v != null)
|
{
|
value = v.ToString();
|
f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
|
}
|
}
|
else if (i.PropertyType == typeof(DateTime?))
|
{
|
if (v != null)
|
{
|
DateTime value = DateTime.Parse(v.ToString());
|
if (i.Name == "createTimeStart")
|
{
|
f = f.And(x => x.createTime > value);
|
}
|
if (i.Name == "createTimeEnd")
|
{
|
f = f.And(x => x.createTime < value);
|
}
|
|
if (i.Name == "finishTimeStart")
|
{
|
f = f.And(x => x.finishTime > value);
|
}
|
if (i.Name == "finishTimeEnd")
|
{
|
f = f.And(x => x.finishTime < value);
|
}
|
}
|
}
|
else
|
{
|
if (v != null)
|
{
|
f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
|
}
|
}
|
}
|
|
List<View_ALERT_ALERT> list = edm.View_ALERT_ALERT.OrderByDescending(x => x.createTime).Where(f.Compile()).ToList();
|
|
return list;
|
}
|
}
|
}
|
}
|