EDP是一套集組織架構,許可權框架【功能許可權,操作許可權,數據訪問許可權,WebApi許可權】,自動化日誌,動態Interface,WebApi管理等基礎功能於一體的,基於.net的企業應用開發框架。通過友好的編碼方式實現數據行、列許可權的管控。 ...
平臺下載地址:https://gitee.com/alwaysinsist/edp
業務模型概述
業務模型管理中所涉及的業務模型,業務模型的屬性,業務模型的視圖都是可以通過許可權設置來實現數據的行(視圖),列(屬性)許可權管控。業務模型是整個EDP平臺的核心基礎,數據的查詢、新增、修改、刪除、行列許可權都是通過業務模型來實現的。
業務模型管理
按分類管理系統內各個業務模型,對應系統中各個業務對象。
業務模型分類
管理業務模型分類。
業務模型
-
新增業務模型
-
編輯業務模型
-
業務模型代碼生成
表設計文檔模板,可以包含多個表結構設計
業務模型開發
定義業務模型
創建業務模型類,繼承基類ModelPermissionBase。定義好的業務模型可以在上述的業務模型管理功能中進行管理,實現數據行、列許可權的管控。
點擊查看代碼
using System;
namespace School.Model
{
using EDP.Common.Model;
using EDP.Common.Help;
using EDP.Database;
using EDP.Framework;
using EDP.Authentication;
/// <summary>
/// 學生業務對象
/// </summary>
[ModelEnitity(TableName = "t_school_student", KeyName = "ID")]
public class StudentModel: ModelPermissionBase
{
/// <summary>
/// 構造函數
/// </summary>
public StudentModel() {
this.SetDefaultQuerySQL(@"select *
from (select a.ID
,a.Name
,a.Number
,a.GenderId
,a.Age
,a.Contact
,a.ContactMobile
,a.Hobby
,a.CampusId
,a.ClassId
,a.CreateUserId
,a.CreateUserName
,a.CreateDepartmentId
,a.CreateOrganizationId
,a.CreateTime
,a.ModifyUserId
,a.ModifyUserName
,a.ModifyTime
,a.Remarks
,b.Name CampusName
,c.Name ClassName
,d.Name SchoolName
,c.FullCode ClassFullCode
from t_school_student a
left join t_sys_organization_unit b on a.CampusId = b.ID
left join t_sys_organization_unit c on a.ClassId = c.ID
left join t_sys_organization_unit d on c.FullCode like d.FullCode + '%' and d.Levels = c.Levels - 3) tab");
}
#region **----實體屬性---------------------------**
/// <summary>
/// 主鍵ID
/// </summary>
[ModelEnitity(ColumnName = "ID")]
public string ID { get; set; }
/// <summary>
/// 姓名
/// </summary>
[ModelEnitity(ColumnName = "Name", IsRequired = true, MaxLength = 50)]
public string Name { get; set; }
/// <summary>
/// 編號
/// <summary>
[ModelEnitity(ColumnName = "Number")]
public string Number { get; set; }
/// <summary>
/// 性別ID
/// </summary>
[ModelEnitity(ColumnName = "GenderId", IsRequired = true)]
public int? GenderId { get; set; }
/// <summary>
/// 年齡
/// </summary>
[ModelEnitity(ColumnName = "Age", IsRequired = true)]
public int? Age { get; set; }
/// <summary>
/// 聯繫人
/// </summary>
[ModelEnitity(ColumnName = "Contact", MaxLength = 50)]
public string Contact { get; set; }
/// <summary>
/// 聯繫人手機
/// </summary>
[ModelEnitity(ColumnName = "ContactMobile", Regex = @"^[1]([3-9])[0-9]{9}$")]
public string ContactMobile { get; set; }
/// <summary>
/// 興趣愛好
/// </summary>
[ModelEnitity(ColumnName = "Hobby", MaxLength = 500)]
public string Hobby { get; set; }
/// <summary>
/// 校區ID
/// </summary>
[ModelEnitity(ColumnName = "CampusId")]
public string CampusId { get; set; }
/// <summary>
/// 班級ID
/// </summary>
[ModelEnitity(ColumnName = "ClassId", IsRequired = true)]
public string ClassId { get; set; }
/// <summary>
/// 創建人ID
/// </summary>
[ModelEnitity(ColumnName = "CreateUserId")]
public string CreateUserId { get; set; }
/// <summary>
/// 創建人
/// </summary>
[ModelEnitity(ColumnName = "CreateUserName")]
public string CreateUserName { get; set; }
/// <summary>
/// 創建部門ID
/// </summary>
[ModelEnitity(ColumnName = "CreateDepartmentId")]
public string CreateDepartmentId { get; set; }
/// <summary>
/// 創建機構ID
/// </summary>
[ModelEnitity(ColumnName = "CreateOrganizationId")]
public string CreateOrganizationId { get; set; }
/// <summary>
/// 創建時間
/// </summary>
[ModelEnitity(ColumnName = "CreateTime")]
public DateTime? CreateTime { get; set; }
/// <summary>
/// 修改人ID
/// </summary>
[ModelEnitity(ColumnName = "ModifyUserId")]
public string ModifyUserId { get; set; }
/// <summary>
/// 修改人
/// </summary>
[ModelEnitity(ColumnName = "ModifyUserName")]
public string ModifyUserName { get; set; }
/// <summary>
/// 修改時間
/// </summary>
[ModelEnitity(ColumnName = "ModifyTime")]
public DateTime? ModifyTime { get; set; }
/// <summary>
/// 備註
/// </summary>
[ModelEnitity(ColumnName = "Remarks", MaxLength = 1000)]
public string Remarks { get; set; }
#endregion
#region **----擴展屬性---------------------------**
/// <summary>
/// 校區名稱
/// </summary>
public string CampusName { get; set; }
/// <summary>
/// 學校名稱
/// </summary>
public string SchoolName { get; set; }
/// <summary>
/// 班級名稱
/// </summary>
public string ClassName { get; set; }
/// <summary>
/// 性別@readonly
/// </summary>
public GenderEnum Gender {
get {
GenderEnum ret = GenderEnum.None;
if (this.GenderId.HasValue) {
try { ret = (GenderEnum)this.GenderId; }
catch { }
}
return ret;
}
}
/// <summary>
/// 性別顯示@readonly
/// </summary>
public string GenderDisplay {
get {
return EnumHelp.GetEnumDescription(this.Gender);
}
}
/// <summary>
/// 班級全編碼
/// </summary>
public string ClassFullCode { get; set; }
/// <summary>
/// 學校
/// </summary>
public Organization.OrganizationModel School { get; set; } = new Organization.OrganizationModel();
#endregion
#region **----方法-------------------------------**
/// <summary>
/// 創建當前實體對象
/// </summary>
/// <param name="db">資料庫介面</param>
public void Create(IDBInstance db = null) {
if (string.IsNullOrWhiteSpace(this.ID))
this.ID = Guid.NewGuid().ToString();
var loginUser = AuthenticationHelp.GetLoginUser(db);
if (loginUser != null && loginUser.IsLogin) {
this.CreateUserId = loginUser.UserInfo.ID;
this.CreateUserName = loginUser.UserInfo.Name;
this.CreateDepartmentId = loginUser.UserInfo.DepartmentId;
this.CreateOrganizationId = loginUser.UserInfo.OrganizationId;
}
this.CreateTime = DateTime.Now;
this.ModifyTime = null;
this.ModifyUserId = null;
this.ModifyUserName = null;
}
/// <summary>
/// 修改當前實體對象
/// </summary>
/// <param name="db">資料庫介面</param>
public void Modify(IDBInstance db = null) {
this.ModifyTime = DateTime.Now;
var loginUser = AuthenticationHelp.GetLoginUser(db);
if (loginUser != null && loginUser.IsLogin) {
this.ModifyUserId = loginUser.UserInfo.ID;
this.ModifyUserName = loginUser.UserInfo.Name;
}
this.ModifyTime = DateTime.Now;
this.CreateDepartmentId = null;
this.CreateOrganizationId = null;
this.CreateTime = null;
this.CreateUserId = null;
this.CreateUserName = null;
}
/// <summary>
/// 修改當前實體對象
/// </summary>
/// <param name="keyValue">主鍵值</param>
/// <param name="db">資料庫介面</param>
public void Modify(string keyValue, IDBInstance db = null) {
this.ID = keyValue;
this.Modify(db);
}
#endregion
}
}
[ModelEnitity(TableName = "t_school_student", KeyName = "ID")]
TableName:表名
KeyName:主鍵對應的屬性名
[ModelEnitity(ColumnName = "Name", IsRequired = true, MaxLength = 50)]
ColumnName:欄位名稱
IsRequired:是否必須,用於驗證屬性值是否必須有值
MaxLength:最大長度,屬性值的最大長度
[ModelEnitity(ColumnName = "ContactMobile", Regex = @"^[1]([3-9])[0-9]{9}$")]
Regex:正則表達式,用於驗證屬性值格式
點擊查看代碼
/// <summary>
/// 構造函數
/// </summary>
public StudentModel() {
this.SetDefaultQuerySQL(@"select *
from (select a.ID
,a.Name
,a.Number
,a.GenderId
,a.Age
,a.Contact
,a.ContactMobile
,a.Hobby
,a.CampusId
,a.ClassId
,a.CreateUserId
,a.CreateUserName
,a.CreateDepartmentId
,a.CreateOrganizationId
,a.CreateTime
,a.ModifyUserId
,a.ModifyUserName
,a.ModifyTime
,a.Remarks
,b.Name CampusName
,c.Name ClassName
,d.Name SchoolName
,c.FullCode ClassFullCode
from t_school_student a
left join t_sys_organization_unit b on a.CampusId = b.ID
left join t_sys_organization_unit c on a.ClassId = c.ID
left join t_sys_organization_unit d on c.FullCode like d.FullCode + '%' and d.Levels = c.Levels - 3) tab");
//引入命名空間 using EDP.Common;
//default:註冊表配置中資料庫連接配置所對應的"Key"值
//若沒有設置連線字串,則取註冊表配置中的預設資料庫連接
//this.SetConnectionString(Config.Database["default"]); 示例項目使用預設資料庫連接,不需要設置
}
public string CampusName { get; set; }
擴展屬性是預設查詢SQL中所查詢的非業務模型映射的表中的欄位。
點擊查看代碼
/// <summary>
/// 創建當前實體對象
/// </summary>
/// <param name="db">資料庫介面</param>
public void Create(IDBInstance db = null) {
if (string.IsNullOrWhiteSpace(this.ID))
this.ID = Guid.NewGuid().ToString();
var loginUser = AuthenticationHelp.GetLoginUser(db);
if (loginUser != null && loginUser.IsLogin) {
this.CreateUserId = loginUser.UserInfo.ID;
this.CreateUserName = loginUser.UserInfo.Name;
this.CreateDepartmentId = loginUser.UserInfo.DepartmentId;
this.CreateOrganizationId = loginUser.UserInfo.OrganizationId;
}
this.CreateTime = DateTime.Now;
this.ModifyTime = null;
this.ModifyUserId = null;
this.ModifyUserName = null;
}
/// <summary>
/// 修改當前實體對象
/// </summary>
/// <param name="db">資料庫介面</param>
public void Modify(IDBInstance db = null) {
this.ModifyTime = DateTime.Now;
var loginUser = AuthenticationHelp.GetLoginUser(db);
if (loginUser != null && loginUser.IsLogin) {
this.ModifyUserId = loginUser.UserInfo.ID;
this.ModifyUserName = loginUser.UserInfo.Name;
}
this.ModifyTime = DateTime.Now;
this.CreateDepartmentId = null;
this.CreateOrganizationId = null;
this.CreateTime = null;
this.CreateUserId = null;
this.CreateUserName = null;
}
/// <summary>
/// 修改當前實體對象
/// </summary>
/// <param name="keyValue">主鍵值</param>
/// <param name="db">資料庫介面</param>
public void Modify(string keyValue, IDBInstance db = null) {
this.ID = keyValue;
this.Modify(db);
}
定義查詢對象
查詢對象是用來實現業務對象的查詢,通過屬性來設置查詢條件。需要繼承QueryModelBase基類。
點擊查看代碼
using System;
namespace School.Model
{
using EDP.Common;
using EDP.Common.Model;
/// <summary>
/// 學生信息查詢對象
/// </summary>
[QueryModel] //標記類的查詢對象特性
public class StudentQueryModel : QueryModelBase //繼承QueryModelBase
{
/// <summary>
/// 姓名
/// </summary>
[QueryModel(Operator = QueryOperator.like, ColumnName = "Name")]
public string Name { get; set; }
/// <summary>
/// 性別
/// </summary>
[QueryModel(Operator = QueryOperator.eq, ColumnName = "GenderId")]
public int? GenderId { get; set; }
/// <summary>
/// 校區
/// </summary>
[QueryModel(Operator = QueryOperator.eq, ColumnName = "CampusId")]
public string CampusId { get; set; }
/// <summary>
/// 班級
/// </summary>
[QueryModel(Operator = QueryOperator.eq, ColumnName = "ClassId")]
public string ClassId { get; set; }
/// <summary>
/// 班級全編碼
/// </summary>
/// <example>ClassFullCode like '值%'</example>
[QueryModel(Operator = QueryOperator.likeRight, ColumnName = "ClassFullCode")]
public string ClassFullCode { get; set; }
/// <summary>
/// 編號姓名關鍵字
/// Number和Name都進行like查詢
/// </summary>
/// <example>Number like '%關鍵字%' or Name like '%值%'</example>
[QueryModel(Operator = QueryOperator.like, ColumnNameList = "Number;Name")]
public string NumberNameKeywrod { get; set; }
/// <summary>
/// 分頁對象
/// </summary>
public PageModel Page { get; set; } = new PageModel();
}
}
[QueryModel(Operator = QueryOperator.like, ColumnName = "Name")]
Operator:查詢操作符(=、>、<、>=、like等)
ColumnName:對應業務對象的屬性(實體屬性,擴展屬性),只要是預設查詢SQL中包含的欄位都可以作為查詢對象的屬性(查詢條件)
業務模型使用
詳見示例項目StudentBLL.cs
-
業務模型屬性驗證
var verify = item.Verify(keyValue);
此處通過Verify()方法驗證業務模型各屬性值,通過屬性驗證標記(IsRequired,MaxLength, Regex)進行驗證。 -
業務模型新增
點擊查看代碼
item.Create(db);
item.ValidateSQL = string.Format("where not exists (select 1 from t_school_student where Number = '{0}')", item.Number);
result = item.InsertModel(db);
通過業務模型Create()方法創建當前實體對象。
通過ValidateSQL屬性值確保不會有重覆的Number值插入到資料庫。
調用InsertModel()方法新增業務模型數據。
- 修改業務模型
點擊查看代碼
item.Modify(keyValue, db);
item.Number = null; //編號不能修改
result = item.UpdateModel(db);
通過業務模型Modify()方法修改當前實體對象。
調用UpdateModel()方法修改業務模型數據。
- 業務模型查詢
點擊查看代碼
ResultModel<StudentModel> ret = new ResultModel<StudentModel>();
ret.Content = new StudentModel();
ret.KeyValue = keyValue;
ret.Content.ID = keyValue;
ret.Content.Query(db);
使用統一的返回對象 ResultModel<StudentModel>。
實例化ret.Content並設置操作的主鍵值ret.KeyValue = keyValue。
設置查詢的主鍵值 ret.Content.ID = keyValue。
調用業務對象Query()方法 ret.Content.Query(db)。
執行Query()查詢後,根據許可權設置給ret.Content賦值。
ret.Content.AccessPropertyList中返回有許可權的屬性名稱。
- 業務模型列表查詢
點擊查看代碼
ResultModel<List<StudentModel>> ret = new ResultModel<List<StudentModel>>();
ret.Content = new List<StudentModel>();
ret.Content.Query(queryModel.Page, queryModel, db);
ret.Page = queryModel.Page;
使用統一的返回對象 ResultModel<List<StudentModel>>。
實例化 ret.Content = new List<StudentModel>()。
調用業務對象Query()方法 ret.Content.Query(queryModel.Page, queryModel, db)。
queryModel為查詢對象StudentQueryModel的實例。
執行Query()查詢後,根據許可權設置給ret.Content賦值(有許可權的數據集合)。
ret.Content[0].AccessPropertyList中返回有許可權的屬性名稱。
- 業務模型刪除
點擊查看代碼
StudentModel item = new StudentModel();
item.Id = keyValue;
item.DeleteModel();
實例化StudentModel。
設置主鍵值 item.Id = keyValue。
調用業務對象DeleteModel()方法 item.DeleteModel()。