|  |  |  | 
|---|
|  |  |  | /// <param name="startPosi">起点/当前位置。</param> | 
|---|
|  |  |  | /// <param name="endPosi">终点。</param> | 
|---|
|  |  |  | /// <returns>返回路由实体集合。</returns> | 
|---|
|  |  |  | //public List<Dt_Router> QueryNextRoutes(string startPosi, string endPosi) | 
|---|
|  |  |  | //{ | 
|---|
|  |  |  | //    //todo 方法需优化 | 
|---|
|  |  |  | //    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; | 
|---|
|  |  |  | //} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 根据起点/当前位置、终点获取下一个子节点。 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="startPosi">起点/当前位置。</param> | 
|---|
|  |  |  | /// <param name="endPosi">终点。</param> | 
|---|
|  |  |  | /// <returns>返回路由实体集合。</returns> | 
|---|
|  |  |  | public List<Dt_Router> QueryNextRoutes(string startPosi, string endPosi) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //todo 方法需优化 | 
|---|
|  |  |  | // 用于记录已经访问过的起点和终点组合,避免重复访问进入死循环 | 
|---|
|  |  |  | HashSet<string> visitedRoutes = new HashSet<string>(); | 
|---|
|  |  |  | return QueryNextRoutesInternal(startPosi, endPosi, visitedRoutes); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private List<Dt_Router> QueryNextRoutesInternal(string startPosi, string endPosi, HashSet<string> visitedRoutes) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<Dt_Router> routers = new List<Dt_Router>(); | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | // 构建一个唯一标识当前起点和终点组合的字符串 | 
|---|
|  |  |  | string routeKey = $"{startPosi}_{endPosi}"; | 
|---|
|  |  |  | if (visitedRoutes.Contains(routeKey)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | // 如果已经访问过,直接返回空列表,避免重复进入相同的递归分支 | 
|---|
|  |  |  | return routers; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | visitedRoutes.Add(routeKey); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<Dt_Router> tempRouters = QueryNextRoutes(startPosi, item.StartPosi); | 
|---|
|  |  |  | List<Dt_Router> tempRouters = QueryNextRoutesInternal(startPosi, item.StartPosi, visitedRoutes); | 
|---|
|  |  |  | foreach (var router in tempRouters) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if (router.StartPosi == startPosi && !routers.Any(x => x.Id == router.Id)) | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch (Exception ex) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | // 这里可以根据实际需求更好地处理异常,比如记录日志等,目前只是注释掉了直接抛出异常 | 
|---|
|  |  |  | //throw new Exception(ex.Message); | 
|---|
|  |  |  | //记录错误信息 | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return content; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /// <summary> | 
|---|
|  |  |  | /// 根据设备编号获取出库路由点位 | 
|---|
|  |  |  | /// </summary> | 
|---|
|  |  |  | /// <param name="deviceCode"></param> | 
|---|
|  |  |  | /// <returns></returns> | 
|---|
|  |  |  | public List<string> QueryOutDeviceCodes(string deviceCode) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<string> positions = new List<string>(); | 
|---|
|  |  |  | try | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<string> outRouterPositions = BaseDal.QueryData(x => x.ChildPosiDeviceCode == deviceCode && x.InOutType == RouterInOutType.Out).GroupBy(x => x.ChildPosi).Select(x => x.Key).ToList(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | positions.AddRange(outRouterPositions); | 
|---|
|  |  |  | return positions.GroupBy(x => x).Select(x => x.Key).ToList(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | catch | 
|---|
|  |  |  | { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return positions; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|