using System;
using System.Collections;
using BP.DA;
using System.Data;
using BP.En;
using BP.Sys;
namespace BP.Cloud.Sys
{
///
/// sss
///
public class SysEnumAttr
{
///
/// 标题
///
public const string Lab="Lab";
///
/// Int key
///
public const string IntKey="IntKey";
///
/// EnumKey
///
public const string EnumKey="EnumKey";
///
/// Language
///
public const string Lang="Lang";
///
/// 组织结构编码
///
public const string OrgNo = "OrgNo";
///
/// RefPK
///
public const string RefPK = "RefPK";
}
///
/// SysEnum
///
public class SysEnum : EntityMyPK
{
///
/// 得到一个String By LabKey.
///
///
///
///
public static string GetLabByPK(string EnumKey, int intKey)
{
SysEnum en = new SysEnum(EnumKey,intKey);
return en.Lab;
}
#region 实现基本的方法
///
/// 标签
///
public string Lab
{
get
{
return this.GetValStringByKey(SysEnumAttr.Lab);
}
set
{
this.SetValByKey(SysEnumAttr.Lab,value);
}
}
public string RefPK
{
get
{
return this.GetValStringByKey(SysEnumAttr.RefPK);
}
set
{
this.SetValByKey(SysEnumAttr.RefPK, value);
}
}
public string OrgNo
{
get
{
return this.GetValStringByKey(SysEnumAttr.OrgNo);
}
set
{
this.SetValByKey(SysEnumAttr.OrgNo, value);
}
}
///
/// 标签
///
public string Lang
{
get
{
return this.GetValStringByKey(SysEnumAttr.Lang);
}
set
{
this.SetValByKey(SysEnumAttr.Lang,value);
}
}
///
/// Int val
///
public int IntKey
{
get
{
return this.GetValIntByKey(SysEnumAttr.IntKey);
}
set
{
this.SetValByKey(SysEnumAttr.IntKey,value);
}
}
///
/// EnumKey
///
public string EnumKey
{
get
{
return this.GetValStringByKey(SysEnumAttr.EnumKey);
}
set
{
this.SetValByKey(SysEnumAttr.EnumKey,value);
}
}
#endregion
#region 构造方法
///
/// SysEnum
///
public SysEnum(){}
///
/// 税务编号
///
/// 编号
public SysEnum(string enumKey, int val)
{
this.EnumKey = enumKey;
this.Lang = BP.Web.WebUser.SysLang;
this.IntKey = val;
this.setMyPK(this.EnumKey + "_" + this.Lang + "_" + this.IntKey);
int i = this.RetrieveFromDBSources();
if (i == 0)
{
i = this.Retrieve(SysEnumAttr.EnumKey, enumKey, SysEnumAttr.Lang, BP.Web.WebUser.SysLang,
SysEnumAttr.IntKey, this.IntKey);
SysEnums ses = new SysEnums();
ses.Full(enumKey);
if (i == 0)
{
//尝试注册系统的枚举的配置.
SysEnums myee = new SysEnums(enumKey);
throw new Exception("@ EnumKey=" + EnumKey + " Val=" + val + " Lang=" + Web.WebUser.SysLang + " ...Error");
}
}
}
public SysEnum(string enumKey, string Lang, int val)
{
this.EnumKey = enumKey;
this.Lang = Lang;
this.IntKey = val;
this.setMyPK(this.EnumKey + "_" + this.Lang + "_" + this.IntKey);
int i = this.RetrieveFromDBSources();
if (i == 0)
{
i = this.Retrieve(SysEnumAttr.EnumKey, enumKey, SysEnumAttr.Lang, Lang,
SysEnumAttr.IntKey, this.IntKey);
SysEnums ses = new SysEnums();
ses.Full(enumKey);
if (i == 0)
throw new Exception("@ EnumKey=" + enumKey + " Val=" + val + " Lang=" + Lang + " Error");
}
}
///
/// Map
///
public override Map EnMap
{
get
{
if (this._enMap != null)
return this._enMap;
Map map = new Map("Sys_Enum", "枚举数据");
/*
* 为了能够支持 cloud 我们做了如下变更.
* 1. 增加了OrgNo 字段.
* 2. 如果是单机版用户,原来的业务逻辑不变化. MyPK= EnumKey+"_"+IntKey+'_'+Lang
* 3. 如果是SAAS模式, MyPK= EnumKey+"_"+IntKey+'_'+Lang +"_"+OrgNo
*/
map.AddMyPK();
map.AddTBString(SysEnumAttr.Lab, null, "Lab", true, false, 1, 300, 8);
map.AddTBString(SysEnumAttr.EnumKey, null, "EnumKey", true, false, 1, 100, 8);
map.AddTBInt(SysEnumAttr.IntKey, 0, "Val", true, false);
map.AddTBString(SysEnumAttr.Lang, "CH", "语言", true, false, 0, 10, 8);
map.AddTBString(SysEnumAttr.OrgNo, null, "OrgNo", true, false, 0, 100, 8);
map.AddTBString(SysEnumAttr.RefPK, null, "RefPK", true, false, 0, 100, 8);
this._enMap = map;
return this._enMap;
}
}
#endregion
///
///
///
///
protected override bool beforeUpdateInsertAction()
{
if (DataType.IsNullOrEmpty(this.Lang ) )
this.Lang = BP.Web.WebUser.SysLang;
//设置部门编号.
this.OrgNo = Web.WebUser.OrgNo;
//设置mypk.
this.setMyPK( this.EnumKey + "_" + this.Lang + "_" + this.IntKey+"_"+this.OrgNo);
// 关联主键.
this.RefPK = this.OrgNo + "_" + this.EnumKey; //关联的主键.
return base.beforeUpdateInsertAction();
}
///
/// 枚举类型新增保存后在Frm_RB中增加新的枚举值
///
protected override void afterInsert()
{
//获取引用枚举的表单
string sql =" select distinct(FK_MapData)from Sys_FrmRB where EnumKey='"+this.EnumKey+"'";
DataTable dt = DBAccess.RunSQLReturnTable(sql);
if (dt.Rows.Count == 0)
{
base.afterInsert();
return;
}
foreach (System.Data.DataRow dr in dt.Rows)
{
if (DataType.IsNullOrEmpty(dr[0].ToString()))
continue;
string fk_mapdata = dr[0].ToString();
string mypk = fk_mapdata + "_" + this.EnumKey + "_" + this.IntKey;
FrmRB frmrb = new FrmRB();
if (frmrb.IsExit("MyPK", mypk) == true)
{
frmrb.setLab(this.Lab);
frmrb.Update();
continue;
}
//获取mapAttr
MapAttr mapAttr = new MapAttr();
mapAttr.setMyPK(fk_mapdata + "_" + this.EnumKey);
int i = mapAttr.RetrieveFromDBSources();
if (i!=0)
{
int RBShowModel = mapAttr.GetParaInt("RBShowModel");
FrmRB frmrb1 = new FrmRB();
frmrb1.setMyPK(fk_mapdata + "_" + this.EnumKey + "_"+ this.IntKey);
frmrb.setFK_MapData(fk_mapdata);
frmrb.setKeyOfEn(this.EnumKey);
frmrb.setEnumKey(this.EnumKey);
frmrb.setLab(this.Lab);
frmrb.setIntKey(this.IntKey);
frmrb.Insert();
}
}
base.afterInsert();
}
}
///
/// 纳税人集合
///
public class SysEnums : Entities
{
///
/// 此枚举类型的个数
///
public int Num = -1;
public string ToDesc()
{
string strs = "";
foreach (SysEnum se in this)
{
strs += se.IntKey + " " + se.Lab + ";";
}
return strs;
}
public string GenerCaseWhenForOracle(string enName, string mTable, string key, string field, string enumKey, int def)
{
string sql = (string)Cash.GetObjFormApplication("ESQL" + enName +mTable+ key + "_" + enumKey, null);
// string sql = "";
if (sql != null)
return sql;
if (this.Count == 0)
throw new Exception("@枚举值" + enumKey + "已被删除。");
sql = " CASE NVL(" + mTable + field+","+def+")";
foreach (SysEnum se1 in this)
{
sql += " WHEN " + se1.IntKey + " THEN '" + se1.Lab + "'";
}
SysEnum se = (SysEnum)this.GetEntityByKey(SysEnumAttr.IntKey, def);
if (se == null)
sql += " END \"" + key + "Text\"";
else
sql += " WHEN NULL THEN '" + se.Lab + "' END \"" + key + "Text\"";
Cash.AddObj("ESQL" + enName + mTable + key + "_" + enumKey, Depositary.Application, sql);
return sql;
}
public string GenerCaseWhenForOracle(string mTable, string key, string field, string enumKey, int def)
{
if (this.Count == 0)
throw new Exception("@枚举值(" + enumKey + ")已被删除,无法形成期望的SQL。");
string sql = "";
sql = " CASE " + mTable + field;
foreach (SysEnum se1 in this)
sql += " WHEN " + se1.IntKey + " THEN '" + se1.Lab + "'";
SysEnum se = (SysEnum)this.GetEntityByKey(SysEnumAttr.IntKey, def);
if (se == null)
sql += " END \"" + key + "Text\"";
else
sql += " WHEN NULL THEN '" + se.Lab + "' END \"" + key + "Text\"";
// Cash.AddObj("ESQL" + enName + key + "_" + enumKey, Depositary.Application, sql);
return sql;
}
public void LoadIt(string enumKey)
{
if (this.Full(enumKey) == false)
{
try
{
BP.DA.DBAccess.RunSQL("UPDATE Sys_Enum SET Lang='" + Web.WebUser.SysLang + "' WHERE LANG IS NULL ");
BP.DA.DBAccess.RunSQL("UPDATE Sys_Enum SET MyPK=EnumKey+'_'+Lang+'_'+cast(IntKey as NVARCHAR )");
//增加数据库类型判断
DBUrl dbUrl = new DBUrl();
if (DBType.MSSQL == dbUrl.DBType)
{
BP.DA.DBAccess.RunSQL("UPDATE Sys_Enum SET MyPK=EnumKey+'_'+Lang+'_'+cast(IntKey as NVARCHAR )");
}
else if (DBType.Oracle == dbUrl.DBType || DBType.KingBaseR3==dbUrl.DBType || DBType.KingBaseR6==dbUrl.DBType)
{
BP.DA.DBAccess.RunSQL("UPDATE Sys_Enum SET MyPK = EnumKey || '_' || Lang || '_' || cast(IntKey as VARCHAR(5))");
}
else if (DBType.MySQL == dbUrl.DBType)
{
BP.DA.DBAccess.RunSQL("UPDATE Sys_Enum SET MyPK = CONCAT (EnumKey,'_', Lang,'_',CAST(IntKey AS CHAR(5)))");
}
}
catch
{
}
try
{
BP.Sys.XML.EnumInfoXml xml = new BP.Sys.XML.EnumInfoXml(enumKey);
this.RegIt(enumKey, xml.Vals);
}
catch (Exception ex)
{
throw new Exception("@你没有预制[" + enumKey + "]枚举值。@在修复枚举值出现错误:" + ex.Message);
}
}
}
///
/// 把所有的枚举注册一遍.
///
public static void RegAll()
{
BP.Sys.XML.EnumInfoXmls xmls = new BP.Sys.XML.EnumInfoXmls();
xmls.RetrieveAll();
SysEnums ses = new SysEnums();
foreach (BP.Sys.XML.EnumInfoXml xml in xmls)
ses.RegIt(xml.Key, xml.Vals);
}
///
/// SysEnums
///
///
public SysEnums(string enumKey)
{
this.LoadIt(enumKey);
}
public SysEnums(string enumKey, string vals)
{
if (DataType.IsNullOrEmpty(vals) )
{
this.LoadIt(enumKey);
return;
}
if (this.Full(enumKey) == false)
this.RegIt(enumKey, vals);
}
public void RegIt(string EnumKey, string vals)
{
try
{
string[] strs = vals.Split('@');
SysEnums ens = new SysEnums();
ens.Delete(SysEnumAttr.EnumKey, EnumKey);
this.Clear();
foreach (string s in strs)
{
if (DataType.IsNullOrEmpty(s ) )
continue;
string[] vk = s.Split('=');
SysEnum se = new SysEnum();
se.IntKey = int.Parse(vk[0]);
//杨玉慧
//解决当 枚举值含有 ‘=’号时,保存不进去的方法
string[] kvsValues = new string[vk.Length - 1];
for (int i = 0; i < kvsValues.Length; i++)
{
kvsValues[i] = vk[i + 1];
}
se.Lab = string.Join("=", kvsValues);
se.EnumKey = EnumKey;
se.Lang = BP.Web.WebUser.SysLang;
se.Insert();
this.AddEntity(se);
}
}
catch (Exception ex)
{
throw new Exception(ex.Message + " - " + vals);
}
// this.Full(EnumKey);
}
public bool Full(string enumKey)
{
Entities ens = (Entities)Cash.GetObjFormApplication("EnumOf" + enumKey + Web.WebUser.SysLang, null);
if (ens != null)
{
this.AddEntities(ens);
return true;
}
QueryObject qo = new QueryObject(this);
qo.AddWhere(SysEnumAttr.EnumKey, enumKey);
qo.addAnd();
qo.AddWhere(SysEnumAttr.Lang, BP.Web.WebUser.SysLang);
qo.addOrderBy(SysEnumAttr.IntKey);
if (qo.DoQuery() == 0)
{
/* 看看xml配置里面是否有?*/
return false;
}
Cash.AddObj("EnumOf" + enumKey + Web.WebUser.SysLang, Depositary.Application, this);
return true;
}
/////
///// DBSimpleNoNames
/////
/////
//public DBSimpleNoNames ToEntitiesNoName()
//{
// DBSimpleNoNames ens = new DBSimpleNoNames();
// foreach (SysEnum en in this)
// {
// ens.AddByNoName(en.IntKey.ToString(), en.Lab);
// }
// return ens;
//}
///
///
///
///
///
///
public new int Delete(string key, object val)
{
try
{
Entity en = this.GetNewEntity;
Paras ps = new Paras();
ps.SQL = "DELETE FROM " + en.EnMap.PhysicsTable + " WHERE OrgNo=" + key + "," + key + "=" + en.HisDBVarStr + "p";
ps.Add("p", val);
return en.RunSQL(ps);
}
catch
{
Entity en = this.GetNewEntity;
en.CheckPhysicsTable();
Paras ps = new Paras();
ps.SQL = "DELETE FROM " + en.EnMap.PhysicsTable + " WHERE " + key + "=" + en.HisDBVarStr + "p";
ps.Add("p", val);
return en.RunSQL(ps);
}
}
///
/// SysEnums
///
public SysEnums(){}
///
/// 得到它的 Entity
///
public override Entity GetNewEntity
{
get
{
return new SysEnum();
}
}
///
/// 通过int 得到Lab
///
/// val
/// string val
public string GetLabByVal(int val)
{
foreach(SysEnum en in this)
{
if (en.IntKey == val)
return en.Lab;
}
return null;
}
#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((SysEnum)this[i]);
}
return list;
}
#endregion 为了适应自动翻译成java的需要,把实体转换成List.
}
}