ningshuxia
2023-02-06 1fc0b4ed96d4c4b1ff7142926239998de32b2ada
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.BLL
{
    /// <summary>
    /// 业务
    /// </summary>
    public partial class LogicArea
    {
        private readonly IDAL.ILogicArea<Entity.LogicArea> _dal
     = DAL.Factory.DataAccessBase.CreateDAL<IDAL.ILogicArea<Entity.LogicArea>>(Settings.DataAccess.LogicDLLName, Settings.DataAccess.DALNameSpace);
 
        #region Cache 
 
        //通过 projectId 获取缓存
        private List<Model.LogicArea> GetProjectCache(long projectId)
        {
            return LogicAreaCacheHelper.GetSet(projectId, () =>
            {
                var entities = _dal.QueryAll(projectId);
                var models = Entity2Models(entities);
                if (models == null)
                {
                    models = new List<Model.LogicArea>();
                }
                return models;
            }, ConfigHelper.CacheKeepTime, ConfigHelper.CacheRandomTime);
        }
 
        //通过 Id 更新缓存
        private void UpdateProjectCache(long projectId, long id)
        {
            var entity_ds = _dal.QueryById(projectId, id);
            var model_ds = Entity2Model(entity_ds);
            var all = GetProjectCache(projectId);
            var model = all.Find(x => x.Id == id);
            if (model == null)
            {
                all.Add(model_ds);
            }
            else
            {
                model.Reset(model_ds);
            }
        }
 
        //通过 Ids 更新缓存
        private void UpdateProjectCache(long projectId, IEnumerable<long> ids)
        {
            if (ids == null || ids.Count() < 1)
                return;
            var entities = _dal.QueryByIds(projectId, ids);
            var models = Entity2Models(entities);
            var all = GetProjectCache(projectId);
            all.RemoveAll(x => ids.Contains(x.Id));
            if (models != null && models.Count > 0)
            {
                all.AddRange(models);
            }
        }
 
        //通过 Id 移除缓存
        private void RemoveProjectCache(long projectId, long Id)
        {
            var all = GetProjectCache(projectId);
            all.RemoveAll(x => x.Id == Id);
        }
 
        #endregion
 
        #region Query 
 
        /// <summary>
        /// 查询全部
        /// </summary>
        public List<Model.LogicArea> QueryAll(long projectId)
        {
            var all = GetProjectCache(projectId);
            return all.OrderBy(x => x.SortCode).ToList();
        }
 
        /// <summary>
        /// 根据Id查询
        /// </summary>
        public Model.LogicArea QueryById(long projectId, long id)
        {
            var all = QueryAll(projectId);
            return all.Find(x => x.Id == id);
        }
 
        /// <summary>
        /// 通过 Ids 获取
        /// </summary>
        public List<Model.LogicArea> QueryByIds(long projectId, IEnumerable<long> ids)
        {
            if (ids == null || ids.Count() < 1)
                return default;
            var all = QueryAll(projectId);
            return all.Where(x => ids.Contains(x.Id)).ToList();
        }
        #endregion
 
        #region Insert
 
        /// <summary>
        /// 插入
        /// </summary>
        public long Insert(long projectId, Model.LogicArea model)
        {
            if (projectId < 1)
                return default;
            if (model == null)
                return default;
            var entities = Model2Entity(model);
            var id = _dal.Insert(projectId, entities);
            if (id > 0)
            {
                UpdateProjectCache(projectId, id);
            }
            return id;
        }
 
        /// <summary>
        /// 批量插入
        /// </summary>
        public bool Inserts(long projectId, IEnumerable<Model.LogicArea> list)
        {
            if (projectId < 1)
                return default;
            if (list == null || list.Count() < 1)
                return default;
            var entities = Model2Entities(list.ToList());
            var ids = _dal.InsertsR(projectId, entities);
            if (ids != null && ids.Count > 0)
            {
                UpdateProjectCache(projectId, ids);
                return true;
            }
            return false;
        }
 
        #endregion
 
        #region Update
 
        /// <summary>
        /// 更新
        /// </summary>
        public bool Update(long projectId, Model.LogicArea model)
        {
            if (projectId < 0)
                return default;
            if (model == null)
                return default;
            if (model.Id < 1)
                return default;
            var entities = Model2Entity(model);
            var bol = _dal.Update(projectId, entities);
            if (bol)
            {
                UpdateProjectCache(projectId, model.Id);
            }
            return bol;
        }
 
        /// <summary>
        /// 批量更新
        /// </summary>
        public bool Updates(long projectId, List<Model.LogicArea> list)
        {
            if (projectId < 0)
                return default;
            if (list == null || list.Count() < 1)
                return default;
            if (list.ToList().Exists(x => x.Id < 1))
                return default;
            var entities = Model2Entities(list.ToList());
            var bol = _dal.Updates(projectId, entities);
            if (bol)
            {
                UpdateProjectCache(projectId, list.Select(x => x.Id).ToList());
            }
            return bol;
        }
 
        #endregion
 
        #region Exist
 
        /// <summary>
        /// 根据 Id 判断是否存在
        /// </summary>
        public bool IsExistById(long projectId, long Id)
        {
            var all = QueryAll(projectId);
            return all.Exists(x => x.Id == Id);
        }
 
        /// <summary>
        /// 判断TagName是否存在
        /// </summary>
        public bool IsExistTagName(long projectId, string TagName)
        {
            if (string.IsNullOrEmpty(TagName))
                return default;
            var all = QueryAll(projectId);
            if (all == null || all.Count < 1)
                return default;
            return all.Exists(x => !string.IsNullOrEmpty(x.TagName) && x.TagName.ToUpper() == TagName.ToUpper());
        }
 
        /// <summary>
        ///  判断TagName是否存在 ExceptId
        /// </summary>
        public bool IsExistTagNameExceptId(long projectId, string TagName, long ExceptID)
        {
            if (string.IsNullOrEmpty(TagName))
                return default;
            var all = QueryAll(projectId);
            if (all == null || all.Count < 1)
                return default;
            return all.Exists(x => !string.IsNullOrEmpty(x.TagName) && x.TagName.ToUpper() == TagName.ToUpper() && x.Id != ExceptID);
        }
 
        #endregion
 
        #region Delete
 
        /// <summary>
        /// 通过 Id 删除
        /// </summary>
        public bool DeleteById(long projectId, long id, out string msg)
        {
            msg = string.Empty;
            var bol = _dal.DeleteById(projectId, id);
            if (bol)
            {
                RemoveProjectCache(projectId, id);
            }
            return bol;
        }
 
        #endregion
    }
}