using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace Lskj.Business.Impl
{
public class BaseSpecImpl : BaseImpl
{
///
/// 说明:删除节点
/// 创建人:龚宇超
/// 创建日期:2018-01-04
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// Name of the table.
/// The key.
/// The value.
/// System.Int32.
public static int DeleteNode(string tableName, string key, string value)
{
string sqlValue = string.Format("delete from {0} where {1}='{2}'", tableName, key, value);
return ExecSqlValue(sqlValue);
}
///
/// 说明:获取子节点个数
/// 创建人:龚宇超
/// 创建日期:2018-01-04
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// Name of the table.
/// The key.
/// The value.
/// System.Int32.
public static int GetNodeChildCount(string tableName,string key,string value)
{
string sqlValue = string.Format("select count(1) from {0} where {1} like '{2}%'", tableName, key, value);
return Convert.ToInt32(GetResult(sqlValue));
}
///
/// 说明:获取最大节点号
/// 创建人:龚宇超
/// 创建日期:2018-01-03
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// Name of the table.
/// The parent identifier.
/// System.String.
///
public static string GetMaxSpeciesNo(string tableName, string parentKey, string parentValue,string firstNode)
{
if (string.IsNullOrWhiteSpace(tableName) || string.IsNullOrWhiteSpace(parentKey) || string.IsNullOrWhiteSpace(parentValue))
return "01";
string maxdivision = string.Empty;
string PrefixNum = string.Empty;
string maxNum = string.Empty;
for (int i = 0; i < firstNode.Length; i++)
{
maxdivision += "_";
if (i != 0) PrefixNum += "0";
maxNum += "9";
}
// SpeciesNo是层级编码,完整编码可能超过bigint范围,只对当前节点编号进行数值运算。
string sqlValue = string.Format("select max(cast(right({1},{4}) as bigint))+1 from {0} where {1} like '{2}{3}'", tableName, parentKey, parentValue, maxdivision, firstNode.Length);
string maxNo = GetResult(sqlValue) + "";
if (string.IsNullOrWhiteSpace(maxNo))
maxNo = parentValue + PrefixNum+"1";
else
maxNo = parentValue + maxNo.PadLeft(firstNode.Length, '0');
maxNo = maxNo.PadLeft(parentValue.Length + firstNode.Length, '0');
if ((PrefixNum + "0").Equals(maxNo.Substring(maxNo.Length - firstNode.Length)))
{
sqlValue = string.Format("select count(1) as cnt from {0} where {1} like '{2}{3}'", tableName, parentKey, parentValue, maxdivision);
if (GetResult(sqlValue) + "" == maxNum)
return maxNum;
// 取未使用的编号
sqlValue = string.Format("select cast(right({1},{4}) as bigint) as SpeciesNo from {0} where {1} like '{2}{3}' order by SpeciesNo", tableName, parentKey, parentValue,maxdivision, firstNode.Length);
DataTable table = GetDataTableResult(sqlValue);
Int64 speciesNo = table.Rows.Count > 0 ? Convert.ToInt64(table.Rows[0]["SpeciesNo"] + "") : 0;
if (speciesNo != 1)
return parentValue +PrefixNum+"1";;
foreach (DataRow item in table.Rows)
{
Int64 rowSpeciesNo = Convert.ToInt64(item["SpeciesNo"] + "");
if (speciesNo == rowSpeciesNo) continue;
if (rowSpeciesNo != speciesNo + 1)
{
maxNo = parentValue + (speciesNo + 1).ToString().PadLeft(firstNode.Length, '0');
break;
}
speciesNo = rowSpeciesNo;
}
}
return maxNo;
}
}
}