AIMS/AIMSExtension/DocumentEntityMethod.cs
2022-08-23 21:12:59 +08:00

335 lines
16 KiB
C#

using HelperDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace AIMSExtension
{
public static class DocumentEntityMethod
{
public static object GetDocumentEntitry(int OperationApplyId, string docName, string objName, object obj)
{
//BindPropertyList(obj, objName);
Assembly assembly = Assembly.Load("KHD_BoardArea");
Type typeDBHelper = assembly.GetType("KHD_BoardArea.DocumentEntity." + objName);
if (typeDBHelper != null)
{
string TableName = typeDBHelper.Name;
if (typeDBHelper.IsDefined(typeof(TableAttribute), false))
{
var attr = (TableAttribute)typeDBHelper.GetCustomAttributes(typeof(TableAttribute), false)[0];
TableName = attr.TableName;
}
List<MyPropInfo> MyPropInfoList = typeDBHelper.GetProperties().Select(a => AttributeHelper.GetPropertyExtend(a)).ToList();
for (int i = MyPropInfoList.Count - 1; i >= 0; i--)
{
if (MyPropInfoList[i].PropertyName == "OperationApplyId" || MyPropInfoList[i].PropertyName == "OpeRecord" || MyPropInfoList[i].PropertyName == "OpeInfo")
{
MyPropInfoList.Remove(MyPropInfoList[i]);
}
}
var fields = string.Join(",", MyPropInfoList.Select(a => $"[{a.DataFieldName}] as [{a.PropertyName}]"));
string docWhere = docName == "" ? "" : string.Format(" and [文书名称]='{0}' ", docName);
var sql = string.Format("select top 10 {1} from [{0}] where OperationApplyId={2} {3} ", TableName, fields == "" ? "*" : fields, OperationApplyId, docWhere);
obj = DBHelper.Get(sql, obj, OperationApplyId);
}
return obj;
}
public static int UpdateDocumentEntitry(string objName, object obj)
{
//BindPropertyList(obj, objName);
Assembly assembly = Assembly.Load("KHD_BoardArea");
Type typeDBHelper = assembly.GetType("KHD_BoardArea.DocumentEntity." + objName);
if (typeDBHelper != null)
{
int updateid = 0;
string TableName = typeDBHelper.Name;
if (typeDBHelper.IsDefined(typeof(TableAttribute), false))
{
var attr = (TableAttribute)typeDBHelper.GetCustomAttributes(typeof(TableAttribute), false)[0];
TableName = attr.TableName;
}
List<MyPropInfo> MyPropInfoList = typeDBHelper.GetProperties().Select(a => AttributeHelper.GetPropertyExtend(a)).ToList();
for (int i = MyPropInfoList.Count - 1; i >= 0; i--)
{
if (MyPropInfoList[i].PropertyName == "Id" || MyPropInfoList[i].PropertyName == "OperationApplyId" || MyPropInfoList[i].PropertyName == "OpeRecord" || MyPropInfoList[i].PropertyName == "OpeInfo")
{
MyPropInfoList.Remove(MyPropInfoList[i]);
continue;
}
foreach (PropertyInfo p in obj.GetType().GetProperties())
{
if (p.Name == "Id")
{
updateid = Convert.ToInt32(p.GetValue(obj, null));
}
if (p.Name == MyPropInfoList[i].DataFieldName)
{
object value = p.GetValue(obj, null);
MyPropInfoList[i].DataValue = value == null ? "" : value.ToString();
if (p.Name == "操作时间")
MyPropInfoList[i].DataValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
var fields = string.Join(",", MyPropInfoList.Select(a => $"[{a.DataFieldName}] = '{a.DataValue}'"));
var sql = string.Format("Update {0} Set {1} where Id={2}", TableName, fields, updateid);
return DBHelper.ExecNonQuery(sql);
}
return 0;
}
public static int InsertDocumentEntitry(string objName, object obj)
{
//BindPropertyList(obj, objName);
Assembly assembly = Assembly.Load("KHD_BoardArea");
Type typeDBHelper = assembly.GetType("KHD_BoardArea.DocumentEntity." + objName);
if (typeDBHelper != null)
{
string TableName = typeDBHelper.Name;
if (typeDBHelper.IsDefined(typeof(TableAttribute), false))
{
var attr = (TableAttribute)typeDBHelper.GetCustomAttributes(typeof(TableAttribute), false)[0];
TableName = attr.TableName;
}
List<MyPropInfo> MyPropInfoList = typeDBHelper.GetProperties().Select(a => AttributeHelper.GetPropertyExtend(a)).ToList();
for (int i = MyPropInfoList.Count - 1; i >= 0; i--)
{
if (MyPropInfoList[i].PropertyName == "Id" || MyPropInfoList[i].PropertyName == "OpeRecord" || MyPropInfoList[i].PropertyName == "OpeInfo")
{
MyPropInfoList.Remove(MyPropInfoList[i]);
continue;
}
foreach (PropertyInfo p in obj.GetType().GetProperties())
{
if (p.Name == MyPropInfoList[i].DataFieldName)
{
object value = p.GetValue(obj, null);
MyPropInfoList[i].DataValue = value == null ? "" : value.ToString();
if (p.Name == "操作时间")
MyPropInfoList[i].DataValue = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}
}
var fields = string.Join(",", MyPropInfoList.Select(a => $"[{a.DataFieldName}]"));
var fields2 = string.Join(",", MyPropInfoList.Select(a => $"'{a.DataValue}'"));
var sql = string.Format("Insert Into {0}({1}) Values({2});select @@identity ", TableName, fields, fields2);
int insertId = Convert.ToInt32(DBHelper.ExecuteScalar(sql));
foreach (PropertyInfo p in obj.GetType().GetProperties())
{
if (p.Name == "Id")
{
p.SetValue(obj, Convert.ChangeType(insertId, p.PropertyType), null);
break;
}
}
return insertId;
}
return 0;
}
public static string GetDictionaryValuesById(string Ids, string DictionaryName)
{
string DictionaryValues = "";
//DictionaryValues = BBasicDictionary.GetBasicDictionaryByIds(Ids, DictionaryName);
return DictionaryValues;
}
public static void SetOperationRecordValue(object operationRecord, string propertyName, string value)
{
//OperationRecord _operationRecord = operationRecord as OperationRecord;
//if (_operationRecord == null) return;
//int i = 0;
//string[] items = propertyName.Split('.');
//try
//{
// if (items.Length < 1) return;
// if (items[1] == "OperationApplyRef")
// {
// //i = BOperationApply.Update(items[2] + "='" + value + "' where Id=" + _operationRecord.OperationApplyId, null);
// }
// else if (items[1] == "PatientRef")
// {
// if (value == "")
// {
// //i = BPatients.Update(items[2] + "= null where Id=" + _operationRecord.PatientId, null);
// }
// else
// {
// //i = BPatients.Update(items[2] + "='" + value + "' where Id=" + _operationRecord.PatientId, null);
// }
// }
// else
// {
// //i = BOperationRecord.Update(items[1] + "='" + value + "' where Id=" + _operationRecord.Id, null);
// }
//}
//catch (Exception ex)
//{
// PublicMethod.WriteLog(new Exception("回写数据出错:" + propertyName + " 值:" + value + " 错误:" + ex.Message));
//}
}
public static void UpdateDataBaseEntitry(Type typeDBHelper)
{
if (typeDBHelper != null)
{
string TableName = typeDBHelper.Name;
if (typeDBHelper.IsDefined(typeof(TableAttribute), false))
{
var attr = (TableAttribute)typeDBHelper.GetCustomAttributes(typeof(TableAttribute), false)[0];
TableName = attr.TableName;
}
//判断如果表不存在 插入新表逻辑
CreateDataBase(TableName);
//如果表存在 判断是否有列变动
List<MyPropInfo> MyPropInfoList = typeDBHelper.GetProperties().Select(a => AttributeHelper.GetPropertyExtend(a)).ToList();
List<TableStructure> tableStructures = GetTableStructure(TableName);
for (int i = MyPropInfoList.Count - 1; i >= 0; i--)
{
if (MyPropInfoList[i].PropertyName == "OpeRecord" || MyPropInfoList[i].PropertyName == "OpeInfo" || MyPropInfoList[i].PropertyName.Contains("Ref") || MyPropInfoList[i].PropertyName.Contains("List"))
{
MyPropInfoList.Remove(MyPropInfoList[i]);
continue;
}
foreach (TableStructure p in tableStructures)
{
if (MyPropInfoList[i].PropertyName == p. || MyPropInfoList[i].PropertyName.ToLower() == p..ToLower())
{
MyPropInfoList.Remove(MyPropInfoList[i]);
break;
}
}
}
//列变动是新增还是修改 动态生成脚本执行
if (MyPropInfoList.Count > 0)
{
try
{
var fields = string.Join(" ", MyPropInfoList.Select(a => $" alter table {TableName} add [{a.DataFieldName}] {(a.SqlDbType == System.Data.SqlDbType.Int ? "int" : "[nvarchar](500)")} NULL "));
PublicMethod.WriteLog(new Exception(fields));
DBHelper.ExecNonQuery(fields);
}
catch (Exception ex)
{
PublicMethod.WriteLog(ex);
}
}
}
}
public static List<TableStructure> GetTableStructure(string TableName)
{
List<TableStructure> tableStructure = new List<TableStructure>();
//string sql = string.Format(@"SELECT a.name 字段名, b.name 类型,
//h.CHARACTER_MAXIMUM_LENGTH as 长度
//FROM dbo.syscolumns a
//left join dbo.systypes b on a.xtype=b.xusertype
//inner join dbo.sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties' LEFT JOIN information_schema.COLUMNS h ON h.TABLE_NAME = d.name AND h.COLUMN_NAME = a.name
//WHERE d.name='{0}' --如果只查询指定表,加上此条件
//order by a.id,a.colorder", TableName);
//using (SqlConnection conn = new SqlConnection(Connection.ConnectionString))
//{
// if (conn.State != ConnectionState.Open)
// conn.Open();
// SqlCommand cmd = new SqlCommand(sql, conn);
// using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
// {
// while (reader.Read())
// {
// TableStructure temp = new TableStructure();
// temp.字段名 = DBHelper.GetString(reader["字段名"]);
// temp.类型 = DBHelper.GetString(reader["类型"]);
// temp.长度 = DBHelper.GetString(reader["长度"]);
// tableStructure.Add(temp);
// }
// reader.Close();
// }
//}
return tableStructure;
}
public static void UpdateDataBaseEntitryLengh(Type typeDBHelper)
{
if (typeDBHelper != null)
{
string TableName = typeDBHelper.Name;
if (typeDBHelper.IsDefined(typeof(TableAttribute), false))
{
var attr = (TableAttribute)typeDBHelper.GetCustomAttributes(typeof(TableAttribute), false)[0];
TableName = attr.TableName;
}
//判断如果表不存在 插入新表逻辑
CreateDataBase(TableName);
//如果表存在 判断是否有列变动
List<MyPropInfo> MyPropInfoList = typeDBHelper.GetProperties().Select(a => AttributeHelper.GetPropertyExtend(a)).ToList();
List<TableStructure> tableStructures = GetTableStructure(TableName);
for (int i = MyPropInfoList.Count - 1; i >= 0; i--)
{
if (MyPropInfoList[i].PropertyName == "OpeRecord" || MyPropInfoList[i].PropertyName == "OpeInfo" || MyPropInfoList[i].PropertyName.Contains("Ref") || MyPropInfoList[i].PropertyName.Contains("List"))
{
MyPropInfoList.Remove(MyPropInfoList[i]);
continue;
}
}
//列变动是新增还是修改 动态生成脚本执行
if (MyPropInfoList.Count > 0)
{
try
{
var fields = string.Join(" ", MyPropInfoList.Select(a => $" alter table {TableName} alter column [{a.DataFieldName}] {(a.SqlDbType == System.Data.SqlDbType.Int ? " int" : " [nvarchar](600)")} "));
PublicMethod.WriteLog(new Exception(fields));
DBHelper.ExecNonQuery(fields);
}
catch (Exception ex)
{
PublicMethod.WriteLog(ex);
}
}
}
}
public static void CreateDataBase(string TableName)
{
int res = 0;
string sql = string.Format(@"IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[{0}](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OperationApplyId] [int] NULL,
[文书名称] [nvarchar](100) NULL,
[操作人] [nvarchar](100) NULL,
[操作时间] [datetime] NULL,
CONSTRAINT [PK_{0}] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
END", TableName);
res = DBHelper.ExecNonQuery(sql);
}
}
}