using System; using System.Data; using System.Text; using System.Data.SqlClient; using YTSoft.DBUtility; using YTSoft.BaseCallCenter.Model; using System.Collections.Generic; namespace YTSoft.BaseCallCenter.DAL { /// /// 数据访问类:T_Com_Class /// public partial class T_Com_Class { public T_Com_Class() { } #region BasicMethod /// /// 是否存在该记录 /// public bool Exists(int Classid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) from T_Com_Class"); strSql.Append(" where Classid=@Classid"); SqlParameter[] parameters = { new SqlParameter("@Classid", SqlDbType.Int,4) }; parameters[0].Value = Classid; return DbHelperSQL.Exists(strSql.ToString(), parameters); } /// /// 增加一条数据 /// public int Add(YTSoft.BaseCallCenter.Model.T_Com_Class model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into T_Com_Class("); strSql.Append("Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid)"); strSql.Append(" values ("); strSql.Append("@Classcode,@Classname,@Parentid,@Parentcode,@ParentName,@Sort,@iconClose,@iconOpen,@Describe,@Remark,@typeid)"); strSql.Append(";select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@Classcode", SqlDbType.VarChar,50), new SqlParameter("@Classname", SqlDbType.VarChar,50), new SqlParameter("@Parentid", SqlDbType.Int,4), new SqlParameter("@Parentcode", SqlDbType.VarChar,50), new SqlParameter("@ParentName", SqlDbType.VarChar,50), new SqlParameter("@Sort", SqlDbType.Int,4), new SqlParameter("@iconClose", SqlDbType.VarChar,200), new SqlParameter("@iconOpen", SqlDbType.VarChar,200), new SqlParameter("@Describe", SqlDbType.VarChar,250), new SqlParameter("@Remark", SqlDbType.VarChar,50), new SqlParameter("@typeid", SqlDbType.Int,4)}; parameters[0].Value = model.Classcode; parameters[1].Value = model.Classname; parameters[2].Value = model.Parentid; parameters[3].Value = model.Parentcode; parameters[4].Value = model.ParentName; parameters[5].Value = model.Sort; parameters[6].Value = model.iconClose; parameters[7].Value = model.iconOpen; parameters[8].Value = model.Describe; parameters[9].Value = model.Remark; parameters[10].Value = model.typeid; object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// /// 更新一条数据 /// public bool Update(YTSoft.BaseCallCenter.Model.T_Com_Class model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update T_Com_Class set "); strSql.Append("Classcode=@Classcode,"); strSql.Append("Classname=@Classname,"); strSql.Append("Parentid=@Parentid,"); strSql.Append("Parentcode=@Parentcode,"); strSql.Append("ParentName=@ParentName,"); strSql.Append("Sort=@Sort,"); strSql.Append("iconClose=@iconClose,"); strSql.Append("iconOpen=@iconOpen,"); strSql.Append("Describe=@Describe,"); strSql.Append("Remark=@Remark,"); strSql.Append("typeid=@typeid"); strSql.Append(" where Classid=@Classid"); SqlParameter[] parameters = { new SqlParameter("@Classcode", SqlDbType.VarChar,50), new SqlParameter("@Classname", SqlDbType.VarChar,50), new SqlParameter("@Parentid", SqlDbType.Int,4), new SqlParameter("@Parentcode", SqlDbType.VarChar,50), new SqlParameter("@ParentName", SqlDbType.VarChar,50), new SqlParameter("@Sort", SqlDbType.Int,4), new SqlParameter("@iconClose", SqlDbType.VarChar,200), new SqlParameter("@iconOpen", SqlDbType.VarChar,200), new SqlParameter("@Describe", SqlDbType.VarChar,250), new SqlParameter("@Remark", SqlDbType.VarChar,50), new SqlParameter("@typeid", SqlDbType.Int,4), new SqlParameter("@Classid", SqlDbType.Int,4)}; parameters[0].Value = model.Classcode; parameters[1].Value = model.Classname; parameters[2].Value = model.Parentid; parameters[3].Value = model.Parentcode; parameters[4].Value = model.ParentName; parameters[5].Value = model.Sort; parameters[6].Value = model.iconClose; parameters[7].Value = model.iconOpen; parameters[8].Value = model.Describe; parameters[9].Value = model.Remark; parameters[10].Value = model.typeid; parameters[11].Value = model.Classid; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 删除一条数据 /// public bool Delete(int Classid) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from T_Com_Class "); strSql.Append(" where Classid=@Classid"); SqlParameter[] parameters = { new SqlParameter("@Classid", SqlDbType.Int,4) }; parameters[0].Value = Classid; int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } } /// /// 批量删除数据 /// public bool DeleteList(string Classidlist) { StringBuilder strSql = new StringBuilder(); strSql.Append("delete from T_Com_Class "); strSql.Append(" where Classid in (" + Classidlist + ") "); int rows = DbHelperSQL.ExecuteSql(strSql.ToString()); if (rows > 0) { return true; } else { return false; } } /// /// 得到一个对象实体 /// public YTSoft.BaseCallCenter.Model.T_Com_Class GetModel(int Classid) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid from T_Com_Class "); strSql.Append(" where Classid=@Classid"); SqlParameter[] parameters = { new SqlParameter("@Classid", SqlDbType.Int,4) }; parameters[0].Value = Classid; YTSoft.BaseCallCenter.Model.T_Com_Class model = new YTSoft.BaseCallCenter.Model.T_Com_Class(); DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return DataRowToModel(ds.Tables[0].Rows[0]); } else { return null; } } public YTSoft.BaseCallCenter.Model.T_Com_Class GetModelByWhere(string where) { StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid from T_Com_Class "); if (!string.IsNullOrEmpty(where)) { strSql.Append(" where " + where); } DataSet ds = DbHelperSQL.Query(strSql.ToString()); if (ds.Tables[0].Rows.Count > 0) { return DataRowToModel(ds.Tables[0].Rows[0]); } else { return null; } } /// /// 得到一个对象实体 /// public YTSoft.BaseCallCenter.Model.T_Com_Class DataRowToModel(DataRow row) { YTSoft.BaseCallCenter.Model.T_Com_Class model = new YTSoft.BaseCallCenter.Model.T_Com_Class(); if (row != null) { if (row["Classid"] != null && row["Classid"].ToString() != "") { model.Classid = int.Parse(row["Classid"].ToString()); } if (row["Classcode"] != null) { model.Classcode = row["Classcode"].ToString(); } if (row["Classname"] != null) { model.Classname = row["Classname"].ToString(); } if (row["Parentid"] != null && row["Parentid"].ToString() != "") { model.Parentid = int.Parse(row["Parentid"].ToString()); } if (row["Parentcode"] != null) { model.Parentcode = row["Parentcode"].ToString(); } if (row["ParentName"] != null) { model.ParentName = row["ParentName"].ToString(); } if (row["Sort"] != null && row["Sort"].ToString() != "") { model.Sort = int.Parse(row["Sort"].ToString()); } if (row["iconClose"] != null) { model.iconClose = row["iconClose"].ToString(); } if (row["iconOpen"] != null) { model.iconOpen = row["iconOpen"].ToString(); } if (row["Describe"] != null) { model.Describe = row["Describe"].ToString(); } if (row["Remark"] != null) { model.Remark = row["Remark"].ToString(); } if (row["typeid"] != null && row["typeid"].ToString() != "") { model.typeid = int.Parse(row["typeid"].ToString()); } } return model; } /// /// 获得数据列表 /// public DataSet GetList(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid "); strSql.Append(" FROM T_Com_Class "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } return DbHelperSQL.Query(strSql.ToString()); } /// /// 获得前几行数据 /// 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(" Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid "); strSql.Append(" FROM T_Com_Class "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } strSql.Append(" order by " + filedOrder); return DbHelperSQL.Query(strSql.ToString()); } /// /// 获取记录总数 /// public int GetRecordCount(string strWhere) { StringBuilder strSql = new StringBuilder(); strSql.Append("select count(1) FROM T_Com_Class "); if (strWhere.Trim() != "") { strSql.Append(" where " + strWhere); } object obj = DbHelperSQL.GetSingle(strSql.ToString()); if (obj == null) { return 0; } else { return Convert.ToInt32(obj); } } /// /// 分页获取数据列表 /// 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.Classid desc"); } strSql.Append(")AS Row, T.* from T_Com_Class 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 DataSet GetList(int PageSize,int PageIndex,string strWhere) { SqlParameter[] parameters = { new SqlParameter("@tblName", SqlDbType.VarChar, 255), new SqlParameter("@fldName", SqlDbType.VarChar, 255), new SqlParameter("@PageSize", SqlDbType.Int), new SqlParameter("@PageIndex", SqlDbType.Int), new SqlParameter("@IsReCount", SqlDbType.Bit), new SqlParameter("@OrderType", SqlDbType.Bit), new SqlParameter("@strWhere", SqlDbType.VarChar,1000), }; parameters[0].Value = "T_Com_Class"; parameters[1].Value = "Classid"; parameters[2].Value = PageSize; parameters[3].Value = PageIndex; parameters[4].Value = 0; parameters[5].Value = 0; parameters[6].Value = strWhere; return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds"); }*/ #endregion BasicMethod #region 获取树结构 /// /// 获得当前节点下的所有子集以tree的格式 /// public TreeNodeModel GetTreeJsonModel(int parentId, int type = 0) { TreeNodeModel returnModel = new TreeNodeModel(); DataSet ds = null; TreeNodeModel model = new TreeNodeModel(); if (parentId == 0) { ds = GetList(string.Format( " 1=1 Order by sort asc", parentId)); List modelList = new List(); model.id = 0; model.name = "通用字典"; model.code = 0; model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png"; model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png"; model.open = "true"; model.children = modelList; } else { ds = GetList(string.Format("typeid={0} Or Classid={0} Order by sort asc", parentId)); Model.T_Com_Class modelclass = GetModel(parentId); if (modelclass == null) { return returnModel; } else { List modelList = new List(); model.id = modelclass.Classid; model.name = modelclass.Classname; model.code = modelclass.Classid; model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png"; model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png"; model.open = "true"; model.IsOrPrivate = 0; model.children = modelList; } } if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { returnModel = GetTreeModel(ds.Tables[0], model, type); } return returnModel; } /// /// 获取chlid /// /// 数据结合 /// 当前model /// public TreeNodeModel GetTreeModel(DataTable table, TreeNodeModel model, int type) { DataRow[] rows = table.Select("Parentid=" + model.id); if (rows != null && rows.Length > 0) { foreach (DataRow row in rows) { List modelList = new List(); TreeNodeModel childModel = new TreeNodeModel(); childModel.id = int.Parse(row["Classid"].ToString()); childModel.name = row["Classname"].ToString(); childModel.code = row["Remark"].ToInt32(); childModel.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png"; childModel.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png"; childModel.open = "false"; childModel.IsOrPrivate = 1; childModel.children = modelList; model.children.Add(childModel); GetTreeModel(table, childModel, type); } } return model; } /// /// 获得当前节点下的所有子集以tree的格式 /// public TreeNodeModel GetTreeJsonModelTurn(string usercode) { // Classid,Classcode,Classname,Parentid,Parentcode,ParentName,Sort,iconClose,iconOpen,Describe,Remark,typeid TreeNodeModel returnModel = new TreeNodeModel(); DataSet ds = null; TreeNodeModel model = new TreeNodeModel(); Model.T_Com_Class modelclass = GetModelByWhere(string.Format("parentid=(select Classid from T_Com_Class where Classname='常用人员' and parentid=0) and Remark='{0}'",usercode)); if (modelclass != null) { ds = GetList(string.Format("typeid={0} Or parentid={0} Order by sort desc", modelclass.Classid)); List modelList = new List(); model.id = modelclass.Classid; model.name = "常用名单"; model.code = modelclass.Classid; model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png"; model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png"; model.open = "true"; model.IsOrPrivate = 0; model.children = modelList; } else { List modelList = new List(); model.id = 0; model.name = "常用名单"; model.code = 0; model.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png"; model.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png"; model.open = "true"; model.IsOrPrivate = 0; model.children = null; } if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { returnModel = GetTreeModelTurn(ds.Tables[0], model); } else { returnModel = model; } return returnModel; } /// /// 获取chlid /// /// 数据结合 /// 当前model /// public TreeNodeModel GetTreeModelTurn(DataTable table, TreeNodeModel model) { DataRow[] rows = table.Select("Parentid=" + model.id); if (rows != null && rows.Length > 0) { foreach (DataRow row in rows) { List modelList = new List(); TreeNodeModel childModel = new TreeNodeModel(); childModel.id = int.Parse(row["Classid"].ToString()); childModel.name = row["Classname"].ToString(); childModel.code = row["Remark"].ToInt32(); childModel.iconClose = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_close.png"; childModel.iconOpen = "../../Content/layui/zTree/v3/css/zTreeStyle/img/diy/1_open.png"; childModel.open = "false"; childModel.IsOrPrivate = 1; childModel.children = modelList; model.children.Add(childModel); GetTreeModelTurn(table, childModel); } } return model; } #endregion } }