AccountService

using System;
using com.sample.dao;
using com.sample.model;
using com.sample.system;
using com.tg.framework.basic.exception;
using com.tg.framework.basic.template;
using com.tg.framework.common;
using com.tg.framework.core;
using System.Collections.Generic;
using com.tg.framework.basic.cache;
using com.tg.framework.basic.logging;
using com.tg.framework.basic.dataaccess;

namespace com.sample.service.account
{
    /// 
    /// This is the account business class.
    /// 
    public class AccountService : BaseService
    {
        #region fields and const

        /// 
        /// Variable LoggerManager.
        /// 
        private static readonly ILogManager LoggerManager = LogFactory.GetLoggerManager("AccountModule");

        #endregion

        #region public Methods for Account
        /// 
        /// Add account method.
        /// 
        ///  parameter -- account
        /// return the execute result
        public string Add(Account account)
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);

            var id = string.Empty;
            try
            {
                //// start a transcation in this method
                DataAccessFactory.StartTransaction(this.RegionName);

                ////add user
                id = userdao.Add(account.User);

                if (!string.IsNullOrEmpty(id))
                {
                    ////add person
                    account.Person.UserId = LangUtil.ToInt32(id);
                    persondao.Add(account.Person);

                    ////add person education
                    foreach (var personaleducation in account.PersonalEducation)
                    {
                        personaleducation.UserId = LangUtil.ToInt32(id);
                        personaleducationdao.Add(personaleducation);
                    }

                    ////add person reward
                    foreach (var personalreward in account.PersonalReward)
                    {
                        personalreward.UserId = LangUtil.ToInt32(id);
                        personalrewarddao.Add(personalreward);
                    }
                }
                ////commit
                DataAccessFactory.CommitTransaction();

                ////logging the operation
                LoggerManager.Info("A account has been added successful! id is : " + id);
            }
            catch (Exception ex)
            {
                //// Rollback the transaction
                DataAccessFactory.RollbackTransaction();
                ////logging the exception
                LoggerManager.Error("AccountService add account method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceAddAccountException, "AccountService add account method has exception.");
            }
            finally
            {
                ////must close this transaction
                DataAccessFactory.CloseTransaction();
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userdao.Dispose();
            }

            return id;
        }

        /// 
        /// Add account method.
        /// 
        ///  parameter -- id
        /// return the execute result
        public Account Get(string id)
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);
            var account = new Account();
            try
            {
                ////search user
                account.User = (User)userdao.Get(id);

                ////search person
                var persondc = new DataCondition();
                persondc.Condition = " UserId = " + id;
                account.Person = (Person)persondao.Get(persondc);

                ////search person education
                var personaleducationdc = new DataCondition();
                personaleducationdc.Condition = " UserId = " + id;
                foreach (var ped in personaleducationdao.GetList(personaleducationdc))
                {
                    account.PersonalEducation.Add((PersonalEducation)ped);
                }
                ////search person reward
                var personalrewarddc = new DataCondition();
                personalrewarddc.Condition = " UserId = " + id;
                foreach (var prd in personalrewarddao.GetList(personalrewarddc))
                {
                    account.PersonalReward.Add((PersonalReward)prd);
                }
            }
            catch (Exception ex)
            {
                ////logging the exception
                LoggerManager.Error("AccountService get account method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceGetAccountException, "AccountService get account method has exception.");
            }
            finally
            {
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userdao.Dispose();
            }

            return account;
        }

        /// 
        /// get account list by top 5
        /// 
        /// the list of account by top 5
        public List GetByTop5()
        {
            var listAccount = new List();
            var listUser = new List();

            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personalEducationdao = new PersonalEducationDAO(this.RegionName);
            var personalRewarddao = new PersonalRewardDAO(this.RegionName);

            try
            {
                string userSql = "select top 5 * from [Sample].[dbo].[User] as [User] where [User].ID in (select distinct UserId from Person) and [User].ID in (select distinct UserId from PersonalEducation) and [User].ID in (select distinct UserId from PersonalReward)";

                listUser = userdao.GetBySql(userSql);

                foreach (var userItem in listUser)
                {
                    var account = new Account();
                    var user = (User)userItem;

                    var dc = new DataCondition();
                    dc.Condition = " UserId = " + user.ID;
                    ////add user
                    account.User = user;
                    ////add person
                    var person = (Person)persondao.Get(dc);
                    account.Person = person;
                    ////add personal education
                    var personaleducationlist = personalEducationdao.GetList(dc);
                    foreach (var personaleducationItem in personaleducationlist)
                    {
                        account.PersonalEducation.Add((PersonalEducation)personaleducationItem);
                    }
                    ////add personal reward
                    var personalrewardlist = personalRewarddao.GetList(dc);
                    foreach (var personalrewardItem in personalrewardlist)
                    {
                        account.PersonalReward.Add((PersonalReward)personalrewardItem);
                    }
                    ////add a account
                    listAccount.Add(account);
                }
            }
            catch (Exception ex)
            {
                ////logging the exception
                LoggerManager.Error("AccountService get accounts by top5 method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceGetByTop5Exception, "AccountService get accounts by top5 method has exception.");
            }
            finally
            {
                persondao.Dispose();
                personalEducationdao.Dispose();
                personalRewarddao.Dispose();
                userdao.Dispose();
            }

            return listAccount;
        }

        /// 
        /// get account top list by parameter number
        /// 
        /// parameter -- the top records count
        /// return the top list 
        public List GetByTopRecords(int topRecords)
        {
            var listAccount = new List();
            var listUser = new List();

            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personalEducationdao = new PersonalEducationDAO(this.RegionName);
            var personalRewarddao = new PersonalRewardDAO(this.RegionName);

            try
            {
                string userSql = "select top " + topRecords + " * from [Sample].[dbo].[User] as [User] where [User].ID in (select distinct UserId from Person) and [User].ID in (select distinct UserId from PersonalEducation) and [User].ID in (select distinct UserId from PersonalReward)";

                listUser = userdao.GetBySql(userSql);

                foreach (var userItem in listUser)
                {
                    var account = new Account();
                    var user = (User)userItem;

                    var dc = new DataCondition();
                    dc.Condition = " UserId = " + user.ID;
                    ////add user
                    account.User = user;
                    ////add person
                    var person = (Person)persondao.Get(dc);
                    account.Person = person;
                    ////add personal education
                    var personaleducationlist = personalEducationdao.GetList(dc);
                    foreach (var personaleducationItem in personaleducationlist)
                    {
                        account.PersonalEducation.Add((PersonalEducation)personaleducationItem);
                    }
                    ////add personal reward
                    var personalrewardlist = personalRewarddao.GetList(dc);
                    foreach (var personalrewardItem in personalrewardlist)
                    {
                        account.PersonalReward.Add((PersonalReward)personalrewardItem);
                    }
                    ////add a account
                    listAccount.Add(account);
                }
            }
            catch (Exception ex)
            {
                ////logging the exception
                LoggerManager.Error("AccountService get by top records method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceGetByTopRecordsException, "AccountService get by top records method has exception.");
            }
            finally
            {
                persondao.Dispose();
                personalEducationdao.Dispose();
                personalRewarddao.Dispose();
                userdao.Dispose();
            }

            return listAccount;
        }

        /// 
        /// whether exist account by user id
        /// 
        /// user id
        /// the result of bool
        public bool Exist(string id)
        {
            var userdao = new UserDAO(this.RegionName);
            bool exist = false;
            try
            {
                exist = userdao.Exist(id);
            }
            catch (Exception ex)
            {
                ////logging the exception
                LoggerManager.Error("AccountService check account exist method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceExistException, "AccountService check account exist method has exception.");
            }
            finally
            {
                userdao.Dispose();
            }

            return exist;
        }

        /// 
        /// Delete account method.
        /// 
        ///  parameter -- id
        /// return the execute result
        public void Remove(string id)
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);

            try
            {
                //// start a transcation in this method
                DataAccessFactory.StartTransaction(this.RegionName);
                var dc = new DataCondition();
                dc.Condition = " UserId = " + id;

                ////delete assistant table information
                persondao.Remove(dc);
                personaleducationdao.Remove(dc);
                personalrewarddao.Remove(dc);
                ////delete user information
                userdao.Remove(id);
                ////commit
                DataAccessFactory.CommitTransaction();

                ////logging the operation
                LoggerManager.Info("A account has been removed successful! id is : " + id);
            }
            catch (Exception ex)
            {
                //// Rollback the transaction
                DataAccessFactory.RollbackTransaction();
                ////logging the exception
                LoggerManager.Error("AccountService remove method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceRemoveException, "AccountService remove method has exception.");
            }
            finally
            {
                ////must close this transaction
                DataAccessFactory.CloseTransaction();
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userdao.Dispose();
            }
        }

        /// 
        /// Delete account method.
        /// 
        ///  parameter -- account
        /// return the execute result
        public string Remove(Account account)
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);

            var id = string.Empty;
            try
            {
                //// start a transcation in this method
                DataAccessFactory.StartTransaction(this.RegionName);
                User user = account.User;
                if (user != null)
                {
                    var userId = user.ID;

                    var dc = new DataCondition();
                    dc.Condition = " UserId = " + userId;

                    ////delete assistant table information
                    persondao.Remove(dc);
                    personaleducationdao.Remove(dc);
                    personalrewarddao.Remove(dc);
                    ////delete user information
                    id = userdao.Remove(userId.ToString()).ToString();
                    ////commit
                    DataAccessFactory.CommitTransaction();


                    ////logging the operation
                    LoggerManager.Info("A account has been removed successful! id is : " + userId);
                }
            }
            catch (Exception ex)
            {
                //// Rollback the transaction
                DataAccessFactory.RollbackTransaction();
                ////logging the exception
                LoggerManager.Error("AccountService remove method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceRemoveException, "AccountService remove method has exception.");
            }
            finally
            {
                ////must close this transaction
                DataAccessFactory.CloseTransaction();
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userdao.Dispose();
            }

            return id;
        }

        /// 
        /// Add account method.
        /// 
        ///  parameter -- account
        /// return the execute result
        public string Update(Account account)
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);

            var id = string.Empty;
            try
            {
                //// start a transcation in this method
                DataAccessFactory.StartTransaction(this.RegionName);
                ////update user
                id = userdao.Update(account.User).ToString();

                ////update person
                persondao.Update(account.Person);

                ////update person education
                foreach (var personaleducation in account.PersonalEducation)
                {
                    personaleducationdao.Update(personaleducation);
                }

                ////update person reward
                foreach (var personalreward in account.PersonalReward)
                {
                    personaleducationdao.Update(personalreward);
                }
                ////commit
                DataAccessFactory.CommitTransaction();

                ////logging the operation
                LoggerManager.Info("A account has been updated successful! id is : " + account.User.ID);
            }
            catch (Exception ex)
            {
                //// Rollback the transaction
                DataAccessFactory.RollbackTransaction();
                ////logging the exception
                LoggerManager.Error("AccountService update method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceUpdateException, "AccountService update method has exception.");
            }
            finally
            {
                ////must close this transaction
                DataAccessFactory.CloseTransaction();
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userdao.Dispose();
            }

            return id;
        }

        /// 
        /// remove invalid personal education
        /// 
        /// return the number of affected person
        public int RemoveAll()
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);
            var userandroledao = new UserAndRoleDAO(this.RegionName);

            var count = 0;
            try
            {
                //// start a transcation in this method
                DataAccessFactory.StartTransaction(this.RegionName);

                var dc = new DataCondition();
                dc.Condition = " islock = 1 ";

                List listUser = userdao.GetList(dc);
                if (listUser != null)
                {
                    for (int i = 0; i < listUser.Count; i++)
                    {
                        var userId = ((User)listUser[i]).ID;
                        ////delete person 
                        var persondc = new DataCondition();
                        persondc.Condition = " UserId = " + userId;
                        persondao.Remove(persondc);
                        ////delete personaleducation 
                        var personaleducationdc = new DataCondition();
                        personaleducationdc.Condition = " UserId = " + userId;
                        personaleducationdao.Remove(personaleducationdc);
                        ////delete personalreward 
                        var personalrewarddc = new DataCondition();
                        personalrewarddc.Condition = " UserId = " + userId;
                        personalrewarddao.Remove(personalrewarddc);
                        ////delete userandrole 
                        var userandroledc = new DataCondition();
                        userandroledc.Condition = " UserId = " + userId;
                        userandroledao.Remove(userandroledc);
                    }
                }
                ////delete user
                count = userdao.Remove(dc);
                ////commit
                DataAccessFactory.CommitTransaction();

                ////logging the operation
                LoggerManager.Info("All accounts have been removed successful!");
            }
            catch (Exception ex)
            {
                //// Rollback the transaction
                DataAccessFactory.RollbackTransaction();
                ////logging the exception
                LoggerManager.Error("AccountService remove all method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceRemoveAllException, "AccountService remove all method has exception.");
            }
            finally
            {
                ////must close this transaction
                DataAccessFactory.CloseTransaction();
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userandroledao.Dispose();
                userdao.Dispose();
            }

            return count;
        }

        /// 
        /// Add a account by transaction
        /// 
        /// parameter -- account object
        /// parameter -- role object
        public void Add(Account account, Role role)
        {
            var userdao = new UserDAO(this.RegionName);
            var persondao = new PersonDAO(this.RegionName);
            var personaleducationdao = new PersonalEducationDAO(this.RegionName);
            var personalrewarddao = new PersonalRewardDAO(this.RegionName);
            var roledao = new RoleDAO(this.RegionName);
            var userandroledao = new UserAndRoleDAO(this.RegionName);

            var userrole = new UserAndRole();

            var userid = string.Empty;
            var roleid = string.Empty;
            try
            {
                //// start a transcation in this method
                DataAccessFactory.StartTransaction(this.RegionName);
                userid = userdao.Add(account.User);
                if (!string.IsNullOrEmpty(userid))
                {
                    ////add person
                    account.Person.UserId = LangUtil.ToInt32(userid);
                    persondao.Add(account.Person);

                    ////add person education
                    foreach (var personaleducation in account.PersonalEducation)
                    {
                        personaleducation.UserId = LangUtil.ToInt32(userid);
                        personaleducationdao.Add(personaleducation);
                    }

                    ////add person reward
                    foreach (var personalreward in account.PersonalReward)
                    {
                        personalreward.UserId = LangUtil.ToInt32(userid);
                        personalrewarddao.Add(personalreward);
                    }
                    ////add role to this account
                    roleid = roledao.Add(role);
                }

                ////add userandrole
                userrole.UserId = userid;
                userrole.RoleId = roleid;
                userandroledao.Add(userrole);

                ////commit
                DataAccessFactory.CommitTransaction();

                ////logging the operation
                LoggerManager.Info("A account has been added successful! userid is : " + userid + " and roleid is : " + roleid);
            }
            catch (Exception ex)
            {
                //// Rollback the transaction
                DataAccessFactory.RollbackTransaction();
                ////logging the exception
                LoggerManager.Error("AccountService add account and role method has exception.");
                //// throw a new exception, use Exception component
                throw new ServiceException(ServiceException.AccountServiceAddAccountException, "AccountService add account and role method has exception.");
            }
            finally
            {
                ////must close this transaction
                DataAccessFactory.CloseTransaction();
                persondao.Dispose();
                personaleducationdao.Dispose();
                personalrewarddao.Dispose();
                userandroledao.Dispose();
                userdao.Dispose();
            }
        }
        #endregion

        #region Test for Account and framework components
        /// 
        /// Set top 5 to Cache 
        /// 
        public void SetTop5ToCache()
        {
            var listAccount = GetByTop5();

            ////caching the search result
            var cache = CacheFactory.GetCache("AccountTop5Search");

            if (cache != null)
            {
                var cacheObject = new Cache();
                cacheObject.CacheObject = listAccount;
                cacheObject.CreateTime = System.DateTime.Now;
                cacheObject.AccessCount = 1;

                ////log this operation
                LoggerManager.Info("Input the cache result : " + cache.Put("accounttop5", cacheObject));
            }
        }

        /// 
        /// Set top 5 to Cache 
        /// 
        /// parameter -- top records
        public void SetTopRecordsToCache(int topRecords)
        {
            var listAccount = GetByTopRecords(topRecords);

            ////caching the search result
            var cache = CacheFactory.GetCache("AccountTopRecordsSearch");

            if (cache != null)
            {
                var cacheObject = new Cache();
                cacheObject.CacheObject = listAccount;
                cacheObject.CreateTime = DateTime.Now;
                cacheObject.AccessCount = 1;

                ////log this operation
                LoggerManager.Info("Input the cache result : " + cache.Put("accounttoprecords", cacheObject, 20000));
            }
        }

        /// 
        /// Get resume
        /// 
        /// parameter -- account
        /// return the render string
        public string GetResume(Account account)
        {
            string fileName = "Resume.htm";

            //// Get template which name is Resume.htm
            ITemplateManager template = TemplateFactory.GetTemplate(fileName);

            var userResources = SystemHelper.GetSystemStrings(account.User);
            var personResources = SystemHelper.GetSystemStrings(account.Person);
            var personEducationResources = SystemHelper.GetSystemStrings(account.PersonalEducation[0]);
            var personRewardResources = SystemHelper.GetSystemStrings(account.PersonalReward[0]);

            string renderText = string.Empty;

            ////render by resource 
            renderText = template.Render(userResources);
            renderText = template.Render(personResources);
            renderText = template.Render(personEducationResources);
            renderText = template.Render(personRewardResources);

            ////render user
            renderText = template.Render(account.User);

            ////render person
            renderText = template.Render(account.Person);

            ////render personal education
            foreach (PersonalEducation personaleducation in account.PersonalEducation)
            {
                renderText = template.Render(personaleducation);
            }

            ////render personal reward
            foreach (PersonalReward personalreward in account.PersonalReward)
            {
                renderText = template.Render(personalreward);
            }

            return renderText;
        }

        /// 
        /// Get resume
        /// 
        /// parameter -- account
        /// return the render string
        public string SendResume(Account account)
        {
            string fileName = "Resume.htm";

            //// Get template which name is Resume.htm
            ITemplateManager template = TemplateFactory.GetTemplate(fileName);

            var titleResources = SystemHelper.GetSystemStrings(account.User);

            string renderText = string.Empty;

            renderText = template.Render(titleResources);

            ////render user
            renderText = template.Render(account.User);

            ////render person
            renderText = template.Render(account.Person);

            ////render personal education
            foreach (PersonalEducation personaleducation in account.PersonalEducation)
            {
                renderText = template.Render(personaleducation);
            }

            ////render personal reward
            foreach (PersonalReward personalreward in account.PersonalReward)
            {
                renderText = template.Render(personalreward);
            }

            return renderText;
        }
        #endregion
    }
}


你可能感兴趣的:(AccountService)