tangxu
2023-04-10 7f8ba13a353ed3da5efbbbf623d586428f4bce96
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
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Net.Http.Headers;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.AspNetCore.Http.Extensions;
using IStation.Untity;
using Furion.DynamicApiController;
using System.ComponentModel.DataAnnotations;
using Mapster;
using Microsoft.AspNetCore.Http;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
using Furion.DataEncryption;
using Furion.FriendlyException;
 
namespace IStation.Application
{
    /// <summary>
    /// Product
    /// </summary>
    [AllowAnonymous]
    [Route("LargeScreen/Demo/Product")]
    [ApiDescriptionSettings("LargeScreen", Name = "Demo(设备)", Order = 666)]
    public class DemoProduct_Controller : IDynamicApiController, ITransient
    {
 
        private readonly IHttpContextAccessor _httpContextAccessor;
 
        /// <summary>
        /// 
        /// </summary>
        public DemoProduct_Controller(IHttpContextAccessor httpContextAccessor) 
        {
            _httpContextAccessor = httpContextAccessor;
        }
 
 
        /// <summary>
        /// 通过 BelongType 和 BelongID 获取逻辑树
        /// </summary>
        [Route("GetLogicalTreeByBelongTypeAndBelongID")]
        [HttpGet]
        public List<LogicalTreeItemDto> GetLogicalTreeByBelongTypeAndBelongID([FromQuery][Required] BelongUnderCorpInput input)
        {
            var corpId = input.CorpID;
            var belongType = input.BelongType;
            var belongId = input.BelongID;
 
            var cacheKey = $"LargeScreen_Demo_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 LogicalTreeItemDto();
                    vm_logical_group.ID = $"{ObjectType.ProductGroup}_{x.ID}";
                    vm_logical_group.ParentID = $"{ObjectType.ProductGroup}_{TreeParentIdsHelper.GetLastParentID(x.ParentIds)}";
                    vm_logical_group.LogicalName = x.Name;
                    vm_logical_group.LogicalType = ObjectType.ProductGroup;
                    vm_logical_group.LogicalID = x.ID;
                    vm_logical_group.LogicalModel = new DemoProductGroupDto(x);
                    vm_logical_group.Children = new List<LogicalTreeItemDto>();
                    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 LogicalTreeItemDto();
                    vm_logical_product.ID = $"{ObjectType.Product}_{x.ID}";
                    if (x.ParentIds != null && x.ParentIds.Count > 0)
                    {
                        vm_logical_product.ParentID = $"{ObjectType.Product}_{x.ParentIds.Last()}";
                    }
                    else
                    {
                        vm_logical_product.ParentID = $"{ObjectType.ProductGroup}_{x.GroupID}";
                    }
                    vm_logical_product.LogicalName = x.Name;
                    vm_logical_product.LogicalType = ObjectType.Product;
                    vm_logical_product.LogicalID = x.ID;
                    vm_logical_product.LogicalModel = new DemoProductDto(x, service_product_type_property_group.GetExItemsByProductTypeID(x.CorpID, x.ProductTypeID));
                    vm_logical_product.Children = new List<LogicalTreeItemDto>();
                    return vm_logical_product;
                }).ToList();
                #endregion
 
                #region 生成树
 
                var vm_cache_list = new List<LogicalTreeItemDto>();
 
                //遍历设备组
                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 vm_list;
        }
 
    }
}