TaskService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using NHibernate;
  2. using NHibernate.Criterion;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using TaskDatabase.Model;
  9. namespace TaskDatabase
  10. {
  11. public class TaskService
  12. {
  13. public bool AddTask(TaskModel task) {
  14. ITransaction transaction = null;
  15. try
  16. {
  17. //如果存在该任务, 则不允许添加
  18. if (isContainsTask(task.Id))
  19. {
  20. return false;
  21. }
  22. using (ISession session = NHibernateHelper.OpenSession()) {
  23. using (transaction = session.BeginTransaction()) {
  24. try
  25. {
  26. session.Save(task);
  27. if(task.DownloadTaskModelList != null)
  28. {
  29. foreach(var download in task.DownloadTaskModelList)
  30. {
  31. session.Save(download);
  32. }
  33. }
  34. transaction.Commit();
  35. }
  36. catch {
  37. if (transaction != null && transaction.IsActive)
  38. {
  39. transaction.Rollback();
  40. }
  41. }
  42. }
  43. }
  44. return true;
  45. }
  46. catch {}
  47. return false;
  48. }
  49. public bool UpdateTask(TaskModel task)
  50. {
  51. ITransaction transaction = null;
  52. try
  53. {
  54. using (ISession session = NHibernateHelper.OpenSession())
  55. {
  56. using (transaction = session.BeginTransaction())
  57. {
  58. session.Update(task);
  59. transaction.Commit();
  60. }
  61. }
  62. return true;
  63. }
  64. catch { }
  65. return false;
  66. }
  67. public bool UpdateDownload(DownloadTaskModel download)
  68. {
  69. ITransaction transaction = null;
  70. try
  71. {
  72. using (ISession session = NHibernateHelper.OpenSession())
  73. {
  74. using (transaction = session.BeginTransaction())
  75. {
  76. session.Update(download);
  77. transaction.Commit();
  78. }
  79. }
  80. return true;
  81. }
  82. catch { }
  83. return false;
  84. }
  85. public IList<TaskModel> GetTasksByStatus(int status)
  86. {
  87. ITransaction transaction = null;
  88. try
  89. {
  90. using (ISession session = NHibernateHelper.OpenSession())
  91. {
  92. using (transaction = session.BeginTransaction())
  93. {
  94. var queryResult = session.CreateCriteria(typeof(TaskModel))
  95. .Add(Restrictions.Eq("Task_status", status))
  96. .List<TaskModel>();
  97. return queryResult;
  98. }
  99. }
  100. }
  101. catch (Exception ex){
  102. Console.WriteLine(ex.Message);
  103. }
  104. return null;
  105. }
  106. public TaskModel GetTasksByTid(int tid)
  107. {
  108. ITransaction transaction = null;
  109. try
  110. {
  111. using (ISession session = NHibernateHelper.OpenSession())
  112. {
  113. using (transaction = session.BeginTransaction())
  114. {
  115. var queryResult = session.CreateCriteria(typeof(TaskModel))
  116. .Add(Restrictions.Eq("Tid", tid))
  117. .UniqueResult<TaskModel>();
  118. return queryResult;
  119. }
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Console.WriteLine(ex.Message);
  125. }
  126. return null;
  127. }
  128. public IList<TaskModel> GetTasksByTaskId(int taskId)
  129. {
  130. ITransaction transaction = null;
  131. try
  132. {
  133. using (ISession session = NHibernateHelper.OpenSession())
  134. {
  135. using (transaction = session.BeginTransaction())
  136. {
  137. var queryResult = session.CreateCriteria(typeof(TaskModel))
  138. .Add(Restrictions.Eq("Id", taskId))
  139. .List<TaskModel>();
  140. return queryResult;
  141. }
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. Console.WriteLine(ex.Message);
  147. }
  148. return null;
  149. }
  150. public bool isContainsTask(int taskId)
  151. {
  152. ITransaction transaction = null;
  153. try
  154. {
  155. using (ISession session = NHibernateHelper.OpenSession())
  156. {
  157. using (transaction = session.BeginTransaction())
  158. {
  159. var queryResult = session.CreateCriteria(typeof(TaskModel))
  160. .Add(Restrictions.Eq("Id", taskId))
  161. .List<TaskModel>();
  162. return queryResult.Count > 0;
  163. }
  164. }
  165. }
  166. catch (Exception ex)
  167. {
  168. Console.WriteLine(ex.Message);
  169. }
  170. return true;
  171. }
  172. public void removeErrorTask()
  173. {
  174. ITransaction transaction = null;
  175. try
  176. {
  177. using (ISession session = NHibernateHelper.OpenSession())
  178. {
  179. using (transaction = session.BeginTransaction())
  180. {
  181. var queryResult = session.CreateCriteria(typeof(TaskModel))
  182. .Add(Restrictions.Le("Task_status", -1))
  183. .List<TaskModel>();
  184. for (int i = 0; i < queryResult.Count; ++i) {
  185. ICollection<DownloadTaskModel> downloadTaskModels = queryResult[i].DownloadTaskModelList;
  186. for (int j = 0; j < downloadTaskModels.Count; ++j) {
  187. session.Delete(downloadTaskModels.ElementAt(j));
  188. }
  189. session.Delete(queryResult[i]);
  190. }
  191. transaction.Commit();
  192. }
  193. }
  194. }
  195. catch (Exception ex)
  196. {
  197. Console.WriteLine(ex.Message);
  198. }
  199. }
  200. }
  201. }