ruspet_portal_back/service/telegram_service/keyboard/approval.go

394 lines
14 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.

package keyboard
import (
"bitrix_balancer_api/core/lib/external/tgkit"
"bitrix_balancer_api/core/typescore"
"bitrix_balancer_api/core/utilscore"
"bitrix_balancer_api/telegram_service/approvalflow"
"bitrix_balancer_api/telegram_service/state"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
// BuildApproverDecision — кнопки для согласующего
func BuildApproverDecision(approvalID uint64, status string, hasFiles bool, filesCount int, commentsCount int) tgbotapi.InlineKeyboardMarkup {
kb := tgkit.NewInlineKeyboard()
if status == "approved" {
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleRevision, "Отозвать", fmt.Sprintf("appr_revoke:%d", approvalID)))
kb.Row(tgkit.Btn("📊 Стадия согласования", fmt.Sprintf("appr_stage:%d", approvalID)))
kb.Row(buildCommentsButton(approvalID, commentsCount))
return kb.Build()
}
if hasFiles {
buttonTitle := fmt.Sprintf("📎 Просмотреть файлы (%d)", filesCount)
kb.Row(tgkit.Btn(buttonTitle, fmt.Sprintf("appr_view_files:%d", approvalID)))
}
kb.Row(
tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Да", fmt.Sprintf("appr_yes:%d", approvalID)),
tgkit.BtnStyled(tgkit.ButtonStyleDanger, "Нет", fmt.Sprintf("appr_no:%d", approvalID)),
).
Row(tgkit.Btn("🔄 Вернуть на доработку", fmt.Sprintf("appr_revision:%d", approvalID))).
Row(
tgkit.Btn("💬 Комментировать", fmt.Sprintf("appr_comment:%d", approvalID)),
buildCommentsButton(approvalID, commentsCount),
).
Row(
tgkit.Btn("📎 Прикрепить файл", fmt.Sprintf("appr_attach:%d", approvalID)),
).
Row(
tgkit.Btn("➕ Добавить согласующего", fmt.Sprintf("add_appr_to_approval:%d", approvalID)),
).
Row(tgkit.Btn("📊 Стадия согласования", fmt.Sprintf("appr_stage:%d", approvalID)))
return kb.Build()
}
// BuildInitiatorCard — кнопки инициатора на карточке заявки
func BuildInitiatorCard(approvalID uint64, status string, hasFiles bool, filesCount int, commentsCount int) interface{} {
kb := tgkit.NewInlineKeyboard()
if hasFiles {
buttonTitle := fmt.Sprintf("📎 Просмотреть файлы (%d)", filesCount)
kb.Row(tgkit.Btn(buttonTitle, fmt.Sprintf("appr_view_files:%d", approvalID)))
}
if status != "approved" {
kb.Row(tgkit.Btn("💬 Комментировать", fmt.Sprintf("appr_comment:%d", approvalID)))
}
if commentsCount > 0 {
buildCommentsButton(approvalID, commentsCount)
}
if status == "pending" || status == "revision" || status == "approved" {
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleRevision, "Отозвать", fmt.Sprintf("appr_revoke:%d", approvalID)))
}
kb.Row(tgkit.Btn("📊 Стадия согласования", fmt.Sprintf("appr_stage:%d", approvalID)))
return kb.Build()
}
// BuildRevokeButton — кнопка отзыва (для директора и инициатора)
func BuildRevokeButton(approvalID uint64, status string, filesCount int, commentsCount int) interface{} {
kb := tgkit.NewInlineKeyboard()
if filesCount > 0 {
kb.Row(tgkit.Btn(fmt.Sprintf("📎 Просмотреть файлы (%d)", filesCount), fmt.Sprintf("appr_view_files:%d", approvalID)))
}
kb.Row(
tgkit.Btn("💬 Комментировать", fmt.Sprintf("appr_comment:%d", approvalID)),
buildCommentsButton(approvalID, commentsCount),
)
if status == "pending" || status == "revision" || status == "approved" {
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleRevision, "Отозвать заявку", fmt.Sprintf("appr_revoke:%d", approvalID)))
}
kb.Row(tgkit.Btn("📊 Стадия согласования", fmt.Sprintf("appr_stage:%d", approvalID)))
return kb.Build()
}
// BuildObserverActions — кнопки наблюдателя
func BuildObserverActions(approvalID uint64, topicCode string, status string, filesCount, commentsCount int) tgbotapi.InlineKeyboardMarkup {
kb := tgkit.NewInlineKeyboard()
if filesCount > 0 {
kb.Row(tgkit.Btn(
fmt.Sprintf("📎 Просмотреть файлы (%d)", filesCount),
fmt.Sprintf("appr_view_files:%d", approvalID),
))
}
kb.Row(tgkit.Btn("💬 Комментировать", fmt.Sprintf("appr_comment:%d", approvalID)))
buildCommentsButton(approvalID, commentsCount)
return kb.Build()
}
// BuildApprovalMenu — список тем для создания заявки
func BuildApprovalMenu(topics []approvalflow.Topic, 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 := page * perPage
end := start + perPage
if end > total {
end = total
}
kb := tgkit.NewInlineKeyboard()
for _, t := range topics[start:end] {
kb.Row(tgkit.Btn(t.Name(), fmt.Sprintf("appr:%s:%s", t.Code(), t.Code())))
}
if totalPages > 1 {
pagination := tgkit.PaginationRow(
page,
totalPages,
fmt.Sprintf("appr_page:%d", page-1),
fmt.Sprintf("appr_page:%d", page+1),
"appr_page_noop",
)
kb.Rows = append(kb.Rows, pagination)
}
return kb.Build()
}
// BuildApprovalStep — шаг флоу согласования
func BuildApprovalStep(topicCode string, nodes []*approvalflow.FlowNode) interface{} {
kb := tgkit.NewInlineKeyboard()
for _, n := range nodes {
if n == nil {
continue
}
kb.Row(tgkit.Btn(n.Label, fmt.Sprintf("appr:%s:%s", topicCode, n.Key)))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", fmt.Sprintf("appr_back:%s", topicCode)))
return kb.Build()
}
// BuildApprovalInputPrompt — подсказка ввода
func BuildApprovalInputPrompt(topicCode string) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", fmt.Sprintf("appr_back:%s", topicCode))).
Build()
}
// BuildRejectOptions — варианты при отклонении
func BuildRejectOptions(approvalID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("💬 Написать причину", fmt.Sprintf("appr_no_comment:%d", approvalID))).
Row(tgkit.Btn("⏳ Отложить", fmt.Sprintf("appr_no_defer:%d", approvalID))).
Row(tgkit.Btn("Отклонить без комментария", fmt.Sprintf("appr_no_skip:%d", approvalID))).
Build()
}
// BuildRevokeOptions — варианты при отзыве
func BuildRevokeOptions(approvalID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("💬 Написать причину", fmt.Sprintf("appr_revoke_comment:%d", approvalID))).
Row(tgkit.Btn("⏳ Отложить", fmt.Sprintf("appr_revoke_defer:%d", approvalID))).
Row(tgkit.Btn("Отозвать без комментария", fmt.Sprintf("appr_revoke_skip:%d", approvalID))).
Build()
}
// BuildApprovalRejectComment — кнопка "без комментария" при вводе причины
func BuildApprovalRejectComment(approvalID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("Отправить без комментария", fmt.Sprintf("appr_no_skip:%d", approvalID))).
Build()
}
// BuildApprovalStagesButton — кнопка стадий
func BuildApprovalStagesButton(approvalID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("📊 Стадия согласования", fmt.Sprintf("appr_stage:%d", approvalID))).
Build()
}
// BuildFileInputKeyboard — управление файлами в процессе флоу
func BuildFileInputKeyboard(topicCode string, files []state.FileAttachment, required bool) tgbotapi.InlineKeyboardMarkup {
var rows [][]tgbotapi.InlineKeyboardButton
for i, f := range files {
label := []rune(f.FileName)
if len(label) > 25 {
label = append(label[:22], []rune("...")...)
}
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("🗑 "+string(label), fmt.Sprintf("appr_file_detach:%d", i)),
))
}
if len(files) > 0 {
submitLabel := fmt.Sprintf("✅ Отправить (%s)", utilscore.PluralFiles(len(files)))
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(submitLabel, "appr_file_submit:"+topicCode),
))
}
// Кнопка "без файлов" только если прикрепление НЕ обязательно
if !required {
skipLabel := "📤 Отправить без файлов"
if len(files) == 0 {
skipLabel = "✅ Отправить на согласование (без файлов)"
}
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(skipLabel, "appr_file_skip:"+topicCode),
))
}
rows = append(rows, tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("← Назад", "appr_back:"+topicCode),
))
return tgbotapi.NewInlineKeyboardMarkup(rows...)
}
// BuildPendingCommentsList — список отложенных заявок
func BuildPendingCommentsList(items []state.PendingCommentItem, topicNames map[uint64]string) interface{} {
kb := tgkit.NewInlineKeyboard()
for _, item := range items {
icon := "❌"
if item.Action == "revoke" {
icon = "↩️"
}
label := fmt.Sprintf("%s #%d", icon, item.ApprovalID)
if name, ok := topicNames[item.ApprovalID]; ok {
label += " — " + name
}
kb.Row(tgkit.Btn(label, fmt.Sprintf("pending_item:%d", item.ApprovalID)))
}
return kb.Build()
}
// BuildPendingItemOptions — действия по отложенной заявке
func BuildPendingItemOptions(approvalID uint64, action string) interface{} {
commentCB := fmt.Sprintf("appr_no_comment:%d", approvalID)
skipCB := fmt.Sprintf("appr_no_skip:%d", approvalID)
if action == "revoke" {
commentCB = fmt.Sprintf("appr_revoke_comment:%d", approvalID)
skipCB = fmt.Sprintf("appr_revoke_skip:%d", approvalID)
}
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("💬 Написать причину", commentCB)).
Row(tgkit.Btn("Без комментария", skipCB)).
Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "К списку", "pending_back")).
Build()
}
// BuildAttachFileCancel — отмена прикрепления файла
func BuildAttachFileCancel(approvalID uint64) interface{} {
return tgkit.NewInlineKeyboard().
Row(tgkit.BtnStyled(tgkit.ButtonStyleDanger, "Отмена", fmt.Sprintf("appr_attach_cancel:%d", approvalID))).
Build()
}
// BuildMultiSelectStep — мультиселект (например, системы ОКБ/1С/Битрикс)
func BuildMultiSelectStep(topicCode string, nodes []*approvalflow.FlowNode, selectedValues []string, allowedTopicCodes map[string]bool) tgbotapi.InlineKeyboardMarkup {
selectedSet := make(map[string]bool, len(selectedValues))
for _, v := range selectedValues {
selectedSet[v] = true
}
kb := tgkit.NewInlineKeyboard()
for _, n := range nodes {
if n == nil {
continue
}
if n.TopicCode != "" && allowedTopicCodes != nil && !allowedTopicCodes[n.TopicCode] {
continue
}
label := n.Label
if selectedSet[n.MultiSelectValue] {
label = "✅ " + label
}
kb.Row(tgkit.Btn(label, fmt.Sprintf("appr_ms_toggle:%s:%s:%s", topicCode, n.Key, n.MultiSelectValue)))
}
confirmLabel := "✅ Подтвердить"
if len(selectedValues) > 0 {
confirmLabel = fmt.Sprintf("✅ Подтвердить (%d)", len(selectedValues))
}
kb.Row(tgkit.Btn(confirmLabel, fmt.Sprintf("appr_ms_confirm:%s", topicCode)))
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", fmt.Sprintf("appr_back:%s", topicCode)))
return kb.Build()
}
// вспомогательная функция пагинации
func buildPageRow(page, totalPages int, prefix, noop string) []tgbotapi.InlineKeyboardButton {
var row []tgbotapi.InlineKeyboardButton
if page > 0 {
row = append(row, tgkit.Btn("←", fmt.Sprintf("%s:%d", prefix, page-1)).Build())
}
if page < totalPages-1 {
row = append(row, tgkit.Btn(fmt.Sprintf("%d/%d →", page+1, totalPages), fmt.Sprintf("%s:%d", prefix, page+1)).Build())
} else {
row = append(row, tgkit.Btn(fmt.Sprintf("%d/%d", page+1, totalPages), noop).Build())
}
return row
}
func BuildAttachSessionKeyboard(approvalID uint64, fileCount int) tgbotapi.InlineKeyboardMarkup {
kb := tgkit.NewInlineKeyboard()
if fileCount > 0 {
kb.Row(tgkit.Btn(
fmt.Sprintf("✅ Отправить файлы (%d)", fileCount),
fmt.Sprintf("attach_submit:%d", approvalID),
))
kb.Row(tgkit.Btn(
"🗑 Очистить все",
fmt.Sprintf("attach_clear:%d", approvalID),
))
}
kb.Row(tgkit.BtnStyled(
tgkit.ButtonStyleDanger,
"Отмена",
fmt.Sprintf("attach_cancel:%d", approvalID),
))
return kb.Build()
}
// BuildExecutorTaskActions — кнопки исполнителя
func BuildExecutorTaskActions(taskID, approvalID uint64, filesCount int, commentsCount int) interface{} {
kb := tgkit.NewInlineKeyboard()
if filesCount > 0 {
kb.Row(tgkit.Btn(
fmt.Sprintf("📎 Файлы заявки (%d)", filesCount),
fmt.Sprintf("appr_view_files:%d", approvalID),
))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Выполнено", fmt.Sprintf("task_complete:%d", taskID))).
Row(tgkit.Btn("📎 Прикрепить файл к задаче", fmt.Sprintf("executor_attach:%d:%d", taskID, approvalID))).
Row(tgkit.Btn("🔁 Передать задачу", fmt.Sprintf("task_transfer_start:%d", taskID))). // 🆕
Row(
tgkit.Btn("💬 Комментировать", fmt.Sprintf("appr_comment:%d", approvalID)),
buildCommentsButton(approvalID, commentsCount),
)
return kb.Build()
}
func BuildCommentVisibilityMenu() tgbotapi.InlineKeyboardMarkup {
return tgkit.NewInlineKeyboard().
Row(tgkit.Btn("📢 Отправить всем участникам", "comment_vis:all")).
Row(tgkit.Btn("👤 Выбрать конкретных", "comment_vis:specific")).
Row(tgkit.Btn("🔇 Заметка к заявке", "comment_vis:silent")).
Row(tgkit.BtnStyled(tgkit.ButtonStyleDanger, "Отмена", "comment_vis:cancel")).
Build()
}
func BuildCommentTargetsMenu(participants []*typescore.User, selected map[uint64]bool) tgbotapi.InlineKeyboardMarkup {
kb := tgkit.NewInlineKeyboard()
for _, p := range participants {
if p.ID == nil || p.FullName == nil {
continue
}
label := *p.FullName
if selected[*p.ID] {
label = "✅ " + label
} else {
label = "⬜ " + label
}
kb.Row(tgkit.Btn(label, fmt.Sprintf("comment_tgt:%d", *p.ID)))
}
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleSuccess, "Отправить выбранным", "comment_tgt_done"))
kb.Row(tgkit.BtnStyled(tgkit.ButtonStyleBack, "Назад", "comment_tgt_back"))
return kb.Build()
}