#region RDIFramework.NET-generated
//------------------------------------------------------------------------------
//     RDIFramework.NET（.NET快速信息化系统开发、整合框架）是基于.NET的快速信息化系统开发、整合框架，给用户和开发者最佳的.Net框架部署方案。
//     RDIFramework.NET平台包含基础公共类库、强大的权限控制、模块分配、数据字典、自动升级、各商业级控件库、工作流平台、代码生成器、开发辅助
//工具等，应用系统的各个业务功能子系统，在系统体系结构设计的过程中被设计成各个原子功能模块，各个子功能模块按照业务功能组织成单独的程序集文
//件，各子系统开发完成后，由RDIFramework.NET平台进行统一的集成部署。
//
//	框架官网：http://www.rdiframework.net/
//	框架博客：http://blog.rdiframework.net/
//	交流QQ：406590790 
//	邮件交流：406590790@qq.com
//	其他博客：
//    http://www.cnblogs.com/huyong 
//    http://blog.csdn.net/chinahuyong
//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由RDIFramework.NET平台代码生成工具自动生成。
//     运行时版本:4.0.30319.1
//
//     对此文件的更改可能会导致不正确的行为，并且如果
//     重新生成代码，这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
#endregion

using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.ServiceModel;

namespace #Project#.BizLogic
{
	using RDIFramework.BizLogic;
	using RDIFramework.Utilities;

	/// <summary>
	/// #ClassName#Service
	/// #Description#服务层
	/// 
	/// 修改记录
	/// 
	///	#DateCreated# 版本：1.0 #Author# 建立#Description#服务层。
	///		
	/// 版本：1.0
	///
	/// <author>
	///	<name>#Author#</name>
	///	<date>#DateCreated#</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class #ClassName#Service : System.MarshalByRefObject, I#ClassName#Service
	{
		private string serviceName = "#ClassName#";
		
		/// <summary>
		/// RDIFramework.NET框架数据库连接
		/// </summary>
		private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

		/// <summary>
		/// 业务数据库连接字符串
		/// </summary>
		private readonly string BusinessDbConnection = SystemInfo.BusinessDbConnection;

		#region public string Add(UserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage) 新增#Description#
		/// <summary>
		/// 新增#Description#
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">#Description#实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态消息</param>
		/// <returns>主鍵</returns>
		public string Add(UserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage)
		{
			string returnValue = string.Empty;            
			statusCode = string.Empty;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "新增实体", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							returnValue = manager.AddEntity(entity);
							// returnValue = manager.Add(entity, out statusCode);
							statusMessage = manager.GetStateMessage(statusCode);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}		
			return returnValue;
		}
		#endregion

		#region public DataTable GetDT(UserInfo userInfo) 获取#Description#列表
		/// <summary>
		/// 获取#Description#列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <returns>数据表</returns>
		public DataTable GetDT(UserInfo userInfo)
		{
			DataTable dataTable = new DataTable(#ClassName#Table.TableName);
			
			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "取得列表", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 取得列表
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							dataTable = manager.GetDT(new KeyValuePair<string, object>(#ClassName#Table.FieldDeleteMark, 0), #ClassName#Table.FieldSortCode);
							dataTable.TableName = #ClassName#Table.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return dataTable;
		}
		#endregion

		#region public DataTable GetDTByPage(UserInfo userInfo, string searchValue, out int recordCount, int pageIndex = 1, int pageSize = 20, string sortExpression = null) 分页获取#Description#列表
		/// <summary>
		/// 分页获取#Description#列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="searchValue">查询关键字</param>
		/// <param name="recordCount">记录数</param>
		/// <param name="pageIndex">当前页</param>
		/// <param name="pageSize">每页显示记录条数</param>
		/// <param name="sortExpression">排序字段</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByPage(UserInfo userInfo, string searchValue, out int recordCount, int pageIndex = 1, int pageSize = 20, string sortExpression = null)
		{
			DataTable dataTable = new DataTable(#ClassName#Table.TableName);
			
			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "取得列表", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 取得列表
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							dataTable = manager.GetDTByPage(out recordCount, pageIndex, pageSize, searchValue, sortExpression);
							dataTable.TableName = #ClassName#Table.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return dataTable;
		}
		#endregion

		#region public #ClassName#Entity GetEntity(UserInfo userInfo, string id) 获取#Description#实体
		/// <summary>
		/// 得到#Description#实体
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
		/// <returns>实体</returns>
		public #ClassName#Entity GetEntity(UserInfo userInfo, string id)
		{
			#ClassName#Entity entity = null;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "取得实体", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							entity = manager.GetEntity(id);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return entity;
		}
		#endregion

		#region public int Update(UserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage) 更新#Description#
		/// <summary>
		/// 更新#Description#
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">#Description#实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态信息</param>
		/// <returns>影响行数</returns>
		public int Update(UserInfo userInfo, #ClassName#Entity entity, out string statusCode, out string statusMessage)
		{
			int returnValue = 0;
			statusCode = string.Empty;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "更新实体", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							returnValue = manager.UpdateEntity(entity);
							// returnValue = manager.Update(entity, out statusCode);
							statusMessage = manager.GetStateMessage(statusCode);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion

		#region public DataTable GetDTByIds(UserInfo userInfo, string[] ids) 根据主键数组获取数据
		/// <summary>
		/// 根据主键数组获取数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主鍵</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
		{
			DataTable dataTable = new DataTable(#ClassName#Table.TableName);

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "根据主键数组获取数据", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							dataTable = manager.GetDT(#ClassName#Table.FieldId, ids, #ClassName#Table.FieldSortCode);
							dataTable.TableName = #ClassName#Table.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return dataTable;
		}
		#endregion
		
		#region public DataTable GetDTByValues(UserInfo userInfo, List<KeyValuePair<string, object>> parameters) 根据条件获取数据
		/// <summary>
		/// 根据条件获取数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="names">字段</param>
		/// <param name="values">值</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByValues(UserInfo userInfo, List<KeyValuePair<string, object>> parameters)
		{
			DataTable dataTable = new DataTable(#ClassName#Table.TableName);

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "根据条件获取数据", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 根据条件获取数据
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							dataTable = manager.GetDT(parameters);
							dataTable.TableName = #ClassName#Table.TableName;
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}
			return dataTable;
		}
		#endregion
		
		#region public int BatchSave(UserInfo userInfo, List<#ClassName#Entity> entites) 批量保存数据
		/// <summary>
		/// 批量保存数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entites">#Description#实体列表</param>
		/// <returns>影响行数</returns>
		public int BatchSave(UserInfo userInfo, List<#ClassName#Entity> entites)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "批量保存数据", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							// returnValue = manager.BatchSave(entites);
							foreach (var entity in entites)
							{
								returnValue += manager.Update(entity);
							}
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion
		
		#region public int Delete(UserInfo userInfo, string id) 删除指定数据
		/// <summary>
		/// 删除指定数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主鍵</param>
		/// <returns>数据表</returns>
		public int Delete(UserInfo userInfo, string id)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "删除指定数据", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							returnValue = manager.Delete(id);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion

		#region public int BatchDelete(UserInfo userInfo, string[] ids) 批量删除数据
		/// <summary>
		/// 批量删除数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键数组</param>
		/// <returns>影响行数</returns>
		public int BatchDelete(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "批量删除数据", MethodBase.GetCurrentMethod());

					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							// 开始事务
							dbProvider.BeginTransaction();
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							returnValue = manager.Delete(ids);
							// 提交事务
							dbProvider.CommitTransaction();
						}
						catch (Exception ex)
						{
							// 回滚事务
							dbProvider.RollbackTransaction();
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion

		#region public int SetDeleted(UserInfo userInfo, string[] ids) 批量设置删除标志
		/// <summary>
		/// 批量设置删除标志
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键数组</param>
		/// <returns>影响行数</returns>
		public int SetDeleted(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;

			using (IDbProvider frameworkDbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					frameworkDbProvider.Open(RDIFrameworkDbConection);
					LogManager.Instance.Add(frameworkDbProvider, userInfo, this.serviceName, "批量设置删除标志", MethodBase.GetCurrentMethod());
					
					using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.BusinessDbType))
					{
						try
						{
							dbProvider.Open(BusinessDbConnection);
							#ClassName#Manager manager = new #ClassName#Manager(dbProvider, userInfo);
							returnValue = manager.SetDeleted(ids);
						}
						catch (Exception ex)
						{
							CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
							throw ex;
						}
						finally
						{
							dbProvider.Close();
						}
					}
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(frameworkDbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					frameworkDbProvider.Close();
				}
			}

			return returnValue;
		}
		#endregion
	}
}