duheng
2024-04-16 668e7746a6fbe30cba78bffe37c9ce9b9ed9a4fe
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
namespace ISupply.BLL
{
    public partial class Part
    {
        private readonly DAL.Part _dal = new DAL.Part();
 
 
 
 
        private List<Model.Part> GetCache()
        {
            return PartCacheHelper.GetSet(() =>
            {
                var entities = _dal.GetAll();
                var models = Entity2Models(entities);
                if (models == null)
                {
                    models = new List<Model.Part>();
                }
                return models;
            }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
        }
 
        /// <summary>
        /// 查询全部
        /// </summary>
        public List<Model.Part> GetAll()
        {
            var all = GetCache();
            return all.OrderBy(x => x.ID).ToList();
        }
 
        public long Insert(Model.Part model)
        {
            if (model == null)
                return default;
            var entity = Model2Entity(model);
            var ID = _dal.Insert(entity);
            if (ID > 0)
            {
                UpdateCache(ID);
            }
            return ID;
        }
 
 
 
 
        private void UpdateCache(long id)
        {
            var entity_ds = _dal.GetByID(id);
            var model_ds = Entity2Model(entity_ds);
            var all = GetCache();
            var model = all.Find(x => x.ID == id);
            if (model == null)
            {
                all.Add(model_ds);
            }
            else
            {
                model.Reset(model_ds);
            }
        }
 
 
 
        /// <summary>
        /// 更新
        /// </summary>
        public bool Update(Model.Part model)
        {
            if (model == null)
                return default;
            if (model.ID < 1)
                return default;
            var entity = Model2Entity(model);
            var bol = _dal.Update(entity);
            if (bol)
            {
                UpdateCache(model.ID);
            }
            return bol;
        }
 
 
        /// <summary>
        /// 根据 ID 删除
        /// </summary>
        public bool DeleteByID(long id, out string msg)
        {
            msg = string.Empty;
            var bol = _dal.DeleteByID(id);
            if (bol)
            {
                RemoveCache(id);
            }
            return bol;
        }
 
 
        //根据 ID 移除缓存
        private void RemoveCache(long id)
        {
            var all = GetCache();
            all.RemoveAll(x => x.ID == id);
        }
    }
}