123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using NHibernate;
- using NHibernate.Criterion;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TaskDatabase.Model;
- namespace TaskDatabase
- {
- public class TaskService
- {
- public bool AddTask(TaskModel task) {
- ITransaction transaction = null;
- try
- {
- //如果存在该任务, 则不允许添加
- if (isContainsTask(task.Id))
- {
- return false;
- }
- using (ISession session = NHibernateHelper.OpenSession()) {
- using (transaction = session.BeginTransaction()) {
- try
- {
- session.Save(task);
- if(task.DownloadTaskModelList != null)
- {
- foreach(var download in task.DownloadTaskModelList)
- {
- session.Save(download);
- }
- }
- transaction.Commit();
- }
- catch {
- if (transaction != null && transaction.IsActive)
- {
- transaction.Rollback();
- }
- }
- }
- }
- return true;
- }
- catch {}
- return false;
- }
- public bool UpdateTask(TaskModel task)
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- session.Update(task);
- transaction.Commit();
- }
- }
- return true;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message+ex.StackTrace);
- }
- return false;
- }
- public bool UpdateDownload(DownloadTaskModel download)
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- session.Update(download);
- transaction.Commit();
- }
- }
- return true;
- }
- catch { }
- return false;
- }
- public IList<TaskModel> GetTasksByStatus(int status)
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- var queryResult = session.CreateCriteria(typeof(TaskModel))
- .Add(Restrictions.Eq("Task_status", status))
- .List<TaskModel>();
- return queryResult;
- }
- }
- }
- catch (Exception ex){
-
- Console.WriteLine(ex.Message);
- }
- return null;
- }
- public TaskModel GetTasksByTid(int tid)
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- var queryResult = session.CreateCriteria(typeof(TaskModel))
- .Add(Restrictions.Eq("Tid", tid))
- .UniqueResult<TaskModel>();
- return queryResult;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
- public IList<TaskModel> GetTasksByTaskId(int taskId)
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- var queryResult = session.CreateCriteria(typeof(TaskModel))
- .Add(Restrictions.Eq("Id", taskId))
- .List<TaskModel>();
- return queryResult;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return null;
- }
- public bool isContainsTask(int taskId)
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- var queryResult = session.CreateCriteria(typeof(TaskModel))
- .Add(Restrictions.Eq("Id", taskId))
- .List<TaskModel>();
- return queryResult.Count > 0;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- return true;
- }
- public void removeErrorTask()
- {
- ITransaction transaction = null;
- try
- {
- using (ISession session = NHibernateHelper.OpenSession())
- {
- using (transaction = session.BeginTransaction())
- {
- var queryResult = session.CreateCriteria(typeof(TaskModel))
- .Add(Restrictions.Le("Task_status", -1))
- .List<TaskModel>();
- for (int i = 0; i < queryResult.Count; ++i) {
- ICollection<DownloadTaskModel> downloadTaskModels = queryResult[i].DownloadTaskModelList;
- for (int j = 0; j < downloadTaskModels.Count; ++j) {
- session.Delete(downloadTaskModels.ElementAt(j));
- }
- session.Delete(queryResult[i]);
- }
- transaction.Commit();
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- }
- }
- }
|