ruspet_portal_back/service/telegram_service/keyboard/delegation.go

323 lines
11 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// telegram_service/keyboard/delegation.go - ПОЛНОСТЬЮ ЗАМЕНИТЬ
package keyboard
import (
"bitrix_balancer_api/core/lib/external/tgkit"
"bitrix_balancer_api/core/typescore"
"bitrix_balancer_api/telegram_service/types"
"context"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const delegationPerPage = 6
func BuildDelegationMainMenu() interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Делегировать заявки", "deleg_create")).
Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Делегировать задачи", "deleg_task_create")).
Row(tgkit.Btn("📋 Мои делегирования (Заявки)", "deleg_list")).
Row(tgkit.Btn("📋 Мои делегирования (Задачи)", "deleg_task_list")).
Build()
}
func BuildDelegationCategoryMenu(categories []*typescore.ApprovalTopicCategory, page int) interface{} {
total := len(categories)
totalPages := (total + delegationPerPage - 1) / delegationPerPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*delegationPerPage, page*delegationPerPage+delegationPerPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
for _, c := range categories[start:end] {
if c.ID == nil || c.Name == nil {
continue
}
kb.Row(tgkit.Btn(*c.Name, fmt.Sprintf("deleg_category:%d", *c.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_category_page", "deleg_page_noop"))
}
return kb.Build()
}
func BuildDelegationTopicMenu(topics []*typescore.ApprovalTopic, categoryID uint64, page int) interface{} {
const perPage = 6
total := len(topics)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Вся категория", fmt.Sprintf("deleg_all_category:%d", categoryID)))
for _, t := range topics[start:end] {
if t.ID == nil || t.Name == nil {
continue
}
kb.Row(tgkit.Btn(*t.Name, fmt.Sprintf("deleg_topic:%d", *t.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages,
fmt.Sprintf("deleg_topic_page:%d", categoryID), "deleg_page_noop"))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К категориям", "deleg_back_categories"))
return kb.Build()
}
func BuildDelegationUserMenu(users []*typescore.User, categoryID, topicID uint64, page int) interface{} {
const perPage = 8
total := len(users)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
for _, u := range users[start:end] {
if u.ID == nil || u.FullName == nil {
continue
}
kb.Row(tgkit.Btn(*u.FullName, fmt.Sprintf("deleg_user:%d", *u.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages,
fmt.Sprintf("deleg_user_page:%d:%d", categoryID, topicID), "deleg_page_noop"))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", fmt.Sprintf("deleg_category:%d", categoryID)))
return kb.Build()
}
func BuildDelegatedRightsList(delegations []*typescore.ApprovalDelegation, page int, ctx context.Context, ipc *types.InternalProviderControl) interface{} {
const perPage = 6
total := len(delegations)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
for _, d := range delegations[start:end] {
if d.ID == nil {
continue
}
scopeName := "—"
if d.TopicID != nil {
if t, _ := ipc.Database.ApprovalTopic.GetApprovalTopicByIDDB(ctx, *d.TopicID); t != nil && t.Name != nil {
scopeName = *t.Name
}
} else if d.CategoryID != nil {
if c, _ := ipc.Database.ApprovalTopicCategory.GetApprovalTopicCategoryByIDDB(ctx, *d.CategoryID); c != nil && c.Name != nil {
scopeName = *c.Name
}
}
kb.Row(tgkit.Btn(scopeName, fmt.Sprintf("deleg_detail:%d", *d.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_list_page", "deleg_page_noop"))
}
return kb.Build()
}
func BuildDelegationActions(delegationID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("📅 Изменить срок", fmt.Sprintf("deleg_extend:%d", delegationID))).
Row(tgkit.BtnStyled(tgkit.ButtonStyleDanger, "Отозвать делегирование", fmt.Sprintf("deleg_revoke:%d", delegationID))).
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К списку", "deleg_list")).
Build()
}
func BuildDelegationExtendCancel(delegationID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Отмена", fmt.Sprintf("deleg_detail:%d", delegationID))).
Build()
}
func BuildDelegationBackToUsers(categoryID, topicID uint64) tgbotapi.InlineKeyboardMarkup {
callback := fmt.Sprintf("deleg_all_category:%d", categoryID)
if topicID > 0 {
callback = fmt.Sprintf("deleg_topic:%d", topicID)
}
return tgkit.NewInlineKeyboard().
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", callback)).
Build()
}
// --- КЛАВИАТУРЫ ДЛЯ ДЕЛЕГИРОВАНИЯ ЗАДАЧ ---
// BuildTaskDelegationUserMenu выводит список пользователей для передачи задач
func BuildTaskDelegationUserMenu(users []*typescore.User, page int) interface{} {
const perPage = 8
total := len(users)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
for _, u := range users[start:end] {
if u.ID == nil || u.FullName == nil {
continue
}
kb.Row(tgkit.Btn(*u.FullName, fmt.Sprintf("deleg_task_user:%d", *u.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_task_user_page", "deleg_page_noop"))
}
// Кнопка возврата в главное меню делегирования
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", "deleg_main_menu"))
return kb.Build()
}
// BuildTaskDelegatedRightsList выводит список ваших активных делегирований задач
func BuildTaskDelegatedRightsList(delegations []*typescore.TaskDelegation, page int, ctx context.Context, ipc *types.InternalProviderControl) interface{} {
const perPage = 6
total := len(delegations)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
for _, d := range delegations[start:end] {
if d.ID == nil || d.DelegateID == nil {
continue
}
delegateName := "Неизвестно"
if u, _ := ipc.Database.User.GetUserByIDDB(ctx, *d.DelegateID); u != nil && u.FullName != nil {
delegateName = *u.FullName
}
kb.Row(tgkit.Btn(fmt.Sprintf("Задачи ➡️ %s", delegateName), fmt.Sprintf("deleg_task_detail:%d", *d.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_task_list_page", "deleg_page_noop"))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "В главное меню", "deleg_main_menu"))
return kb.Build()
}
// BuildTaskDelegationActions выводит кнопки управления (Отозвать/Продлить) конкретным делегированием задач
func BuildTaskDelegationActions(delegationID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("📅 Изменить срок", fmt.Sprintf("deleg_task_extend:%d", delegationID))).
Row(tgkit.BtnStyled(tgkit.ButtonStyleDanger, "Отозвать делегирование", fmt.Sprintf("deleg_task_revoke:%d", delegationID))).
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К списку", "deleg_task_list")).
Build()
}
// BuildTaskDelegationExtendCancel кнопка отмены при вводе новой даты
func BuildTaskDelegationExtendCancel(delegationID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Отмена", fmt.Sprintf("deleg_task_detail:%d", delegationID))).
Build()
}
func BuildTaskDelegationCategoryMenu(categories []*typescore.ApprovalTopicCategory, page int) interface{} {
const perPage = 6
total := len(categories)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
// Кнопка для делегирования ВСЕХ задач разом
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "🌍 Все задачи (Глобально)", "deleg_task_all"))
for _, c := range categories[start:end] {
if c.ID == nil || c.Name == nil {
continue
}
kb.Row(tgkit.Btn(*c.Name, fmt.Sprintf("deleg_task_category:%d", *c.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, "deleg_task_cat_page", "deleg_page_noop"))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "В главное меню", "deleg_main_menu"))
return kb.Build()
}
func BuildTaskDelegationTopicMenu(topics []*typescore.ApprovalTopic, categoryID uint64, page int) interface{} {
const perPage = 6
total := len(topics)
totalPages := (total + perPage - 1) / perPage
if page < 0 {
page = 0
}
if page >= totalPages && totalPages > 0 {
page = totalPages - 1
}
start, end := page*perPage, page*perPage+perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
// Кнопка для делегирования всех задач внутри конкретной категории
kb.Row(tgkit.BtnStyled(tgkit.ButtonStylePrimary, "📂 Вся категория", fmt.Sprintf("deleg_task_all_cat:%d", categoryID)))
for _, t := range topics[start:end] {
if t.ID == nil || t.Name == nil {
continue
}
kb.Row(tgkit.Btn(*t.Name, fmt.Sprintf("deleg_task_topic:%d", *t.ID)))
}
if totalPages > 1 {
kb.Rows = append(kb.Rows, buildPageRow(page, totalPages, fmt.Sprintf("deleg_task_top_page:%d", categoryID), "deleg_page_noop"))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К категориям", "deleg_task_create"))
return kb.Build()
}