zhaolei
3 days ago 4a2e5b9a21940f11757be37d99f0944e240e908b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
 
namespace Bootstrap.Security.DataAccess
{
    /// <summary>
    /// PetaPoco ORM 数据库映射类
    /// </summary>
    internal class BootstrapAdminConventionMapper : ConventionMapper
    {
        private readonly Dictionary<Type, TableInfo> _tableInfos;
        private readonly Dictionary<Type, IEnumerable<string>> _ignoreColumns;
        private readonly Dictionary<Type, IEnumerable<string>> _resultColumns;
 
        /// <summary>
        /// 构造函数
        /// </summary>
        public BootstrapAdminConventionMapper()
        {
            _tableInfos = new Dictionary<Type, TableInfo>
            {
                { typeof(BootstrapDict), new TableInfo() { TableName = "Dicts", PrimaryKey = "Id", AutoIncrement = true, SequenceName = "SEQ_DICTS_ID" } },
                { typeof(BootstrapMenu), new TableInfo() { TableName = "Navigations", PrimaryKey = "Id", AutoIncrement = true , SequenceName = "SEQ_NAVIGATIONS_ID"} },
                { typeof(BootstrapGroup), new TableInfo() { TableName = "Groups", PrimaryKey = "Id", AutoIncrement = true , SequenceName = "SEQ_GROUPS_ID"} },
                { typeof(BootstrapUser), new TableInfo() { TableName = "Users", PrimaryKey = "Id", AutoIncrement = true , SequenceName = "SEQ_USERS_ID"} }
            };
 
            _ignoreColumns = new Dictionary<Type, IEnumerable<string>>
            {
                { typeof(BootstrapMenu), new string[] { "Menus" } }
            };
 
            _resultColumns = new Dictionary<Type, IEnumerable<string>>
            {
                { typeof(BootstrapMenu), new string[] { "ParentName", "Active", "CategoryName" } }
            };
        }
 
        /// <summary>
        /// 通过属性获取列信息
        /// </summary>
        /// <param name="pocoProperty"></param>
        /// <returns></returns>
        public override ColumnInfo? GetColumnInfo(PropertyInfo pocoProperty)
        {
            if (_ignoreColumns.TryGetValue(pocoProperty.DeclaringType, out var ignoreColumns) && ignoreColumns.Any(c => c.Equals(pocoProperty.Name, StringComparison.OrdinalIgnoreCase))) return null;
            var ci = new ColumnInfo() { ColumnName = pocoProperty.Name };
            if (_resultColumns.TryGetValue(pocoProperty.DeclaringType, out var resultColumns) && resultColumns.Any(c => c.Equals(pocoProperty.Name, StringComparison.OrdinalIgnoreCase))) ci.ResultColumn = true;
            return ci;
        }
 
        /// <summary>
        /// 通过类型获取表信息
        /// </summary>
        /// <param name="pocoType"></param>
        /// <returns></returns>
        public override TableInfo GetTableInfo(Type pocoType) => _tableInfos[pocoType];
    }
}