TsysMessageRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. using Newtonsoft.Json;
  2. using Ropin.Inspection.Model;
  3. using Ropin.Inspection.Model.Common;
  4. using Ropin.Inspection.Model.Entities;
  5. using Ropin.Inspection.Model.SearchModel.MTN;
  6. using Ropin.Inspection.Model.ViewModel.MTN;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace Ropin.Inspection.Repository
  13. {
  14. public class TsysMessageRepository : RepositoryBase<TSYS_Message, string>, ITsysMessageRepository
  15. {
  16. public TsysMessageRepository(InspectionDbContext dbContext) : base(dbContext)
  17. {
  18. }
  19. public Task<bool> UpdateMsgStatus(string id,int? msgStatus)
  20. {
  21. MySqlConnector.MySqlParameter[] parameters = new[] {
  22. new MySqlConnector.MySqlParameter("code", id),
  23. new MySqlConnector.MySqlParameter("msgStatus", msgStatus)
  24. };
  25. string sql = $" UPDATE TSYS_Message SET I_MsgStatus = @msgStatus WHERE (C_ID =@code); ";
  26. int iResult = EntityFrameworkCoreExtensions.ExecuteSqlNoQuery(DbContext.Database, sql, parameters);
  27. bool result = iResult > 0;
  28. return Task.FromResult(result);
  29. }
  30. public Task<IEnumerable<TsysMessageViewModel>> GetList(TsysMessageSearchModel searchModel)
  31. {
  32. MySqlConnector.MySqlParameter[] parameters = new[] {
  33. new MySqlConnector.MySqlParameter("Status", searchModel.C_Status),
  34. new MySqlConnector.MySqlParameter("id", searchModel.C_ID),
  35. new MySqlConnector.MySqlParameter("generationType", searchModel.I_GenerationType),
  36. new MySqlConnector.MySqlParameter("msgTypeCode", searchModel.C_MsgTypeCode),
  37. new MySqlConnector.MySqlParameter("pushMsgToCode", searchModel.C_PushMsgToCode),
  38. new MySqlConnector.MySqlParameter("storeCode", searchModel.C_StoreCode),
  39. new MySqlConnector.MySqlParameter("devCode", searchModel.C_DevCode),
  40. new MySqlConnector.MySqlParameter("content", "%"+searchModel.C_Content+"%"),
  41. new MySqlConnector.MySqlParameter("beginTme", searchModel.BeginTime?.ToString("yyyy-MM-dd")),
  42. new MySqlConnector.MySqlParameter("endTime ", searchModel.EndTime?.ToString("yyyy-MM-dd"))
  43. };
  44. StringBuilder sql = new StringBuilder();
  45. StringBuilder pushMsgSql= new StringBuilder();
  46. if (!string.IsNullOrEmpty(searchModel.C_PushMsgToCode))
  47. {
  48. pushMsgSql.Append(" and C_PushMsgToCode=@pushMsgToCode ");
  49. }
  50. sql.Append(@"
  51. select * from (
  52. select m.*,d.C_StoreCode,d.C_Name as C_DevName,d.C_Url,d.C_StaticUrl,c.C_Name as C_MsgTypeName,ifnull(f.cut4,0) as IsVideo
  53. from TSYS_Message m
  54. LEFT JOIN TDEV_DevStore d on (m.C_DevStoreCode=d.C_ID)
  55. LEFT JOIN TBDM_CodeDetail c on (m.C_MsgTypeCode=c.C_Code)
  56. LEFT JOIN ( select C_MessageCode,count(CASE WHEN C_Type='FILE_TYP_001' THEN 1 END) as cut1,
  57. count(CASE WHEN C_Type='FILE_TYP_002' THEN 1 END) as cut2,count(CASE WHEN C_Type='FILE_TYP_003' THEN 1 END) as cut3,
  58. count(CASE WHEN C_Type='FILE_TYP_004' THEN 1 END) as cut4,count(CASE WHEN C_Type='FILE_TYP_005' THEN 1 END) as cut5,
  59. count(CASE WHEN C_Type='FILE_TYP_006' THEN 1 END) as cut6
  60. from TSYS_MessageFile group by C_MessageCode) f on (m.C_ID=f.C_MessageCode)
  61. ) tab ");
  62. if (!string.IsNullOrEmpty(searchModel.C_Status))
  63. {
  64. sql.Append(" where C_Status=@Status ");
  65. }
  66. else
  67. {
  68. sql.Append(" where C_Status!='0' ");
  69. }
  70. if (!string.IsNullOrEmpty(searchModel.C_ID))
  71. {
  72. sql.Append(" and C_ID=@id ");
  73. }
  74. if (searchModel.I_GenerationType!=null)
  75. {
  76. sql.Append(" and I_GenerationType=@generationType ");
  77. }
  78. if (searchModel.MsgStatus != null&& searchModel.MsgStatus.Count>0)
  79. {
  80. string msgStatus=string.Join(", ", searchModel.MsgStatus);
  81. sql.Append($" and I_MsgStatus in ({msgStatus})");
  82. }
  83. if (!string.IsNullOrEmpty(searchModel.C_MsgTypeCode))
  84. {
  85. sql.Append(" and C_MsgTypeCode=@msgTypeCode ");
  86. }
  87. if (searchModel.MsgTypeList != null && searchModel.MsgTypeList.Count > 0)
  88. {
  89. StringBuilder contsql = new StringBuilder();
  90. sql.Append($" and C_MsgTypeCode in ('{String.Join("','", searchModel.MsgTypeList)}') ");
  91. }
  92. if (!string.IsNullOrEmpty(searchModel.C_StoreCode))
  93. {
  94. sql.Append(" and C_StoreCode=@storeCode ");
  95. }
  96. if (!string.IsNullOrEmpty(searchModel.C_DevCode))
  97. {
  98. sql.Append(" and C_DevStoreCode=@devCode ");
  99. }
  100. if (!string.IsNullOrEmpty(searchModel.C_Content))
  101. {
  102. sql.Append(" and C_Content like @content ");
  103. }
  104. if (searchModel.BeginTime != null && searchModel.BeginTime != DateTime.MinValue)
  105. {
  106. sql.Append(" and DATE_FORMAT(D_MsgCreateOn, '%Y-%m-%d')>=@beginTme ");
  107. }
  108. if (searchModel.EndTime != null && searchModel.EndTime != DateTime.MinValue)
  109. {
  110. sql.Append(" and DATE_FORMAT(D_MsgCreateOn, '%Y-%m-%d')<=@endTime ");
  111. }
  112. if (pushMsgSql.Length>0)
  113. {
  114. sql.Append($" and C_ID in (select C_MessageCode from TMTN_PushMsgResult where C_Status='1' {pushMsgSql.ToString()} group by C_MessageCode) ");
  115. }
  116. sql.Append(" order by D_MsgCreateOn desc ");
  117. IEnumerable<TsysMessageViewModel> recordItemlist = EntityFrameworkCoreExtensions.GetList<TsysMessageViewModel>(DbContext.Database, sql.ToString(), parameters);
  118. searchModel.TotalCount = recordItemlist.First() != null ? recordItemlist.ToList().Count : 0;
  119. if (searchModel.TotalCount == 0)
  120. {
  121. recordItemlist = new List<TsysMessageViewModel>();
  122. }
  123. return Task.FromResult(searchModel.IsPagination ? recordItemlist.Skip((searchModel.PageIndex - 1) * searchModel.PageSize).Take(searchModel.PageSize) : recordItemlist);
  124. }
  125. /// <summary>
  126. /// 获取设备运行时长
  127. /// </summary>
  128. /// <param name="searchModel"></param>
  129. /// <returns></returns>
  130. public Task<DevAlarmCount> GetMsgDevRunTimeAsync(TsysMessageSearchModel searchModel)
  131. {
  132. MySqlConnector.MySqlParameter[] parameters = new[] {
  133. new MySqlConnector.MySqlParameter("devStoreCode", searchModel.C_DevCode),
  134. new MySqlConnector.MySqlParameter("time", searchModel.BeginTime),
  135. };
  136. StringBuilder sql = new StringBuilder();
  137. sql.Append($"select C_Content,D_MsgCreateOn from TSYS_Message where C_Status='1' and C_MsgTypeCode='MSG_TYPE_011' ");
  138. if (!string.IsNullOrEmpty(searchModel.C_DevCode))
  139. {
  140. sql.Append(" and C_DevStoreCode=@devStoreCode");
  141. }
  142. if (searchModel.BeginTime != null)
  143. {
  144. sql.Append(" and D_MsgCreateOn> @time ");
  145. }
  146. sql.Append(" order by D_MsgCreateOn asc ");
  147. IEnumerable<TSYS_Message> recordItemlist = EntityFrameworkCoreExtensions.GetList<TSYS_Message>(DbContext.Database, sql.ToString(), parameters);
  148. DateTime? time1 = null, time2 = null;
  149. int day = 0; int Hour = 0; int minutes = 0;
  150. int dayTotal = 0; int HourTotal = 0; int minutesTotal = 0;
  151. int dayStop = 0; int HourStop = 0; int minutesStop = 0;
  152. int dayStopTotal = 0; int HourStopTotal = 0; int minutesStopTotal = 0;
  153. string FistTime = null, LastTime = null;
  154. foreach (var item in recordItemlist)
  155. {
  156. try
  157. {
  158. var msgMode = JsonConvert.DeserializeObject<TpushMsgModel>(item.C_Content);
  159. if (msgMode.Msg == "开启")
  160. {
  161. time1 = string.IsNullOrEmpty(msgMode.CreateOn) ? null : Convert.ToDateTime(msgMode.CreateOn);
  162. if (time2 != null && time1 != null)
  163. {
  164. int dateCompare = DateTime.Compare(time1.Value, time2.Value);
  165. if (dateCompare > 0)//time2小于time1
  166. {
  167. TimeSpan diff = (time1.Value).Subtract(time2.Value);
  168. dayStop = diff.Days;
  169. HourStop = diff.Hours;
  170. minutesStop = diff.Minutes;
  171. minutesStopTotal = minutesStopTotal + minutesStop;
  172. if (minutesStopTotal >= 60)
  173. {
  174. HourStopTotal++;
  175. minutesStopTotal = minutesStopTotal - 60;
  176. }
  177. HourStopTotal = HourStopTotal + HourStop;
  178. if (HourStopTotal >= 24)
  179. {
  180. dayStopTotal++;
  181. HourStopTotal = HourStopTotal - 24;
  182. }
  183. dayStopTotal = dayStopTotal + dayStop;
  184. }
  185. }
  186. time2 = null;
  187. if (FistTime == null)
  188. {
  189. FistTime = msgMode.CreateOn;
  190. }
  191. }
  192. else if (msgMode.Msg == "关闭" && time1 != null)
  193. {
  194. LastTime = msgMode.CreateOn;
  195. time2 = string.IsNullOrEmpty(msgMode.CreateOn) ? null : Convert.ToDateTime(msgMode.CreateOn);
  196. TimeSpan timeDiff = (time2.Value).Subtract(time1.Value);
  197. day = timeDiff.Days;
  198. Hour = timeDiff.Hours;
  199. minutes = timeDiff.Minutes;
  200. minutesTotal = minutesTotal + minutes;
  201. if (minutesTotal >= 60)
  202. {
  203. HourTotal++;
  204. minutesTotal = minutesTotal - 60;
  205. }
  206. HourTotal = HourTotal + Hour;
  207. if (HourTotal >= 24)
  208. {
  209. dayTotal++;
  210. HourTotal = HourTotal - 24;
  211. }
  212. dayTotal = dayTotal + day;
  213. }
  214. }
  215. catch (Exception ex)
  216. {
  217. }
  218. }
  219. DevAlarmCount alarmCount = new DevAlarmCount();
  220. alarmCount.FiratOnDate = FistTime;
  221. alarmCount.LastOffDate = LastTime;
  222. alarmCount.nowTime = day + "." + Hour + "." + minutes;
  223. alarmCount.TotalTime = dayTotal + "." + HourTotal + "." + minutesTotal;
  224. alarmCount.nowSpotTime = dayStop + "." + HourStop + "." + minutesStop;
  225. alarmCount.TotalSpotTime = dayStopTotal + "." + HourStopTotal + "." + minutesStopTotal;
  226. return Task.FromResult(alarmCount);
  227. }
  228. }
  229. }