tangxu
2023-03-20 3bf8bf5179de8b83bbd6ad997e82d98302abd1c3
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IStation.Model.Api;
using AutoMapper;
using IStation.WebApi.Models;
using IStation.ViewModel;
using IStation.Untity;
using Microsoft.Web.Http;
 
namespace IStation.WebApi.Controllers
{
    /// <summary>
    /// 设备业务标准api
    /// </summary>
    [RoutePrefix("v1/Standard/Product")]
    [ApiVersion("v1")]
    public class Product_StandardController : ApiController
    {
 
        /// <summary>
        /// 通过 BelongType 和 BelongID 获取逻辑树
        /// </summary>
        [Route("GetLogicalTreeByBelongTypeAndBelongID")]
        [HttpGet]
        public Result GetLogicalTreeByBelongTypeAndBelongID(long CorpID, string BelongType, long BelongID)
        {
            if (CorpID < 1)
            {
                return new Result(Code.Error, "CorpID 参数错误");
            }
            if (string.IsNullOrEmpty(BelongType))
            {
                return new Result(Code.Error, "BelongType 参数错误");
            }
            if (BelongID < 1)
            {
                return new Result(Code.Error, "BelongID 参数错误");
            }
 
            var cacheKey = $"Standard_Product_GetLogicalTreeByBelongTypeAndBelongID_{CorpID}_{BelongType}_{BelongID}";
            var vm_list = MemoryCacheHelper.GetSet(cacheKey, () =>
            {
 
                #region 设备组
                var group_list = new Service.ProductGroup().GetByBelongTypeAndBelongID(CorpID, BelongType, BelongID);
                var vm_logical_group_list = group_list?.Select(x =>
                {
                    var vm_logical_group = new ViewModel.LogicalTreeItem();
                    vm_logical_group.ID = $"{ObjectType.Product_设备组}_{x.ID}";
                    vm_logical_group.ParentID = $"{ObjectType.Product_设备组}_{TreeParentIdsHelper.GetLastParentID(x.ParentIds)}";
                    vm_logical_group.LogicalName = x.Name;
                    vm_logical_group.LogicalType = IStation.ObjectType.Product_设备组;
                    vm_logical_group.LogicalID = x.ID;
                    vm_logical_group.LogicalModel = new ViewModel.ProductGroup(x);
                    vm_logical_group.Children = new List<LogicalTreeItem>();
                    return vm_logical_group;
                }).ToList();
 
                #endregion
 
                #region 设备
 
                var service_product_type_property_group = new Service.ProductTypePropertyGroup();
                var product_list = new Service.Product().GetByBelongTypeAndBelongID(CorpID, BelongType, BelongID);
                var vm_logical_product_list = product_list?.Select(x =>
                {
                    var vm_logical_product = new ViewModel.LogicalTreeItem();
                    vm_logical_product.ID = $"{IStation.ObjectType.Product_设备}_{x.ID}";
                    if (x.ParentIds != null && x.ParentIds.Count > 0)
                    {
                        vm_logical_product.ParentID = $"{IStation.ObjectType.Product_设备}_{x.ParentIds.Last()}";
                    }
                    else
                    {
                        vm_logical_product.ParentID = $"{IStation.ObjectType.Product_设备组}_{x.GroupID}";
                    }
                    vm_logical_product.LogicalName = x.Name;
                    vm_logical_product.LogicalType = IStation.ObjectType.Product_设备;
                    vm_logical_product.LogicalID = x.ID;
                    vm_logical_product.LogicalModel = new ViewModel.Product(x, service_product_type_property_group.GetExItemsByProductTypeID(x.CorpID, x.ProductTypeID));
                    vm_logical_product.Children = new List<LogicalTreeItem>();
                    return vm_logical_product;
                }).ToList();
                #endregion
 
                #region 生成树
 
                var vm_cache_list = new List<ViewModel.LogicalTreeItem>();
 
                //遍历设备组
                if (vm_logical_group_list != null && vm_logical_group_list.Count > 0)
                {
                    foreach (var vm_logical_group in vm_logical_group_list)
                    {
                        var vm_logical_group_parent = vm_logical_group_list.Find(t => t.ID == vm_logical_group.ParentID);
                        if (vm_logical_group_parent == null)
                        {
                            vm_cache_list.Add(vm_logical_group);
                        }
                        else
                        {
                            vm_logical_group_parent.Children.Add(vm_logical_group);
                        }
                    }
                }
 
                //遍历设备
                if (vm_logical_product_list != null && vm_logical_product_list.Count > 0)
                {
                    foreach (var vm_logical_product in vm_logical_product_list)
                    {
                        var vm_logical_product_parent = vm_logical_product_list.Find(t => t.ID == vm_logical_product.ParentID);
                        if (vm_logical_product_parent == null)
                        {
                            vm_logical_product_parent = vm_logical_group_list?.Find(t => t.ID == vm_logical_product.ParentID);
                        }
                        if (vm_logical_product_parent == null)
                        {
                            vm_cache_list.Add(vm_logical_product);
                        }
                        else
                        {
                            vm_logical_product_parent.Children.Add(vm_logical_product);
                        }
                    }
                }
 
 
 
                return vm_cache_list;
 
                #endregion
 
            }, CacheHelper.CacheLevel2);
 
            return new Result<List<ViewModel.LogicalTreeItem>>(vm_list);
        }
 
        /// <summary>
        /// 通过 BelongType 和 BelongID 获取机泵列表
        /// </summary>
        [Route("GetEnginePumpListByBelongTypeAndBelongID")]
        [HttpGet]
        public Result GetEnginePumpListByBelongTypeAndBelongID(long CorpID, string BelongType, long BelongID)
        {
            if (CorpID < 1)
            {
                return new Result(Code.Error, "CorpID 参数错误");
            }
            if (string.IsNullOrEmpty(BelongType))
            {
                return new Result(Code.Error, "BelongType 参数错误");
            }
            if (BelongID < 1)
            {
                return new Result(Code.Error, "BelongID 参数错误");
            }
            var cacheKey = $"Standard_Product_GetEnginePumpListByBelongTypeAndBelongID_{CorpID}_{BelongType}_{BelongID}";
            var vm_list = MemoryCacheHelper.GetSet(cacheKey, () =>
            {
                var service_product = new Service.Product();
                var product_list = service_product.GetEnginePumpListByBelongTypeAndBelongID(CorpID, BelongType, BelongID);
                if (product_list == null || product_list.Count < 1)
                    return default;
                var service_product_type_property_group = new Service.ProductTypePropertyGroup();
                var vm_cache_list = product_list.Select(x => new ViewModel.Product<Model.EnginePump>(x, service_product_type_property_group.GetExItemsByProductTypeID(x.CorpID, x.ProductTypeID))).ToList();
                return vm_cache_list;
            }, CacheHelper.CacheLevel5);
 
            return new Result<List<ViewModel.Product<Model.EnginePump>>>(vm_list);
        }
 
 
 
        /// <summary>
        /// 通过 BelongType 和 BelongID 获取泵列表
        /// </summary>
        [Route("GetPumpListByBelongTypeAndBelongID")]
        [HttpGet]
        public Result GetPumpListByBelongTypeAndBelongID(long CorpID, string BelongType, long BelongID)
        {
            if (CorpID < 1)
            {
                return new Result(Code.Error, "CorpID 参数错误");
            }
            if (string.IsNullOrEmpty(BelongType))
            {
                return new Result(Code.Error, "BelongType 参数错误");
            }
            if (BelongID < 1)
            {
                return new Result(Code.Error, "BelongID 参数错误");
            }
            var cacheKey = $"Standard_Product_GetPumpListByBelongTypeAndBelongID_{CorpID}_{BelongType}_{BelongID}";
            var vm_list = MemoryCacheHelper.GetSet(cacheKey, () =>
            {
                var service_product = new Service.Product();
                var product_list = service_product.GetPumpListByBelongTypeAndBelongID(CorpID, BelongType, BelongID);
                if (product_list == null || product_list.Count < 1)
                    return default;
                var service_product_type_property_group = new Service.ProductTypePropertyGroup();
                var vm_cache_list = product_list.Select(x => new ViewModel.Product<Model.Pump>(x, service_product_type_property_group.GetExItemsByProductTypeID(x.CorpID, x.ProductTypeID))).ToList();
                return vm_cache_list;
            }, CacheHelper.CacheLevel5);
 
            return new Result<List<ViewModel.Product<Model.Pump>>>(vm_list);
        }
 
        /// <summary>
        /// 通过 BelongType 和 BelongID 获取泵拓展曲线列表
        /// </summary>
        [Route("GetPumpExCurveListByBelongTypeAndBelongID")]
        [HttpGet]
        public Result GetPumpExCurveListByBelongTypeAndBelongID(long CorpID, string BelongType, long BelongID)
        {
            if (CorpID < 1)
            {
                return new Result(Code.Error, "CorpID 参数错误");
            }
            if (string.IsNullOrEmpty(BelongType))
            {
                return new Result(Code.Error, "BelongType 参数错误");
            }
            if (BelongID < 1)
            {
                return new Result(Code.Error, "BelongID 参数错误");
            }
            var cacheKey = $"Standard_Product_GetPumpExCurveListByBelongTypeAndBelongID_{CorpID}_{BelongType}_{BelongID}";
            var vm_list = MemoryCacheHelper.GetSet(cacheKey, () =>
            {
                var service_product = new Service.Product();
                var product_list = service_product.GetPumpListByBelongTypeAndBelongID(CorpID, BelongType, BelongID);
                if (product_list == null || product_list.Count < 1)
                    return default;
                var service_product_type_property_group = new Service.ProductTypePropertyGroup();
                var service_pump_curve_mapping = new Service.PumpCurveExMapping();
                var vm_cache_list = new List<ViewModel.PumpExCurveList>();
                foreach (var product in product_list)
                {
                    var property_group_list = service_product_type_property_group.GetExItemsByProductTypeID(product.CorpID,product.ProductTypeID);
                    var curve_list = service_pump_curve_mapping.GetByPumpID(product.CorpID,product.ID);
                    var vm_cache = new ViewModel.PumpExCurveList(product,property_group_list,curve_list);
                    vm_cache_list.Add(vm_cache);
                }
                return vm_cache_list;
            }, CacheHelper.CacheLevel3);
 
            return new Result<List<ViewModel.PumpExCurveList>>(vm_list);
        }
 
        /// <summary>
        /// 通过 ID 获取
        /// </summary>
        [Route("GetByID")]
        [HttpGet]
        public Result GetByID(long CorpID, long ID)
        {
            if (CorpID < 1)
            {
                return new Result(Code.Alert,"CorpID 参数错误");
            }
            if (ID < 1)
            {
                return new Result(Code.Alert,"ID 参数错误");
            }
            var product = new Service.Product().GetByID(CorpID,ID);
            if (product == null)
            {
                return new Result(Code.Error,"未检索到设备信息");
            }
            var property_list = new Service.ProductTypePropertyGroup().GetExItemsByProductTypeID(product.CorpID,product.ProductTypeID);
            var vm = new ViewModel.Product(product, property_list);
            return new Result<ViewModel.Product>(vm);
        }
 
    }
}