AdviceHistoryModelRepository.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using GDNXFD.Data.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace GDNXFD.Data.Repositories
  8. {
  9. public class AdviceHistoryModelRepository
  10. {
  11. /// <summary>
  12. /// 查询计算结果历史数据
  13. /// </summary>
  14. /// <param name="filter">查询条件,长度为6的数组,数组索引1=开始时间,2=结束时间,3=风场编号,4=风机编号,5=建议操作类型,6=实际操作类型</param>
  15. /// <param name="pageIndex">页码</param>
  16. /// <param name="pageSize">分页尺寸</param>
  17. /// <param name="total">总数量</param>
  18. /// <returns></returns>
  19. public static IList<AdviceHistoryModel> GetHistoryList(string filter, int pageIndex, int pageSize, ref int total)
  20. {
  21. string[] conditionArr = filter.Split(',');
  22. DateTime? startTime = null;
  23. DateTime? endTime = null;
  24. string stationId = "";
  25. string windturbineId = "";
  26. OperateStyle? adviceSendType = null;
  27. OperateStyle? executeSendType = null;
  28. if (!string.IsNullOrEmpty(conditionArr[0]))
  29. startTime = DateTime.Parse(conditionArr[0]);
  30. if (!string.IsNullOrEmpty(conditionArr[1]))
  31. endTime = DateTime.Parse(conditionArr[1]);
  32. if (!string.IsNullOrEmpty(conditionArr[2]))
  33. stationId = conditionArr[2];
  34. if (!string.IsNullOrEmpty(conditionArr[3]))
  35. windturbineId = conditionArr[3];
  36. if (!string.IsNullOrEmpty(conditionArr[4]))
  37. adviceSendType = (OperateStyle)Convert.ToInt32(conditionArr[4]);
  38. if (!string.IsNullOrEmpty(conditionArr[5]))
  39. executeSendType = (OperateStyle)Convert.ToInt32(conditionArr[5]);
  40. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  41. {
  42. // IList<AdviceHistoryModel> lst = ctx.AdviceHistoryModel.Select(s => s).ToList();
  43. var r1 = ctx.AdviceHistoryModel.AsQueryable();
  44. if (startTime.HasValue && endTime.HasValue)
  45. r1 = r1.Where(s => s.LastUpdateTime > startTime && s.LastUpdateTime < endTime.Value);
  46. if (!string.IsNullOrEmpty(stationId))
  47. r1 = r1.Where(s => s.StationId == stationId);
  48. if (!string.IsNullOrEmpty(windturbineId))
  49. r1 = r1.Where(s => s.WindturbineId == windturbineId);
  50. if (adviceSendType.HasValue)
  51. r1 = r1.Where(s => s.AdviseOperation == adviceSendType.Value);
  52. if (executeSendType.HasValue)
  53. r1 = r1.Where(s => s.ExecuteOperation == executeSendType.Value);
  54. total = r1.Count();
  55. var results = r1.OrderByDescending(q => q.LastUpdateTime)
  56. .Skip(--pageIndex * pageSize)
  57. .Take(pageSize)
  58. .ToList();
  59. return results.ToList();
  60. }
  61. }
  62. public static void InsertHistory(IList<AdviceHistoryModel> list)
  63. {
  64. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  65. {
  66. for (int i = 0; i < list.Count; i++)
  67. {
  68. ctx.AdviceHistoryModel.Add(list[i]);
  69. ctx.SaveChanges();
  70. }
  71. }
  72. }
  73. /// <summary>
  74. /// 更新计算结果
  75. /// </summary>
  76. /// <param name="windturbineId">风机编号</param>
  77. /// <param name="userId">更新人,0为系统</param>
  78. /// <param name="userId">更新人姓名,0为系统</param>
  79. /// <param name="userActive">动作类型,2用户执行,3用户取消,4系统取消</param>
  80. public static void UpdateHistory(string windturbineId, string userId, string userName, int userActive)
  81. {
  82. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  83. {
  84. //查找流水表中,风机编号,状态为未执行,实际执行时间为空的数据(理论上只有一条)
  85. AdviceHistoryModel m = ctx.AdviceHistoryModel.Where(s => s.WindturbineId == windturbineId && s.Status == 1 && s.ExecuteTime == null).FirstOrDefault();
  86. if (m == null)
  87. {
  88. return;
  89. }
  90. m.Status = userActive;
  91. m.ExecuteTime = DateTime.Now;
  92. m.Operater = userId;
  93. m.OperaterName = userName;
  94. if (userActive == 2)//如果是用户执行推荐动作
  95. {
  96. m.ExecuteOperation = m.AdviseOperation;
  97. }
  98. else // 系统取消,用户取消
  99. {
  100. m.ExecuteOperation = OperateStyle.Unknow;
  101. }
  102. ctx.Entry(m).State = System.Data.Entity.EntityState.Modified;
  103. ctx.SaveChanges();
  104. }
  105. }
  106. /// <summary>
  107. /// 将上次所有未执行的计算结果全部更新未系统取消
  108. /// </summary>
  109. public static void UpdateAllCalcResult()
  110. {
  111. try
  112. {
  113. using (GDNXFDDbContext ctx = new GDNXFDDbContext())
  114. {
  115. string sql = String.Format("update wisdom_calc_history set status=4,executetime=sysdate,operater=0,operatername='系统' where status=1");
  116. ctx.Database.ExecuteSqlCommand(sql, new object[] { });
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. Console.WriteLine("123");
  122. }
  123. }
  124. }
  125. }