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 Microsoft.CSharp;
using BP.Web;
using BP.Difference;
namespace BP.Sys
{
///
/// 表数据来源类型
///
public enum SrcType
{
///
/// 本地的类
///
BPClass = 0,
///
/// 通过ccform创建表
///
CreateTable = 1,
///
/// 表或视图
///
TableOrView = 2,
///
/// SQL查询数据
///
SQL = 3,
///
/// WebServices
///
WebServices = 4,
///
/// hand
///
Handler = 5,
///
/// JS请求数据.
///
JQuery = 6,
///
/// 系统字典表
///
SysDict = 7,
///
/// WebApi接口
///
WebApi = 8
}
///
/// 编码表类型
///
public enum CodeStruct
{
///
/// 普通的编码表
///
NoName,
///
/// 树编码表(No,Name,ParentNo)
///
Tree,
///
/// 行政机构编码表
///
GradeNoName
}
///
/// 编号生成规则
///
public enum NoGenerModel
{
///
/// 自定义
///
None,
///
/// 流水号
///
ByLSH,
///
/// 标签的全拼
///
ByQuanPin,
///
/// 标签的简拼
///
ByJianPin,
///
/// 按GUID生成
///
ByGUID
}
///
/// 用户自定义表
///
public class SFTableAttr : EntityNoNameAttr
{
///
/// 是否可以删除
///
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";
///
/// 是否自动生成编号
///
public const string IsAutoGenerNo = "IsAutoGenerNo";
///
/// 编号生成规则
///
public const string NoGenerModel = "NoGenerModel";
#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";
///
/// 组织编号
///
public const string OrgNo = "OrgNo";
#endregion 链接到其他系统获取数据的属性。
///
/// AtPara
///
public const string AtPara = "AtPara";
}
///
/// 用户自定义表
///
public class SFTable : EntityNoName
{
#region 数据源属性.
///
/// 判断是否存在 @honyan.
///
public override bool IsExits
{
get
{
if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
return base.IsExits;
this.No = BP.Web.WebUser.OrgNo + "_" + this.No;
return base.IsExits;
}
}
///
/// 获得外部数据表
///
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_DeptName"))
pa[1] = pa[1].Replace("@WebUser.FK_DeptName", BP.Web.WebUser.FK_DeptName);
if (pa[1].Contains("@WebUser.FK_Dept"))
pa[1] = pa[1].Replace("@WebUser.FK_Dept", BP.Web.WebUser.FK_Dept);
}
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());
if (DataType.IsNullOrEmpty(result as string) == true)
throw new Exception("err@获得结果错误.");
switch (td[1])
{
case "DataSet":
return result == null ? new DataTable() : (result as DataSet).Tables[0];
case "DataTable":
return result as DataTable;
case "Json":
return BP.Tools.Json.ToDataTable(result as string);
//var jdata = bp. 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];
DataTable 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 WebApi接口
if (this.SrcType == BP.Sys.SrcType.WebApi)
{
//返回值
string postData = "";
//用户输入的webAPI地址
string apiUrl = this.SelectStatement;
if (apiUrl.Contains("@WebApiHost"))//可以替换配置文件中配置的webapi地址
apiUrl = apiUrl.Replace("@WebApiHost", BP.Difference.SystemConfig.AppSettings["WebApiHost"]);
string[] strs = apiUrl.Split('?');
//api接口地址
string apiHost = strs[0];
//api参数
string apiParams = "";
if (strs.Length == 2)
apiParams = strs[1];
//执行POST
postData = BP.Tools.PubGlo.HttpPostConnect(apiHost, apiParams);
DataTable dt = null;
try
{
dt = BP.Tools.Json.ToDataTable(postData);
dt.Columns[0].ColumnName = "No";
dt.Columns[1].ColumnName = "Name";
if (dt.Columns.Count == 3)
dt.Columns[2].ColumnName = "ParentNo";
return dt;
}
catch (Exception ex)
{
throw new Exception("设置的WebAPI接口返回的数据出错,请检查接口返回值。");
}
}
#endregion WebApi接口
#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;
DataTable dt = src.RunSQLReturnTable(sql);
if (SystemConfig.AppCenterDBFieldCaseModel != FieldCaseModel.None)
{
dt.Columns[0].ColumnName = "No";
dt.Columns[1].ColumnName = "Name";
if (dt.Columns.Count == 3)
dt.Columns[2].ColumnName = "ParentNo";
}
return dt;
}
#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_DeptName"))
runObj = runObj.Replace("@WebUser.FK_DeptName", BP.Web.WebUser.FK_DeptName);
if (runObj.Contains("@WebUser.FK_Dept"))
runObj = runObj.Replace("@WebUser.FK_Dept", BP.Web.WebUser.FK_Dept);
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 = null;
try
{
dt = src.RunSQLReturnTable(runObj);
}
catch (Exception ex)
{
throw new Exception("err@获得SFTable(" + this.No + "," + this.Name + ")出现错误:SQL[" + runObj + "],数据库异常信息:" + ex.Message);
}
if (SystemConfig.AppCenterDBFieldCaseModel != FieldCaseModel.None)
{
dt.Columns[0].ColumnName = "No";
dt.Columns[1].ColumnName = "Name";
if (dt.Columns.Count == 3)
dt.Columns[2].ColumnName = "ParentNo";
}
return dt;
}
#endregion
#region 自定义表.
if (this.SrcType == BP.Sys.SrcType.CreateTable)
{
string sql = "SELECT No, Name FROM " + this.No;
DataTable dt= src.RunSQLReturnTable(sql);
if (SystemConfig.AppCenterDBFieldCaseModel != FieldCaseModel.None)
{
dt.Columns[0].ColumnName = "No";
dt.Columns[1].ColumnName = "Name";
if (dt.Columns.Count == 3)
dt.Columns[2].ColumnName = "ParentNo";
}
return dt;
}
if (this.SrcType == BP.Sys.SrcType.SysDict)
{
string sql = "";
if (this.CodeStruct == 0)
sql = "SELECT MyPK, BH AS No, Name FROM Sys_SFTableDtl WHERE FK_SFTable='" + this.No + "'";
else
sql = "SELECT MyPK, BH AS No, Name,ParentNo FROM Sys_SFTableDtl WHERE FK_SFTable='" + this.No + "'";
DataTable dt = src.RunSQLReturnTable(sql);
if (dt.Rows.Count == 0)
{
if (this.CodeStruct == CodeStruct.NoName)
{
SFTableDtl dtl = new SFTableDtl();
dtl.BH = "001";
dtl.Name = "Name 001";
dtl.MyPK = this.No + "_" + dtl.BH;
dtl.FK_SFTable = this.No;
dtl.Insert();
dtl = new SFTableDtl();
dtl.BH = "002";
dtl.Name = "Name 002";
dtl.MyPK = this.No + "_" + dtl.BH;
dtl.FK_SFTable = this.No;
dtl.Insert();
dtl = new SFTableDtl();
dtl.BH = "003";
dtl.Name = "Name 002";
dtl.MyPK = this.No + "_" + dtl.BH;
dtl.FK_SFTable = this.No;
dtl.Insert();
}
//如果是tree.
if (this.CodeStruct == CodeStruct.Tree)
{
SFTableDtl dtl = new SFTableDtl();
dtl.BH = "001";
dtl.Name = "根目录";
dtl.MyPK = this.No + "_" + dtl.BH;
dtl.FK_SFTable = this.No;
dtl.ParentNo = this.No;
dtl.Insert();
dtl = new SFTableDtl();
dtl.BH = "002";
dtl.Name = "Node 001";
dtl.MyPK = this.No + "_" + dtl.BH;
dtl.FK_SFTable = this.No;
dtl.ParentNo = "001";
dtl.Insert();
dtl = new SFTableDtl();
dtl.BH = "003";
dtl.Name = "Node 002";
dtl.MyPK = this.No + "_" + dtl.BH;
dtl.FK_SFTable = this.No;
dtl.ParentNo = "001";
dtl.Insert();
}
dt = src.RunSQLReturnTable(sql);
}
if (SystemConfig.AppCenterDBFieldCaseModel != FieldCaseModel.None)
{
dt.Columns[0].ColumnName = "No";
dt.Columns[1].ColumnName = "Name";
if (dt.Columns.Count == 3)
dt.Columns[2].ColumnName = "ParentNo";
}
return dt;
}
#endregion
throw new Exception("err@没有判断的类型." + this.SrcTable);
}
///
/// 修改外键数据
///
///
public void UpdateData(string No, string Name, string FK_SFTable)
{
var sql = "";
if (this.SrcType == BP.Sys.SrcType.SysDict)
sql = "UPDATE Sys_SFTableDtl SET Name = '" + Name + "' WHERE MyPK='" + FK_SFTable + "_" + No + "'";
else
sql = "UPDATE " + FK_SFTable + " SET Name = '" + Name + "' WHERE No = '" + No + "'";
DBAccess.RunSQL(sql);
}
///
/// 新增外键数据
///
///
public void InsertData(string No, string Name, string FK_SFTable)
{
var sql = "";
if (this.SrcType == BP.Sys.SrcType.SysDict)
sql = "insert into Sys_SFTableDtl(MyPK,FK_SFTable,BH,Name) values('" + FK_SFTable + "_" + No + "','" + FK_SFTable + "','" + No + "','" + Name + "')";
else
sql = "insert into " + FK_SFTable + "(No,Name) values('" + No + "','" + Name + "')";
DBAccess.RunSQL(sql);
}
///
/// 删除外键数据
///
///
public void DeleteData(string No, string FK_SFTable)
{
var sql = "";
if (this.SrcType == BP.Sys.SrcType.SysDict)
sql = "delete from Sys_SFTableDtl where MyPK='" + FK_SFTable + "_" + No + "'";
else
sql = "delete from " + FK_SFTable + " where No = '" + No + "'";
DBAccess.RunSQL(sql);
}
///
/// @wwh 修改了名字.
///
///
public string GenerJson()
{
return BP.Tools.Json.ToJson(this.GenerHisDataTable());
}
///
/// 自动生成编号
///
///
public string GenerSFTableNewNo()
{
string table = this.SrcTable;
NoGenerModel NoGenerModel = this.NoGenerModel;
if (NoGenerModel == NoGenerModel.ByGUID)//编号按guid生成
{
string guid = DBAccess.GenerGUID();
return guid;
}
else if (NoGenerModel == NoGenerModel.ByLSH)//编号按流水号生成
{
if (this.SrcType == SrcType.SysDict)//如果是按系统字典表
{
try
{
string sql = null;
string field = "BH";
switch (this.EnMap.EnDBUrl.DBType)
{
case DBType.MSSQL:
sql = "SELECT CONVERT(INT, MAX(CAST(" + field + " as int)) )+1 AS No FROM Sys_SFTableDtl where FK_SFTable='" + table + "'";
break;
case DBType.PostgreSQL:
case DBType.UX:
sql = "SELECT to_number( MAX(" + field + ") ,'99999999')+1 FROM Sys_SFTableDtl where FK_SFTable='" + table + "'";
break;
case DBType.Oracle:
case DBType.KingBaseR3:
case DBType.KingBaseR6:
sql = "SELECT MAX(" + field + ") +1 AS No FROM Sys_SFTableDtl where FK_SFTable='" + table + "'";
break;
case DBType.MySQL:
sql = "SELECT CONVERT(MAX(CAST(" + field + " AS SIGNED INTEGER)),SIGNED) +1 AS No FROM Sys_SFTableDtl where FK_SFTable='" + table + "'";
break;
case DBType.Informix:
sql = "SELECT MAX(" + field + ") +1 AS No FROM Sys_SFTableDtl where FK_SFTable='" + table + "'";
break;
case DBType.Access:
sql = "SELECT MAX( [" + field + "]) +1 AS No FROM Sys_SFTableDtl where FK_SFTable='" + table + "'";
break;
default:
throw new Exception("error");
}
string str = DBAccess.RunSQLReturnValInt(sql, 1).ToString();
if (str == "0" || str == "")
str = "1";
return str.PadLeft(3, '0');
}
catch (Exception)
{
return "";
}
}
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 (str == "0" || str == "")
str = "1";
return str.PadLeft(3, '0');
}
catch (Exception)
{
return "";
}
}
else //其他的生成编号默认为空
{
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 OrgNo
{
get
{
return this.GetValStringByKey(SFTableAttr.OrgNo);
}
set
{
this.SetValByKey(SFTableAttr.OrgNo, value);
}
}
///
/// 数据源
///
public string FK_SFDBSrc
{
get
{
return this.GetValStringByKey(SFTableAttr.FK_SFDBSrc);
}
set
{
this.SetValByKey(SFTableAttr.FK_SFDBSrc, 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) == true)
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.ToUpper().Contains("BP.") == true)
return SrcType.BPClass;
else
{
SrcType src = (SrcType)this.GetValIntByKey(SFTableAttr.SrcType);
if (src == BP.Sys.SrcType.BPClass)
return Sys.SrcType.CreateTable;
return src;
}
}
set
{
this.SetValByKey(SFTableAttr.SrcType, (int)value);
}
}
///
/// 数据源类型名称
///
public string SrcTypeText
{
get
{
switch (this.SrcType)
{
case Sys.SrcType.TableOrView:
if (this.IsClass)
return "实体类";
else
return "表/视图";
case Sys.SrcType.SQL:
return "SQL表达式";
case Sys.SrcType.WebServices:
return "WebServices";
case Sys.SrcType.WebApi:
return "WebApi接口";
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 NoGenerModel NoGenerModel
{
get
{
return (NoGenerModel)this.GetValIntByKey(SFTableAttr.NoGenerModel);
}
set
{
this.SetValByKey(SFTableAttr.NoGenerModel, (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.Readonly(); //@hongyan.
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=系统字典表@8=WebApi接口");
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.AddDDLSysEnum(SFTableAttr.NoGenerModel, 1, "编号生成规则", true, true, SFTableAttr.NoGenerModel,
"@0=自定义@1=流水号@2=标签的全拼@3=标签的简拼@4=按GUID生成");
//数据源.
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(SFTableAttr.OrgNo, null, "组织编号", false, false, 0, 100, 20);
map.AddTBString(SFTableAttr.AtPara, null, "AtPara", false, false, 0, 50, 20);
for (int i = 0; i < 50; i++)
{
map.AddTBString("BH" + i, null, "编号", true, true, 0, 3, 20);
map.AddTBString("Name" + i, null, "名称", true, false, 0, 50, 20);
}
//查找.
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 = "修改属性";
rm.ClassMethodName = this.ToString() + ".DoAttr";
rm.RefMethodType = RefMethodType.RightFrameOpen;
rm.IsForEns = true;
map.AddRefMethod(rm);
rm = new RefMethod();
rm.Title = "新建字典";
rm.ClassMethodName = this.ToString() + ".DoNew";
rm.RefMethodType = RefMethodType.RightFrameOpen;
rm.IsForEns = true;
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 DoAttr()
{
return "../../Comm/EnOnly.htm?EnName=BP.Sys.SFTable&No=" + this.No;
}
public string DoNew()
{
return "../../Admin/FoolFormDesigner/SFTable/Default.htm?DoType=New&FromApp=SL&s=0.3256071044807922";
}
///
/// 数据源管理
///
///
public string DoMangDBSrc()
{
return "../../Comm/Sys/SFDBSrcNewGuide.htm";
}
///
/// 创建表向导
///
///
public string DoGuide()
{
return "../../Admin/FoolFormDesigner/CreateSFGuide.htm";
}
///
/// 编辑数据
///
///
public string DoEdit()
{
if (this.IsClass)
{
return "../../Comm/Ens.htm?EnsName=" + this.No;
}
else
{
return "../../Admin/FoolFormDesigner/SFTableEditData.htm?FK_SFTable=" + this.No;
}
}
///
/// 检查是否有依赖的引用?
///
///
public string IsCanDelete()
{
MapAttrs mattrs = new MapAttrs();
mattrs.Retrieve(MapAttrAttr.UIBindKey, this.No);
if (mattrs.Count != 0)
{
string err = "";
foreach (MapAttr item in mattrs)
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()
{
if (BP.Difference.SystemConfig.CCBPMRunModel != CCBPMRunModel.Single)
{
this.OrgNo = BP.Web.WebUser.OrgNo;
this.No = this.OrgNo + "_" + this.No;
}
//利用这个时间串进行排序.
this.RDT = DataType.CurrentDateTime;
#region 如果是 系统字典表.
if (this.SrcType == BP.Sys.SrcType.SysDict && BP.Difference.SystemConfig.CCBPMRunModel == CCBPMRunModel.SAAS)
{
if (this.CodeStruct == CodeStruct.NoName)
{
SFTableDtl dtl = new SFTableDtl();
dtl.setMyPK(this.No + "_001");
dtl.BH = "001";
dtl.Name = "Item1";
dtl.FK_SFTable = this.No;
dtl.Insert();
dtl = new SFTableDtl();
dtl.setMyPK(this.No + "_002");
dtl.BH = "002";
dtl.Name = "Item2";
dtl.FK_SFTable = this.No;
dtl.Insert();
dtl = new SFTableDtl();
dtl.setMyPK(this.No + "_003");
dtl.BH = "003";
dtl.Name = "Item3";
dtl.FK_SFTable = this.No;
dtl.Insert();
}
if (this.CodeStruct == CodeStruct.Tree)
{
SFTableDtl dtl = new SFTableDtl();
dtl.setMyPK(this.No + "_001");
dtl.BH = "001";
dtl.Name = "Item1";
dtl.FK_SFTable = this.No;
dtl.ParentNo = "0";
dtl.Insert();
dtl = new SFTableDtl();
dtl.setMyPK(this.No + "_002");
dtl.BH = "002";
dtl.Name = "Item2";
dtl.FK_SFTable = this.No;
dtl.ParentNo = "001";
dtl.Insert();
dtl = new SFTableDtl();
dtl.setMyPK(this.No + "_003");
dtl.BH = "003";
dtl.Name = "Item3";
dtl.FK_SFTable = this.No;
dtl.ParentNo = "001";
dtl.Insert();
}
}
#endregion 如果是 系统字典表.
#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);
}
}
catch (Exception ex)
{
//创建视图失败时,删除此记录,并提示错误
this.DirectDelete();
throw ex;
}
base.afterInsert();
}
///
/// 返回json.
///
///
public string GenerDataOfJson()
{
// BP.DA.Log.DebugWriteInfo("******** "+this.No+" - "+this.Name+" ************************ ");
// BP.DA.Log.DebugWriteInfo(this.ToJson());
DataTable dt = this.GenerHisDataTable();
string json = BP.Tools.Json.ToJson(dt);
// BP.DA.Log.DebugWriteInfo("************************"+ this.No+this.Name+ " ************************ json: \t\n" + //json);
return json;
// 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();
}
}
///
/// 重写查询全部的方法
///
///
public override int RetrieveAll()
{
if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
return base.RetrieveAll("RDT");
return this.Retrieve("OrgNo", WebUser.OrgNo, "RDT");
}
#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.
}
}