TaskService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 (Exception ex)
  65. {
  66. Console.WriteLine(ex.Message+ex.StackTrace);
  67. }
  68. return false;
  69. }
  70. public bool UpdateDownload(DownloadTaskModel download)
  71. {
  72. ITransaction transaction = null;
  73. try
  74. {
  75. using (ISession session = NHibernateHelper.OpenSession())
  76. {
  77. using (transaction = session.BeginTransaction())
  78. {
  79. session.Update(download);
  80. transaction.Commit();
  81. }
  82. }
  83. return true;
  84. }
  85. catch { }
  86. return false;
  87. }
  88. public IList<TaskModel> GetTasksByStatus(int status)
  89. {
  90. ITransaction transaction = null;
  91. try
  92. {
  93. using (ISession session = NHibernateHelper.OpenSession())
  94. {
  95. using (transaction = session.BeginTransaction())
  96. {
  97. var queryResult = session.CreateCriteria(typeof(TaskModel))
  98. .Add(Restrictions.Eq("Task_status", status))
  99. .List<TaskModel>();
  100. return queryResult;
  101. }
  102. }
  103. }
  104. catch (Exception ex){
  105. Console.WriteLine(ex.Message);
  106. }
  107. return null;
  108. }
  109. public TaskModel GetTasksByTid(int tid)
  110. {
  111. ITransaction transaction = null;
  112. try
  113. {
  114. using (ISession session = NHibernateHelper.OpenSession())
  115. {
  116. using (transaction = session.BeginTransaction())
  117. {
  118. var queryResult = session.CreateCriteria(typeof(TaskModel))
  119. .Add(Restrictions.Eq("Tid", tid))
  120. .UniqueResult<TaskModel>();
  121. return queryResult;
  122. }
  123. }
  124. }
  125. catch (Exception ex)
  126. {
  127. Console.WriteLine(ex.Message);
  128. }
  129. return null;
  130. }
  131. public IList<TaskModel> GetTasksByTaskId(int taskId)
  132. {
  133. ITransaction transaction = null;
  134. try
  135. {
  136. using (ISession session = NHibernateHelper.OpenSession())
  137. {
  138. using (transaction = session.BeginTransaction())
  139. {
  140. var queryResult = session.CreateCriteria(typeof(TaskModel))
  141. .Add(Restrictions.Eq("Id", taskId))
  142. .List<TaskModel>();
  143. return queryResult;
  144. }
  145. }
  146. }
  147. catch (Exception ex)
  148. {
  149. Console.WriteLine(ex.Message);
  150. }
  151. return null;
  152. }
  153. public bool isContainsTask(int taskId)
  154. {
  155. ITransaction transaction = null;
  156. try
  157. {
  158. using (ISession session = NHibernateHelper.OpenSession())
  159. {
  160. using (transaction = session.BeginTransaction())
  161. {
  162. var queryResult = session.CreateCriteria(typeof(TaskModel))
  163. .Add(Restrictions.Eq("Id", taskId))
  164. .List<TaskModel>();
  165. return queryResult.Count > 0;
  166. }
  167. }
  168. }
  169. catch (Exception ex)
  170. {
  171. Console.WriteLine(ex.Message);
  172. }
  173. return true;
  174. }
  175. public void removeErrorTask()
  176. {
  177. ITransaction transaction = null;
  178. try
  179. {
  180. using (ISession session = NHibernateHelper.OpenSession())
  181. {
  182. using (transaction = session.BeginTransaction())
  183. {
  184. var queryResult = session.CreateCriteria(typeof(TaskModel))
  185. .Add(Restrictions.Le("Task_status", -1))
  186. .List<TaskModel>();
  187. for (int i = 0; i < queryResult.Count; ++i) {
  188. ICollection<DownloadTaskModel> downloadTaskModels = queryResult[i].DownloadTaskModelList;
  189. for (int j = 0; j < downloadTaskModels.Count; ++j) {
  190. session.Delete(downloadTaskModels.ElementAt(j));
  191. }
  192. session.Delete(queryResult[i]);
  193. }
  194. transaction.Commit();
  195. }
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. Console.WriteLine(ex.Message);
  201. }
  202. }
  203. }
  204. }