| | |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEAWCS_Core; |
| | | using WIDESEAWCS_Core.BaseServices; |
| | | using WIDESEAWCS_Core.Enums; |
| | | using WIDESEAWCS_DTO.BasicInfo; |
| | | using WIDESEAWCS_IBasicInfoRepository; |
| | | using WIDESEAWCS_IBasicInfoService; |
| | | using WIDESEAWCS_Model.Models; |
| | | using WIDESEAWCS_QuartzJob.Models; |
| | | using WIDESEAWCS_QuartzJob.Repository; |
| | | |
| | | namespace WIDESEAWCS_BasicInfoService |
| | | { |
| | | public class RouterService : ServiceBase<Dt_Router, IRouterRepository>, IRouterService |
| | | { |
| | | public RouterService(IRouterRepository BaseDal) : base(BaseDal) |
| | | private readonly IDeviceProtocolRepository _deviceProtocolRepository; |
| | | private readonly IDeviceInfoRepository _deviceInfoRepository; |
| | | public RouterService(IRouterRepository BaseDal, IDeviceProtocolRepository deviceProtocolRepository, IDeviceInfoRepository deviceInfoRepository) : base(BaseDal) |
| | | { |
| | | _deviceProtocolRepository = deviceProtocolRepository; |
| | | _deviceInfoRepository = deviceInfoRepository; |
| | | } |
| | | |
| | | //todo 方法需优化 |
| | | public List<Dt_Router> QueryNextRoutes(string startPosi, string endPosi) |
| | | { |
| | | List<Dt_Router> routers = new List<Dt_Router>(); |
| | |
| | | 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 List<dynamic> GetAllWholeRouters() |
| | | { |
| | | List<dynamic> data = new List<dynamic>(); |
| | | List<Dt_Router> allRouters = BaseDal.QueryData(x => true); |
| | | List<Dt_Router> dt_Routers = allRouters.Where(x => x.IsEnd).OrderBy(x => x.Id).ToList(); |
| | | |
| | | foreach (var item in dt_Routers) |
| | | { |
| | | string routes = $"{item.ChildPosi},"; |
| | | string str = GetPreviousRoutes(item.StartPosi, allRouters, item.InOutType); |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | return data; |
| | | } |
| | | |
| | | public string GetPreviousRoutes(string startPosi, List<Dt_Router> allRouters, RouterInOutType routerType) |
| | | { |
| | | string routers = string.Empty; |
| | | if (!string.IsNullOrEmpty(startPosi)) |
| | | { |
| | | if (!routers.EndsWith(",")) |
| | | routers += $"{startPosi},"; |
| | | else |
| | | routers += $"{startPosi}"; |
| | | } |
| | | List<Dt_Router> preRouters = allRouters.Where(x => x.NextPosi == startPosi && x.InOutType == routerType).ToList(); |
| | | foreach (var item in preRouters) |
| | | { |
| | | string str = GetPreviousRoutes(item.StartPosi, allRouters, routerType); |
| | | if (!string.IsNullOrEmpty(str)) |
| | | { |
| | | if (routers.EndsWith(",")) |
| | | routers += $"{str}"; |
| | | else |
| | | routers += $"{str},"; |
| | | } |
| | | } |
| | | return routers; |
| | | } |
| | | |
| | | public WebResponseContent AddRouters(List<RoutersAddDTO> routersAddDTOs, int routerType) |
| | | { |
| | | WebResponseContent content = new WebResponseContent(); |
| | | try |
| | | { |
| | | if (routersAddDTOs.GroupBy(x => x.ChildPositionCode).Where(x => !string.IsNullOrEmpty(x.Key)).Select(x => x.Count()).Any(x => x > 1)) |
| | | { |
| | | return content = WebResponseContent.Instance.Error("子位置编号重复"); |
| | | } |
| | | |
| | | if (routersAddDTOs.GroupBy(x => x.PositionCode).Select(x => x.Count()).Any(x => x > 1)) |
| | | { |
| | | return content = WebResponseContent.Instance.Error("根位置编号重复"); |
| | | } |
| | | List<dynamic> deviceCode = _deviceInfoRepository.QueryTabs<Dt_DeviceInfo, Dt_DeviceProtocol, dynamic>((a, b) => new object[] { JoinType.Inner, a.Id == b.DeviceId }, (a, b) => new { b.DeviceChildCode, a.DeviceCode }, (a, b) => true, x => true).Distinct().ToList(); |
| | | |
| | | List<Dt_Router> routers = new List<Dt_Router>(); |
| | | for (int i = 0; i < routersAddDTOs.Count - 1; i++) |
| | | { |
| | | dynamic obj = deviceCode.FirstOrDefault(x => x.DeviceChildCode == routersAddDTOs[i + 1].PositionCode || x.DeviceChildCode == routersAddDTOs[i + 1].ChildPositionCode); |
| | | Dt_Router router = new Dt_Router() |
| | | { |
| | | ChildPosi = routersAddDTOs[i + 1].PositionCode, |
| | | ChildPosiDeviceCode = obj.DeviceCode, |
| | | Depth = 1, |
| | | InOutType = (RouterInOutType)routerType, |
| | | NextPosi = routersAddDTOs[i + 1].PositionCode, |
| | | SrmColumn = string.IsNullOrEmpty(routersAddDTOs[i].SCColumn) ? int.TryParse(routersAddDTOs[i + 1].SCColumn, out int col) ? col : null : int.TryParse(routersAddDTOs[i].SCColumn, out int col2) ? col2 : null, |
| | | SrmLayer = string.IsNullOrEmpty(routersAddDTOs[i].SCLayer) ? int.TryParse(routersAddDTOs[i + 1].SCLayer, out int lay) ? lay : null : int.TryParse(routersAddDTOs[i].SCLayer, out int lay2) ? lay2 : null, |
| | | SrmRow = string.IsNullOrEmpty(routersAddDTOs[i].SCRow) ? int.TryParse(routersAddDTOs[i + 1].SCRow, out int row) ? row : null : int.TryParse(routersAddDTOs[i].SCRow, out int row2) ? row2 : null, |
| | | StartPosi = routersAddDTOs[i].PositionCode, |
| | | IsEnd = false |
| | | }; |
| | | if (i == routersAddDTOs.Count - 2) |
| | | { |
| | | if (routerType == (int)RouterInOutType.Out) |
| | | router.ChildPosi = routersAddDTOs[i + 1].ChildPositionCode; |
| | | router.IsEnd = true; |
| | | } |
| | | routers.Add(router); |
| | | } |
| | | if (routers.Any(x => x.StartPosi == x.ChildPosi)) |
| | | { |
| | | return content = WebResponseContent.Instance.Error("输入数据起点位置编号与子位置编号相同"); |
| | | } |
| | | if (routers.Any(x => x.StartPosi == x.NextPosi)) |
| | | { |
| | | return content = WebResponseContent.Instance.Error("输入数据起点位置编号与终点位置编号相同"); |
| | | } |
| | | List<Dt_Router> dt_Routers = BaseDal.QueryData(x => x.InOutType == (RouterInOutType)routerType); |
| | | |
| | | dt_Routers.ForEach(x => |
| | | { |
| | | var t = routers.FirstOrDefault(v => v.StartPosi == x.StartPosi && v.NextPosi == x.NextPosi); |
| | | if (t != null) |
| | | { |
| | | routers.Remove(t); |
| | | } |
| | | var r = routers.FirstOrDefault(v => v.StartPosi == x.StartPosi && v.ChildPosi == x.ChildPosi); |
| | | if (r != null) |
| | | { |
| | | routers.Remove(r); |
| | | } |
| | | }); |
| | | |
| | | BaseDal.AddData(routers); |
| | | content = WebResponseContent.Instance.OK(); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | content = WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | return content; |
| | | } |
| | | } |
| | | } |