fixed hierarchy sql scan #23
|
|
@ -6,6 +6,7 @@ import (
|
|||
dbutils "bitrix_balancer_api/core/utilscore/db"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
|
|
@ -235,19 +236,41 @@ func (u *UserHierarchyDB) GetApproversDB(ctx context.Context, subordinateID, dep
|
|||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (u *UserHierarchyDB) GetHierarchiesWithRelations(ctx context.Context, opts typescore.ListDbOptions) ([]*typescore.UserHierarchy, uint64, error) {
|
||||
options, filter, err := dbutils.GetOptionsDB[typescore.UserHierarchy](opts)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
query := squirrel.Select("uh.*",
|
||||
"sub.id", "sub.full_name", "sub.username", // поля Subordinate
|
||||
"app.id", "app.full_name", "app.username", // поля Approver
|
||||
"dep.id", "dep.name", // поля Department
|
||||
"cat.id", "cat.name", // поля Category
|
||||
"top.id", "top.name", // поля Topic
|
||||
query := squirrel.Select(
|
||||
// Основные поля таблицы user_hierarchy (в порядке сканирования)
|
||||
"uh.id",
|
||||
"uh.subordinate_id",
|
||||
"uh.approver_id",
|
||||
"uh.department_id",
|
||||
"uh.topic_category_id",
|
||||
"uh.topic_id",
|
||||
"uh.priority",
|
||||
"uh.created_at",
|
||||
"uh.updated_at",
|
||||
// Поля Subordinate
|
||||
"sub.id",
|
||||
"sub.full_name",
|
||||
"sub.username",
|
||||
// Поля Approver
|
||||
"app.id",
|
||||
"app.full_name",
|
||||
"app.username",
|
||||
// Поля Department
|
||||
"dep.id",
|
||||
"dep.name",
|
||||
// Поля Category
|
||||
"cat.id",
|
||||
"cat.name",
|
||||
// Поля Topic
|
||||
"top.id",
|
||||
"top.name",
|
||||
// Общее количество записей для пагинации
|
||||
"COUNT(*) OVER() AS total_count").
|
||||
From("user_hierarchy uh").
|
||||
LeftJoin("users sub ON uh.subordinate_id = sub.id").
|
||||
|
|
@ -258,7 +281,7 @@ func (u *UserHierarchyDB) GetHierarchiesWithRelations(ctx context.Context, opts
|
|||
|
||||
query = dbutils.SetterLimitAndOffsetQuery(query, options.Offset, options.Limit)
|
||||
query = dbutils.ApplyFilters(query, filter, options.LikeFields, "uh")
|
||||
query = query.OrderBy("created_at DESC")
|
||||
query = query.OrderBy("uh.created_at DESC")
|
||||
|
||||
sql, args, err := query.PlaceholderFormat(squirrel.Dollar).ToSql()
|
||||
if err != nil {
|
||||
|
|
@ -282,18 +305,39 @@ func (u *UserHierarchyDB) GetHierarchiesWithRelations(ctx context.Context, opts
|
|||
h.TopicCategory = &typescore.ApprovalTopicCategory{}
|
||||
h.Topic = &typescore.ApprovalTopic{}
|
||||
|
||||
// Сканируем все поля в соответствующие структуры
|
||||
err := rows.Scan(
|
||||
&h.ID, &h.SubordinateID, &h.ApproverID, &h.DepartmentID, &h.TopicCategoryID, &h.TopicID, &h.Priority, &h.CreatedAt, &h.UpdatedAt,
|
||||
&h.Subordinate.ID, &h.Subordinate.FullName, &h.Subordinate.Username,
|
||||
&h.Approver.ID, &h.Approver.FullName, &h.Approver.Username,
|
||||
&h.Department.ID, &h.Department.Name,
|
||||
&h.TopicCategory.ID, &h.TopicCategory.Name,
|
||||
&h.Topic.ID, &h.Topic.Name,
|
||||
// Основные поля (9 полей)
|
||||
&h.ID,
|
||||
&h.SubordinateID,
|
||||
&h.ApproverID,
|
||||
&h.DepartmentID,
|
||||
&h.TopicCategoryID,
|
||||
&h.TopicID,
|
||||
&h.Priority,
|
||||
&h.CreatedAt,
|
||||
&h.UpdatedAt,
|
||||
// Subordinate (3 поля)
|
||||
&h.Subordinate.ID,
|
||||
&h.Subordinate.FullName,
|
||||
&h.Subordinate.Username,
|
||||
// Approver (3 поля)
|
||||
&h.Approver.ID,
|
||||
&h.Approver.FullName,
|
||||
&h.Approver.Username,
|
||||
// Department (2 поля)
|
||||
&h.Department.ID,
|
||||
&h.Department.Name,
|
||||
// TopicCategory (2 поля)
|
||||
&h.TopicCategory.ID,
|
||||
&h.TopicCategory.Name,
|
||||
// Topic (2 поля)
|
||||
&h.Topic.ID,
|
||||
&h.Topic.Name,
|
||||
// Общее количество
|
||||
&totalCount,
|
||||
)
|
||||
if err != nil {
|
||||
continue
|
||||
return nil, 0, fmt.Errorf("scan error: %w", err)
|
||||
}
|
||||
results = append(results, h)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue