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
{
///
/// Product
///
[AllowAnonymous]
[Route("LargeScreen/Demo/Product")]
[ApiDescriptionSettings("LargeScreen", Name = "Demo(设备)", Order = 666)]
public class DemoProduct_Controller : IDynamicApiController, ITransient
{
private readonly IHttpContextAccessor _httpContextAccessor;
///
///
///
public DemoProduct_Controller(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
///
/// 通过 BelongType 和 BelongID 获取逻辑树
///
[Route("GetLogicalTreeByBelongTypeAndBelongID")]
[HttpGet]
public List 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();
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();
return vm_logical_product;
}).ToList();
#endregion
#region 生成树
var vm_cache_list = new List();
//遍历设备组
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;
}
}
}