using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Net;
using System.Xml;
using BP.DA;
using BP.En;
using BP.Sys;
using Microsoft.CSharp;
using BP.Web;
using BP.Difference;
namespace BP.Cloud.Sys
{
///
/// 用户自定义表
///
public class SFTableAttr : EntityNoNameAttr
{
///
/// 组织机构编码
///
public const string OrgNo = "OrgNo";
///
/// 是否可以删除
///
public const string IsDel = "IsDel";
///
/// 字段
///
public const string FK_Val = "FK_Val";
///
/// 数据表描述
///
public const string TableDesc = "TableDesc";
///
/// 默认值
///
public const string DefVal = "DefVal";
///
/// 数据源
///
public const string DBSrc = "DBSrc";
///
/// 是否是树
///
public const string IsTree = "IsTree";
///
/// 表类型
///
public const string SrcType = "SrcType";
///
/// 字典表类型
///
public const string CodeStruct = "CodeStruct";
///
/// 表的ID ForSAAS
///
public const string TableID = "TableID";
#region 链接到其他系统获取数据的属性。
///
/// 数据源
///
public const string FK_SFDBSrc = "FK_SFDBSrc";
///
/// 数据源表
///
public const string SrcTable = "SrcTable";
///
/// 显示的值
///
public const string ColumnValue = "ColumnValue";
///
/// 显示的文字
///
public const string ColumnText = "ColumnText";
///
/// 父结点值
///
public const string ParentValue = "ParentValue";
///
/// 查询语句
///
public const string SelectStatement = "SelectStatement";
///
/// 缓存分钟数
///
public const string CashMinute = "CashMinute";
///
/// 最近缓存的时间
///
public const string RootVal = "RootVal";
///
/// 加入日期
///
public const string RDT = "RDT";
#endregion 链接到其他系统获取数据的属性。
}
///
/// 用户自定义表
///
public class SFTable : EntityNoName
{
#region 数据源属性.
///
/// 获得外部数据表
///
public DataTable GenerHisDataTable(Hashtable ht = null)
{
//创建数据源.
SFDBSrc src = new SFDBSrc(this.FK_SFDBSrc);
#region BP类
if (this.SrcType == BP.Sys.SrcType.BPClass)
{
Entities ens = ClassFactory.GetEns(this.No);
return ens.RetrieveAllToTable();
}
#endregion
#region WebServices
// this.SrcType == BP.Sys.SrcType.WebServices,by liuxc
//暂只考虑No,Name结构的数据源,2015.10.04,added by liuxc
if (this.SrcType == BP.Sys.SrcType.WebServices)
{
var td = this.TableDesc.Split(','); //接口名称,返回类型
var ps = (this.SelectStatement ?? string.Empty).Split('&');
var args = new ArrayList();
string[] pa = null;
foreach (var p in ps)
{
if (string.IsNullOrWhiteSpace(p)) continue;
pa = p.Split('=');
if (pa.Length != 2) continue;
//此处要SL中显示表单时,会有问题
try
{
if (pa[1].Contains("@WebUser.No"))
pa[1] = pa[1].Replace("@WebUser.No", BP.Web.WebUser.No);
if (pa[1].Contains("@WebUser.Name"))
pa[1] = pa[1].Replace("@WebUser.Name", BP.Web.WebUser.Name);
if (pa[1].Contains("@WebUser.FK_Dept"))
pa[1] = pa[1].Replace("@WebUser.FK_Dept", BP.Web.WebUser.FK_Dept);
if (pa[1].Contains("@WebUser.FK_DeptName"))
pa[1] = pa[1].Replace("@WebUser.FK_DeptName", BP.Web.WebUser.FK_DeptName);
}
catch
{ }
if (pa[1].Contains("@WorkID"))
pa[1] = pa[1].Replace("@WorkID", HttpContextHelper.RequestParams("WorkID") ?? "");
if (pa[1].Contains("@NodeID"))
pa[1] = pa[1].Replace("@NodeID", HttpContextHelper.RequestParams("NodeID") ?? "");
if (pa[1].Contains("@FK_Node"))
pa[1] = pa[1].Replace("@FK_Node", HttpContextHelper.RequestParams("FK_Node") ?? "");
if (pa[1].Contains("@FK_Flow"))
pa[1] = pa[1].Replace("@FK_Flow", HttpContextHelper.RequestParams("FK_Flow") ?? "");
if (pa[1].Contains("@FID"))
pa[1] = pa[1].Replace("@FID", HttpContextHelper.RequestParams("FID") ?? "");
args.Add(pa[1]);
}
var result = InvokeWebService(src.IP, td[0], args.ToArray());
switch (td[1])
{
case "DataSet":
return result == null ? new DataTable() : (result as DataSet).Tables[0];
case "DataTable":
return result as DataTable;
case "Json":
var jdata = LitJson.JsonMapper.ToObject(result as string);
if (!jdata.IsArray)
throw new Exception("@返回的JSON格式字符串“" + (result ?? string.Empty) + "”不正确");
var dt = new DataTable();
dt.Columns.Add("No", typeof(string));
dt.Columns.Add("Name", typeof(string));
for (var i = 0; i < jdata.Count; i++)
{
dt.Rows.Add(jdata[i]["No"].ToString(), jdata[i]["Name"].ToString());
}
return dt;
case "Xml":
if (result == null || string.IsNullOrWhiteSpace(result.ToString()))
throw new Exception("@返回的XML格式字符串不正确。");
var xml = new XmlDocument();
xml.LoadXml(result as string);
XmlNode root = null;
if (xml.ChildNodes.Count < 2)
root = xml.ChildNodes[0];
else
root = xml.ChildNodes[1];
dt = new DataTable();
dt.Columns.Add("No", typeof(string));
dt.Columns.Add("Name", typeof(string));
foreach (XmlNode node in root.ChildNodes)
{
dt.Rows.Add(node.SelectSingleNode("No").InnerText,
node.SelectSingleNode("Name").InnerText);
}
return dt;
default:
throw new Exception("@不支持的返回类型" + td[1]);
}
}
#endregion
#region SQL查询.外键表/视图,edited by liuxc,2016-12-29
if (this.SrcType == BP.Sys.SrcType.TableOrView)
{
string sql = "SELECT " + this.ColumnValue + " No, " + this.ColumnText + " Name";
if (this.CodeStruct == BP.Sys.CodeStruct.Tree)
sql += ", " + this.ParentValue + " ParentNo";
sql += " FROM " + this.SrcTable;
return src.RunSQLReturnTable(sql);
}
#endregion SQL查询.外键表/视图,edited by liuxc,2016-12-29
#region 动态SQL,edited by liuxc,2016-12-29
if (this.SrcType == BP.Sys.SrcType.SQL)
{
string runObj = this.SelectStatement;
if (DataType.IsNullOrEmpty(runObj))
throw new Exception("@外键类型SQL配置错误," + this.No + " " + this.Name + " 是一个(SQL)类型(" + this.GetValStrByKey("SrcType") + "),但是没有配置sql.");
if (runObj == null)
runObj = string.Empty;
runObj = runObj.Replace("~", "'");
runObj = runObj.Replace("/#", "+"); //为什么?
runObj = runObj.Replace("/$", "-"); //为什么?
if (runObj.Contains("@WebUser.No"))
runObj = runObj.Replace("@WebUser.No", BP.Web.WebUser.No);
if (runObj.Contains("@WebUser.Name"))
runObj = runObj.Replace("@WebUser.Name", BP.Web.WebUser.Name);
if (runObj.Contains("@WebUser.FK_Dept"))
runObj = runObj.Replace("@WebUser.FK_Dept", BP.Web.WebUser.FK_Dept);
if (runObj.Contains("@") == true && ht == null)
throw new Exception("@外键类型SQL错误," + runObj + "部分查询条件没有被替换.");
if (runObj.Contains("@") == true && ht != null)
{
foreach (string key in ht.Keys)
{
//值为空或者null不替换
if (ht[key] == null || ht[key].Equals("") == true)
continue;
if (runObj.Contains("@" + key))
runObj = runObj.Replace("@" + key, ht[key].ToString());
//不包含@则返回SQL语句
if (runObj.Contains("@") == false)
break;
}
}
if (runObj.Contains("@") && BP.Difference.SystemConfig.IsBSsystem == true)
{
/*如果是bs*/
foreach (string key in HttpContextHelper.RequestParamKeys)
{
if (string.IsNullOrEmpty(key))
continue;
runObj = runObj.Replace("@" + key, HttpContextHelper.RequestParams(key));
}
}
if (runObj.Contains("@") == true)
throw new Exception("@外键类型SQL错误," + runObj + "部分查询条件没有被替换.");
DataTable dt = src.RunSQLReturnTable(runObj);
return dt;
}
#endregion
#region 自定义表.
if (this.SrcType == BP.Sys.SrcType.CreateTable)
{
string sql = "SELECT No, Name FROM " + this.No;
return src.RunSQLReturnTable(sql);
}
if (this.SrcType == BP.Sys.SrcType.SysDict)
{
string sql = "SELECT MyPK, BH AS No, Name FROM Sys_SFTableDtl where FK_SFTable='" + this.No + "'";
return src.RunSQLReturnTable(sql);
}
#endregion
return null;
}
public string GenerHisJson()
{
return BP.Tools.Json.ToJson(this.GenerHisDataTable());
}
///
/// 自动生成编号
///
///
public string GenerSFTableNewNo()
{
string table = this.SrcTable;
try
{
string sql = null;
string field = "No";
switch (this.EnMap.EnDBUrl.DBType)
{
case DBType.MSSQL:
sql = "SELECT CONVERT(INT, MAX(CAST(" + field + " as int)) )+1 AS No FROM " + table;
break;
case DBType.PostgreSQL:
case DBType.UX:
sql = "SELECT to_number( MAX(" + field + ") ,'99999999')+1 FROM " + table;
break;
case DBType.Oracle:
case DBType.KingBaseR3:
case DBType.KingBaseR6:
sql = "SELECT MAX(" + field + ") +1 AS No FROM " + table;
break;
case DBType.MySQL:
sql = "SELECT CONVERT(MAX(CAST(" + field + " AS SIGNED INTEGER)),SIGNED) +1 AS No FROM " + table;
break;
case DBType.Informix:
sql = "SELECT MAX(" + field + ") +1 AS No FROM " + table;
break;
case DBType.Access:
sql = "SELECT MAX( [" + field + "]) +1 AS No FROM " + table;
break;
default:
throw new Exception("error");
}
string str = DBAccess.RunSQLReturnValInt(sql, 1).ToString();
if (DataType.IsNullOrEmpty(str ) || str.Equals("0"))
str = "1";
return str.PadLeft(3, '0');
}
catch (Exception)
{
return "";
}
}
///
/// 实例化 WebServices
///
/// WebServices地址
/// 调用的方法
/// 把webservices里需要的参数按顺序放到这个object[]里
public object InvokeWebService(string url, string methodname, object[] args)
{
return null;
/* TODO 2019-07-25 为了合并core,注释掉
//这里的namespace是需引用的webservices的命名空间,在这里是写死的,大家可以加一个参数从外面传进来。
string @namespace = "BP.RefServices";
try
{
if (url.EndsWith(".asmx"))
url += "?wsdl";
else if (url.EndsWith(".svc"))
url += "?singleWsdl";
//获取WSDL
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url);
ServiceDescription sd = ServiceDescription.Read(stream);
string classname = sd.Services[0].Name;
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd, "", "");
CodeNamespace cn = new CodeNamespace(@namespace);
//生成客户端代理类代码
CodeCompileUnit ccu = new CodeCompileUnit();
ccu.Namespaces.Add(cn);
sdi.Import(cn, ccu);
CSharpCodeProvider csc = new CSharpCodeProvider();
ICodeCompiler icc = csc.CreateCompiler();
//设定编译参数
CompilerParameters cplist = new CompilerParameters();
cplist.GenerateExecutable = false;
cplist.GenerateInMemory = true;
cplist.ReferencedAssemblies.Add("System.dll");
cplist.ReferencedAssemblies.Add("System.XML.dll");
cplist.ReferencedAssemblies.Add("System.Web.Services.dll");
cplist.ReferencedAssemblies.Add("System.Data.dll");
//编译代理类
CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);
if (true == cr.Errors.HasErrors)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
{
sb.Append(ce.ToString());
sb.Append(System.Environment.NewLine);
}
throw new Exception(sb.ToString());
}
//生成代理实例,并调用方法
System.Reflection.Assembly assembly = cr.CompiledAssembly;
Type t = assembly.GetType(@namespace + "." + classname, true, true);
object obj = Activator.CreateInstance(t);
System.Reflection.MethodInfo mi = t.GetMethod(methodname);
return mi.Invoke(obj, args);
}
catch
{
return null;
}
*/
}
#endregion
#region 链接到其他系统获取数据的属性
///
/// 数据源
///
public string FK_SFDBSrc
{
get
{
return this.GetValStringByKey(SFTableAttr.FK_SFDBSrc);
}
set
{
this.SetValByKey(SFTableAttr.FK_SFDBSrc, value);
}
}
public string OrgNo
{
get
{
return this.GetValStringByKey(SysEnumAttr.OrgNo);
}
set
{
this.SetValByKey(SysEnumAttr.OrgNo, value);
}
}
public string FK_SFDBSrcT
{
get
{
return this.GetValRefTextByKey(SFTableAttr.FK_SFDBSrc);
}
}
///
/// 数据缓存时间
///
public string RootVal
{
get
{
return this.GetValStringByKey(SFTableAttr.RootVal);
}
set
{
this.SetValByKey(SFTableAttr.RootVal, value);
}
}
///
/// 同步间隔
///
public int CashMinute
{
get
{
return this.GetValIntByKey(SFTableAttr.CashMinute);
}
set
{
this.SetValByKey(SFTableAttr.CashMinute, value);
}
}
///
/// 物理表名称
///
public string SrcTable
{
get
{
string str = this.GetValStringByKey(SFTableAttr.SrcTable);
if (DataType.IsNullOrEmpty(str ))
return this.No;
return str;
}
set
{
this.SetValByKey(SFTableAttr.SrcTable, value);
}
}
///
/// 值/主键字段名
///
public string ColumnValue
{
get
{
return this.GetValStringByKey(SFTableAttr.ColumnValue);
}
set
{
this.SetValByKey(SFTableAttr.ColumnValue, value);
}
}
///
/// 显示字段/显示字段名
///
public string ColumnText
{
get
{
return this.GetValStringByKey(SFTableAttr.ColumnText);
}
set
{
this.SetValByKey(SFTableAttr.ColumnText, value);
}
}
///
/// 父结点字段名
///
public string ParentValue
{
get
{
return this.GetValStringByKey(SFTableAttr.ParentValue);
}
set
{
this.SetValByKey(SFTableAttr.ParentValue, value);
}
}
///
/// 查询语句
///
public string SelectStatement
{
get
{
return this.GetValStringByKey(SFTableAttr.SelectStatement);
}
set
{
this.SetValByKey(SFTableAttr.SelectStatement, value);
}
}
///
/// 加入日期
///
public string RDT
{
get
{
return this.GetValStringByKey(SFTableAttr.RDT);
}
set
{
this.SetValByKey(SFTableAttr.RDT, value);
}
}
#endregion
#region 属性
///
/// 是否是类
///
public bool IsClass
{
get
{
if (this.No.Contains("."))
return true;
else
return false;
}
}
///
/// 是否是树形实体?
///
public bool IsTree
{
get
{
if (this.CodeStruct == BP.Sys.CodeStruct.NoName)
return false;
return true;
}
}
///
/// 数据源类型
///
public SrcType SrcType
{
get
{
if (this.No.Contains("BP.") == true)
return SrcType.BPClass;
else
{
SrcType src = (SrcType)this.GetValIntByKey(SFTableAttr.SrcType);
if (src == BP.Sys.SrcType.BPClass)
return BP.Sys.SrcType.CreateTable;
return src;
}
}
set
{
this.SetValByKey(SFTableAttr.SrcType, (int)value);
}
}
///
/// 数据源类型名称
///
public string SrcTypeText
{
get
{
switch (this.SrcType)
{
case BP.Sys.SrcType.TableOrView:
if (this.IsClass)
return "实体类";
else
return "表/视图";
case BP.Sys.SrcType.SQL:
return "SQL表达式";
case BP.Sys.SrcType.WebServices:
return "WebServices";
default:
return "";
}
}
}
///
/// 字典表类型
/// 0:NoName类型
/// 1:NoNameTree类型
/// 2:NoName行政区划类型
///
public CodeStruct CodeStruct
{
get
{
return (CodeStruct)this.GetValIntByKey(SFTableAttr.CodeStruct);
}
set
{
this.SetValByKey(SFTableAttr.CodeStruct, (int)value);
}
}
///
/// 编码类型
///
public string CodeStructT
{
get
{
return this.GetValRefTextByKey(SFTableAttr.CodeStruct);
}
}
///
/// 值
///
public string FK_Val
{
get
{
return this.GetValStringByKey(SFTableAttr.FK_Val);
}
set
{
this.SetValByKey(SFTableAttr.FK_Val, value);
}
}
///
/// 描述
///
public string TableDesc
{
get
{
return this.GetValStringByKey(SFTableAttr.TableDesc);
}
set
{
this.SetValByKey(SFTableAttr.TableDesc, value);
}
}
///
/// 默认值
///
public string DefVal
{
get
{
return this.GetValStringByKey(SFTableAttr.DefVal);
}
set
{
this.SetValByKey(SFTableAttr.DefVal, value);
}
}
public EntitiesNoName HisEns
{
get
{
if (this.IsClass)
{
EntitiesNoName ens = (EntitiesNoName)BP.En.ClassFactory.GetEns(this.No);
ens.RetrieveAll();
return ens;
}
BP.En.GENoNames ges = new GENoNames(this.No, this.Name);
ges.RetrieveAll();
return ges;
}
}
#endregion
#region 构造方法
public override UAC HisUAC
{
get
{
UAC uac = new UAC();
uac.OpenForSysAdmin();
uac.IsInsert = false;
return uac;
}
}
///
/// 用户自定义表
///
public SFTable()
{
}
public SFTable(string mypk)
{
this.No = mypk;
try
{
this.Retrieve();
}
catch (Exception ex)
{
switch (this.No)
{
case "BP.Pub.NYs":
this.Name = "年月";
this.FK_Val = "FK_NY";
this.Insert();
break;
case "BP.Pub.YFs":
this.Name = "月";
this.FK_Val = "FK_YF";
this.Insert();
break;
case "BP.Pub.Days":
this.Name = "天";
this.FK_Val = "FK_Day";
this.Insert();
break;
case "BP.Pub.NDs":
this.Name = "年";
this.FK_Val = "FK_ND";
this.Insert();
break;
default:
throw new Exception(ex.Message);
}
}
}
///
/// EnMap
///
public override Map EnMap
{
get
{
if (this._enMap != null)
return this._enMap;
Map map = new Map("Sys_SFTable", "字典表");
map.AddTBStringPK(SFTableAttr.No, null, "表英文名称", true, false, 1, 200, 20);
map.AddTBString(SFTableAttr.Name, null, "表中文名称", true, false, 0, 200, 20);
map.AddDDLSysEnum(SFTableAttr.SrcType, 0, "数据表类型", true, false, SFTableAttr.SrcType,
"@0=本地的类@1=创建表@2=表或视图@3=SQL查询表@4=WebServices@5=微服务Handler外部数据源@6=JavaScript外部数据源@7=动态Json");
map.AddDDLSysEnum(SFTableAttr.CodeStruct, 0, "字典表类型", true, false, SFTableAttr.CodeStruct);
map.AddTBString(SFTableAttr.RootVal, null, "根节点值", false, false, 0, 200, 20);
map.AddTBString(SFTableAttr.FK_Val, null, "默认创建的字段名", true, false, 0, 200, 20);
map.AddTBString(SFTableAttr.TableDesc, null, "表描述", true, false, 0, 200, 20);
map.AddTBString(SFTableAttr.DefVal, null, "默认值", true, false, 0, 200, 20);
//数据源.
map.AddDDLEntities(SFTableAttr.FK_SFDBSrc, "local", "数据源", new BP.Sys.SFDBSrcs(), true);
map.AddTBString(SFTableAttr.SrcTable, null, "数据源表", false, false, 0, 200, 20);
map.AddTBString(SFTableAttr.ColumnValue, null, "显示的值(编号列)", false, false, 0, 200, 20);
map.AddTBString(SFTableAttr.ColumnText, null, "显示的文字(名称列)", false, false, 0, 200, 20);
map.AddTBString(SFTableAttr.ParentValue, null, "父级值(父级列)", false, false, 0, 200, 20);
map.AddTBString(SFTableAttr.SelectStatement, null, "查询语句", true, false, 0, 1000, 600, true);
map.AddTBDateTime(SFTableAttr.RDT, null, "加入日期", false, false);
map.AddTBString(SysEnumAttr.OrgNo, null, "OrgNo", true, false, 0, 100, 8);
//查找.
map.AddSearchAttr(SFTableAttr.FK_SFDBSrc);
RefMethod rm = new RefMethod();
rm.Title = "查看数据";
rm.ClassMethodName = this.ToString() + ".DoEdit";
rm.RefMethodType = RefMethodType.RightFrameOpen;
rm.IsForEns = false;
map.AddRefMethod(rm);
//rm = new RefMethod();
//rm.Title = "创建Table向导";
//rm.ClassMethodName = this.ToString() + ".DoGuide";
//rm.RefMethodType = RefMethodType.RightFrameOpen;
//rm.IsForEns = false;
//map.AddRefMethod(rm);
//rm = new RefMethod();
//rm.Title = "数据源管理";
//rm.ClassMethodName = this.ToString() + ".DoMangDBSrc";
//rm.RefMethodType = RefMethodType.RightFrameOpen;
//rm.IsForEns = false;
//map.AddRefMethod(rm);
this._enMap = map;
return this._enMap;
}
}
#endregion
///
/// 数据源管理
///
///
public string DoMangDBSrc()
{
return "../../Comm/Sys/SFDBSrcNewGuide.htm";
}
///
/// 创建表向导
///
///
public string DoGuide()
{
return "../../../WF/Admin/FoolFormDesigner/CreateSFGuide.htm";
}
///
/// 编辑数据
///
///
public string DoEdit()
{
if (this.IsClass)
return "../../Comm/Ens.htm?EnsName=" + this.No;
else
return "../../Admin/CCFormDesigner/SFTable/SFTableEditData.htm?FK_SFTable=" + this.No;
}
public string IsCanDelete()
{
MapAttrs attrs = new MapAttrs();
attrs.Retrieve(MapAttrAttr.UIBindKey, this.No);
if (attrs.Count != 0)
{
string err = "";
foreach (MapAttr item in attrs)
err += " @ " + item.MyPK + " " + item.Name;
return "err@如下实体字段在引用:" + err + "。您不能删除该表。";
}
return null;
}
protected override bool beforeDelete()
{
string delMsg = this.IsCanDelete();
if (delMsg != null)
throw new Exception(delMsg);
return base.beforeDelete();
}
protected override bool beforeInsert()
{
//利用这个时间串进行排序.
this.RDT = DataType.CurrentDateTime;
//SaaS模式下前缀增加OrgNo
this.No = BP.Web.WebUser.OrgNo + "_" + this.No;
this.OrgNo = BP.Web.WebUser.OrgNo;
#region 如果是本地类. @于庆海.
if (this.SrcType == BP.Sys.SrcType.BPClass)
{
Entities ens = ClassFactory.GetEns(this.No);
Entity en = ens.GetNewEntity;
this.Name = en.EnDesc;
//检查是否是树结构.
if (en.IsTreeEntity == true)
this.CodeStruct = BP.Sys.CodeStruct.Tree;
else
this.CodeStruct = BP.Sys.CodeStruct.NoName;
}
#endregion 如果是本地类.
#region 本地类,物理表..
if (this.SrcType == BP.Sys.SrcType.CreateTable)
{
if (DBAccess.IsExitsObject(this.No) == true)
{
return base.beforeInsert();
//throw new Exception("err@表名[" + this.No + "]已经存在,请使用其他的表名.");
}
string sql = "";
if (this.CodeStruct == BP.Sys.CodeStruct.NoName || this.CodeStruct == BP.Sys.CodeStruct.GradeNoName)
{
sql = "CREATE TABLE " + this.No + " (";
sql += "No varchar(30) NOT NULL,";
sql += "Name varchar(3900) NULL";
sql += ")";
}
if (this.CodeStruct == BP.Sys.CodeStruct.Tree)
{
sql = "CREATE TABLE " + this.No + " (";
sql += "No varchar(30) NOT NULL,";
sql += "Name varchar(3900) NULL,";
sql += "ParentNo varchar(3900) NULL";
sql += ")";
}
this.RunSQL(sql);
//初始化数据.
this.InitDataTable();
}
#endregion 如果是本地类.
return base.beforeInsert();
}
protected override void afterInsert()
{
try
{
if (this.SrcType == BP.Sys.SrcType.TableOrView)
{
//暂时这样处理
string sql = "CREATE VIEW " + this.No + " (";
sql += "[No],";
sql += "[Name]";
sql += (this.CodeStruct == BP.Sys.CodeStruct.Tree ? ",[ParentNo])" : ")");
sql += " AS ";
sql += "SELECT " + this.ColumnValue + " No," + this.ColumnText + " Name" + (this.CodeStruct == BP.Sys.CodeStruct.Tree ? ("," + this.ParentValue + " ParentNo") : "") + " FROM " + this.SrcTable + (string.IsNullOrWhiteSpace(this.SelectStatement) ? "" : (" WHERE " + this.SelectStatement));
if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
{
sql = sql.Replace("[", "`").Replace("]", "`");
}
else
{
sql = sql.Replace("[", "").Replace("]", "");
}
this.RunSQL(sql);
}
//if (this.SrcType == BP.Sys.SrcType.SQL)
//{
// //暂时这样处理
// string sql = "CREATE VIEW " + this.No + " (";
// sql += "[No],";
// sql += "[Name]";
// sql += (this.CodeStruct == BP.Sys.CodeStruct.Tree ? ",[ParentNo])" : ")");
// sql += " AS ";
// sql += this.SelectStatement;
// if (BP.Difference.SystemConfig.AppCenterDBType == DBType.MySQL)
// {
// sql = sql.Replace("[", "`").Replace("]", "`");
// }
// else
// {
// sql = sql.Replace("[", "").Replace("]", "");
// }
// this.RunSQL(sql);
//}
}
catch (Exception ex)
{
//创建视图失败时,删除此记录,并提示错误
this.DirectDelete();
throw ex;
}
base.afterInsert();
}
///
/// 获得该数据源的数据
///
///
public DataTable GenerData_bak()
{
string sql = "";
DataTable dt = null;
if (this.SrcType == BP.Sys.SrcType.CreateTable)
{
sql = "SELECT No,Name FROM " + this.SrcTable;
dt = this.RunSQLReturnTable(sql);
}
if (this.SrcType == BP.Sys.SrcType.TableOrView)
{
sql = "SELECT No,Name FROM " + this.SrcTable;
dt = this.RunSQLReturnTable(sql);
}
if (dt == null)
throw new Exception("@没有判断的数据.");
dt.Columns[0].ColumnName = "No";
dt.Columns[1].ColumnName = "Name";
return dt;
}
///
/// 返回json.
///
///
public string GenerDataOfJson()
{
return BP.Tools.Json.ToJson(this.GenerHisDataTable());
}
///
/// 初始化数据.
///
public void InitDataTable()
{
DataTable dt = this.GenerHisDataTable();
string sql = "";
if (dt.Rows.Count == 0)
{
/*初始化数据.*/
if (this.CodeStruct == BP.Sys.CodeStruct.Tree)
{
sql = "INSERT INTO " + this.SrcTable + " (No,Name,ParentNo) VALUES('1','" + this.Name + "','0') ";
this.RunSQL(sql);
for (int i = 1; i < 4; i++)
{
string no = i.ToString();
no = no.PadLeft(3, '0');
sql = "INSERT INTO " + this.SrcTable + " (No,Name,ParentNo) VALUES('" + no + "','Item" + no + "','1') ";
this.RunSQL(sql);
}
}
if (this.CodeStruct == BP.Sys.CodeStruct.NoName)
{
for (int i = 1; i < 4; i++)
{
string no = i.ToString();
no = no.PadLeft(3, '0');
sql = "INSERT INTO " + this.SrcTable + " (No,Name) VALUES('" + no + "','Item" + no + "') ";
this.RunSQL(sql);
}
}
}
}
}
///
/// 用户自定义表s
///
public class SFTables : EntitiesNoName
{
#region 构造
///
/// 用户自定义表s
///
public SFTables()
{
}
///
/// 得到它的 Entity
///
public override Entity GetNewEntity
{
get
{
return new SFTable();
}
}
#endregion
#region 为了适应自动翻译成java的需要,把实体转换成List.
///
/// 转化成 java list,C#不能调用.
///
/// List
public System.Collections.Generic.IList ToJavaList()
{
return (System.Collections.Generic.IList)this;
}
///
/// 转化成list
///
/// List
public System.Collections.Generic.List Tolist()
{
System.Collections.Generic.List list = new System.Collections.Generic.List();
for (int i = 0; i < this.Count; i++)
{
list.Add((SFTable)this[i]);
}
return list;
}
#endregion 为了适应自动翻译成java的需要,把实体转换成List.
}
}