using System; using System.Collections.Generic; using System.ComponentModel; using System.Runtime.Serialization; using System.Text; using HcUtility.Comm; namespace HcUtility.Core { /// /// 功能: ModelObject的基类 /// 创建: 吴伟 2009-05-07 /// 说明: /// [DataContract] public class ModelObjectBase_Base : IModelObject { #region 公共属性 //存储此数据的对应的数据表名 public string TableName { get; set; } //明细数据 public List> BodyList { get { return _bodyList; } set { _bodyList = value; } } //保存时的动作(如新增、修改等) public DbOperationType DbOperationType { get { return _dbOperationType; } set { _dbOperationType = value; } } public virtual string getSQL() { //在实际实体类中实现,为实体类确定查询语句。 return ""; } #endregion /// /// 功能: 根据属性指定的ModelDBAttribute[]反射取得Insert SQL语句 /// 创建: 吴伟 2009-05-07 /// 说明: 子类可以覆盖此方法,生成自己的Insert语句 /// /// Insert Sql语句 public virtual string GenInsertSql() { var sbInto = new StringBuilder(); var sbParam = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = string.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute) attr; if (ModelDBOprationType.Insert == (ModelDBOprationType.Insert & a.MDBType)) { if (sbInto.Length != 0) { sbInto.Append(","); sbParam.Append(","); } fieldName = a.InsFieldName == String.Empty ? pdc.Name : a.InsFieldName; paramName = a.InsParamName == String.Empty ? pdc.Name : a.InsParamName; sbInto.Append("["+fieldName+"]"); //如果属性设置了FieldName值,则取设置的FieldName值,否则取属性值 sbParam.Append("@" + paramName); //如果属性设置了ParamName值,则取设置的ParamName值,否则取属性值 } } } } sbSql.Append("Insert Into " + TableName); sbSql.Append(" ("); sbSql.Append(sbInto.ToString()); sbSql.Append(" ) "); sbSql.Append(" Values"); sbSql.Append(" ("); sbSql.Append(sbParam.ToString()); sbSql.Append(" ) "); return sbSql.ToString(); } /// /// 功能: 根据属性指定的ModelDBAttribute[]反射取得Update SQL语句 /// 创建: 吴伟 2009-05-07 /// 说明: 子类可以覆盖此方法,生成自己的Update语句 /// /// Update Sql语句 public virtual string GenUpdateSql() { var sbUpdAndParam = new StringBuilder(); var sbWhere = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = String.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute) attr; if (ModelDBOprationType.Edit == (ModelDBOprationType.Edit & a.MDBType)) { fieldName = a.UpdFieldName == String.Empty ? pdc.Name : a.UpdFieldName; paramName = a.UpdParamName == String.Empty ? pdc.Name : a.UpdParamName; if (!a.IsPrimary) { if (sbUpdAndParam.Length != 0) { sbUpdAndParam.Append(","); } sbUpdAndParam.Append("["+fieldName+"]" + "=@" + paramName); } if (a.IsPrimary) { if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append("[" + fieldName + "]" + "=@" + paramName); } } } } } sbSql.Append("update " + TableName); sbSql.Append(" set "); sbSql.Append(sbUpdAndParam.ToString()); sbSql.Append(" where "); sbSql.Append(sbWhere.ToString()); //后台sql执行语句 return sbSql.ToString(); } /// /// 功能: 根据属性指定的ModelDBAttribute[]反射取得Delete SQL语句 /// 创建: 吴伟 2009-05-07 /// 说明: 子类可以覆盖此方法,生成自己的Delete语句 /// /// Delete Sql语句 public virtual string GenDeleteSql() { var sbUpdAndParam = new StringBuilder(); var sbWhere = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = String.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute) attr; if (ModelDBOprationType.Delete == (ModelDBOprationType.Delete & a.MDBType)) { fieldName = a.DelFieldName == String.Empty ? pdc.Name : a.DelFieldName; paramName = a.DelParamName == String.Empty ? pdc.Name : a.DelParamName; if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append(fieldName + "=@" + paramName); } } } } if (sbWhere.ToString() == String.Empty) { throw new NotSupportedException("请增加Model的属性已组成条件"); } sbSql.Append(" delete from " + TableName); sbSql.Append(" where "); sbSql.Append(sbWhere.ToString()); return sbSql.ToString(); } public virtual string GenDeleteSql(string USERID) { var sbUpdAndParam = new StringBuilder(); var sbWhere = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = String.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute)attr; if (ModelDBOprationType.Delete == (ModelDBOprationType.Delete & a.MDBType)) { fieldName = a.DelFieldName == String.Empty ? pdc.Name : a.DelFieldName; paramName = a.DelParamName == String.Empty ? pdc.Name : a.DelParamName; if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append(fieldName + "=@" + paramName); } } } } if (sbWhere.ToString() == String.Empty) { throw new NotSupportedException("请增加Model的属性已组成条件"); } sbSql.Append(" update " + TableName + " set ISDELETE=1,DELETETIME=getdate(),DELETEUSER='" + USERID + "'"); sbSql.Append(" where "); sbSql.Append(sbWhere.ToString()); return sbSql.ToString(); } //public virtual string GenSelectSql() //{ // var sbSelect = new StringBuilder(); // PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); // foreach (PropertyDescriptor pdc in pdcList) // { // foreach (Attribute attr in pdc.Attributes) // { // if (attr is ModelDBAttribute) // { // var a = (ModelDBAttribute)attr; // if (ModelDBOprationType.Select == (ModelDBOprationType.Select & a.MDBType)) // { // if (sbSelect.Length != 0) // { // sbSelect.Append(","); // } // var fieldName = a.InsFieldName == String.Empty ? pdc.Name : a.InsFieldName; // sbSelect.Append(fieldName); //如果属性设置了FieldName值,则取设置的FieldName值,否则取属性值 // } // } // } // } // var sbSql = new StringBuilder(); // sbSql.Append(" select "); // sbSql.Append(sbSelect.ToString()); // sbSql.Append(" from " + TableName); // return sbSql.ToString(); //} #region 私有/保护变量 protected List> _bodyList = new List>(); protected DbOperationType _dbOperationType; #endregion } public class ExtendData : Object { public string Name { get; set; } public string Value { get; set; } public ModelDBAttribute MDB { get; set; } public ExtendData(string Name, string Value, ModelDBAttribute ma) { this.Name = Name; this.Value = Value; this.MDB = ma; } } /// /// 包含扩展数据类型的新基类 兼容原有功能 /// public class ModelObjectBase : ModelObjectBase_Base { #region protected List _ExtendList = new List(); public List ExtendDataList { get { return _ExtendList; } set { _ExtendList = value; } } public bool Extended() { if (_ExtendList.Count > 0) { return true; } return false; } protected Dictionary _SaveDic = new Dictionary(); public Dictionary SaveDic { get { return _SaveDic; } set { _SaveDic = value; } } public bool CanSave(string Name) { if (SaveDic.ContainsKey(Name)) { if (SaveDic[Name] == "1") return true; } return false; } public void NoSave(string Name) { if (SaveDic.ContainsKey(Name)) { SaveDic[Name] = "0"; } } public void LetSave(string Name) { if (SaveDic.ContainsKey(Name)) { SaveDic[Name] = "1"; } } public void TimeNext() { // } public Dictionary ExtendDic { get { var result = new Dictionary(); if (Extended()) { foreach (var d in ExtendDataList) { result.Add(d.Name, d.Value); } } return result; } } public bool Have(string name) { if (ExtendDataList.Find(x => x.Name == name) == null) { return false; } else { return true; } } virtual public Dictionary GetPKey() {//用于给扩展的动态数据提供主键字段名 var d = new Dictionary() { { "ID", ModelDBOprationType.EditDelete }, { "GID", ModelDBOprationType.All } }; return d; } /// /// 添加扩展参数 /// /// /// /// private void AddExtendDate(string Name, string Value, ModelDBAttribute ma) { var ed = new ExtendData(Name, Value, ma); ExtendDataList.Add(ed); } /// /// 添加扩展参数 只增加普通类型(即可保存 非insert delete主键) /// /// /// public void AddExtendDate(string Name, string Value) { ModelDBAttribute ma = new ModelDBAttribute(); ma.IsPrimary = false; var _dt = GetPKey(); if (_dt.ContainsKey(Name)) { if ((_dt[Name] & ModelDBOprationType.All) > 0) { AddExtendDate(Name, Value, true, _dt[Name]); } } else { if (CanSave(Name)) { ma.MDBType = ModelDBOprationType.InsertEdit; } else { ma.MDBType = 0; } var ed = new ExtendData(Name, Value, ma); ExtendDataList.Add(ed); } } public void AddExtendDate_Show(string Name, string Value) { ModelDBAttribute ma = new ModelDBAttribute(); ma.IsPrimary = false; ma.MDBType = 0; var ed = new ExtendData(Name, Value, ma); ExtendDataList.Add(ed); } private void AddExtendDate(string Name, string Value, bool IsPrimary, ModelDBOprationType mot) { ModelDBAttribute ma = new ModelDBAttribute(); ma.IsPrimary = IsPrimary; ma.MDBType = mot; var ed = new ExtendData(Name, Value, ma); ExtendDataList.Add(ed); } #endregion /// /// 功能: 根据属性指定的ModelDBAttribute[]反射取得Insert SQL语句 /// 创建: ddlucky 2017-03-01 /// 说明: 子类可以覆盖此方法,生成自己的Insert语句 /// /// Insert Sql语句 public override string GenInsertSql() { var sbInto = new StringBuilder(); var sbParam = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = string.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute)attr; if (ModelDBOprationType.Insert == (ModelDBOprationType.Insert & a.MDBType)) { if (sbInto.Length != 0) { sbInto.Append(","); sbParam.Append(","); } fieldName = a.InsFieldName == String.Empty ? pdc.Name : a.InsFieldName; paramName = a.InsParamName == String.Empty ? pdc.Name : a.InsParamName; sbInto.Append("[" + fieldName + "]"); //如果属性设置了FieldName值,则取设置的FieldName值,否则取属性值 sbParam.Append("@" + paramName); //如果属性设置了ParamName值,则取设置的ParamName值,否则取属性值 } } } } if (Extended()) { foreach (var d in ExtendDataList) { if (d.MDB.needInsert()) { if (sbInto.Length != 0) { sbInto.Append(","); sbParam.Append(","); } sbInto.Append("[" + d.Name + "]"); sbParam.Append("@" + d.Name); } } } sbSql.Append("Insert Into " + TableName); sbSql.Append(" ("); sbSql.Append(sbInto.ToString()); sbSql.Append(" ) "); sbSql.Append(" Values"); sbSql.Append(" ("); sbSql.Append(sbParam.ToString()); sbSql.Append(" ) "); return sbSql.ToString(); } /// /// 功能: 根据属性指定的ModelDBAttribute[]反射取得Update SQL语句 /// 创建: ddlucky 2017-03-01 /// 说明: 子类可以覆盖此方法,生成自己的Update语句 /// /// Update Sql语句 public override string GenUpdateSql() { var sbUpdAndParam = new StringBuilder(); var sbWhere = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = String.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute)attr; if (ModelDBOprationType.Edit == (ModelDBOprationType.Edit & a.MDBType)) { fieldName = a.UpdFieldName == String.Empty ? pdc.Name : a.UpdFieldName; paramName = a.UpdParamName == String.Empty ? pdc.Name : a.UpdParamName; if (!a.IsPrimary) { if (sbUpdAndParam.Length != 0) { sbUpdAndParam.Append(","); } sbUpdAndParam.Append("[" + fieldName + "]" + "=@" + paramName); } if (a.IsPrimary) { if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append("[" + fieldName + "]" + "=@" + paramName); } } } } } if (Extended()) { foreach (var d in ExtendDataList) { if (d.MDB.needInsert()) { if (sbUpdAndParam.Length != 0) { sbUpdAndParam.Append(","); } sbUpdAndParam.Append("[" + d.Name + "]" + "=@" + d.Name); } if (d.MDB.IsPrimary) { if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append("[" + d.Name + "]" + "=@" + d.Name); } } } sbSql.Append("update " + TableName); sbSql.Append(" set "); sbSql.Append(sbUpdAndParam.ToString()); sbSql.Append(" where "); sbSql.Append(sbWhere.ToString()); //后台sql执行语句 return sbSql.ToString(); } /// /// 功能: 根据属性指定的ModelDBAttribute[]反射取得Delete SQL语句 /// 创建: ddlucky 2017-03-01 /// 说明: 子类可以覆盖此方法,生成自己的Delete语句 /// /// Delete Sql语句 public override string GenDeleteSql() { var sbUpdAndParam = new StringBuilder(); var sbWhere = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = String.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute)attr; if (a.needDelete()) { fieldName = a.DelFieldName == String.Empty ? pdc.Name : a.DelFieldName; paramName = a.DelParamName == String.Empty ? pdc.Name : a.DelParamName; if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append(fieldName + "=@" + paramName); } } } } if (Extended()) { foreach (var d in ExtendDataList) { if (d.MDB.needDelete()) { if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append(d.Name + "=@" + d.Name); } } } if (sbWhere.ToString() == String.Empty) { throw new NotSupportedException("请增加Model的属性已组成条件"); } sbSql.Append(" delete from " + TableName); sbSql.Append(" where "); sbSql.Append(sbWhere.ToString()); return sbSql.ToString(); } public override string GenDeleteSql(string USERID) { var sbUpdAndParam = new StringBuilder(); var sbWhere = new StringBuilder(); var sbSql = new StringBuilder(); string insertSql = String.Empty; string fieldName = String.Empty; string paramName = String.Empty; PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); foreach (PropertyDescriptor pdc in pdcList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) { var a = (ModelDBAttribute)attr; if (ModelDBOprationType.Delete == (ModelDBOprationType.Delete & a.MDBType)) { fieldName = a.DelFieldName == String.Empty ? pdc.Name : a.DelFieldName; paramName = a.DelParamName == String.Empty ? pdc.Name : a.DelParamName; if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append(fieldName + "=@" + paramName); } } } } if (Extended()) { foreach (var d in ExtendDataList) { if (d.MDB.needDelete()) { if (sbWhere.Length != 0) { sbWhere.Append(" and "); } sbWhere.Append(d.Name + "=@" + d.Name); } } } if (sbWhere.ToString() == String.Empty) { throw new NotSupportedException("请增加Model的属性已组成条件"); } sbSql.Append(" update " + TableName + " set ISDELETE=1,DELETETIME=getdate(),DELETEUSER='" + USERID + "'"); sbSql.Append(" where "); sbSql.Append(sbWhere.ToString()); return sbSql.ToString(); } /// /// 参数是目标对象,应该是【来源对象.Copy(目标对象)】 /// /// public void Copy(ModelObjectBase tar) { PropertyDescriptorCollection pdcList = TypeDescriptor.GetProperties(this); PropertyDescriptorCollection tarList = TypeDescriptor.GetProperties(tar); string fieldName1 = String.Empty; string paramName1 = String.Empty; string fieldName2 = String.Empty; string paramName2 = String.Empty; bool needcontinue = false; foreach (PropertyDescriptor pdc in pdcList) { needcontinue = false; foreach (PropertyDescriptor tarpdc in tarList) { foreach (Attribute attr in pdc.Attributes) { if (attr is ModelDBAttribute) foreach (Attribute tarattr in tarpdc.Attributes) { if (tarattr is ModelDBAttribute) { var a = (ModelDBAttribute)attr; var b = (ModelDBAttribute)tarattr; fieldName1 = a.InsFieldName == String.Empty ? pdc.Name : a.InsFieldName; fieldName2 = b.InsFieldName == String.Empty ? tarpdc.Name : b.InsFieldName; if (fieldName1 == fieldName2) tarpdc.SetValue(tar, pdc.GetValue(this)); needcontinue = true; continue; } } if (needcontinue) continue; } if (needcontinue) continue; } if (needcontinue) continue; } if (Extended()) { foreach (var d in ExtendDataList) { tar.ExtendDataList.Add(d); } } if (SaveDic.Count > 0) { foreach (var d in SaveDic) { tar.SaveDic.Add(d.Key, d.Value); } } } public void SetSaveDic(ModelObjectBase tar) { if (SaveDic.Count > 0) { foreach (var d in SaveDic) { tar.SaveDic.Add(d.Key, d.Value); } } } } }