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
{
    /// <summary>
    /// This is the account business class.
    /// </summary>
    public class AccountService : BaseService
    {
        #region fields and const

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

        #endregion

        #region public Methods for Account
        /// <summary>
        /// Add account method.
        /// </summary>
        /// <param name="account"> parameter -- account</param>
        /// <returns>return the execute result</returns>
        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;
        }

        /// <summary>
        /// Add account method.
        /// </summary>
        /// <param name="id"> parameter -- id</param>
        /// <returns>return the execute result</returns>
        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;
        }

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

            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;
        }

        /// <summary>
        /// get account top list by parameter number
        /// </summary>
        /// <param name="topRecords">parameter -- the top records count</param>
        /// <returns>return the top list </returns>
        public List<IDataModel> GetByTopRecords(int topRecords)
        {
            var listAccount = new List<IDataModel>();
            var listUser = new List<IDataModel>();

            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;
        }

        /// <summary>
        /// whether exist account by user id
        /// </summary>
        /// <param name="id">user id</param>
        /// <returns>the result of bool</returns>
        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;
        }

        /// <summary>
        /// Delete account method.
        /// </summary>
        /// <param name="id"> parameter -- id</param>
        /// <returns>return the execute result</returns>
        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();
            }
        }

        /// <summary>
        /// Delete account method.
        /// </summary>
        /// <param name="account"> parameter -- account</param>
        /// <returns>return the execute result</returns>
        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;
        }

        /// <summary>
        /// Add account method.
        /// </summary>
        /// <param name="account"> parameter -- account</param>
        /// <returns>return the execute result</returns>
        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;
        }

        /// <summary>
        /// remove invalid personal education
        /// </summary>
        /// <returns>return the number of affected person</returns>
        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<IDataModel> 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;
        }

        /// <summary>
        /// Add a account by transaction
        /// </summary>
        /// <param name="account">parameter -- account object</param>
        /// <param name="role">parameter -- role object</param>
        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
        /// <summary>
        /// Set top 5 to Cache 
        /// </summary>
        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));
            }
        }

        /// <summary>
        /// Set top 5 to Cache 
        /// </summary>
        /// <param name="topRecords">parameter -- top records</param>
        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));
            }
        }

        /// <summary>
        /// Get resume
        /// </summary>
        /// <param name="account">parameter -- account</param>
        /// <returns>return the render string</returns>
        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;
        }

        /// <summary>
        /// Get resume
        /// </summary>
        /// <param name="account">parameter -- account</param>
        /// <returns>return the render string</returns>
        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
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值