asp.net(C#)无限栏目分级程序代码分享(3) 核心代码之移动栏目
移动栏目#region 移动栏目
public int moveClass(string table,int classid,int target)
{
if (!checkExist(table,classid) )
return 1;//要移动的栏目不存在
if(target!=0)
{
if(!checkExist(table,target))
return 2;//目标栏目不存在
}
//要移动的类别的所有的子栏目
string moveclassids;
moveclassids=getChildren(table,classid);
string[] arr=moveclassids.Split('','');
string temp="," + moveclassids;
if (temp.IndexOf("," + target.ToString())!=-1)
return 3;//不能指定该类别的下属类别作为所属类别
string sql;
//先处理要移动的栏目的信息#region //先处理要移动的栏目的信息
string parentpath="";
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
int last_orderid=0;
sql="select * from "+ table + " where classid=" + classid;
IDataReader dr=base.getdr(sql);
if (dr.Read() )
{
parentpath=dr["parentpath"].ToString();
orderid=(int)dr["orderid"];
rootid=(int)dr["rootid"];
previd=(int)dr["previd"];
nextid=(int)dr["nextid"];
depth=(int)dr["depth"];
parentid=(int)dr["parentid"];
child=(int)dr["child"];
}
dr.Close();
dr.Dispose();
if(depth>0)
//更新父系栏目的子栏目数目
base.exesql("update "+ table +" set child=child-1 where ClassID=" + parentid);
//修改上一类别的NextID和下一类别的PrevID
if (previd>0)
base.exesql("update "+ table +" set NextID=" + nextid + " where ClassID=" + previd);
if (nextid>0)
base.exesql("update "+ table +" set PrevID=" + previd + " where ClassID=" + nextid);
//得到要移动的栏目的子栏目里最大的orderid
if(child>0)
{
sql="select max(orderid) from " + table + " where classid in (" + moveclassids + ")";
last_orderid=(int)base.getvalue(sql);
//处理要移动的类别树后面的栏目的orderid,全部-(arr.Length+1)
base.exesql("update " + table + " set orderid=orderid-" + (arr.Length+1) + " where orderid>" + last_orderid);
}
else
{
last_orderid=orderid;
//处理要移动的类别树后面的栏目的orderid,全部-1
base.exesql("update " + table + " set orderid=orderid-1 where orderid>" + last_orderid);
}
#endregion
//处理目标类别的信息#region//处理目标类别的信息
string target_parentpath="0";
int target_orderid=0,target_rootid=0,target_previd=0,target_nextid=0,target_depth=0,target_parentid=0,target_child=0;
int target_last_child_classid=0;
int target_last_orderid=0;
string target_children="";
//是不是作为一级栏目#region//是不是作为一级栏目
if(target>0)
{
sql="select * from "+ table + " where classid=" + target;
dr=base.getdr(sql);
if (dr.Read() )
{
target_parentpath=dr["parentpath"].ToString()+"," + target;//新的父系路径
target_orderid=(int)dr["orderid"];
target_rootid=(int)dr["rootid"];
target_previd=(int)dr["previd"];
target_nextid=(int)dr["nextid"];
target_depth=(int)dr["depth"];
target_parentid=(int)dr["parentid"];
target_child=(int)dr["child"];
}
dr.Close();
dr.Dispose();
//更新目标栏目的子栏目数目
base.exesql("update "+ table +" set child=child+1 where ClassID=" + target);
target_children=this.getChildren(table,target);
if (target_child>0)
{
//如果有子栏目找到最后一个子栏目的orderid
sql="select max(orderid) from " + table + " where classid in (" + target_children + ")";
target_last_orderid=(int)base.getvalue(sql);
//找到目标类别的一级子类别最后一个classid,并更新它的nextid=classid
sql="select classid from " + table + " where parentid=" + target + " order by orderid desc";
object temp_=base.getvalue(sql);
target_last_child_classid=(int)temp_;
base.exesql("update " + table + " set nextid=" + classid + " where classid=" + target_last_child_classid);
}
else
{
target_last_orderid=target_orderid;
target_last_child_classid=0;
}
if (child>0)
//处理目标类别树后面的栏目的orderid,全部加上(arr.Length+1)
base.exesql("update " + table + " set orderid=orderid + " + (arr.Length+1) + " where orderid>" + target_last_orderid);
else
base.exesql("update " + table + " set orderid=orderid + 1 where orderid>" + target_last_orderid);
}
else//作为一级类别
{
//重新定义target_depth
target_depth=-1;
//重新定义target_last_orderid
if (child >0)
sql="select max(orderid) from " + table + " where classid not in (" + (classid + "," + moveclassids) + ")";
//_______________________________________________________________这里解决当要移动的栏目有子栏目的时候,orderid出现的小问题.
else
sql="select max(orderid) from " + table + " where classid<>" + classid;//有可能要移动的类别在最后一个,这里把它排除
object _temp;
_temp=base.getvalue(sql);
if(_temp!=null)
target_last_orderid=(int)_temp;
//重新定义target_rootid
target_rootid=classid;
//得到previd
sql="select max(classid) from " + table + " where parentid=0 and classid<>" + classid;
_temp=base.getvalue(sql);
if(_temp!=null)
{
target_previd=(int)_temp;
//修改上一个一级栏目的nextid
base.exesql("update " + table + "set nextid="+ classid + " where classid=" + target_previd);
}
}
#endregion
#endregion
综合处理#region 综合处理
if (child>0)//要移动的类别有子栏目
{
int depth_d=0;//depth的减量
if(target==0)
depth_d=depth;
else
depth_d=depth-(target_depth+1);
//更新字栏目的父系路径为 target_parentpath + "," + classid ,orderid
for(int x=0;x<arr.Length;x++)
{
//注意这里要解决depth和parentpath的嵌套问题,即要移动的类别的子类别还有子类别
string path;
sql="select parentpath from " + table + " where classid=" + arr[x];
path=base.getvalue(sql).ToString();
path=path.Replace((parentpath + "," + classid),(target_parentpath + "," + classid));
base.exesql("update " + table +" set depth=depth - "+ depth_d +", parentpath=''"+ path + "'',rootid="+ target_rootid +",orderid="+ (target_last_orderid + x + 2) +" where classid=" + arr[x]);
//__________________________________________________________________________________________________________________________________________________________//其父系栏目已经加1,这里至少加2
}
}
if (target>0)//更新要移动的类别的parentpath和previd,rootid,orderid,parentid,depth,nextid,target_previd
base.exesql("update " + table +" set nextid=0,depth="+ (target_depth+1) +",parentid="+ target +", parentpath=''"+ target_parentpath +"'',previd="+ target_last_child_classid +" ,rootid="+ target_rootid +",orderid="+ (target_last_orderid+1) +" where classid =" + classid);
else//区别在于previd
base.exesql("update " + table +" set previd="+ target_previd +",nextid=0,depth="+ (target_depth+1) +",parentid="+ target +", parentpath=''"+ target_parentpath +"'' ,rootid="+ target_rootid +",orderid="+ (target_last_orderid+1) +" where classid =" + classid);
#endregion
return 0;
}
#endregion
public int moveClass(string table,int classid,int target)
{
if (!checkExist(table,classid) )
return 1;//要移动的栏目不存在
if(target!=0)
{
if(!checkExist(table,target))
return 2;//目标栏目不存在
}
//要移动的类别的所有的子栏目
string moveclassids;
moveclassids=getChildren(table,classid);
string[] arr=moveclassids.Split('','');
string temp="," + moveclassids;
if (temp.IndexOf("," + target.ToString())!=-1)
return 3;//不能指定该类别的下属类别作为所属类别
string sql;
//先处理要移动的栏目的信息#region //先处理要移动的栏目的信息
string parentpath="";
int orderid=0,rootid=0,previd=0,nextid=0,depth=0,parentid=0,child=0;
int last_orderid=0;
sql="select * from "+ table + " where classid=" + classid;
IDataReader dr=base.getdr(sql);
if (dr.Read() )
{
parentpath=dr["parentpath"].ToString();
orderid=(int)dr["orderid"];
rootid=(int)dr["rootid"];
previd=(int)dr["previd"];
nextid=(int)dr["nextid"];
depth=(int)dr["depth"];
parentid=(int)dr["parentid"];
child=(int)dr["child"];
}
dr.Close();
dr.Dispose();
if(depth>0)
//更新父系栏目的子栏目数目
base.exesql("update "+ table +" set child=child-1 where ClassID=" + parentid);
//修改上一类别的NextID和下一类别的PrevID
if (previd>0)
base.exesql("update "+ table +" set NextID=" + nextid + " where ClassID=" + previd);
if (nextid>0)
base.exesql("update "+ table +" set PrevID=" + previd + " where ClassID=" + nextid);
//得到要移动的栏目的子栏目里最大的orderid
if(child>0)
{
sql="select max(orderid) from " + table + " where classid in (" + moveclassids + ")";
last_orderid=(int)base.getvalue(sql);
//处理要移动的类别树后面的栏目的orderid,全部-(arr.Length+1)
base.exesql("update " + table + " set orderid=orderid-" + (arr.Length+1) + " where orderid>" + last_orderid);
}
else
{
last_orderid=orderid;
//处理要移动的类别树后面的栏目的orderid,全部-1
base.exesql("update " + table + " set orderid=orderid-1 where orderid>" + last_orderid);
}
#endregion
//处理目标类别的信息#region//处理目标类别的信息
string target_parentpath="0";
int target_orderid=0,target_rootid=0,target_previd=0,target_nextid=0,target_depth=0,target_parentid=0,target_child=0;
int target_last_child_classid=0;
int target_last_orderid=0;
string target_children="";
//是不是作为一级栏目#region//是不是作为一级栏目
if(target>0)
{
sql="select * from "+ table + " where classid=" + target;
dr=base.getdr(sql);
if (dr.Read() )
{
target_parentpath=dr["parentpath"].ToString()+"," + target;//新的父系路径
target_orderid=(int)dr["orderid"];
target_rootid=(int)dr["rootid"];
target_previd=(int)dr["previd"];
target_nextid=(int)dr["nextid"];
target_depth=(int)dr["depth"];
target_parentid=(int)dr["parentid"];
target_child=(int)dr["child"];
}
dr.Close();
dr.Dispose();
//更新目标栏目的子栏目数目
base.exesql("update "+ table +" set child=child+1 where ClassID=" + target);
target_children=this.getChildren(table,target);
if (target_child>0)
{
//如果有子栏目找到最后一个子栏目的orderid
sql="select max(orderid) from " + table + " where classid in (" + target_children + ")";
target_last_orderid=(int)base.getvalue(sql);
//找到目标类别的一级子类别最后一个classid,并更新它的nextid=classid
sql="select classid from " + table + " where parentid=" + target + " order by orderid desc";
object temp_=base.getvalue(sql);
target_last_child_classid=(int)temp_;
base.exesql("update " + table + " set nextid=" + classid + " where classid=" + target_last_child_classid);
}
else
{
target_last_orderid=target_orderid;
target_last_child_classid=0;
}
if (child>0)
//处理目标类别树后面的栏目的orderid,全部加上(arr.Length+1)
base.exesql("update " + table + " set orderid=orderid + " + (arr.Length+1) + " where orderid>" + target_last_orderid);
else
base.exesql("update " + table + " set orderid=orderid + 1 where orderid>" + target_last_orderid);
}
else//作为一级类别
{
//重新定义target_depth
target_depth=-1;
//重新定义target_last_orderid
if (child >0)
sql="select max(orderid) from " + table + " where classid not in (" + (classid + "," + moveclassids) + ")";
//_______________________________________________________________这里解决当要移动的栏目有子栏目的时候,orderid出现的小问题.
else
sql="select max(orderid) from " + table + " where classid<>" + classid;//有可能要移动的类别在最后一个,这里把它排除
object _temp;
_temp=base.getvalue(sql);
if(_temp!=null)
target_last_orderid=(int)_temp;
//重新定义target_rootid
target_rootid=classid;
//得到previd
sql="select max(classid) from " + table + " where parentid=0 and classid<>" + classid;
_temp=base.getvalue(sql);
if(_temp!=null)
{
target_previd=(int)_temp;
//修改上一个一级栏目的nextid
base.exesql("update " + table + "set nextid="+ classid + " where classid=" + target_previd);
}
}
#endregion
#endregion
综合处理#region 综合处理
if (child>0)//要移动的类别有子栏目
{
int depth_d=0;//depth的减量
if(target==0)
depth_d=depth;
else
depth_d=depth-(target_depth+1);
//更新字栏目的父系路径为 target_parentpath + "," + classid ,orderid
for(int x=0;x<arr.Length;x++)
{
//注意这里要解决depth和parentpath的嵌套问题,即要移动的类别的子类别还有子类别
string path;
sql="select parentpath from " + table + " where classid=" + arr[x];
path=base.getvalue(sql).ToString();
path=path.Replace((parentpath + "," + classid),(target_parentpath + "," + classid));
base.exesql("update " + table +" set depth=depth - "+ depth_d +", parentpath=''"+ path + "'',rootid="+ target_rootid +",orderid="+ (target_last_orderid + x + 2) +" where classid=" + arr[x]);
//__________________________________________________________________________________________________________________________________________________________//其父系栏目已经加1,这里至少加2
}
}
if (target>0)//更新要移动的类别的parentpath和previd,rootid,orderid,parentid,depth,nextid,target_previd
base.exesql("update " + table +" set nextid=0,depth="+ (target_depth+1) +",parentid="+ target +", parentpath=''"+ target_parentpath +"'',previd="+ target_last_child_classid +" ,rootid="+ target_rootid +",orderid="+ (target_last_orderid+1) +" where classid =" + classid);
else//区别在于previd
base.exesql("update " + table +" set previd="+ target_previd +",nextid=0,depth="+ (target_depth+1) +",parentid="+ target +", parentpath=''"+ target_parentpath +"'' ,rootid="+ target_rootid +",orderid="+ (target_last_orderid+1) +" where classid =" + classid);
#endregion
return 0;
}
#endregion