TaskService.cs 7.1 KB

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