领胜LDS 键盘AOI检测项目
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using Bro.M071.Model;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
 
namespace Bro.M071.DBManager
{
    public class ModelManager<T> where T : BaseModel, new()
    {
        private PropertyInfo TableProperty = null;
        public static string ConnectionString = ConfigurationManager.ConnectionStrings["DataBase"].ConnectionString;
        public ModelManager()
        {
            T t = new T();
            using (DBModel db = new DBModel())
            {
                TableProperty = db.GetType().GetProperties().FirstOrDefault(u => u.Name == t.GetType().Name);
            }
        }
 
        public void CreateModel(T t, string userId = "")
        {
            using (DBModel context = new DBModel())
            {
                ActionBeforeNewModel(context, t);
 
                t.Create(userId);
                (TableProperty.GetValue(context) as DbSet<T>).Add(t);
 
                context.SaveChanges();
            }
        }
 
        public void BatchAdd(List<T> t, string userId = "")
        {
            using (DBModel context = new DBModel())
            {
                (TableProperty.GetValue(context) as DbSet<T>).AddRange(t);
                context.SaveChanges();
            }
        }
 
        public void UpdateModel(T t, string userId = "")
        {
            using (DBModel context = new DBModel())
            {
                ActionBeforeUpdateModel(context, t);
 
                DbSet<T> set = TableProperty.GetValue(context) as DbSet<T>;
                T oldT = set.FirstOrDefault(u => u.ID == t.ID);
 
                oldT.DataTransfer(t);
                oldT.Update(userId);
                context.SaveChanges();
            }
        }
 
        public void DeleteModel(string id, bool isDelete = true, string userId = "")
        {
            using (DBModel context = new DBModel())
            {
                ActionBeforeDeleteModel(context, id);
 
                DbSet<T> set = TableProperty.GetValue(context) as DbSet<T>;
                T oldT = set.FirstOrDefault(u => u.ID == id);
                oldT.IS_DELETED = isDelete ? 1 : 0;
                oldT.Update(userId);
 
                context.SaveChanges();
            }
        }
 
        public void DisableModel(string id, bool isDisable = true, string userId = "")
        {
            using (DBModel context = new DBModel())
            {
                ActionBeforeEnableModel(context, id);
 
                DbSet<T> set = TableProperty.GetValue(context) as DbSet<T>;
                T oldT = set.FirstOrDefault(u => u.ID == id);
                oldT.IS_DISABLED = isDisable ? 1 : 0;
                oldT.Update(userId);
 
                context.SaveChanges();
            }
        }
 
        #region ""
        protected virtual void ActionBeforeNewModel(DBModel context, T t)
        {
        }
 
        protected virtual void ActionBeforeUpdateModel(DBModel context, T t)
        {
        }
 
        protected virtual void ActionBeforeDeleteModel(DBModel context, string id)
        {
        }
 
        protected virtual void ActionBeforeEnableModel(DBModel context, string id)
        {
        }
        #endregion
    }
}