hutongqing
2024-08-23 e98b07c84a2a496da895ef6b523b29ccc75e004d
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 SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.BaseServices;
using WIDESEAWCS_Core.Enums;
using WIDESEAWCS_IBasicInfoRepository;
using WIDESEAWCS_IBasicInfoService;
using WIDESEAWCS_Model.Models;
 
namespace WIDESEAWCS_BasicInfoService
{
    public class RouterService : ServiceBase<Dt_Router, IRouterRepository>, IRouterService
    {
        public RouterService(IRouterRepository BaseDal) : base(BaseDal)
        {
        }
 
        public List<Dt_Router> QueryNextRoutes(string startPosi, string endPosi)
        {
            List<Dt_Router> routers = new List<Dt_Router>();
            try
            {
                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => x.NextPosi == endPosi || x.ChildPosi == endPosi, new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
                if (dt_Routers.Count > 0)
                {
                    foreach (var item in dt_Routers)
                    {
                        if (item.StartPosi == startPosi && !routers.Any(x => x.Id == item.Id))
                        {
                            routers.Add(item);
                        }
                        else
                        {
                            List<Dt_Router> tempRouters = QueryNextRoutes(startPosi, item.StartPosi);
                            foreach (var router in tempRouters)
                            {
                                if (router.StartPosi == startPosi && !routers.Any(x => x.Id == router.Id))
                                {
                                    routers.Add(router);
                                }
                            }
                        }
                    }
                }
                else
                {
                    throw new Exception($"该路径未配置或配置错误,请检查设备路由信息,起点:【{startPosi}】,终点:【{endPosi}】");
                }
            }
            catch (Exception ex)
            {
                //throw new Exception(ex.Message);
                //记录错误信息
            }
            return routers;
        }
 
 
        public List<string> QueryAllPositions(string deviceCode)
        {
            List<string> positions = new List<string>();
            try
            {
                List<string> inRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.In).GroupBy(x => x.StartPosi).Select(x => x.Key).ToList();
 
                List<string> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out).GroupBy(x => x.ChildPosi).Select(x => x.Key).ToList();
 
                positions.AddRange(inRouterPositions);
                positions.AddRange(outRouterPositions);
                return positions.GroupBy(x => x).Select(x => x.Key).ToList();
            }
            catch
            {
 
            }
            return positions;
        }
 
        public WebResponseContent GetAllWholeRouters()
        {
            WebResponseContent content = new();
            try
            {
                List<object> data = new List<object>();
                List<Dt_Router> dt_Routers = BaseDal.QueryData(x => x.IsEnd, new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
                
                foreach (var item in dt_Routers)
                {
                    string routes = $"{item.ChildPosi},";
                    string str = GetPreviousRoutes(item.StartPosi);
                    if (!string.IsNullOrEmpty(str))
                    {
                        if (str.EndsWith(","))
                            str = str.Substring(0, str.Length - 1);
                        routes += str;
                    }
                    if (item.InOutType == RouterInOutType.In)
                    {
                        List<string> itemRouters = routes.Split(",").Reverse().ToList();
                        object obj = new { type = RouterInOutType.In, routes = itemRouters };
                        data.Add(obj);
                    }
                    else
                    {
                        List<string> itemRouters = routes.Split(",").Reverse().ToList();
                        object obj = new { type = RouterInOutType.Out, routes = itemRouters };
                        data.Add(obj);
                    }
                }
 
                content = WebResponseContent.Instance.OK(data: data);
            }
            catch (Exception ex)
            {
 
            }
            return content;
        }
 
        public string GetPreviousRoutes(string startPosi)
        {
            string routers = string.Empty;
            if (!string.IsNullOrEmpty(startPosi))
            {
                if (!routers.EndsWith(","))
                    routers += $"{startPosi},";
                else
                    routers += $"{startPosi}";
            }
            List<Dt_Router> preRouters = BaseDal.QueryData(x => x.NextPosi == startPosi, new Dictionary<string, OrderByType> { { nameof(Dt_Router.IsEnd), OrderByType.Desc } });
            foreach (var item in preRouters)
            {
                string str = GetPreviousRoutes(item.StartPosi);
                if (!string.IsNullOrEmpty(str))
                {
                    if (routers.EndsWith(","))
                        routers += $"{str}";
                    else
                        routers += $"{str},";
                }
            }
            return routers;
        }
    }
}