using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using CB.Common; using CB.Data; using CB.Entity; using CB.Entity.Frequency; using CB.Interface.Infrastructure; namespace CB.Data.SqlServer { public class TemplateManage : Repository, ITemplateService { public TemplateManage(string interfaceId) : base(interfaceId) { } public override bool Save(TemplateInfo entity) { DbParameter[] pars ={ DbHelper.MakeInParam(InterfaceId,"@Name",(DbType)SqlDbType.NVarChar,20,entity.Name), DbHelper.MakeInParam(InterfaceId,"@FilePath",(DbType)SqlDbType.NVarChar,100,entity.FilePath), DbHelper.MakeInParam(InterfaceId,"@TemplateType",(DbType)SqlDbType.Int,4,entity.TemplateType), DbHelper.MakeInParam(InterfaceId,"@MethodType",(DbType)SqlDbType.Int,4,(int)entity.MethodType), DbHelper.MakeInParam(InterfaceId,"@Descript",(DbType)SqlDbType.NVarChar,500,entity.Descript) }; return DbHelper.ExecuteNonQuery(InterfaceId, CommandType.Text, @"INSERT INTO [DT_Template]([Name],[FilePath],[TemplateType],[MethodType],[Descript]) VALUES(@Name,@FilePath,@TemplateType,@MethodType,@Descript)", pars) > 0 ? true : false; } public override bool Update(TemplateInfo entity) { DbParameter[] pars ={ DbHelper.MakeInParam(InterfaceId,"@Id",(DbType)SqlDbType.Int,4,entity.Id), DbHelper.MakeInParam(InterfaceId,"@Name",(DbType)SqlDbType.NVarChar,20,entity.Name), DbHelper.MakeInParam(InterfaceId,"@FilePath",(DbType)SqlDbType.NVarChar,100,entity.FilePath), DbHelper.MakeInParam(InterfaceId,"@TemplateType",(DbType)SqlDbType.Int,4,entity.TemplateType), DbHelper.MakeInParam(InterfaceId,"@MethodType",(DbType)SqlDbType.Int,4,(int)entity.MethodType), DbHelper.MakeInParam(InterfaceId,"@Descript",(DbType)SqlDbType.NVarChar,500,entity.Descript) }; return DbHelper.ExecuteNonQuery(InterfaceId, CommandType.Text, @"UPDATE [DT_Template] SET [Name]=@Name,[FilePath]=@FilePath,[TemplateType]=@TemplateType,[MethodType]=@MethodType, [Descript]=@Descript,[UpdateDate]=GETDATE() WHERE [Id]=@Id", pars) > 0 ? true : false; } public override bool Delete(int Id) { DbParameter[] pars = { DbHelper.MakeInParam(InterfaceId,"@statement",(DbType)SqlDbType.NVarChar,150, "DELETE FROM [DT_Template] WHERE [Id]=@Id"), DbHelper.MakeInParam(InterfaceId,"@params",(DbType)SqlDbType.NVarChar,10,"@Id int"), DbHelper.MakeInParam(InterfaceId,"@Id",(DbType)SqlDbType.Int,4,Id) }; return DbHelper.ExecuteNonQuery(InterfaceId, CommandType.StoredProcedure, "dbo.sp_executesql", pars) > 0; } public override TemplateInfo Get(TKey key) { TemplateInfo Entity = null; DbParameter[] para = { DbHelper.MakeInParam(InterfaceId, "@Id", (DbType) SqlDbType.Int, 4, key) }; using (IDataReader reader = DbHelper.ExecuteReader(InterfaceId, CommandType.Text, "SELECT TOP 1 [Id],[Name],[FilePath],[TemplateType],[MethodType],[Descript],[UpdateDate],[CreateDate] FROM [DT_Template] WHERE Id= @Id", para)) { if (reader.Read()) { Entity = LoadEntity(reader); } reader.Dispose(); } return Entity; } public override IList ToList() { IList list = new List(); using (IDataReader reader = DbHelper.ExecuteReader(InterfaceId, CommandType.Text, "SELECT [Id],[Name],[FilePath],[TemplateType],[MethodType],[Descript],[UpdateDate],[CreateDate] FROM [DT_Template]")) { while (reader.Read()) { list.Add(LoadEntity(reader)); } reader.Dispose(); } return list; } public override IList ToList(TemplateInfo entity) { throw new NotImplementedException(); } public override IList ToPaging(TemplateInfo entity, int pageSize, int pageIndex, out int recordCount) { string where = "1=1 "; if (entity != null && !string.IsNullOrEmpty(entity.Name)) { where += " AND [Name] like '%" + entity.Name + "%'"; } recordCount = 0; string field = "[Id],[Name],[FilePath],[TemplateType],[MethodType],[Descript],[UpdateDate],[CreateDate]" , orderField = "[Id] ASC" , tableName = "[DT_Template]"; DbParameter[] para = { DbHelper.MakeInParam(InterfaceId,"@pageSize",(DbType)SqlDbType.Int,4,pageSize), DbHelper.MakeInParam(InterfaceId,"@page",(DbType)SqlDbType.Int,4,pageIndex), DbHelper.MakeInParam(InterfaceId,"@tableName",(DbType)SqlDbType.NVarChar,500,tableName), DbHelper.MakeInParam(InterfaceId,"@field",(DbType)SqlDbType.NVarChar,1000,field), DbHelper.MakeInParam(InterfaceId,"@orderField",(DbType)SqlDbType.NVarChar,50,orderField), DbHelper.MakeInParam(InterfaceId,"@where",(DbType)SqlDbType.NVarChar,2000,where) }; IList list = new List(); using (IDataReader reader = DbHelper.ExecuteReader(InterfaceId, CommandType.StoredProcedure, "usp_st_page", para)) { while (reader.Read()) { list.Add(LoadEntity(reader)); } if (reader.NextResult() && reader.Read()) { recordCount = reader.GetInt32(0); } reader.Dispose(); } return list; } protected override TemplateInfo LoadEntity(DataRow dr) { return new TemplateInfo { Id = TypeConverter.ObjectToInt(dr["Id"]), Name = dr["Name"].ToString(), FilePath = dr["FilePath"].ToString(), TemplateType = TypeConverter.ObjectToInt(dr["TemplateType"]), MethodType = (TemplateMethodType)Enum.Parse(typeof(TemplateMethodType), dr["MethodType"].ToString()), Descript = dr["Descript"].ToString(), UpdateDate = TypeConverter.ObjectToDateTime(dr["UpdateDate"]), CreateDate = TypeConverter.ObjectToDateTime(dr["CreateDate"]) }; } protected override TemplateInfo LoadEntity(IDataReader reader) { return new TemplateInfo { Id = TypeConverter.ObjectToInt(reader["Id"]), Name = reader["Name"].ToString(), FilePath = reader["FilePath"].ToString(), TemplateType = TypeConverter.ObjectToInt(reader["TemplateType"]), MethodType = (TemplateMethodType)Enum.Parse(typeof(TemplateMethodType), reader["MethodType"].ToString()), Descript = reader["Descript"].ToString(), UpdateDate = TypeConverter.ObjectToDateTime(reader["UpdateDate"]), CreateDate = TypeConverter.ObjectToDateTime(reader["CreateDate"]) }; } } }