using GDNXFD.Data.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GDNXFD.Data.Repositories
{
public class AdviceHistoryModelRepository
{
///
/// 查询计算结果历史数据
///
/// 查询条件,长度为6的数组,数组索引1=开始时间,2=结束时间,3=风场编号,4=风机编号,5=建议操作类型,6=实际操作类型
/// 页码
/// 分页尺寸
/// 总数量
///
public static IList GetHistoryList(string filter, int pageIndex, int pageSize, ref int total)
{
string[] conditionArr = filter.Split(',');
DateTime? startTime = null;
DateTime? endTime = null;
string stationId = "";
string windturbineId = "";
OperateStyle? adviceSendType = null;
OperateStyle? executeSendType = null;
if (!string.IsNullOrEmpty(conditionArr[0]))
startTime = DateTime.Parse(conditionArr[0]);
if (!string.IsNullOrEmpty(conditionArr[1]))
endTime = DateTime.Parse(conditionArr[1]);
if (!string.IsNullOrEmpty(conditionArr[2]))
stationId = conditionArr[2];
if (!string.IsNullOrEmpty(conditionArr[3]))
windturbineId = conditionArr[3];
if (!string.IsNullOrEmpty(conditionArr[4]))
adviceSendType = (OperateStyle)Convert.ToInt32(conditionArr[4]);
if (!string.IsNullOrEmpty(conditionArr[5]))
executeSendType = (OperateStyle)Convert.ToInt32(conditionArr[5]);
using (GDNXFDDbContext ctx = new GDNXFDDbContext())
{
// IList lst = ctx.AdviceHistoryModel.Select(s => s).ToList();
var r1 = ctx.AdviceHistoryModel.AsQueryable();
if (startTime.HasValue && endTime.HasValue)
r1 = r1.Where(s => s.LastUpdateTime > startTime && s.LastUpdateTime < endTime.Value);
if (!string.IsNullOrEmpty(stationId))
r1 = r1.Where(s => s.StationId == stationId);
if (!string.IsNullOrEmpty(windturbineId))
r1 = r1.Where(s => s.WindturbineId == windturbineId);
if (adviceSendType.HasValue)
r1 = r1.Where(s => s.AdviseOperation == adviceSendType.Value);
if (executeSendType.HasValue)
r1 = r1.Where(s => s.ExecuteOperation == executeSendType.Value);
total = r1.Count();
var results = r1.OrderByDescending(q => q.LastUpdateTime)
.Skip(--pageIndex * pageSize)
.Take(pageSize)
.ToList();
return results.ToList();
}
}
public static void InsertHistory(IList list)
{
using (GDNXFDDbContext ctx = new GDNXFDDbContext())
{
for (int i = 0; i < list.Count; i++)
{
ctx.AdviceHistoryModel.Add(list[i]);
ctx.SaveChanges();
}
}
}
///
/// 更新计算结果
///
/// 风机编号
/// 更新人,0为系统
/// 更新人姓名,0为系统
/// 动作类型,2用户执行,3用户取消,4系统取消
public static void UpdateHistory(string windturbineId, string userId, string userName, int userActive)
{
using (GDNXFDDbContext ctx = new GDNXFDDbContext())
{
//查找流水表中,风机编号,状态为未执行,实际执行时间为空的数据(理论上只有一条)
AdviceHistoryModel m = ctx.AdviceHistoryModel.Where(s => s.WindturbineId == windturbineId && s.Status == 1 && s.ExecuteTime == null).FirstOrDefault();
if (m == null)
{
return;
}
m.Status = userActive;
m.ExecuteTime = DateTime.Now;
m.Operater = userId;
m.OperaterName = userName;
if (userActive == 2)//如果是用户执行推荐动作
{
m.ExecuteOperation = m.AdviseOperation;
}
else // 系统取消,用户取消
{
m.ExecuteOperation = OperateStyle.Unknow;
}
ctx.Entry(m).State = System.Data.Entity.EntityState.Modified;
ctx.SaveChanges();
}
}
///
/// 将上次所有未执行的计算结果全部更新未系统取消
///
public static void UpdateAllCalcResult()
{
try
{
using (GDNXFDDbContext ctx = new GDNXFDDbContext())
{
string sql = String.Format("update wisdom_calc_history set status=4,executetime=sysdate,operater=0,operatername='系统' where status=1");
ctx.Database.ExecuteSqlCommand(sql, new object[] { });
}
}
catch (Exception ex)
{
Console.WriteLine("123");
}
}
}
}