using System; using System.Collections.Generic; using System.Linq; using System.Text; using Maticsoft.DBUtility; using System.Data.SqlClient; using System.Data; namespace Maticsoft.SQLServerDAL { public class ChannelProduct : Maticsoft.IDAL.IChannelProduct { /// <summary> /// 是否存在该记录 /// </summary> public bool Exists(string Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from ChannelProduct"); strSql.Append(" where Id=@Id "); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Char,36) }; parameters[0].Value = Id; return DbHelperSQL.Exists(strSql.ToString(), parameters); } public Boolean Exists(String channelId, Int32 productSectionId,Boolean? isShow = null) { StringBuilder sql = new StringBuilder(); List<SqlParameter> parameters = new List<SqlParameter>(); sql.Append("select count(1) from ChannelProduct"); sql.Append(" where ChannelId = @ChannelId AND ProductSectionId = @ProductSectionId"); //SqlParameter[] parameters = { // new SqlParameter("@ChannelId",SqlDbType.Char,36){Value = channelId}, // new SqlParameter("@ProductSectionId",SqlDbType.Int,4){Value = productSectionId} // }; parameters.Add(new SqlParameter("@ChannelId", SqlDbType.Char, 36) { Value = channelId }); parameters.Add(new SqlParameter("@ProductSectionId", SqlDbType.Int, 4) { Value = productSectionId }); if (isShow != null && isShow.HasValue) { sql.Append(" AND IsShow = @IsShow"); parameters.Add(new SqlParameter("@IsShow", SqlDbType.Bit, 1) { Value = isShow.Value }); } return DbHelperSQL.Exists(sql.ToString(), parameters.ToArray()); } /// <summary> /// 增加一条数据 /// </summary> public bool Add(Model.ChannelProduct model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into ChannelProduct("); strSql.Append("Id,ChannelId,ProductSectionId,ProductSectionParintId,IsShow,Abstruct,Image,Describe)"); strSql.Append(" values ("); strSql.Append("@Id,@ChannelId,@ProductSectionId,@ProductSectionParintId,@IsShow,@Abstruct,@Image,@Describe)"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Char,36), new SqlParameter("@ChannelId", SqlDbType.Char,36), new SqlParameter("@ProductSectionId", SqlDbType.Int,4), new SqlParameter("@ProductSectionParintId", SqlDbType.Int,4), new SqlParameter("@IsShow", SqlDbType.Bit,1), new SqlParameter("@Abstruct", SqlDbType.VarChar), new SqlParameter("@Image", SqlDbType.VarChar,1000), new SqlParameter("@Describe", SqlDbType.VarChar)}; parameters[0].Value = model.Id; parameters[1].Value = model.ChannelId; parameters[2].Value = model.ProductSectionId; parameters[3].Value = model.ProductSectionParintId; parameters[4].Value = model.IsShow; parameters[5].Value = model.Abstruct; parameters[6].Value = model.Image; parameters[7].Value = model.Describe; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// <summary> /// 更新一条数据 /// </summary> public bool Update(Model.ChannelProduct model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update ChannelProduct set "); strSql.Append("ChannelId=@ChannelId,"); strSql.Append("ProductSectionId=@ProductSectionId,"); strSql.Append("ProductSectionParintId=@ProductSectionParintId,"); strSql.Append("IsShow=@IsShow,"); strSql.Append("Abstruct=@Abstruct,"); strSql.Append("Image=@Image,"); strSql.Append("Describe=@Describe"); strSql.Append(" where Id=@Id "); SqlParameter[] parameters = { new SqlParameter("@ChannelId", SqlDbType.Char,36), new SqlParameter("@ProductSectionId", SqlDbType.Int,4), new SqlParameter("@ProductSectionParintId", SqlDbType.Int,4), new SqlParameter("@IsShow", SqlDbType.Bit,1), new SqlParameter("@Abstruct", SqlDbType.VarChar), new SqlParameter("@Image", SqlDbType.VarChar,1000), new SqlParameter("@Describe", SqlDbType.VarChar), new SqlParameter("@Id", SqlDbType.Char,36)}; parameters[0].Value = model.ChannelId; parameters[1].Value = model.ProductSectionId; parameters[2].Value = model.ProductSectionParintId; parameters[3].Value = model.IsShow; parameters[4].Value = model.Abstruct; parameters[5].Value = model.Image; parameters[6].Value = model.Describe; parameters[7].Value = model.Id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// <summary> /// 删除一条数据 /// </summary> public bool Delete(string Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from ChannelProduct "); strSql.Append(" where Id=@Id "); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Char,36) }; parameters[0].Value = Id; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } public Int32 DeleteWhere(String where) { StringBuilder sql = new StringBuilder(); sql.Append("delete from ChannelProduct "); sql.Append(" where "); sql.Append(where); return DbHelperSQL.ExecuteSql(sql.ToString()); } /// <summary> /// 批量删除数据 /// </summary> public bool DeleteList(string Idlist) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from ChannelProduct "); strSql.Append(" where Id in (" + Idlist + ") "); int rows = DbHelperSQL.ExecuteSql(strSql.ToString()); if (rows > 0) { return true; } else { return false; } } public Model.ChannelProduct GetModelByChannelAndProductSectionId(String channelId, Int32 ProductSectionId) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,ChannelId,ProductSectionId,ProductSectionParintId,IsShow,Abstruct,Image,Describe from ChannelProduct "); strSql.Append(" where ChannelId=@Id AND ProductSectionId=@ProductSectionId"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Char,36), new SqlParameter("@ProductSectionId",SqlDbType.Int,4)}; parameters[0].Value = channelId; parameters[1].Value = ProductSectionId; Model.ChannelProduct model = new Model.ChannelProduct(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "") { model.Id = ds.Tables[0].Rows[0]["Id"].ToString(); } if (ds.Tables[0].Rows[0]["ChannelId"] != null && ds.Tables[0].Rows[0]["ChannelId"].ToString() != "") { model.ChannelId = ds.Tables[0].Rows[0]["ChannelId"].ToString(); } if (ds.Tables[0].Rows[0]["ProductSectionId"] != null && ds.Tables[0].Rows[0]["ProductSectionId"].ToString() != "") { model.ProductSectionId = int.Parse(ds.Tables[0].Rows[0]["ProductSectionId"].ToString()); } if (ds.Tables[0].Rows[0]["ProductSectionParintId"] != null && ds.Tables[0].Rows[0]["ProductSectionParintId"].ToString() != "") { model.ProductSectionParintId = int.Parse(ds.Tables[0].Rows[0]["ProductSectionParintId"].ToString()); } if (ds.Tables[0].Rows[0]["IsShow"] != null && ds.Tables[0].Rows[0]["IsShow"].ToString() != "") { if ((ds.Tables[0].Rows[0]["IsShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsShow"].ToString().ToLower() == "true")) { model.IsShow = true; } else { model.IsShow = false; } } if (ds.Tables[0].Rows[0]["Abstruct"] != null && ds.Tables[0].Rows[0]["Abstruct"].ToString() != "") { model.Abstruct = ds.Tables[0].Rows[0]["Abstruct"].ToString(); } if (ds.Tables[0].Rows[0]["Image"] != null && ds.Tables[0].Rows[0]["Image"].ToString() != "") { model.Image = ds.Tables[0].Rows[0]["Image"].ToString(); } if (ds.Tables[0].Rows[0]["Describe"] != null && ds.Tables[0].Rows[0]["Describe"].ToString() != "") { model.Describe = ds.Tables[0].Rows[0]["Describe"].ToString(); } return model; } else { return null; } } public Model.ChannelProduct GetModelByChannelAndProductSectionParintId(String channelId, Int32 ProductSectionParintId) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,ChannelId,ProductSectionId,ProductSectionParintId,IsShow,Abstruct,Image,Describe from ChannelProduct "); strSql.Append(" where ChannelId=@Id AND ProductSectionParintId=@ProductSectionParintId"); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Char,36), new SqlParameter("@ProductSectionParintId",SqlDbType.Int,4)}; parameters[0].Value = channelId; parameters[1].Value = ProductSectionParintId; Model.ChannelProduct model = new Model.ChannelProduct(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "") { model.Id = ds.Tables[0].Rows[0]["Id"].ToString(); } if (ds.Tables[0].Rows[0]["ChannelId"] != null && ds.Tables[0].Rows[0]["ChannelId"].ToString() != "") { model.ChannelId = ds.Tables[0].Rows[0]["ChannelId"].ToString(); } if (ds.Tables[0].Rows[0]["ProductSectionId"] != null && ds.Tables[0].Rows[0]["ProductSectionId"].ToString() != "") { model.ProductSectionId = int.Parse(ds.Tables[0].Rows[0]["ProductSectionId"].ToString()); } if (ds.Tables[0].Rows[0]["ProductSectionParintId"] != null && ds.Tables[0].Rows[0]["ProductSectionParintId"].ToString() != "") { model.ProductSectionParintId = int.Parse(ds.Tables[0].Rows[0]["ProductSectionParintId"].ToString()); } if (ds.Tables[0].Rows[0]["IsShow"] != null && ds.Tables[0].Rows[0]["IsShow"].ToString() != "") { if ((ds.Tables[0].Rows[0]["IsShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsShow"].ToString().ToLower() == "true")) { model.IsShow = true; } else { model.IsShow = false; } } if (ds.Tables[0].Rows[0]["Abstruct"] != null && ds.Tables[0].Rows[0]["Abstruct"].ToString() != "") { model.Abstruct = ds.Tables[0].Rows[0]["Abstruct"].ToString(); } if (ds.Tables[0].Rows[0]["Image"] != null && ds.Tables[0].Rows[0]["Image"].ToString() != "") { model.Image = ds.Tables[0].Rows[0]["Image"].ToString(); } if (ds.Tables[0].Rows[0]["Describe"] != null && ds.Tables[0].Rows[0]["Describe"].ToString() != "") { model.Describe = ds.Tables[0].Rows[0]["Describe"].ToString(); } return model; } else { return null; } } /// <summary> /// 得到一个对象实体 /// </summary> public Model.ChannelProduct GetModel(string Id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Id,ChannelId,ProductSectionId,ProductSectionParintId,IsShow,Abstruct,Image,Describe from ChannelProduct "); strSql.Append(" where Id=@Id "); SqlParameter[] parameters = { new SqlParameter("@Id", SqlDbType.Char,36) }; parameters[0].Value = Id; Model.ChannelProduct model = new Model.ChannelProduct(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "") { model.Id = ds.Tables[0].Rows[0]["Id"].ToString(); } if (ds.Tables[0].Rows[0]["ChannelId"] != null && ds.Tables[0].Rows[0]["ChannelId"].ToString() != "") { model.ChannelId = ds.Tables[0].Rows[0]["ChannelId"].ToString(); } if (ds.Tables[0].Rows[0]["ProductSectionId"] != null && ds.Tables[0].Rows[0]["ProductSectionId"].ToString() != "") { model.ProductSectionId = int.Parse(ds.Tables[0].Rows[0]["ProductSectionId"].ToString()); } if (ds.Tables[0].Rows[0]["ProductSectionParintId"] != null && ds.Tables[0].Rows[0]["ProductSectionParintId"].ToString() != "") { model.ProductSectionParintId = int.Parse(ds.Tables[0].Rows[0]["ProductSectionParintId"].ToString()); } if (ds.Tables[0].Rows[0]["IsShow"] != null && ds.Tables[0].Rows[0]["IsShow"].ToString() != "") { if ((ds.Tables[0].Rows[0]["IsShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsShow"].ToString().ToLower() == "true")) { model.IsShow = true; } else { model.IsShow = false; } } if (ds.Tables[0].Rows[0]["Abstruct"] != null && ds.Tables[0].Rows[0]["Abstruct"].ToString() != "") { model.Abstruct = ds.Tables[0].Rows[0]["Abstruct"].ToString(); } if (ds.Tables[0].Rows[0]["Image"] != null && ds.Tables[0].Rows[0]["Image"].ToString() != "") { model.Image = ds.Tables[0].Rows[0]["Image"].ToString(); } if (ds.Tables[0].Rows[0]["Describe"] != null && ds.Tables[0].Rows[0]["Describe"].ToString() != "") { model.Describe = ds.Tables[0].Rows[0]["Describe"].ToString(); } return model; } else { return null; } } /// <summary> /// 获得数据列表 /// </summary> public DataSet GetList(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select Id,ChannelId,ProductSectionId,ProductSectionParintId,IsShow,Abstruct,Image,Describe "); strSql.Append(" FROM ChannelProduct "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } return DbHelperSQL.Query(strSql.ToString()); } /// <summary> /// 获得前几行数据 /// </summary> public DataSet GetList(int Top, string strWhere, string filedOrder) { StringBuilder strSql = new StringBuilder(); strSql.Append("select "); if (Top > 0) { strSql.Append(" top " + Top.ToString()); } strSql.Append(" Id,ChannelId,ProductSectionId,ProductSectionParintId,IsShow,Abstruct,Image,Describe "); strSql.Append(" FROM ChannelProduct "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } strSql.Append(" order by " + filedOrder); return DbHelperSQL.Query(strSql.ToString()); } /// <summary> /// 获取记录总数 /// </summary> public int GetRecordCount(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) FROM ChannelProduct "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } object obj = DbHelperSQL.GetSingle(strSql.ToString()); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// <summary> /// 分页获取数据列表 /// </summary> public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex) { StringBuilder strSql = new StringBuilder(); strSql.Append("SELECT * FROM ( "); strSql.Append(" SELECT ROW_NUMBER() OVER ("); if (!string.IsNullOrEmpty(orderby.Trim())) { strSql.Append("order by T." + orderby); } else { strSql.Append("order by T.Id desc"); } strSql.Append(")AS Row, T.* from ChannelProduct T "); if (!string.IsNullOrEmpty(strWhere.Trim())) { strSql.Append(" WHERE " + strWhere); } strSql.Append(" ) TT"); strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex); return DbHelperSQL.Query(strSql.ToString()); } public String GetChildProductSectionId(String ChannelId, Int32 ProductSectionId, Boolean? IsShow) { StringBuilder strSql = new StringBuilder(); List<SqlParameter> parameters = new List<SqlParameter>(); strSql.Append("SELECT ProductSectionId FROM "); strSql.Append("ChannelProduct"); strSql.Append(" WHERE ProductSectionParintId = @ProductSectionParintId"); parameters.Add(new SqlParameter("@ProductSectionParintId", SqlDbType.Int, 4) { Value = ProductSectionId }); if (IsShow != null && IsShow.HasValue) { strSql.Append(" AND "); strSql.Append(" IsShow = @IsShow"); parameters.Add(new SqlParameter("@IsShow", SqlDbType.Bit, 1) { Value = IsShow.Value }); } var ds = DbHelperSQL.Query(strSql.ToString(),parameters.ToArray()); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { var dt = ds.Tables[0]; StringBuilder ids = new StringBuilder(ProductSectionId.ToString()); for (int i = 0; i < dt.Rows.Count; i++) { ids.Append("," + dt.Rows[i][0]); } return ids.ToString(); } else { return ProductSectionId.ToString(); } } } }