chore: import clean Agent Desk snapshot (2/7)

This commit is contained in:
2026-10-16 11:57:21 +00:00
parent 79176541c7
commit 89c5231c4c
461 changed files with 60092 additions and 0 deletions

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentProfileRepository = newAgentProfileRepository()
func newAgentProfileRepository() *agentProfileRepository {
return &agentProfileRepository{}
}
type agentProfileRepository struct {
}
func (r *agentProfileRepository) Get(db *gorm.DB, id int64) *models.AgentProfile {
ret := &models.AgentProfile{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *agentProfileRepository) Take(db *gorm.DB, where ...interface{}) *models.AgentProfile {
ret := &models.AgentProfile{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *agentProfileRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentProfile) {
cnd.Find(db, &list)
return
}
func (r *agentProfileRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.AgentProfile {
ret := &models.AgentProfile{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *agentProfileRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AgentProfile, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *agentProfileRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentProfile, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AgentProfile{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *agentProfileRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.AgentProfile) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *agentProfileRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *agentProfileRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.AgentProfile{})
}
func (r *agentProfileRepository) Create(db *gorm.DB, t *models.AgentProfile) (err error) {
err = db.Create(t).Error
return
}
func (r *agentProfileRepository) Update(db *gorm.DB, t *models.AgentProfile) (err error) {
err = db.Save(t).Error
return
}
func (r *agentProfileRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.AgentProfile{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *agentProfileRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.AgentProfile{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *agentProfileRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.AgentProfile{}, "id = ?", id)
}

View File

@@ -0,0 +1,45 @@
package repositories
import (
"agent-desk/internal/models"
"gorm.io/gorm"
)
var AgentRevisionRepository = newAgentRevisionRepository()
func newAgentRevisionRepository() *agentRevisionRepository {
return &agentRevisionRepository{}
}
type agentRevisionRepository struct{}
func (r *agentRevisionRepository) Get(db *gorm.DB, id int64) *models.AgentRevision {
ret := &models.AgentRevision{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *agentRevisionRepository) Create(db *gorm.DB, item *models.AgentRevision) error {
return db.Create(item).Error
}
func (r *agentRevisionRepository) FindByAgentID(db *gorm.DB, agentID int64) []models.AgentRevision {
if agentID <= 0 {
return []models.AgentRevision{}
}
items := make([]models.AgentRevision, 0)
db.Where("agent_id = ?", agentID).Order("revision DESC, id DESC").Find(&items)
return items
}
func (r *agentRevisionRepository) MaxRevisionByAgentID(db *gorm.DB, agentID int64) int {
if agentID <= 0 {
return 0
}
var ret int
db.Model(&models.AgentRevision{}).Where("agent_id = ?", agentID).Select("COALESCE(MAX(revision), 0)").Scan(&ret)
return ret
}

View File

@@ -0,0 +1,45 @@
package repositories
import (
"agent-desk/internal/models"
"gorm.io/gorm"
)
var AgentRunQualityFeedbackRepository = newAgentRunQualityFeedbackRepository()
func newAgentRunQualityFeedbackRepository() *agentRunQualityFeedbackRepository {
return &agentRunQualityFeedbackRepository{}
}
type agentRunQualityFeedbackRepository struct{}
func (r *agentRunQualityFeedbackRepository) GetByAgentRunID(db *gorm.DB, agentRunID int64) *models.AgentRunQualityFeedback {
if agentRunID <= 0 {
return nil
}
item := &models.AgentRunQualityFeedback{}
if err := db.Where("agent_run_id = ?", agentRunID).First(item).Error; err != nil {
return nil
}
return item
}
func (r *agentRunQualityFeedbackRepository) FindByAgentRunIDs(db *gorm.DB, agentRunIDs []int64) []models.AgentRunQualityFeedback {
if len(agentRunIDs) == 0 {
return []models.AgentRunQualityFeedback{}
}
var items []models.AgentRunQualityFeedback
if err := db.Where("agent_run_id IN ?", agentRunIDs).Find(&items).Error; err != nil {
return []models.AgentRunQualityFeedback{}
}
return items
}
func (r *agentRunQualityFeedbackRepository) Create(db *gorm.DB, item *models.AgentRunQualityFeedback) error {
return db.Create(item).Error
}
func (r *agentRunQualityFeedbackRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.AgentRunQualityFeedback{}).Where("id = ?", id).Updates(columns).Error
}

View File

@@ -0,0 +1,68 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentRunRepository = newAgentRunRepository()
func newAgentRunRepository() *agentRunRepository {
return &agentRunRepository{}
}
type agentRunRepository struct{}
func (r *agentRunRepository) Get(db *gorm.DB, id int64) *models.AgentRun {
ret := &models.AgentRun{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *agentRunRepository) TakeByWorkflowRunID(db *gorm.DB, workflowRunID int64) *models.AgentRun {
if workflowRunID <= 0 {
return nil
}
ret := &models.AgentRun{}
if err := db.Where("workflow_run_id = ?", workflowRunID).Order("id DESC").First(ret).Error; err != nil {
return nil
}
return ret
}
func (r *agentRunRepository) Create(db *gorm.DB, item *models.AgentRun) error {
return db.Create(item).Error
}
func (r *agentRunRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentRun, paging *sqls.Paging) {
cnd.Find(db, &list)
return list, &sqls.Paging{Page: cnd.Paging.Page, Limit: cnd.Paging.Limit, Total: cnd.Count(db, &models.AgentRun{})}
}
func (r *agentRunRepository) FindPageByParams(db *gorm.DB, queryParams *params.QueryParams) (list []models.AgentRun, paging *sqls.Paging) {
return r.FindPageByCnd(db, &queryParams.Cnd)
}
func (r *agentRunRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.AgentRun{}).Where("id = ?", id).Updates(columns).Error
}
func (r *agentRunRepository) FindRecent(db *gorm.DB, aiAgentID int64, limit int) []models.AgentRun {
if limit <= 0 || limit > 5000 {
limit = 5000
}
query := db.Order("id DESC").Limit(limit)
if aiAgentID > 0 {
query = query.Where("ai_agent_id = ?", aiAgentID)
}
var items []models.AgentRun
if err := query.Find(&items).Error; err != nil {
return nil
}
return items
}

View File

@@ -0,0 +1,54 @@
package repositories
import (
"agent-desk/internal/models"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentStepRepository = newAgentStepRepository()
func newAgentStepRepository() *agentStepRepository {
return &agentStepRepository{}
}
type agentStepRepository struct{}
func (r *agentStepRepository) Create(db *gorm.DB, item *models.AgentStep) error {
return db.Create(item).Error
}
func (r *agentStepRepository) FindByAgentRunID(db *gorm.DB, agentRunID int64) []models.AgentStep {
if agentRunID <= 0 {
return []models.AgentStep{}
}
return r.Find(db, sqls.NewCnd().Eq("agent_run_id", agentRunID).Asc("id"))
}
func (r *agentStepRepository) LastByAgentRunID(db *gorm.DB, agentRunID int64) *models.AgentStep {
if agentRunID <= 0 {
return nil
}
ret := &models.AgentStep{}
if err := db.Where("agent_run_id = ?", agentRunID).Order("id DESC").First(ret).Error; err != nil {
return nil
}
return ret
}
func (r *agentStepRepository) FindByAgentRunIDs(db *gorm.DB, agentRunIDs []int64) []models.AgentStep {
if len(agentRunIDs) == 0 {
return nil
}
var items []models.AgentStep
if err := db.Where("agent_run_id IN ?", agentRunIDs).Find(&items).Error; err != nil {
return nil
}
return items
}
func (r *agentStepRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentStep) {
cnd.Find(db, &list)
return
}

View File

@@ -0,0 +1,111 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentTeamRepository = newAgentTeamRepository()
func newAgentTeamRepository() *agentTeamRepository {
return &agentTeamRepository{}
}
type agentTeamRepository struct {
}
func (r *agentTeamRepository) Get(db *gorm.DB, id int64) *models.AgentTeam {
ret := &models.AgentTeam{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *agentTeamRepository) Take(db *gorm.DB, where ...interface{}) *models.AgentTeam {
ret := &models.AgentTeam{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *agentTeamRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentTeam) {
cnd.Find(db, &list)
return
}
func (r *agentTeamRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.AgentTeam {
ret := &models.AgentTeam{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *agentTeamRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AgentTeam, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *agentTeamRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentTeam, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AgentTeam{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *agentTeamRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.AgentTeam) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *agentTeamRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *agentTeamRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.AgentTeam{})
}
func (r *agentTeamRepository) Create(db *gorm.DB, t *models.AgentTeam) (err error) {
err = db.Create(t).Error
return
}
func (r *agentTeamRepository) Update(db *gorm.DB, t *models.AgentTeam) (err error) {
err = db.Save(t).Error
return
}
func (r *agentTeamRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.AgentTeam{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *agentTeamRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.AgentTeam{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *agentTeamRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.AgentTeam{}, "id = ?", id)
}
func (r *agentTeamRepository) FindByIds(db *gorm.DB, ids []int64) []models.AgentTeam {
if len(ids) == 0 {
return []models.AgentTeam{}
}
var list []models.AgentTeam
db.Where("id IN ?", ids).Find(&list)
return list
}

View File

@@ -0,0 +1,134 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"time"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentTeamScheduleRepository = newAgentTeamScheduleRepository()
func newAgentTeamScheduleRepository() *agentTeamScheduleRepository {
return &agentTeamScheduleRepository{}
}
type agentTeamScheduleRepository struct {
}
func (r *agentTeamScheduleRepository) Get(db *gorm.DB, id int64) *models.AgentTeamSchedule {
ret := &models.AgentTeamSchedule{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *agentTeamScheduleRepository) Take(db *gorm.DB, where ...interface{}) *models.AgentTeamSchedule {
ret := &models.AgentTeamSchedule{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *agentTeamScheduleRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentTeamSchedule) {
cnd.Find(db, &list)
return
}
func (r *agentTeamScheduleRepository) FindByTimeRange(db *gorm.DB, startAt, endAt time.Time, teamID int64) (list []models.AgentTeamSchedule) {
query := db.Model(&models.AgentTeamSchedule{}).
Where("start_at < ? AND end_at > ?", endAt, startAt)
if teamID > 0 {
query = query.Where("team_id = ?", teamID)
}
query.Order("team_id ASC").Order("start_at ASC").Order("id ASC").Find(&list)
return
}
func (r *agentTeamScheduleRepository) FindOverlappingByTeamIDsAndTimeRange(db *gorm.DB, teamIDs []int64, startAt, endAt time.Time) (list []models.AgentTeamSchedule) {
if len(teamIDs) == 0 {
return
}
db.Model(&models.AgentTeamSchedule{}).
Where("team_id IN ? AND status = ? AND start_at < ? AND end_at > ?", teamIDs, enums.StatusOk, endAt, startAt).
Order("team_id ASC").
Order("start_at ASC").
Order("id ASC").
Find(&list)
return
}
func (r *agentTeamScheduleRepository) CreateBatch(db *gorm.DB, list []models.AgentTeamSchedule) error {
if len(list) == 0 {
return nil
}
return db.Create(&list).Error
}
func (r *agentTeamScheduleRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.AgentTeamSchedule {
ret := &models.AgentTeamSchedule{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *agentTeamScheduleRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AgentTeamSchedule, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *agentTeamScheduleRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentTeamSchedule, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AgentTeamSchedule{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *agentTeamScheduleRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.AgentTeamSchedule) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *agentTeamScheduleRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *agentTeamScheduleRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.AgentTeamSchedule{})
}
func (r *agentTeamScheduleRepository) Create(db *gorm.DB, t *models.AgentTeamSchedule) (err error) {
err = db.Create(t).Error
return
}
func (r *agentTeamScheduleRepository) Update(db *gorm.DB, t *models.AgentTeamSchedule) (err error) {
err = db.Save(t).Error
return
}
func (r *agentTeamScheduleRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.AgentTeamSchedule{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *agentTeamScheduleRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.AgentTeamSchedule{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *agentTeamScheduleRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.AgentTeamSchedule{}, "id = ?", id)
}

View File

@@ -0,0 +1,43 @@
package repositories
import (
"agent-desk/internal/models"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentToolCallRepository = newAgentToolCallRepository()
func newAgentToolCallRepository() *agentToolCallRepository {
return &agentToolCallRepository{}
}
type agentToolCallRepository struct{}
func (r *agentToolCallRepository) Create(db *gorm.DB, item *models.AgentToolCall) error {
return db.Create(item).Error
}
func (r *agentToolCallRepository) FindByAgentRunID(db *gorm.DB, agentRunID int64) []models.AgentToolCall {
if agentRunID <= 0 {
return []models.AgentToolCall{}
}
return r.Find(db, sqls.NewCnd().Eq("agent_run_id", agentRunID).Asc("id"))
}
func (r *agentToolCallRepository) FindByAgentRunIDs(db *gorm.DB, agentRunIDs []int64) []models.AgentToolCall {
if len(agentRunIDs) == 0 {
return nil
}
var items []models.AgentToolCall
if err := db.Where("agent_run_id IN ?", agentRunIDs).Find(&items).Error; err != nil {
return nil
}
return items
}
func (r *agentToolCallRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentToolCall) {
cnd.Find(db, &list)
return
}

View File

@@ -0,0 +1,34 @@
package repositories
import (
"agent-desk/internal/models"
"gorm.io/gorm"
)
var AgentToolInvocationRepository = newAgentToolInvocationRepository()
func newAgentToolInvocationRepository() *agentToolInvocationRepository {
return &agentToolInvocationRepository{}
}
type agentToolInvocationRepository struct{}
func (r *agentToolInvocationRepository) GetByIdempotencyKey(db *gorm.DB, conversationID int64, toolCode, idempotencyKey string) *models.AgentToolInvocation {
if conversationID <= 0 || toolCode == "" || idempotencyKey == "" {
return nil
}
var item models.AgentToolInvocation
if err := db.Where("conversation_id = ? AND tool_code = ? AND idempotency_key = ?", conversationID, toolCode, idempotencyKey).First(&item).Error; err != nil {
return nil
}
return &item
}
func (r *agentToolInvocationRepository) Create(db *gorm.DB, item *models.AgentToolInvocation) error {
return db.Create(item).Error
}
func (r *agentToolInvocationRepository) Updates(db *gorm.DB, id int64, values map[string]any) error {
return db.Model(&models.AgentToolInvocation{}).Where("id = ?", id).Updates(values).Error
}

View File

@@ -0,0 +1,110 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIAgentRepository = newAIAgentRepository()
func newAIAgentRepository() *aIAgentRepository {
return &aIAgentRepository{}
}
type aIAgentRepository struct {
}
func (r *aIAgentRepository) Get(db *gorm.DB, id int64) *models.AIAgent {
ret := &models.AIAgent{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *aIAgentRepository) Take(db *gorm.DB, where ...interface{}) *models.AIAgent {
ret := &models.AIAgent{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *aIAgentRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIAgent) {
cnd.Find(db, &list)
return
}
func (r *aIAgentRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.AIAgent {
ret := &models.AIAgent{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *aIAgentRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AIAgent, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *aIAgentRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIAgent, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AIAgent{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *aIAgentRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.AIAgent) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *aIAgentRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *aIAgentRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.AIAgent{})
}
func (r *aIAgentRepository) Create(db *gorm.DB, t *models.AIAgent) (err error) {
err = db.Create(t).Error
return
}
func (r *aIAgentRepository) Update(db *gorm.DB, t *models.AIAgent) (err error) {
err = db.Save(t).Error
return
}
func (r *aIAgentRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.AIAgent{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *aIAgentRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.AIAgent{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *aIAgentRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.AIAgent{}, "id = ?", id)
}
func (r *aIAgentRepository) FindByIds(db *gorm.DB, ids []int64) []models.AIAgent {
if len(ids) == 0 {
return []models.AIAgent{}
}
var list []models.AIAgent
db.Where("id IN ?", ids).Find(&list)
return list
}

View File

@@ -0,0 +1,57 @@
package repositories
import (
"agent-desk/internal/models"
"gorm.io/gorm"
)
var AIAgentWorkflowBindingRepository = newAIAgentWorkflowBindingRepository()
func newAIAgentWorkflowBindingRepository() *aiAgentWorkflowBindingRepository {
return &aiAgentWorkflowBindingRepository{}
}
type aiAgentWorkflowBindingRepository struct{}
func (r *aiAgentWorkflowBindingRepository) FindByAgentID(db *gorm.DB, agentID int64) []models.AIAgentWorkflowBinding {
ret := make([]models.AIAgentWorkflowBinding, 0)
if agentID > 0 {
db.Where("ai_agent_id = ?", agentID).Order("priority ASC, id ASC").Find(&ret)
}
return ret
}
func (r *aiAgentWorkflowBindingRepository) FindEnabledByAgentID(db *gorm.DB, agentID int64) []models.AIAgentWorkflowBinding {
ret := make([]models.AIAgentWorkflowBinding, 0)
if agentID > 0 {
db.Where("ai_agent_id = ? AND enabled = ?", agentID, true).Order("priority ASC, id ASC").Find(&ret)
}
return ret
}
func (r *aiAgentWorkflowBindingRepository) FindByWorkflowID(db *gorm.DB, workflowID int64) []models.AIAgentWorkflowBinding {
ret := make([]models.AIAgentWorkflowBinding, 0)
if workflowID > 0 {
db.Where("workflow_id = ?", workflowID).Order("priority ASC, id ASC").Find(&ret)
}
return ret
}
func (r *aiAgentWorkflowBindingRepository) CountByWorkflowID(db *gorm.DB, workflowID int64) int64 {
var count int64
if workflowID > 0 {
db.Model(&models.AIAgentWorkflowBinding{}).Where("workflow_id = ?", workflowID).Count(&count)
}
return count
}
func (r *aiAgentWorkflowBindingRepository) ReplaceByAgentID(db *gorm.DB, agentID int64, items []models.AIAgentWorkflowBinding) error {
if err := db.Where("ai_agent_id = ?", agentID).Delete(&models.AIAgentWorkflowBinding{}).Error; err != nil {
return err
}
if len(items) == 0 {
return nil
}
return db.Create(&items).Error
}

View File

@@ -0,0 +1,111 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIConfigRepository = newAIConfigRepository()
func newAIConfigRepository() *aIConfigRepository {
return &aIConfigRepository{}
}
type aIConfigRepository struct {
}
func (r *aIConfigRepository) Get(db *gorm.DB, id int64) *models.AIConfig {
ret := &models.AIConfig{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *aIConfigRepository) Take(db *gorm.DB, where ...interface{}) *models.AIConfig {
ret := &models.AIConfig{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *aIConfigRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIConfig) {
cnd.Find(db, &list)
return
}
func (r *aIConfigRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.AIConfig {
ret := &models.AIConfig{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *aIConfigRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AIConfig, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *aIConfigRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIConfig, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AIConfig{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *aIConfigRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.AIConfig) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *aIConfigRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *aIConfigRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.AIConfig{})
}
func (r *aIConfigRepository) Create(db *gorm.DB, t *models.AIConfig) (err error) {
err = db.Create(t).Error
return
}
func (r *aIConfigRepository) Update(db *gorm.DB, t *models.AIConfig) (err error) {
err = db.Save(t).Error
return
}
func (r *aIConfigRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.AIConfig{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *aIConfigRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.AIConfig{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *aIConfigRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.AIConfig{}, "id = ?", id)
}
func (r *aIConfigRepository) GetEnabled(db *gorm.DB, modelType enums.AIModelType) *models.AIConfig {
return r.FindOne(db, sqls.NewCnd().
Eq("model_type", modelType).
Eq("status", enums.StatusOk).
Desc("sort_no").
Desc("id"))
}

View File

@@ -0,0 +1,37 @@
package repositories
import (
"agent-desk/internal/models"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIWorkflowNodeRunRepository = newAIWorkflowNodeRunRepository()
func newAIWorkflowNodeRunRepository() *aiWorkflowNodeRunRepository {
return &aiWorkflowNodeRunRepository{}
}
type aiWorkflowNodeRunRepository struct{}
func (r *aiWorkflowNodeRunRepository) Get(db *gorm.DB, id int64) *models.AIWorkflowNodeRun {
ret := &models.AIWorkflowNodeRun{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *aiWorkflowNodeRunRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflowNodeRun) {
cnd.Find(db, &list)
return
}
func (r *aiWorkflowNodeRunRepository) Create(db *gorm.DB, t *models.AIWorkflowNodeRun) error {
return db.Create(t).Error
}
func (r *aiWorkflowNodeRunRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) error {
return db.Model(&models.AIWorkflowNodeRun{}).Where("id = ?", id).Updates(columns).Error
}

View File

@@ -0,0 +1,61 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIWorkflowRepository = newAIWorkflowRepository()
func newAIWorkflowRepository() *aiWorkflowRepository {
return &aiWorkflowRepository{}
}
type aiWorkflowRepository struct{}
func (r *aiWorkflowRepository) Get(db *gorm.DB, id int64) *models.AIWorkflow {
ret := &models.AIWorkflow{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *aiWorkflowRepository) Take(db *gorm.DB, where ...interface{}) *models.AIWorkflow {
ret := &models.AIWorkflow{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *aiWorkflowRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflow) {
cnd.Find(db, &list)
return
}
func (r *aiWorkflowRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AIWorkflow, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *aiWorkflowRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflow, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AIWorkflow{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *aiWorkflowRepository) Create(db *gorm.DB, t *models.AIWorkflow) error {
return db.Create(t).Error
}
func (r *aiWorkflowRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) error {
return db.Model(&models.AIWorkflow{}).Where("id = ?", id).Updates(columns).Error
}

View File

@@ -0,0 +1,53 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIWorkflowRunRepository = newAIWorkflowRunRepository()
func newAIWorkflowRunRepository() *aiWorkflowRunRepository {
return &aiWorkflowRunRepository{}
}
type aiWorkflowRunRepository struct{}
func (r *aiWorkflowRunRepository) Get(db *gorm.DB, id int64) *models.AIWorkflowRun {
ret := &models.AIWorkflowRun{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *aiWorkflowRunRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflowRun) {
cnd.Find(db, &list)
return
}
func (r *aiWorkflowRunRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AIWorkflowRun, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *aiWorkflowRunRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflowRun, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AIWorkflowRun{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *aiWorkflowRunRepository) Create(db *gorm.DB, t *models.AIWorkflowRun) error {
return db.Create(t).Error
}
func (r *aiWorkflowRunRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) error {
return db.Model(&models.AIWorkflowRun{}).Where("id = ?", id).Updates(columns).Error
}

View File

@@ -0,0 +1,58 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIWorkflowVersionRepository = newAIWorkflowVersionRepository()
func newAIWorkflowVersionRepository() *aiWorkflowVersionRepository {
return &aiWorkflowVersionRepository{}
}
type aiWorkflowVersionRepository struct{}
func (r *aiWorkflowVersionRepository) Get(db *gorm.DB, id int64) *models.AIWorkflowVersion {
ret := &models.AIWorkflowVersion{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *aiWorkflowVersionRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflowVersion) {
cnd.Find(db, &list)
return
}
func (r *aiWorkflowVersionRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.AIWorkflowVersion, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *aiWorkflowVersionRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AIWorkflowVersion, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.AIWorkflowVersion{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *aiWorkflowVersionRepository) Create(db *gorm.DB, t *models.AIWorkflowVersion) error {
return db.Create(t).Error
}
func (r *aiWorkflowVersionRepository) MaxVersionByWorkflowID(db *gorm.DB, workflowID int64) int {
var maxVersion int
db.Model(&models.AIWorkflowVersion{}).
Where("workflow_id = ?", workflowID).
Select("COALESCE(MAX(version), 0)").
Scan(&maxVersion)
return maxVersion
}

View File

@@ -0,0 +1,118 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AssetRepository = newAssetRepository()
func newAssetRepository() *assetRepository {
return &assetRepository{}
}
type assetRepository struct {
}
func (r *assetRepository) Get(db *gorm.DB, id int64) *models.Asset {
ret := &models.Asset{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *assetRepository) Take(db *gorm.DB, where ...interface{}) *models.Asset {
ret := &models.Asset{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *assetRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Asset) {
cnd.Find(db, &list)
return
}
func (r *assetRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Asset {
ret := &models.Asset{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *assetRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Asset, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *assetRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Asset, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Asset{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *assetRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Asset) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *assetRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *assetRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Asset{})
}
func (r *assetRepository) Create(db *gorm.DB, t *models.Asset) (err error) {
err = db.Create(t).Error
return
}
func (r *assetRepository) Update(db *gorm.DB, t *models.Asset) (err error) {
err = db.Save(t).Error
return
}
func (r *assetRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Asset{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *assetRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Asset{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *assetRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Asset{}, "id = ?", id)
}
func (r *assetRepository) GetByAssetID(db *gorm.DB, assetID string) *models.Asset {
ret := &models.Asset{}
if err := db.First(ret, "asset_id = ?", assetID).Error; err != nil {
return nil
}
return ret
}
func (r *assetRepository) GetByStorageKey(db *gorm.DB, storageKey string) *models.Asset {
ret := &models.Asset{}
if err := db.First(ret, "storage_key = ?", storageKey).Error; err != nil {
return nil
}
return ret
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ChannelMessageOutboxRepository = newChannelMessageOutboxRepository()
func newChannelMessageOutboxRepository() *channelMessageOutboxRepository {
return &channelMessageOutboxRepository{}
}
type channelMessageOutboxRepository struct {
}
func (r *channelMessageOutboxRepository) Get(db *gorm.DB, id int64) *models.ChannelMessageOutbox {
ret := &models.ChannelMessageOutbox{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *channelMessageOutboxRepository) Take(db *gorm.DB, where ...interface{}) *models.ChannelMessageOutbox {
ret := &models.ChannelMessageOutbox{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *channelMessageOutboxRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ChannelMessageOutbox) {
cnd.Find(db, &list)
return
}
func (r *channelMessageOutboxRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ChannelMessageOutbox {
ret := &models.ChannelMessageOutbox{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *channelMessageOutboxRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.ChannelMessageOutbox, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *channelMessageOutboxRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.ChannelMessageOutbox, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.ChannelMessageOutbox{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *channelMessageOutboxRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.ChannelMessageOutbox) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *channelMessageOutboxRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *channelMessageOutboxRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.ChannelMessageOutbox{})
}
func (r *channelMessageOutboxRepository) Create(db *gorm.DB, t *models.ChannelMessageOutbox) (err error) {
err = db.Create(t).Error
return
}
func (r *channelMessageOutboxRepository) Update(db *gorm.DB, t *models.ChannelMessageOutbox) (err error) {
err = db.Save(t).Error
return
}
func (r *channelMessageOutboxRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.ChannelMessageOutbox{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *channelMessageOutboxRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.ChannelMessageOutbox{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *channelMessageOutboxRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.ChannelMessageOutbox{}, "id = ?", id)
}

View File

@@ -0,0 +1,145 @@
package repositories
import (
"testing"
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
)
// Updates passes the map straight to GORM, so every key becomes a column name
// in the generated SQL. A key with no matching column makes the whole UPDATE
// fail, which is how the email channel silently lost every status transition:
// it wrote "send_detail" while the model only had LastError.
func TestChannelMessageOutboxUpdatesAcceptsServiceColumnNames(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:channel_message_outbox_repository_test?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite error = %v", err)
}
if err := db.AutoMigrate(&models.ChannelMessageOutbox{}); err != nil {
t.Fatalf("auto migrate error = %v", err)
}
if err := db.Exec("DELETE FROM channel_message_outboxes").Error; err != nil {
t.Fatalf("clean channel message outboxes error = %v", err)
}
item := &models.ChannelMessageOutbox{
ChannelType: enums.ChannelTypeEmail,
ConversationID: 1,
MessageID: 1,
SendStatus: string(enums.ChannelMessageOutboxStatusPending),
}
if err := db.Create(item).Error; err != nil {
t.Fatalf("create outbox error = %v", err)
}
// The exact column sets the outbound services write.
tests := []struct {
name string
columns map[string]interface{}
}{
{
name: "email marks sent",
columns: map[string]interface{}{
"send_status": string(enums.ChannelMessageOutboxStatusSent),
"send_detail": "sent to user@example.com via smtp",
"sent_at": nil,
"updated_at": nil,
"next_retry_at": nil,
},
},
{
name: "email marks failed",
columns: map[string]interface{}{
"send_status": string(enums.ChannelMessageOutboxStatusFailed),
"send_detail": "smtp: connection refused",
"updated_at": nil,
"next_retry_at": nil,
},
},
{
name: "telegram records last error",
columns: map[string]interface{}{
"send_status": string(enums.ChannelMessageOutboxStatusFailed),
"last_error": "telegram: 429 too many requests",
"updated_at": nil,
},
},
{
name: "retry bookkeeping",
columns: map[string]interface{}{
"retry_count": 2,
"next_retry_at": nil,
"updated_at": nil,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := ChannelMessageOutboxRepository.Updates(db, item.ID, tt.columns); err != nil {
t.Fatalf("Updates() error = %v", err)
}
})
}
var stored models.ChannelMessageOutbox
if err := db.Where("id = ?", item.ID).First(&stored).Error; err != nil {
t.Fatalf("reload outbox error = %v", err)
}
if stored.SendDetail != "smtp: connection refused" {
t.Fatalf("SendDetail = %q, want %q", stored.SendDetail, "smtp: connection refused")
}
if stored.LastError != "telegram: 429 too many requests" {
t.Fatalf("LastError = %q, want %q", stored.LastError, "telegram: 429 too many requests")
}
if stored.RetryCount != 2 {
t.Fatalf("RetryCount = %d, want 2", stored.RetryCount)
}
}
// A retried row must come back as eligible for the dispatcher again.
func TestChannelMessageOutboxUpdatesClearsNextRetryAt(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:channel_message_outbox_retry_test?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite error = %v", err)
}
if err := db.AutoMigrate(&models.ChannelMessageOutbox{}); err != nil {
t.Fatalf("auto migrate error = %v", err)
}
if err := db.Exec("DELETE FROM channel_message_outboxes").Error; err != nil {
t.Fatalf("clean channel message outboxes error = %v", err)
}
item := &models.ChannelMessageOutbox{
ChannelType: enums.ChannelTypeEmail,
MessageID: 2,
SendStatus: string(enums.ChannelMessageOutboxStatusPending),
}
if err := db.Create(item).Error; err != nil {
t.Fatalf("create outbox error = %v", err)
}
if err := ChannelMessageOutboxRepository.Updates(db, item.ID, map[string]interface{}{
"send_status": string(enums.ChannelMessageOutboxStatusFailed),
"send_detail": "temporary failure",
"retry_count": 1,
"updated_at": nil,
"next_retry_at": nil,
}); err != nil {
t.Fatalf("Updates() error = %v", err)
}
var stored models.ChannelMessageOutbox
if err := db.Where("id = ?", item.ID).First(&stored).Error; err != nil {
t.Fatalf("reload outbox error = %v", err)
}
if stored.SendStatus != string(enums.ChannelMessageOutboxStatusFailed) {
t.Fatalf("SendStatus = %q, want %q", stored.SendStatus, enums.ChannelMessageOutboxStatusFailed)
}
if stored.NextRetryAt != nil {
t.Fatalf("NextRetryAt = %v, want nil so the row is eligible again", stored.NextRetryAt)
}
}

View File

@@ -0,0 +1,110 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/common/strs"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ChannelRepository = newChannelRepository()
func newChannelRepository() *channelRepository {
return &channelRepository{}
}
type channelRepository struct {
}
func (r *channelRepository) Get(db *gorm.DB, id int64) *models.Channel {
ret := &models.Channel{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *channelRepository) Take(db *gorm.DB, where ...interface{}) *models.Channel {
ret := &models.Channel{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *channelRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Channel) {
cnd.Find(db, &list)
return
}
func (r *channelRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Channel {
ret := &models.Channel{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *channelRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Channel, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *channelRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Channel, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Channel{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *channelRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Channel) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *channelRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *channelRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Channel{})
}
func (r *channelRepository) Create(db *gorm.DB, t *models.Channel) (err error) {
err = db.Create(t).Error
return
}
func (r *channelRepository) Update(db *gorm.DB, t *models.Channel) (err error) {
err = db.Save(t).Error
return
}
func (r *channelRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Channel{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *channelRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Channel{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *channelRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Channel{}, "id = ?", id)
}
func (r *channelRepository) GetByChannelID(db *gorm.DB, channelID string) *models.Channel {
if strs.IsBlank(channelID) {
return nil
}
return r.FindOne(db, sqls.NewCnd().Where("channel_id = ?", channelID))
}

View File

@@ -0,0 +1,110 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var CompanyRepository = newCompanyRepository()
func newCompanyRepository() *companyRepository {
return &companyRepository{}
}
type companyRepository struct {
}
func (r *companyRepository) GetByName(db *gorm.DB, name string) *models.Company {
ret := &models.Company{}
if err := db.First(ret, "name = ?", name).Error; err != nil {
return nil
}
return ret
}
func (r *companyRepository) Get(db *gorm.DB, id int64) *models.Company {
ret := &models.Company{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *companyRepository) Take(db *gorm.DB, where ...interface{}) *models.Company {
ret := &models.Company{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *companyRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Company) {
cnd.Find(db, &list)
return
}
func (r *companyRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Company {
ret := &models.Company{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *companyRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Company, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *companyRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Company, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Company{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *companyRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Company) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *companyRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *companyRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Company{})
}
func (r *companyRepository) Create(db *gorm.DB, t *models.Company) (err error) {
err = db.Create(t).Error
return
}
func (r *companyRepository) Update(db *gorm.DB, t *models.Company) (err error) {
err = db.Save(t).Error
return
}
func (r *companyRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Company{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *companyRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Company{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *companyRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Company{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationAssignmentRepository = newConversationAssignmentRepository()
func newConversationAssignmentRepository() *conversationAssignmentRepository {
return &conversationAssignmentRepository{}
}
type conversationAssignmentRepository struct {
}
func (r *conversationAssignmentRepository) Get(db *gorm.DB, id int64) *models.ConversationAssignment {
ret := &models.ConversationAssignment{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationAssignmentRepository) Take(db *gorm.DB, where ...interface{}) *models.ConversationAssignment {
ret := &models.ConversationAssignment{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *conversationAssignmentRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationAssignment) {
cnd.Find(db, &list)
return
}
func (r *conversationAssignmentRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ConversationAssignment {
ret := &models.ConversationAssignment{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationAssignmentRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.ConversationAssignment, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *conversationAssignmentRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationAssignment, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.ConversationAssignment{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *conversationAssignmentRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.ConversationAssignment) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *conversationAssignmentRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *conversationAssignmentRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.ConversationAssignment{})
}
func (r *conversationAssignmentRepository) Create(db *gorm.DB, t *models.ConversationAssignment) (err error) {
err = db.Create(t).Error
return
}
func (r *conversationAssignmentRepository) Update(db *gorm.DB, t *models.ConversationAssignment) (err error) {
err = db.Save(t).Error
return
}
func (r *conversationAssignmentRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.ConversationAssignment{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *conversationAssignmentRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.ConversationAssignment{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *conversationAssignmentRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.ConversationAssignment{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationEventLogRepository = newConversationEventLogRepository()
func newConversationEventLogRepository() *conversationEventLogRepository {
return &conversationEventLogRepository{}
}
type conversationEventLogRepository struct {
}
func (r *conversationEventLogRepository) Get(db *gorm.DB, id int64) *models.ConversationEventLog {
ret := &models.ConversationEventLog{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationEventLogRepository) Take(db *gorm.DB, where ...interface{}) *models.ConversationEventLog {
ret := &models.ConversationEventLog{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *conversationEventLogRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationEventLog) {
cnd.Find(db, &list)
return
}
func (r *conversationEventLogRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ConversationEventLog {
ret := &models.ConversationEventLog{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationEventLogRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.ConversationEventLog, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *conversationEventLogRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationEventLog, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.ConversationEventLog{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *conversationEventLogRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.ConversationEventLog) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *conversationEventLogRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *conversationEventLogRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.ConversationEventLog{})
}
func (r *conversationEventLogRepository) Create(db *gorm.DB, t *models.ConversationEventLog) (err error) {
err = db.Create(t).Error
return
}
func (r *conversationEventLogRepository) Update(db *gorm.DB, t *models.ConversationEventLog) (err error) {
err = db.Save(t).Error
return
}
func (r *conversationEventLogRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.ConversationEventLog{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *conversationEventLogRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.ConversationEventLog{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *conversationEventLogRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.ConversationEventLog{}, "id = ?", id)
}

View File

@@ -0,0 +1,103 @@
package repositories
import (
"agent-desk/internal/models"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationInterruptRepository = newConversationInterruptRepository()
func newConversationInterruptRepository() *conversationInterruptRepository {
return &conversationInterruptRepository{}
}
type conversationInterruptRepository struct{}
func (r *conversationInterruptRepository) Get(db *gorm.DB, id int64) *models.ConversationInterrupt {
ret := &models.ConversationInterrupt{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationInterruptRepository) GetByCheckPointID(db *gorm.DB, checkPointID string) *models.ConversationInterrupt {
ret := &models.ConversationInterrupt{}
if err := db.Where("check_point_id = ?", checkPointID).First(ret).Error; err != nil {
return nil
}
return ret
}
func (r *conversationInterruptRepository) FindLatestPendingByConversationID(db *gorm.DB, conversationID int64) *models.ConversationInterrupt {
ret := &models.ConversationInterrupt{}
if err := db.Where("conversation_id = ? AND status = ?", conversationID, "pending").Order("id DESC").First(ret).Error; err != nil {
return nil
}
return ret
}
func (r *conversationInterruptRepository) FindByAgentRunIDs(db *gorm.DB, agentRunIDs []int64) []models.ConversationInterrupt {
if len(agentRunIDs) == 0 {
return []models.ConversationInterrupt{}
}
var items []models.ConversationInterrupt
if err := db.Where("agent_run_id IN ?", agentRunIDs).Find(&items).Error; err != nil {
return []models.ConversationInterrupt{}
}
return items
}
func (r *conversationInterruptRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationInterrupt) {
cnd.Find(db, &list)
return
}
func (r *conversationInterruptRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ConversationInterrupt {
ret := &models.ConversationInterrupt{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationInterruptRepository) Create(db *gorm.DB, t *models.ConversationInterrupt) error {
return db.Create(t).Error
}
func (r *conversationInterruptRepository) Update(db *gorm.DB, t *models.ConversationInterrupt) error {
return db.Save(t).Error
}
func (r *conversationInterruptRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.ConversationInterrupt{}).Where("id = ?", id).Updates(columns).Error
}
func (r *conversationInterruptRepository) UpsertByCheckPointID(db *gorm.DB, item *models.ConversationInterrupt) error {
current := r.GetByCheckPointID(db, item.CheckPointID)
if current == nil {
return r.Create(db, item)
}
columns := map[string]any{
"conversation_id": item.ConversationID,
"ai_agent_id": item.AIAgentID,
"agent_run_id": item.AgentRunID,
"agent_step_id": item.AgentStepID,
"source_message_id": item.SourceMessageID,
"last_resume_message_id": item.LastResumeMessageID,
"workflow_run_id": item.WorkflowRunID,
"workflow_node_id": item.WorkflowNodeID,
"interrupt_id": item.InterruptID,
"interrupt_type": item.InterruptType,
"status": item.Status,
"prompt_text": item.PromptText,
"request_data": item.RequestData,
"check_point_data": item.CheckPointData,
"resume_count": item.ResumeCount,
"expires_at": item.ExpiresAt,
"updated_at": item.UpdatedAt,
}
return r.Updates(db, current.ID, columns)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationParticipantRepository = newConversationParticipantRepository()
func newConversationParticipantRepository() *conversationParticipantRepository {
return &conversationParticipantRepository{}
}
type conversationParticipantRepository struct {
}
func (r *conversationParticipantRepository) Get(db *gorm.DB, id int64) *models.ConversationParticipant {
ret := &models.ConversationParticipant{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationParticipantRepository) Take(db *gorm.DB, where ...interface{}) *models.ConversationParticipant {
ret := &models.ConversationParticipant{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *conversationParticipantRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationParticipant) {
cnd.Find(db, &list)
return
}
func (r *conversationParticipantRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ConversationParticipant {
ret := &models.ConversationParticipant{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationParticipantRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.ConversationParticipant, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *conversationParticipantRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationParticipant, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.ConversationParticipant{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *conversationParticipantRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.ConversationParticipant) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *conversationParticipantRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *conversationParticipantRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.ConversationParticipant{})
}
func (r *conversationParticipantRepository) Create(db *gorm.DB, t *models.ConversationParticipant) (err error) {
err = db.Create(t).Error
return
}
func (r *conversationParticipantRepository) Update(db *gorm.DB, t *models.ConversationParticipant) (err error) {
err = db.Save(t).Error
return
}
func (r *conversationParticipantRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.ConversationParticipant{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *conversationParticipantRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.ConversationParticipant{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *conversationParticipantRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.ConversationParticipant{}, "id = ?", id)
}

View File

@@ -0,0 +1,92 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationReadStateRepository = newConversationReadStateRepository()
func newConversationReadStateRepository() *conversationReadStateRepository {
return &conversationReadStateRepository{}
}
type conversationReadStateRepository struct {
}
func (r *conversationReadStateRepository) Get(db *gorm.DB, id int64) *models.ConversationReadState {
ret := &models.ConversationReadState{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationReadStateRepository) Take(db *gorm.DB, where ...interface{}) *models.ConversationReadState {
ret := &models.ConversationReadState{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *conversationReadStateRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationReadState) {
cnd.Find(db, &list)
return
}
func (r *conversationReadStateRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ConversationReadState {
ret := &models.ConversationReadState{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationReadStateRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.ConversationReadState, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *conversationReadStateRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationReadState, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.ConversationReadState{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *conversationReadStateRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.ConversationReadState{})
}
func (r *conversationReadStateRepository) Create(db *gorm.DB, t *models.ConversationReadState) (err error) {
err = db.Create(t).Error
return
}
func (r *conversationReadStateRepository) Update(db *gorm.DB, t *models.ConversationReadState) (err error) {
err = db.Save(t).Error
return
}
func (r *conversationReadStateRepository) Updates(db *gorm.DB, id int64, columns map[string]any) (err error) {
err = db.Model(&models.ConversationReadState{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *conversationReadStateRepository) UpdateColumn(db *gorm.DB, id int64, name string, value any) (err error) {
err = db.Model(&models.ConversationReadState{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *conversationReadStateRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.ConversationReadState{}, "id = ?", id)
}

View File

@@ -0,0 +1,131 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationRepository = newConversationRepository()
func newConversationRepository() *conversationRepository {
return &conversationRepository{}
}
type conversationRepository struct {
}
func (r *conversationRepository) Get(db *gorm.DB, id int64) *models.Conversation {
ret := &models.Conversation{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationRepository) Take(db *gorm.DB, where ...interface{}) *models.Conversation {
ret := &models.Conversation{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *conversationRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Conversation) {
cnd.Find(db, &list)
return
}
func (r *conversationRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Conversation {
ret := &models.Conversation{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Conversation, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *conversationRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Conversation, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Conversation{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *conversationRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Conversation) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *conversationRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *conversationRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Conversation{})
}
func (r *conversationRepository) CountByAIAgentID(db *gorm.DB, aiAgentID int64) int64 {
query := db.Model(&models.Conversation{})
if aiAgentID > 0 {
query = query.Where("ai_agent_id = ?", aiAgentID)
}
var count int64
if err := query.Count(&count).Error; err != nil {
return 0
}
return count
}
func (r *conversationRepository) CountHandoffByAIAgentID(db *gorm.DB, aiAgentID int64) int64 {
query := db.Model(&models.Conversation{}).Where("handoff_at IS NOT NULL")
if aiAgentID > 0 {
query = query.Where("ai_agent_id = ?", aiAgentID)
}
var count int64
if err := query.Count(&count).Error; err != nil {
return 0
}
return count
}
func (r *conversationRepository) Create(db *gorm.DB, t *models.Conversation) (err error) {
err = db.Create(t).Error
return
}
func (r *conversationRepository) Update(db *gorm.DB, t *models.Conversation) (err error) {
err = db.Save(t).Error
return
}
func (r *conversationRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Conversation{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *conversationRepository) UpdatesByCustomerID(db *gorm.DB, customerID int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Conversation{}).Where("customer_id = ?", customerID).Updates(columns).Error
return
}
func (r *conversationRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Conversation{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *conversationRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Conversation{}, "id = ?", id)
}

View File

@@ -0,0 +1,67 @@
package repositories
import (
"agent-desk/internal/models"
"gorm.io/gorm"
)
var ConversationSatisfactionRepository = newConversationSatisfactionRepository()
func newConversationSatisfactionRepository() *conversationSatisfactionRepository {
return &conversationSatisfactionRepository{}
}
type conversationSatisfactionRepository struct{}
func (r *conversationSatisfactionRepository) Get(db *gorm.DB, id int64) *models.ConversationSatisfaction {
if id <= 0 {
return nil
}
item := &models.ConversationSatisfaction{}
if err := db.First(item, "id = ?", id).Error; err != nil {
return nil
}
return item
}
func (r *conversationSatisfactionRepository) GetByConversationID(db *gorm.DB, conversationID int64) *models.ConversationSatisfaction {
if conversationID <= 0 {
return nil
}
item := &models.ConversationSatisfaction{}
if err := db.Where("conversation_id = ?", conversationID).First(item).Error; err != nil {
return nil
}
return item
}
func (r *conversationSatisfactionRepository) Create(db *gorm.DB, item *models.ConversationSatisfaction) error {
return db.Create(item).Error
}
func (r *conversationSatisfactionRepository) Update(db *gorm.DB, item *models.ConversationSatisfaction) error {
return db.Save(item).Error
}
func (r *conversationSatisfactionRepository) Count(db *gorm.DB, where ...any) int64 {
var count int64
tx := db.Model(&models.ConversationSatisfaction{})
if len(where) > 0 {
tx = tx.Where(where[0], where[1:]...)
}
tx.Count(&count)
return count
}
func (r *conversationSatisfactionRepository) AverageScore(db *gorm.DB, where ...any) float64 {
var avg *float64
tx := db.Model(&models.ConversationSatisfaction{})
if len(where) > 0 {
tx = tx.Where(where[0], where[1:]...)
}
if err := tx.Select("AVG(score)").Scan(&avg).Error; err != nil || avg == nil {
return 0
}
return *avg
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var ConversationTagRepository = newConversationTagRepository()
func newConversationTagRepository() *conversationTagRepository {
return &conversationTagRepository{}
}
type conversationTagRepository struct {
}
func (r *conversationTagRepository) Get(db *gorm.DB, id int64) *models.ConversationTag {
ret := &models.ConversationTag{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *conversationTagRepository) Take(db *gorm.DB, where ...interface{}) *models.ConversationTag {
ret := &models.ConversationTag{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *conversationTagRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationTag) {
cnd.Find(db, &list)
return
}
func (r *conversationTagRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.ConversationTag {
ret := &models.ConversationTag{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *conversationTagRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.ConversationTag, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *conversationTagRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.ConversationTag, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.ConversationTag{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *conversationTagRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.ConversationTag) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *conversationTagRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *conversationTagRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.ConversationTag{})
}
func (r *conversationTagRepository) Create(db *gorm.DB, t *models.ConversationTag) (err error) {
err = db.Create(t).Error
return
}
func (r *conversationTagRepository) Update(db *gorm.DB, t *models.ConversationTag) (err error) {
err = db.Save(t).Error
return
}
func (r *conversationTagRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.ConversationTag{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *conversationTagRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.ConversationTag{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *conversationTagRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.ConversationTag{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var CustomerContactRepository = newCustomerContactRepository()
func newCustomerContactRepository() *customerContactRepository {
return &customerContactRepository{}
}
type customerContactRepository struct {
}
func (r *customerContactRepository) Get(db *gorm.DB, id int64) *models.CustomerContact {
ret := &models.CustomerContact{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *customerContactRepository) Take(db *gorm.DB, where ...interface{}) *models.CustomerContact {
ret := &models.CustomerContact{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *customerContactRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.CustomerContact) {
cnd.Find(db, &list)
return
}
func (r *customerContactRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.CustomerContact {
ret := &models.CustomerContact{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *customerContactRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.CustomerContact, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *customerContactRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.CustomerContact, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.CustomerContact{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *customerContactRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.CustomerContact) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *customerContactRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *customerContactRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.CustomerContact{})
}
func (r *customerContactRepository) Create(db *gorm.DB, t *models.CustomerContact) (err error) {
err = db.Create(t).Error
return
}
func (r *customerContactRepository) Update(db *gorm.DB, t *models.CustomerContact) (err error) {
err = db.Save(t).Error
return
}
func (r *customerContactRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.CustomerContact{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *customerContactRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.CustomerContact{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *customerContactRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.CustomerContact{}, "id = ?", id)
}

View File

@@ -0,0 +1,121 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/common/strs"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var CustomerIdentityRepository = newCustomerIdentityRepository()
func newCustomerIdentityRepository() *customerIdentityRepository {
return &customerIdentityRepository{}
}
type customerIdentityRepository struct {
}
func (r *customerIdentityRepository) Get(db *gorm.DB, id int64) *models.CustomerIdentity {
ret := &models.CustomerIdentity{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *customerIdentityRepository) Take(db *gorm.DB, where ...interface{}) *models.CustomerIdentity {
ret := &models.CustomerIdentity{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
// GetBy 按外部来源 + 外部用户标识查询身份映射(与 uk_customer_external 一致)。
func (r *customerIdentityRepository) GetBy(db *gorm.DB, externalSource enums.ExternalSource, externalID string) *models.CustomerIdentity {
if strs.IsAnyBlank(string(externalSource), externalID) {
return nil
}
return r.FindOne(db, sqls.NewCnd().
Eq("external_source", externalSource).
Eq("external_id", externalID))
}
func (r *customerIdentityRepository) FindByCustomerID(db *gorm.DB, customerID int64) []models.CustomerIdentity {
if customerID <= 0 {
return nil
}
return r.Find(db, sqls.NewCnd().Eq("customer_id", customerID).Eq("status", enums.StatusOk).Desc("id"))
}
func (r *customerIdentityRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.CustomerIdentity) {
cnd.Find(db, &list)
return
}
func (r *customerIdentityRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.CustomerIdentity {
ret := &models.CustomerIdentity{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *customerIdentityRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.CustomerIdentity, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *customerIdentityRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.CustomerIdentity, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.CustomerIdentity{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *customerIdentityRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.CustomerIdentity) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *customerIdentityRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *customerIdentityRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.CustomerIdentity{})
}
func (r *customerIdentityRepository) Create(db *gorm.DB, t *models.CustomerIdentity) (err error) {
err = db.Create(t).Error
return
}
func (r *customerIdentityRepository) Update(db *gorm.DB, t *models.CustomerIdentity) (err error) {
err = db.Save(t).Error
return
}
func (r *customerIdentityRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.CustomerIdentity{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *customerIdentityRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.CustomerIdentity{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *customerIdentityRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.CustomerIdentity{}, "id = ?", id)
}

View File

@@ -0,0 +1,127 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var CustomerRepository = newCustomerRepository()
func newCustomerRepository() *customerRepository {
return &customerRepository{}
}
type customerRepository struct {
}
type CompanyCustomerCount struct {
CompanyID int64 `gorm:"column:company_id"`
Count int64 `gorm:"column:cnt"`
}
func (r *customerRepository) CountByCompanyIDs(db *gorm.DB, companyIDs []int64, excludeStatus int) map[int64]int64 {
ret := make(map[int64]int64)
if len(companyIDs) == 0 {
return ret
}
rows := make([]CompanyCustomerCount, 0, len(companyIDs))
db.Model(&models.Customer{}).
Select("company_id, count(1) as cnt").
Where("company_id in ?", companyIDs).
Where("status <> ?", excludeStatus).
Group("company_id").
Scan(&rows)
for _, row := range rows {
ret[row.CompanyID] = row.Count
}
return ret
}
func (r *customerRepository) Get(db *gorm.DB, id int64) *models.Customer {
ret := &models.Customer{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *customerRepository) Take(db *gorm.DB, where ...interface{}) *models.Customer {
ret := &models.Customer{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *customerRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Customer) {
cnd.Find(db, &list)
return
}
func (r *customerRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Customer {
ret := &models.Customer{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *customerRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Customer, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *customerRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Customer, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Customer{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *customerRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Customer) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *customerRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *customerRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Customer{})
}
func (r *customerRepository) Create(db *gorm.DB, t *models.Customer) (err error) {
err = db.Create(t).Error
return
}
func (r *customerRepository) Update(db *gorm.DB, t *models.Customer) (err error) {
err = db.Save(t).Error
return
}
func (r *customerRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Customer{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *customerRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Customer{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *customerRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Customer{}, "id = ?", id)
}

View File

@@ -0,0 +1,108 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"time"
"gorm.io/gorm"
)
var DashboardRepository = newDashboardRepository()
func newDashboardRepository() *dashboardRepository {
return &dashboardRepository{}
}
type dashboardRepository struct {
}
func (r *dashboardRepository) CountConversations(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) int64 {
var count int64
tx := db.Model(&models.Conversation{})
if query != nil {
tx = query(tx)
}
tx.Count(&count)
return count
}
func (r *dashboardRepository) ListConversations(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) []models.Conversation {
var list []models.Conversation
tx := db.Model(&models.Conversation{})
if query != nil {
tx = query(tx)
}
tx.Find(&list)
return list
}
func (r *dashboardRepository) ListEnabledAgentProfiles(db *gorm.DB) []models.AgentProfile {
var list []models.AgentProfile
db.Model(&models.AgentProfile{}).Where("status = ?", enums.StatusOk).Find(&list)
return list
}
func (r *dashboardRepository) ListEnabledAgentTeams(db *gorm.DB) []models.AgentTeam {
var list []models.AgentTeam
db.Model(&models.AgentTeam{}).Where("status = ?", enums.StatusOk).Order("id asc").Find(&list)
return list
}
func (r *dashboardRepository) ListActiveTeamSchedules(db *gorm.DB, startAt, endAt time.Time) []models.AgentTeamSchedule {
var list []models.AgentTeamSchedule
db.Model(&models.AgentTeamSchedule{}).
Where("status = ? AND start_at <= ? AND end_at >= ?", enums.StatusOk, endAt, startAt).
Find(&list)
return list
}
func (r *dashboardRepository) CountAIAgents(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) int64 {
var count int64
tx := db.Model(&models.AIAgent{})
if query != nil {
tx = query(tx)
}
tx.Count(&count)
return count
}
func (r *dashboardRepository) ListAIAgents(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) []models.AIAgent {
var list []models.AIAgent
tx := db.Model(&models.AIAgent{})
if query != nil {
tx = query(tx)
}
tx.Find(&list)
return list
}
func (r *dashboardRepository) CountChannels(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) int64 {
var count int64
tx := db.Model(&models.Channel{})
if query != nil {
tx = query(tx)
}
tx.Count(&count)
return count
}
func (r *dashboardRepository) CountKnowledgeRetrieveLogs(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) int64 {
var count int64
tx := db.Model(&models.KnowledgeRetrieveLog{})
if query != nil {
tx = query(tx)
}
tx.Count(&count)
return count
}
func (r *dashboardRepository) CountAgentRuns(db *gorm.DB, query func(tx *gorm.DB) *gorm.DB) int64 {
var count int64
tx := db.Model(&models.AgentRun{})
if query != nil {
tx = query(tx)
}
tx.Count(&count)
return count
}

View File

@@ -0,0 +1,92 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var KnowledgeBaseRepository = newKnowledgeBaseRepository()
func newKnowledgeBaseRepository() *knowledgeBaseRepository {
return &knowledgeBaseRepository{}
}
type knowledgeBaseRepository struct {
}
func (r *knowledgeBaseRepository) Get(db *gorm.DB, id int64) *models.KnowledgeBase {
ret := &models.KnowledgeBase{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeBaseRepository) Take(db *gorm.DB, where ...interface{}) *models.KnowledgeBase {
ret := &models.KnowledgeBase{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeBaseRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeBase) {
cnd.Find(db, &list)
return
}
func (r *knowledgeBaseRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.KnowledgeBase {
ret := &models.KnowledgeBase{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *knowledgeBaseRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.KnowledgeBase, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *knowledgeBaseRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeBase, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.KnowledgeBase{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *knowledgeBaseRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.KnowledgeBase{})
}
func (r *knowledgeBaseRepository) Create(db *gorm.DB, t *models.KnowledgeBase) (err error) {
err = db.Create(t).Error
return
}
func (r *knowledgeBaseRepository) Update(db *gorm.DB, t *models.KnowledgeBase) (err error) {
err = db.Save(t).Error
return
}
func (r *knowledgeBaseRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.KnowledgeBase{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *knowledgeBaseRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.KnowledgeBase{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *knowledgeBaseRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.KnowledgeBase{}, "id = ?", id)
}

View File

@@ -0,0 +1,174 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var KnowledgeChunkRepository = newKnowledgeChunkRepository()
func newKnowledgeChunkRepository() *knowledgeChunkRepository {
return &knowledgeChunkRepository{}
}
type knowledgeChunkRepository struct {
}
func (r *knowledgeChunkRepository) Get(db *gorm.DB, id int64) *models.KnowledgeChunk {
ret := &models.KnowledgeChunk{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeChunkRepository) Take(db *gorm.DB, where ...interface{}) *models.KnowledgeChunk {
ret := &models.KnowledgeChunk{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeChunkRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeChunk) {
cnd.Find(db, &list)
return
}
func (r *knowledgeChunkRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.KnowledgeChunk {
ret := &models.KnowledgeChunk{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *knowledgeChunkRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.KnowledgeChunk, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *knowledgeChunkRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeChunk, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.KnowledgeChunk{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *knowledgeChunkRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.KnowledgeChunk{})
}
func (r *knowledgeChunkRepository) Create(db *gorm.DB, t *models.KnowledgeChunk) (err error) {
err = db.Create(t).Error
return
}
func (r *knowledgeChunkRepository) BatchCreate(db *gorm.DB, list []models.KnowledgeChunk) (err error) {
err = db.Create(&list).Error
return
}
func (r *knowledgeChunkRepository) ReplaceByDocumentID(db *gorm.DB, documentID int64, list []models.KnowledgeChunk) error {
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
if err := ctx.Tx.Where("document_id = ?", documentID).Delete(&models.KnowledgeChunk{}).Error; err != nil {
return err
}
if len(list) == 0 {
return nil
}
return ctx.Tx.Create(&list).Error
})
}
func (r *knowledgeChunkRepository) ReplaceByFaqID(db *gorm.DB, faqID int64, item *models.KnowledgeChunk) error {
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
if err := ctx.Tx.Where("faq_id = ?", faqID).Delete(&models.KnowledgeChunk{}).Error; err != nil {
return err
}
if item == nil {
return nil
}
return ctx.Tx.Create(item).Error
})
}
func (r *knowledgeChunkRepository) Update(db *gorm.DB, t *models.KnowledgeChunk) (err error) {
err = db.Save(t).Error
return
}
func (r *knowledgeChunkRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.KnowledgeChunk{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *knowledgeChunkRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.KnowledgeChunk{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *knowledgeChunkRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.KnowledgeChunk{}, "id = ?", id)
}
func (r *knowledgeChunkRepository) DeleteByDocumentID(db *gorm.DB, documentID int64) error {
return db.Delete(&models.KnowledgeChunk{}, "document_id = ?", documentID).Error
}
func (r *knowledgeChunkRepository) DeleteByFaqID(db *gorm.DB, faqID int64) error {
return db.Delete(&models.KnowledgeChunk{}, "faq_id = ?", faqID).Error
}
func (r *knowledgeChunkRepository) DeleteByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) error {
return db.Delete(&models.KnowledgeChunk{}, "knowledge_base_id = ?", knowledgeBaseID).Error
}
func (r *knowledgeChunkRepository) FindByDocumentID(db *gorm.DB, documentID int64) (list []models.KnowledgeChunk) {
db.Where("document_id = ?", documentID).Order("chunk_no asc").Find(&list)
return
}
func (r *knowledgeChunkRepository) FindByFaqID(db *gorm.DB, faqID int64) (list []models.KnowledgeChunk) {
db.Where("faq_id = ?", faqID).Order("chunk_no asc").Find(&list)
return
}
func (r *knowledgeChunkRepository) FindByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) (list []models.KnowledgeChunk) {
db.Where("knowledge_base_id = ?", knowledgeBaseID).Order("id asc").Find(&list)
return
}
func (r *knowledgeChunkRepository) FindByVectorIDs(db *gorm.DB, vectorIDs []string) (list []models.KnowledgeChunk) {
if len(vectorIDs) == 0 {
return nil
}
db.Where("vector_id IN ?", vectorIDs).Find(&list)
return
}
func (r *knowledgeChunkRepository) CountByDocumentID(db *gorm.DB, documentID int64) int64 {
var count int64
db.Model(&models.KnowledgeChunk{}).Where("document_id = ?", documentID).Count(&count)
return count
}
func (r *knowledgeChunkRepository) CountByFaqID(db *gorm.DB, faqID int64) int64 {
var count int64
db.Model(&models.KnowledgeChunk{}).Where("faq_id = ?", faqID).Count(&count)
return count
}
func (r *knowledgeChunkRepository) CountByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) int64 {
var count int64
db.Model(&models.KnowledgeChunk{}).Where("knowledge_base_id = ?", knowledgeBaseID).Count(&count)
return count
}

View File

@@ -0,0 +1,58 @@
package repositories
import (
"agent-desk/internal/models"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var KnowledgeDirectoryRepository = newKnowledgeDirectoryRepository()
func newKnowledgeDirectoryRepository() *knowledgeDirectoryRepository {
return &knowledgeDirectoryRepository{}
}
type knowledgeDirectoryRepository struct {
}
func (r *knowledgeDirectoryRepository) Get(db *gorm.DB, id int64) *models.KnowledgeDirectory {
ret := &models.KnowledgeDirectory{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeDirectoryRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeDirectory) {
cnd.Find(db, &list)
return
}
func (r *knowledgeDirectoryRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.KnowledgeDirectory {
ret := &models.KnowledgeDirectory{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *knowledgeDirectoryRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.KnowledgeDirectory{})
}
func (r *knowledgeDirectoryRepository) Create(db *gorm.DB, t *models.KnowledgeDirectory) error {
return db.Create(t).Error
}
func (r *knowledgeDirectoryRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.KnowledgeDirectory{}).Where("id = ?", id).Updates(columns).Error
}
func (r *knowledgeDirectoryRepository) UpdateColumn(db *gorm.DB, id int64, name string, value any) error {
return db.Model(&models.KnowledgeDirectory{}).Where("id = ?", id).UpdateColumn(name, value).Error
}
func (r *knowledgeDirectoryRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.KnowledgeDirectory{}, "id = ?", id).Error
}

View File

@@ -0,0 +1,131 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var KnowledgeDocumentRepository = newKnowledgeDocumentRepository()
func newKnowledgeDocumentRepository() *knowledgeDocumentRepository {
return &knowledgeDocumentRepository{}
}
type knowledgeDocumentRepository struct {
}
func (r *knowledgeDocumentRepository) Get(db *gorm.DB, id int64) *models.KnowledgeDocument {
ret := &models.KnowledgeDocument{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeDocumentRepository) Take(db *gorm.DB, where ...interface{}) *models.KnowledgeDocument {
ret := &models.KnowledgeDocument{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeDocumentRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeDocument) {
cnd.Find(db, &list)
return
}
func (r *knowledgeDocumentRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.KnowledgeDocument {
ret := &models.KnowledgeDocument{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *knowledgeDocumentRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.KnowledgeDocument, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *knowledgeDocumentRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeDocument, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.KnowledgeDocument{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *knowledgeDocumentRepository) FindPageListByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeDocument, paging *sqls.Paging) {
cnd.Find(db.Omit("content"), &list)
count := cnd.Count(db, &models.KnowledgeDocument{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *knowledgeDocumentRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.KnowledgeDocument{})
}
func (r *knowledgeDocumentRepository) CountActiveByDirectoryID(db *gorm.DB, directoryID int64) int64 {
var count int64
db.Model(&models.KnowledgeDocument{}).
Where("directory_id = ? AND status <> ?", directoryID, enums.StatusDeleted).
Count(&count)
return count
}
func (r *knowledgeDocumentRepository) Create(db *gorm.DB, t *models.KnowledgeDocument) (err error) {
err = db.Create(t).Error
return
}
func (r *knowledgeDocumentRepository) Update(db *gorm.DB, t *models.KnowledgeDocument) (err error) {
err = db.Save(t).Error
return
}
func (r *knowledgeDocumentRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.KnowledgeDocument{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *knowledgeDocumentRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.KnowledgeDocument{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *knowledgeDocumentRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.KnowledgeDocument{}, "id = ?", id)
}
func (r *knowledgeDocumentRepository) DeleteByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) error {
return db.Delete(&models.KnowledgeDocument{}, "knowledge_base_id = ?", knowledgeBaseID).Error
}
func (r *knowledgeDocumentRepository) FindByIDs(db *gorm.DB, ids []int64) (list []models.KnowledgeDocument) {
if len(ids) == 0 {
return nil
}
db.Where("id IN ?", ids).Find(&list)
return
}
func (r *knowledgeDocumentRepository) CountByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) int64 {
var count int64
db.Model(&models.KnowledgeDocument{}).Where("knowledge_base_id = ? AND status <> ?", knowledgeBaseID, enums.StatusDeleted).Count(&count)
return count
}

View File

@@ -0,0 +1,55 @@
package repositories
import (
"fmt"
"strings"
"testing"
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"github.com/glebarez/sqlite"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
func TestKnowledgeDocumentRepositoryFindPageListByCndOmitsContentAndPaginates(t *testing.T) {
db, err := gorm.Open(sqlite.Open("file:knowledge_document_repository_test?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite error = %v", err)
}
if err := db.AutoMigrate(&models.KnowledgeDocument{}); err != nil {
t.Fatalf("auto migrate error = %v", err)
}
if err := db.Exec("DELETE FROM knowledge_documents").Error; err != nil {
t.Fatalf("clean knowledge documents error = %v", err)
}
sqls.SetDB(db)
for i := 1; i <= 3; i++ {
item := &models.KnowledgeDocument{
KnowledgeBaseID: 1,
Title: fmt.Sprintf("doc-%d", i),
ContentType: enums.KnowledgeDocumentContentTypeMarkdown,
Content: strings.Repeat("large document content ", 200),
Status: enums.StatusOk,
IndexStatus: enums.KnowledgeDocumentIndexStatusPending,
}
if err := db.Create(item).Error; err != nil {
t.Fatalf("create document %d error = %v", i, err)
}
}
list, paging := KnowledgeDocumentRepository.FindPageListByCnd(db, sqls.NewCnd().Eq("knowledge_base_id", 1).Asc("id").Page(1, 2))
if len(list) != 2 {
t.Fatalf("len(list) = %d, want 2", len(list))
}
if paging.Total != 3 {
t.Fatalf("paging.Total = %d, want 3", paging.Total)
}
for _, item := range list {
if item.Content != "" {
t.Fatalf("list item content should be empty, got %q", item.Content)
}
}
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var KnowledgeFAQRepository = newKnowledgeFAQRepository()
func newKnowledgeFAQRepository() *knowledgeFAQRepository {
return &knowledgeFAQRepository{}
}
type knowledgeFAQRepository struct{}
func (r *knowledgeFAQRepository) Get(db *gorm.DB, id int64) *models.KnowledgeFAQ {
ret := &models.KnowledgeFAQ{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *knowledgeFAQRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeFAQ) {
cnd.Find(db, &list)
return
}
func (r *knowledgeFAQRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeFAQ, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.KnowledgeFAQ{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *knowledgeFAQRepository) FindPageByParams(db *gorm.DB, queryParams *params.QueryParams) (list []models.KnowledgeFAQ, paging *sqls.Paging) {
return r.FindPageByCnd(db, &queryParams.Cnd)
}
func (r *knowledgeFAQRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.KnowledgeFAQ{})
}
func (r *knowledgeFAQRepository) CountActiveByDirectoryID(db *gorm.DB, directoryID int64) int64 {
var count int64
db.Model(&models.KnowledgeFAQ{}).
Where("directory_id = ? AND status <> ?", directoryID, enums.StatusDeleted).
Count(&count)
return count
}
func (r *knowledgeFAQRepository) Create(db *gorm.DB, t *models.KnowledgeFAQ) error {
return db.Create(t).Error
}
func (r *knowledgeFAQRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.KnowledgeFAQ{}).Where("id = ?", id).Updates(columns).Error
}
func (r *knowledgeFAQRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.KnowledgeFAQ{}, "id = ?", id).Error
}
func (r *knowledgeFAQRepository) DeleteByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) error {
return db.Delete(&models.KnowledgeFAQ{}, "knowledge_base_id = ?", knowledgeBaseID).Error
}
func (r *knowledgeFAQRepository) FindByIDs(db *gorm.DB, ids []int64) (list []models.KnowledgeFAQ) {
if len(ids) == 0 {
return nil
}
db.Where("id IN ?", ids).Find(&list)
return
}
func (r *knowledgeFAQRepository) FindAllByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) (list []models.KnowledgeFAQ) {
db.Where("knowledge_base_id = ? AND status <> ?", knowledgeBaseID, enums.StatusDeleted).Order("id DESC").Find(&list)
return
}
func (r *knowledgeFAQRepository) FindByKnowledgeBaseIDAndQuestions(db *gorm.DB, knowledgeBaseID int64, questions []string) (list []models.KnowledgeFAQ) {
if len(questions) == 0 {
return nil
}
db.Where("knowledge_base_id = ? AND question IN ?", knowledgeBaseID, questions).Find(&list)
return
}
func (r *knowledgeFAQRepository) CountByKnowledgeBaseID(db *gorm.DB, knowledgeBaseID int64) int64 {
var count int64
db.Model(&models.KnowledgeFAQ{}).Where("knowledge_base_id = ? AND status <> ?", knowledgeBaseID, enums.StatusDeleted).Count(&count)
return count
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var LoginCredentialLogRepository = newLoginCredentialLogRepository()
func newLoginCredentialLogRepository() *loginCredentialLogRepository {
return &loginCredentialLogRepository{}
}
type loginCredentialLogRepository struct {
}
func (r *loginCredentialLogRepository) Get(db *gorm.DB, id int64) *models.LoginCredentialLog {
ret := &models.LoginCredentialLog{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *loginCredentialLogRepository) Take(db *gorm.DB, where ...interface{}) *models.LoginCredentialLog {
ret := &models.LoginCredentialLog{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *loginCredentialLogRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.LoginCredentialLog) {
cnd.Find(db, &list)
return
}
func (r *loginCredentialLogRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.LoginCredentialLog {
ret := &models.LoginCredentialLog{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *loginCredentialLogRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.LoginCredentialLog, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *loginCredentialLogRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.LoginCredentialLog, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.LoginCredentialLog{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *loginCredentialLogRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.LoginCredentialLog) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *loginCredentialLogRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *loginCredentialLogRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.LoginCredentialLog{})
}
func (r *loginCredentialLogRepository) Create(db *gorm.DB, t *models.LoginCredentialLog) (err error) {
err = db.Create(t).Error
return
}
func (r *loginCredentialLogRepository) Update(db *gorm.DB, t *models.LoginCredentialLog) (err error) {
err = db.Save(t).Error
return
}
func (r *loginCredentialLogRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.LoginCredentialLog{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *loginCredentialLogRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.LoginCredentialLog{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *loginCredentialLogRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.LoginCredentialLog{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var LoginSessionRepository = newLoginSessionRepository()
func newLoginSessionRepository() *loginSessionRepository {
return &loginSessionRepository{}
}
type loginSessionRepository struct {
}
func (r *loginSessionRepository) Get(db *gorm.DB, id int64) *models.LoginSession {
ret := &models.LoginSession{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *loginSessionRepository) Take(db *gorm.DB, where ...interface{}) *models.LoginSession {
ret := &models.LoginSession{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *loginSessionRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.LoginSession) {
cnd.Find(db, &list)
return
}
func (r *loginSessionRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.LoginSession {
ret := &models.LoginSession{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *loginSessionRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.LoginSession, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *loginSessionRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.LoginSession, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.LoginSession{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *loginSessionRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.LoginSession) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *loginSessionRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *loginSessionRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.LoginSession{})
}
func (r *loginSessionRepository) Create(db *gorm.DB, t *models.LoginSession) (err error) {
err = db.Create(t).Error
return
}
func (r *loginSessionRepository) Update(db *gorm.DB, t *models.LoginSession) (err error) {
err = db.Save(t).Error
return
}
func (r *loginSessionRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.LoginSession{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *loginSessionRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.LoginSession{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *loginSessionRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.LoginSession{}, "id = ?", id)
}

View File

@@ -0,0 +1,119 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var MessageRepository = newMessageRepository()
func newMessageRepository() *messageRepository {
return &messageRepository{}
}
type messageRepository struct {
}
func (r *messageRepository) Get(db *gorm.DB, id int64) *models.Message {
ret := &models.Message{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *messageRepository) Take(db *gorm.DB, where ...interface{}) *models.Message {
ret := &models.Message{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *messageRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Message) {
cnd.Find(db, &list)
return
}
func (r *messageRepository) FindLastUnrecalledByConversationID(db *gorm.DB, conversationID int64) *models.Message {
ret := &models.Message{}
if err := db.
Where("conversation_id = ? AND recalled_at IS NULL AND send_status <> ?", conversationID, 6).
Order("id DESC").
Limit(1).
Take(ret).Error; err != nil {
return nil
}
return ret
}
func (r *messageRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Message {
ret := &models.Message{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *messageRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Message, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *messageRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Message, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Message{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *messageRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Message) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *messageRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *messageRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Message{})
}
func (r *messageRepository) Create(db *gorm.DB, t *models.Message) (err error) {
err = db.Create(t).Error
return
}
func (r *messageRepository) Update(db *gorm.DB, t *models.Message) (err error) {
err = db.Save(t).Error
return
}
func (r *messageRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Message{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *messageRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Message{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *messageRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Message{}, "id = ?", id)
}
// GetByClientMsgID 根据 conversationID 和 clientMsgID 获取消息
func (r *messageRepository) GetByClientMsgID(db *gorm.DB, conversationID int64, clientMsgID string) *models.Message {
return r.FindOne(db, sqls.NewCnd().Where("conversation_id = ? AND client_msg_id = ?", conversationID, clientMsgID))
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var MigrationRepository = newMigrationRepository()
func newMigrationRepository() *migrationRepository {
return &migrationRepository{}
}
type migrationRepository struct {
}
func (r *migrationRepository) Get(db *gorm.DB, id int64) *models.Migration {
ret := &models.Migration{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *migrationRepository) Take(db *gorm.DB, where ...interface{}) *models.Migration {
ret := &models.Migration{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *migrationRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Migration) {
cnd.Find(db, &list)
return
}
func (r *migrationRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Migration {
ret := &models.Migration{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *migrationRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Migration, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *migrationRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Migration, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Migration{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *migrationRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Migration) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *migrationRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *migrationRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Migration{})
}
func (r *migrationRepository) Create(db *gorm.DB, t *models.Migration) (err error) {
err = db.Create(t).Error
return
}
func (r *migrationRepository) Update(db *gorm.DB, t *models.Migration) (err error) {
err = db.Save(t).Error
return
}
func (r *migrationRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Migration{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *migrationRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Migration{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *migrationRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Migration{}, "id = ?", id)
}

View File

@@ -0,0 +1,68 @@
package repositories
import (
"time"
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var NotificationRepository = newNotificationRepository()
func newNotificationRepository() *notificationRepository {
return &notificationRepository{}
}
type notificationRepository struct {
}
func (r *notificationRepository) Get(db *gorm.DB, id int64) *models.Notification {
ret := &models.Notification{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *notificationRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Notification) {
cnd.Find(db, &list)
return
}
func (r *notificationRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Notification, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *notificationRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Notification, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Notification{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *notificationRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Notification{})
}
func (r *notificationRepository) Create(db *gorm.DB, item *models.Notification) error {
return db.Create(item).Error
}
func (r *notificationRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.Notification{}).Where("id = ?", id).Updates(columns).Error
}
func (r *notificationRepository) MarkAllRead(db *gorm.DB, userID int64, readAt time.Time) error {
return db.Model(&models.Notification{}).
Where("recipient_user_id = ? AND read_at IS NULL", userID).
Updates(map[string]any{"read_at": readAt}).Error
}

View File

@@ -0,0 +1,90 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var OrganizationMemberRepository = newOrganizationMemberRepository()
func newOrganizationMemberRepository() *organizationMemberRepository {
return &organizationMemberRepository{}
}
type organizationMemberRepository struct{}
func (r *organizationMemberRepository) GetByOrgAndUser(db *gorm.DB, orgID, userID int64) *models.OrganizationMember {
ret := &models.OrganizationMember{}
if err := db.First(ret, "organization_id = ? AND user_id = ?", orgID, userID).Error; err != nil {
return nil
}
return ret
}
func (r *organizationMemberRepository) Get(db *gorm.DB, id int64) *models.OrganizationMember {
ret := &models.OrganizationMember{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *organizationMemberRepository) Take(db *gorm.DB, where ...interface{}) *models.OrganizationMember {
ret := &models.OrganizationMember{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *organizationMemberRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.OrganizationMember) {
cnd.Find(db, &list)
return
}
func (r *organizationMemberRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.OrganizationMember {
ret := &models.OrganizationMember{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *organizationMemberRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.OrganizationMember, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *organizationMemberRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.OrganizationMember, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.OrganizationMember{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *organizationMemberRepository) Create(db *gorm.DB, t *models.OrganizationMember) error {
return db.Create(t).Error
}
func (r *organizationMemberRepository) Update(db *gorm.DB, t *models.OrganizationMember) error {
return db.Save(t).Error
}
func (r *organizationMemberRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) error {
return db.Model(&models.OrganizationMember{}).Where("id = ?", id).Updates(columns).Error
}
func (r *organizationMemberRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) error {
return db.Model(&models.OrganizationMember{}).Where("id = ?", id).UpdateColumn(name, value).Error
}
func (r *organizationMemberRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.OrganizationMember{}, "id = ?", id).Error
}

View File

@@ -0,0 +1,90 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var OrganizationRepository = newOrganizationRepository()
func newOrganizationRepository() *organizationRepository {
return &organizationRepository{}
}
type organizationRepository struct{}
func (r *organizationRepository) GetByCode(db *gorm.DB, code string) *models.Organization {
ret := &models.Organization{}
if err := db.First(ret, "code = ?", code).Error; err != nil {
return nil
}
return ret
}
func (r *organizationRepository) Get(db *gorm.DB, id int64) *models.Organization {
ret := &models.Organization{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *organizationRepository) Take(db *gorm.DB, where ...interface{}) *models.Organization {
ret := &models.Organization{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *organizationRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Organization) {
cnd.Find(db, &list)
return
}
func (r *organizationRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Organization {
ret := &models.Organization{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *organizationRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Organization, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *organizationRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Organization, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Organization{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *organizationRepository) Create(db *gorm.DB, t *models.Organization) error {
return db.Create(t).Error
}
func (r *organizationRepository) Update(db *gorm.DB, t *models.Organization) error {
return db.Save(t).Error
}
func (r *organizationRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) error {
return db.Model(&models.Organization{}).Where("id = ?", id).Updates(columns).Error
}
func (r *organizationRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) error {
return db.Model(&models.Organization{}).Where("id = ?", id).UpdateColumn(name, value).Error
}
func (r *organizationRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.Organization{}, "id = ?", id).Error
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var PermissionRepository = newPermissionRepository()
func newPermissionRepository() *permissionRepository {
return &permissionRepository{}
}
type permissionRepository struct {
}
func (r *permissionRepository) Get(db *gorm.DB, id int64) *models.Permission {
ret := &models.Permission{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *permissionRepository) Take(db *gorm.DB, where ...interface{}) *models.Permission {
ret := &models.Permission{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *permissionRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Permission) {
cnd.Find(db, &list)
return
}
func (r *permissionRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Permission {
ret := &models.Permission{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *permissionRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Permission, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *permissionRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Permission, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Permission{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *permissionRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Permission) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *permissionRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *permissionRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Permission{})
}
func (r *permissionRepository) Create(db *gorm.DB, t *models.Permission) (err error) {
err = db.Create(t).Error
return
}
func (r *permissionRepository) Update(db *gorm.DB, t *models.Permission) (err error) {
err = db.Save(t).Error
return
}
func (r *permissionRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Permission{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *permissionRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Permission{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *permissionRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Permission{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var QuickReplyRepository = newQuickReplyRepository()
func newQuickReplyRepository() *quickReplyRepository {
return &quickReplyRepository{}
}
type quickReplyRepository struct {
}
func (r *quickReplyRepository) Get(db *gorm.DB, id int64) *models.QuickReply {
ret := &models.QuickReply{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *quickReplyRepository) Take(db *gorm.DB, where ...interface{}) *models.QuickReply {
ret := &models.QuickReply{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *quickReplyRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.QuickReply) {
cnd.Find(db, &list)
return
}
func (r *quickReplyRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.QuickReply {
ret := &models.QuickReply{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *quickReplyRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.QuickReply, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *quickReplyRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.QuickReply, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.QuickReply{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *quickReplyRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.QuickReply) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *quickReplyRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *quickReplyRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.QuickReply{})
}
func (r *quickReplyRepository) Create(db *gorm.DB, t *models.QuickReply) (err error) {
err = db.Create(t).Error
return
}
func (r *quickReplyRepository) Update(db *gorm.DB, t *models.QuickReply) (err error) {
err = db.Save(t).Error
return
}
func (r *quickReplyRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.QuickReply{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *quickReplyRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.QuickReply{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *quickReplyRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.QuickReply{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var RolePermissionRepository = newRolePermissionRepository()
func newRolePermissionRepository() *rolePermissionRepository {
return &rolePermissionRepository{}
}
type rolePermissionRepository struct {
}
func (r *rolePermissionRepository) Get(db *gorm.DB, id int64) *models.RolePermission {
ret := &models.RolePermission{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *rolePermissionRepository) Take(db *gorm.DB, where ...interface{}) *models.RolePermission {
ret := &models.RolePermission{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *rolePermissionRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.RolePermission) {
cnd.Find(db, &list)
return
}
func (r *rolePermissionRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.RolePermission {
ret := &models.RolePermission{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *rolePermissionRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.RolePermission, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *rolePermissionRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.RolePermission, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.RolePermission{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *rolePermissionRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.RolePermission) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *rolePermissionRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *rolePermissionRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.RolePermission{})
}
func (r *rolePermissionRepository) Create(db *gorm.DB, t *models.RolePermission) (err error) {
err = db.Create(t).Error
return
}
func (r *rolePermissionRepository) Update(db *gorm.DB, t *models.RolePermission) (err error) {
err = db.Save(t).Error
return
}
func (r *rolePermissionRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.RolePermission{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *rolePermissionRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.RolePermission{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *rolePermissionRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.RolePermission{}, "id = ?", id)
}

View File

@@ -0,0 +1,106 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var RoleRepository = newRoleRepository()
func newRoleRepository() *roleRepository {
return &roleRepository{}
}
type roleRepository struct {
}
func (r *roleRepository) Get(db *gorm.DB, id int64) *models.Role {
ret := &models.Role{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *roleRepository) Take(db *gorm.DB, where ...interface{}) *models.Role {
ret := &models.Role{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *roleRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Role) {
cnd.Find(db, &list)
return
}
func (r *roleRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Role {
ret := &models.Role{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *roleRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Role, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *roleRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Role, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Role{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *roleRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Role) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *roleRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *roleRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Role{})
}
func (r *roleRepository) Create(db *gorm.DB, t *models.Role) (err error) {
err = db.Create(t).Error
return
}
func (r *roleRepository) Update(db *gorm.DB, t *models.Role) (err error) {
err = db.Save(t).Error
return
}
func (r *roleRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Role{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *roleRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Role{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *roleRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Role{}, "id = ?", id)
}
func (r *roleRepository) GetByCode(db *gorm.DB, code string) *models.Role {
return r.FindOne(db, sqls.NewCnd().Eq("code", code))
}

View File

@@ -0,0 +1,117 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var SkillDefinitionRepository = newSkillDefinitionRepository()
func newSkillDefinitionRepository() *skillDefinitionRepository {
return &skillDefinitionRepository{}
}
type skillDefinitionRepository struct {
}
func (r *skillDefinitionRepository) Get(db *gorm.DB, id int64) *models.SkillDefinition {
ret := &models.SkillDefinition{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *skillDefinitionRepository) Take(db *gorm.DB, where ...interface{}) *models.SkillDefinition {
ret := &models.SkillDefinition{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *skillDefinitionRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.SkillDefinition) {
cnd.Find(db, &list)
return
}
func (r *skillDefinitionRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.SkillDefinition {
ret := &models.SkillDefinition{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *skillDefinitionRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.SkillDefinition, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *skillDefinitionRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.SkillDefinition, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.SkillDefinition{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *skillDefinitionRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.SkillDefinition) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *skillDefinitionRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *skillDefinitionRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.SkillDefinition{})
}
func (r *skillDefinitionRepository) Create(db *gorm.DB, t *models.SkillDefinition) (err error) {
err = db.Create(t).Error
return
}
func (r *skillDefinitionRepository) Update(db *gorm.DB, t *models.SkillDefinition) (err error) {
err = db.Save(t).Error
return
}
func (r *skillDefinitionRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.SkillDefinition{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *skillDefinitionRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.SkillDefinition{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *skillDefinitionRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.SkillDefinition{}, "id = ?", id)
}
func (r *skillDefinitionRepository) GetByIDs(db *gorm.DB, ids []int64) map[int64]models.SkillDefinition {
if len(ids) == 0 {
return nil
}
list := r.Find(db, sqls.NewCnd().Where("id IN (?)", ids))
if len(list) == 0 {
return nil
}
result := make(map[int64]models.SkillDefinition, len(list))
for _, item := range list {
result[item.ID] = item
}
return result
}

View File

@@ -0,0 +1,208 @@
package repositories
import (
"agent-desk/internal/models"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var (
DocPageRepository = &docPageRepository{}
CategoryRepository = &supportCategoryRepository{}
PostRepository = &supportPostRepository{}
CommentRepository = &supportCommentRepository{}
ReactionRepository = &supportReactionRepository{}
CommentReportRepository = &supportCommentReportRepository{}
)
type docPageRepository struct{}
func (r *docPageRepository) Get(db *gorm.DB, id int64) *models.DocPage {
ret := &models.DocPage{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *docPageRepository) GetByParentIDAndSlug(db *gorm.DB, parentID int64, slug string) *models.DocPage {
ret := &models.DocPage{}
if err := db.First(ret, "parent_id = ? AND slug = ?", parentID, slug).Error; err != nil {
return nil
}
return ret
}
func (r *docPageRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.DocPage) {
cnd.Find(db, &list)
return
}
// FindNavigationItems deliberately selects only fields required to render the
// public help navigation. Article content is fetched on demand by its detail API.
func (r *docPageRepository) FindNavigationItems(db *gorm.DB, cnd *sqls.Cnd) (list []models.DocPage) {
cnd.Find(db.Select("id", "parent_id", "title", "slug", "sort_no"), &list)
return
}
func (r *docPageRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.DocPage, paging *sqls.Paging) {
cnd.Find(db, &list)
paging = &sqls.Paging{Page: cnd.Paging.Page, Limit: cnd.Paging.Limit, Total: cnd.Count(db, &models.DocPage{})}
return
}
func (r *docPageRepository) Create(db *gorm.DB, item *models.DocPage) error {
return db.Create(item).Error
}
func (r *docPageRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.DocPage{}).Where("id = ?", id).Updates(columns).Error
}
func (r *docPageRepository) UpdateColumn(db *gorm.DB, id int64, column string, value any) error {
return db.Model(&models.DocPage{}).Where("id = ?", id).UpdateColumn(column, value).Error
}
func (r *docPageRepository) UpdateSort(db *gorm.DB, id int64, sortNo int) error {
return db.Model(&models.DocPage{}).Where("id = ?", id).UpdateColumn("sort_no", sortNo).Error
}
func (r *docPageRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.DocPage{}, "id = ?", id).Error
}
type supportCategoryRepository struct{}
func (r *supportCategoryRepository) Get(db *gorm.DB, id int64) *models.Category {
ret := &models.Category{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *supportCategoryRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Category) {
cnd.Find(db, &list)
return
}
func (r *supportCategoryRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Category, paging *sqls.Paging) {
cnd.Find(db, &list)
paging = &sqls.Paging{Page: cnd.Paging.Page, Limit: cnd.Paging.Limit, Total: cnd.Count(db, &models.Category{})}
return
}
func (r *supportCategoryRepository) Create(db *gorm.DB, item *models.Category) error {
return db.Create(item).Error
}
func (r *supportCategoryRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.Category{}).Where("id = ?", id).Updates(columns).Error
}
func (r *supportCategoryRepository) UpdateColumn(db *gorm.DB, id int64, column string, value any) error {
return db.Model(&models.Category{}).Where("id = ?", id).UpdateColumn(column, value).Error
}
func (r *supportCategoryRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.Category{}, "id = ?", id).Error
}
type supportPostRepository struct{}
func (r *supportPostRepository) Get(db *gorm.DB, id int64) *models.Post {
ret := &models.Post{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *supportPostRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Post) {
cnd.Find(db, &list)
return
}
func (r *supportPostRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Post, paging *sqls.Paging) {
cnd.Find(db, &list)
paging = &sqls.Paging{Page: cnd.Paging.Page, Limit: cnd.Paging.Limit, Total: cnd.Count(db, &models.Post{})}
return
}
func (r *supportPostRepository) Create(db *gorm.DB, item *models.Post) error {
return db.Create(item).Error
}
func (r *supportPostRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.Post{}).Where("id = ?", id).Updates(columns).Error
}
func (r *supportPostRepository) UpdateColumn(db *gorm.DB, id int64, column string, value any) error {
return db.Model(&models.Post{}).Where("id = ?", id).UpdateColumn(column, value).Error
}
type supportCommentRepository struct{}
func (r *supportCommentRepository) Get(db *gorm.DB, id int64) *models.Comment {
ret := &models.Comment{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *supportCommentRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Comment) {
cnd.Find(db, &list)
return
}
func (r *supportCommentRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Comment, paging *sqls.Paging) {
cnd.Find(db, &list)
paging = &sqls.Paging{Page: cnd.Paging.Page, Limit: cnd.Paging.Limit, Total: cnd.Count(db, &models.Comment{})}
return
}
func (r *supportCommentRepository) Create(db *gorm.DB, item *models.Comment) error {
return db.Create(item).Error
}
func (r *supportCommentRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.Comment{}).Where("id = ?", id).Updates(columns).Error
}
func (r *supportCommentRepository) UpdateColumn(db *gorm.DB, id int64, column string, value any) error {
return db.Model(&models.Comment{}).Where("id = ?", id).UpdateColumn(column, value).Error
}
type supportReactionRepository struct{}
func (r *supportReactionRepository) Get(db *gorm.DB, targetType string, targetID, userID int64, reactionType string) *models.Reaction {
ret := &models.Reaction{}
if err := db.First(ret, "target_type = ? AND target_id = ? AND user_id = ? AND reaction_type = ?", targetType, targetID, userID, reactionType).Error; err != nil {
return nil
}
return ret
}
func (r *supportReactionRepository) Create(db *gorm.DB, item *models.Reaction) error {
return db.Create(item).Error
}
func (r *supportReactionRepository) Delete(db *gorm.DB, targetType string, targetID, userID int64, reactionType string) error {
return db.Delete(&models.Reaction{}, "target_type = ? AND target_id = ? AND user_id = ? AND reaction_type = ?", targetType, targetID, userID, reactionType).Error
}
type supportCommentReportRepository struct{}
func (r *supportCommentReportRepository) Get(db *gorm.DB, commentID, userID int64) *models.CommentReport {
ret := &models.CommentReport{}
if err := db.First(ret, "comment_id = ? AND user_id = ?", commentID, userID).Error; err != nil {
return nil
}
return ret
}
func (r *supportCommentReportRepository) Create(db *gorm.DB, item *models.CommentReport) error {
return db.Create(item).Error
}

View File

@@ -0,0 +1,115 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var SystemConfigRepository = newSystemConfigRepository()
func newSystemConfigRepository() *systemConfigRepository {
return &systemConfigRepository{}
}
type systemConfigRepository struct {
}
func (r *systemConfigRepository) Get(db *gorm.DB, id int64) *models.SystemConfig {
ret := &models.SystemConfig{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *systemConfigRepository) Take(db *gorm.DB, where ...interface{}) *models.SystemConfig {
ret := &models.SystemConfig{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *systemConfigRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.SystemConfig) {
cnd.Find(db, &list)
return
}
func (r *systemConfigRepository) FindByGroupCode(db *gorm.DB, groupCode string) (list []models.SystemConfig) {
db.Where("group_code = ?", groupCode).Find(&list)
return
}
func (r *systemConfigRepository) FindByGroupAndKey(db *gorm.DB, groupCode, key string) *models.SystemConfig {
ret := &models.SystemConfig{}
if err := db.Where("group_code = ? and config_key = ?", groupCode, key).Take(ret).Error; err != nil {
return nil
}
return ret
}
func (r *systemConfigRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.SystemConfig {
ret := &models.SystemConfig{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *systemConfigRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.SystemConfig, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *systemConfigRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.SystemConfig, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.SystemConfig{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *systemConfigRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.SystemConfig) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *systemConfigRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *systemConfigRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.SystemConfig{})
}
func (r *systemConfigRepository) Create(db *gorm.DB, t *models.SystemConfig) (err error) {
err = db.Create(t).Error
return
}
func (r *systemConfigRepository) Update(db *gorm.DB, t *models.SystemConfig) (err error) {
err = db.Save(t).Error
return
}
func (r *systemConfigRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.SystemConfig{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *systemConfigRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.SystemConfig{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *systemConfigRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.SystemConfig{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var TagRepository = newTagRepository()
func newTagRepository() *tagRepository {
return &tagRepository{}
}
type tagRepository struct {
}
func (r *tagRepository) Get(db *gorm.DB, id int64) *models.Tag {
ret := &models.Tag{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *tagRepository) Take(db *gorm.DB, where ...interface{}) *models.Tag {
ret := &models.Tag{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *tagRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Tag) {
cnd.Find(db, &list)
return
}
func (r *tagRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Tag {
ret := &models.Tag{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *tagRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Tag, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *tagRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Tag, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Tag{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *tagRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Tag) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *tagRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *tagRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Tag{})
}
func (r *tagRepository) Create(db *gorm.DB, t *models.Tag) (err error) {
err = db.Create(t).Error
return
}
func (r *tagRepository) Update(db *gorm.DB, t *models.Tag) (err error) {
err = db.Save(t).Error
return
}
func (r *tagRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Tag{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *tagRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Tag{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *tagRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Tag{}, "id = ?", id)
}

View File

@@ -0,0 +1,120 @@
package repositories
import (
"agent-desk/internal/models"
"errors"
"time"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
var TicketNoSequenceRepository = newTicketNoSequenceRepository()
func newTicketNoSequenceRepository() *ticketNoSequenceRepository {
return &ticketNoSequenceRepository{}
}
type ticketNoSequenceRepository struct{}
func (r *ticketNoSequenceRepository) Get(db *gorm.DB, id int64) *models.TicketNoSequence {
ret := &models.TicketNoSequence{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *ticketNoSequenceRepository) Take(db *gorm.DB, where ...any) *models.TicketNoSequence {
ret := &models.TicketNoSequence{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *ticketNoSequenceRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketNoSequence) {
cnd.Find(db, &list)
return
}
func (r *ticketNoSequenceRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.TicketNoSequence {
ret := &models.TicketNoSequence{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *ticketNoSequenceRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.TicketNoSequence, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *ticketNoSequenceRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketNoSequence, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.TicketNoSequence{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *ticketNoSequenceRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.TicketNoSequence{})
}
func (r *ticketNoSequenceRepository) GetByDateKey(db *gorm.DB, dateKey string) *models.TicketNoSequence {
ret := &models.TicketNoSequence{}
if err := db.Take(ret, "date_key = ?", dateKey).Error; err != nil {
return nil
}
return ret
}
func (r *ticketNoSequenceRepository) GetByDateKeyForUpdate(db *gorm.DB, dateKey string) (*models.TicketNoSequence, error) {
ret := &models.TicketNoSequence{}
err := db.Clauses(clause.Locking{Strength: "UPDATE"}).Take(ret, "date_key = ?", dateKey).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
if err != nil {
return nil, err
}
return ret, nil
}
func (r *ticketNoSequenceRepository) Create(db *gorm.DB, t *models.TicketNoSequence) error {
return db.Create(t).Error
}
func (r *ticketNoSequenceRepository) Update(db *gorm.DB, t *models.TicketNoSequence) error {
return db.Save(t).Error
}
func (r *ticketNoSequenceRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.TicketNoSequence{}).Where("id = ?", id).Updates(columns).Error
}
func (r *ticketNoSequenceRepository) UpdateColumn(db *gorm.DB, id int64, name string, value any) error {
return db.Model(&models.TicketNoSequence{}).Where("id = ?", id).UpdateColumn(name, value).Error
}
func (r *ticketNoSequenceRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.TicketNoSequence{}, "id = ?", id)
}
func (r *ticketNoSequenceRepository) UpdateNextSeq(db *gorm.DB, id int64, currentSeq, nextSeq int64, updatedAt time.Time) (bool, error) {
result := db.Model(&models.TicketNoSequence{}).
Where("id = ? AND next_seq = ?", id, currentSeq).
Updates(map[string]any{
"next_seq": nextSeq,
"updated_at": updatedAt,
})
return result.RowsAffected == 1, result.Error
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var TicketProgressRepository = newTicketProgressRepository()
func newTicketProgressRepository() *ticketProgressRepository {
return &ticketProgressRepository{}
}
type ticketProgressRepository struct {
}
func (r *ticketProgressRepository) Get(db *gorm.DB, id int64) *models.TicketProgress {
ret := &models.TicketProgress{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *ticketProgressRepository) Take(db *gorm.DB, where ...any) *models.TicketProgress {
ret := &models.TicketProgress{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *ticketProgressRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketProgress) {
cnd.Find(db, &list)
return
}
func (r *ticketProgressRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.TicketProgress {
ret := &models.TicketProgress{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *ticketProgressRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.TicketProgress, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *ticketProgressRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketProgress, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.TicketProgress{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *ticketProgressRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...any) (list []models.TicketProgress) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *ticketProgressRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...any) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *ticketProgressRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.TicketProgress{})
}
func (r *ticketProgressRepository) Create(db *gorm.DB, t *models.TicketProgress) (err error) {
err = db.Create(t).Error
return
}
func (r *ticketProgressRepository) Update(db *gorm.DB, t *models.TicketProgress) (err error) {
err = db.Save(t).Error
return
}
func (r *ticketProgressRepository) Updates(db *gorm.DB, id int64, columns map[string]any) (err error) {
err = db.Model(&models.TicketProgress{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *ticketProgressRepository) UpdateColumn(db *gorm.DB, id int64, name string, value any) (err error) {
err = db.Model(&models.TicketProgress{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *ticketProgressRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.TicketProgress{}, "id = ?", id)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var TicketRepository = newTicketRepository()
func newTicketRepository() *ticketRepository {
return &ticketRepository{}
}
type ticketRepository struct {
}
func (r *ticketRepository) Get(db *gorm.DB, id int64) *models.Ticket {
ret := &models.Ticket{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *ticketRepository) Take(db *gorm.DB, where ...interface{}) *models.Ticket {
ret := &models.Ticket{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *ticketRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.Ticket) {
cnd.Find(db, &list)
return
}
func (r *ticketRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.Ticket {
ret := &models.Ticket{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *ticketRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.Ticket, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *ticketRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.Ticket, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.Ticket{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *ticketRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.Ticket) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *ticketRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *ticketRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.Ticket{})
}
func (r *ticketRepository) Create(db *gorm.DB, t *models.Ticket) (err error) {
err = db.Create(t).Error
return
}
func (r *ticketRepository) Update(db *gorm.DB, t *models.Ticket) (err error) {
err = db.Save(t).Error
return
}
func (r *ticketRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.Ticket{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *ticketRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.Ticket{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *ticketRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.Ticket{}, "id = ?", id)
}

View File

@@ -0,0 +1,74 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var TicketTagRepository = newTicketTagRepository()
func newTicketTagRepository() *ticketTagRepository {
return &ticketTagRepository{}
}
type ticketTagRepository struct{}
func (r *ticketTagRepository) Get(db *gorm.DB, id int64) *models.TicketTag {
ret := &models.TicketTag{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *ticketTagRepository) Take(db *gorm.DB, where ...interface{}) *models.TicketTag {
ret := &models.TicketTag{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *ticketTagRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketTag) {
cnd.Find(db, &list)
return
}
func (r *ticketTagRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.TicketTag {
ret := &models.TicketTag{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *ticketTagRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.TicketTag, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *ticketTagRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketTag, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.TicketTag{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *ticketTagRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.TicketTag{})
}
func (r *ticketTagRepository) Create(db *gorm.DB, t *models.TicketTag) error {
return db.Create(t).Error
}
func (r *ticketTagRepository) DeleteByTicketID(db *gorm.DB, ticketID int64) error {
return db.Where("ticket_id = ?", ticketID).Delete(&models.TicketTag{}).Error
}

View File

@@ -0,0 +1,84 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var TicketViewRepository = newTicketViewRepository()
func newTicketViewRepository() *ticketViewRepository {
return &ticketViewRepository{}
}
type ticketViewRepository struct {
}
func (r *ticketViewRepository) Get(db *gorm.DB, id int64) *models.TicketView {
ret := &models.TicketView{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *ticketViewRepository) Take(db *gorm.DB, where ...any) *models.TicketView {
ret := &models.TicketView{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *ticketViewRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketView) {
cnd.Find(db, &list)
return
}
func (r *ticketViewRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.TicketView {
ret := &models.TicketView{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *ticketViewRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.TicketView, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *ticketViewRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.TicketView, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.TicketView{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *ticketViewRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.TicketView{})
}
func (r *ticketViewRepository) Create(db *gorm.DB, t *models.TicketView) error {
return db.Create(t).Error
}
func (r *ticketViewRepository) Update(db *gorm.DB, t *models.TicketView) error {
return db.Save(t).Error
}
func (r *ticketViewRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.TicketView{}).Where("id = ?", id).Updates(columns).Error
}
func (r *ticketViewRepository) Delete(db *gorm.DB, id int64) error {
return db.Delete(&models.TicketView{}, "id = ?", id).Error
}

View File

@@ -0,0 +1,111 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/enums"
"strings"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var UserIdentityRepository = newUserIdentityRepository()
func newUserIdentityRepository() *userIdentityRepository {
return &userIdentityRepository{}
}
type userIdentityRepository struct {
}
func (r *userIdentityRepository) Get(db *gorm.DB, id int64) *models.UserIdentity {
ret := &models.UserIdentity{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *userIdentityRepository) Take(db *gorm.DB, where ...interface{}) *models.UserIdentity {
ret := &models.UserIdentity{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *userIdentityRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserIdentity) {
cnd.Find(db, &list)
return
}
func (r *userIdentityRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.UserIdentity {
ret := &models.UserIdentity{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *userIdentityRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.UserIdentity, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *userIdentityRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserIdentity, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.UserIdentity{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *userIdentityRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.UserIdentity) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *userIdentityRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *userIdentityRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.UserIdentity{})
}
func (r *userIdentityRepository) Create(db *gorm.DB, t *models.UserIdentity) (err error) {
err = db.Create(t).Error
return
}
func (r *userIdentityRepository) Update(db *gorm.DB, t *models.UserIdentity) (err error) {
err = db.Save(t).Error
return
}
func (r *userIdentityRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.UserIdentity{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *userIdentityRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.UserIdentity{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *userIdentityRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.UserIdentity{}, "id = ?", id)
}
func (r *userIdentityRepository) GetBy(db *gorm.DB, provider enums.ThirdProvider, corpId, userId string) *models.UserIdentity {
return r.FindOne(db, sqls.NewCnd().
Eq("provider", provider).
Eq("provider_corp_id", strings.TrimSpace(corpId)).
Eq("provider_user_id", strings.TrimSpace(userId)))
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var UserPermissionRepository = newUserPermissionRepository()
func newUserPermissionRepository() *userPermissionRepository {
return &userPermissionRepository{}
}
type userPermissionRepository struct {
}
func (r *userPermissionRepository) Get(db *gorm.DB, id int64) *models.UserPermission {
ret := &models.UserPermission{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *userPermissionRepository) Take(db *gorm.DB, where ...interface{}) *models.UserPermission {
ret := &models.UserPermission{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *userPermissionRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserPermission) {
cnd.Find(db, &list)
return
}
func (r *userPermissionRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.UserPermission {
ret := &models.UserPermission{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *userPermissionRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.UserPermission, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *userPermissionRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserPermission, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.UserPermission{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *userPermissionRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.UserPermission) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *userPermissionRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *userPermissionRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.UserPermission{})
}
func (r *userPermissionRepository) Create(db *gorm.DB, t *models.UserPermission) (err error) {
err = db.Create(t).Error
return
}
func (r *userPermissionRepository) Update(db *gorm.DB, t *models.UserPermission) (err error) {
err = db.Save(t).Error
return
}
func (r *userPermissionRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.UserPermission{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *userPermissionRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.UserPermission{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *userPermissionRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.UserPermission{}, "id = ?", id)
}

View File

@@ -0,0 +1,136 @@
package repositories
import (
"agent-desk/internal/models"
"strings"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var UserRepository = newUserRepository()
func newUserRepository() *userRepository {
return &userRepository{}
}
type userRepository struct {
}
func (r *userRepository) Get(db *gorm.DB, id int64) *models.User {
ret := &models.User{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *userRepository) Take(db *gorm.DB, where ...interface{}) *models.User {
ret := &models.User{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *userRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.User) {
cnd.Find(db, &list)
return
}
func (r *userRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.User {
ret := &models.User{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *userRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.User, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *userRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.User, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.User{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *userRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.User) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *userRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *userRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.User{})
}
func (r *userRepository) Create(db *gorm.DB, t *models.User) (err error) {
err = db.Create(t).Error
return
}
func (r *userRepository) Update(db *gorm.DB, t *models.User) (err error) {
err = db.Save(t).Error
return
}
func (r *userRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.User{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *userRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.User{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *userRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.User{}, "id = ?", id)
}
func (r *userRepository) FindByIds(db *gorm.DB, ids []int64) []models.User {
if len(ids) == 0 {
return []models.User{}
}
var list []models.User
db.Where("id IN ?", ids).Find(&list)
return list
}
func (r *userRepository) GetByUsername(db *gorm.DB, username string) *models.User {
username = strings.TrimSpace(username)
if username == "" {
return nil
}
return r.Take(db, "username = ?", username)
}
func (r *userRepository) GetByMobile(db *gorm.DB, mobile string) *models.User {
mobile = strings.TrimSpace(mobile)
if mobile == "" {
return nil
}
return r.Take(db, "mobile = ?", mobile)
}
func (r *userRepository) GetByEmail(db *gorm.DB, email string) *models.User {
email = strings.TrimSpace(email)
if email == "" {
return nil
}
return r.Take(db, "email = ?", email)
}

View File

@@ -0,0 +1,102 @@
package repositories
import (
"agent-desk/internal/models"
"agent-desk/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var UserRoleRepository = newUserRoleRepository()
func newUserRoleRepository() *userRoleRepository {
return &userRoleRepository{}
}
type userRoleRepository struct {
}
func (r *userRoleRepository) Get(db *gorm.DB, id int64) *models.UserRole {
ret := &models.UserRole{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *userRoleRepository) Take(db *gorm.DB, where ...interface{}) *models.UserRole {
ret := &models.UserRole{}
if err := db.Take(ret, where...).Error; err != nil {
return nil
}
return ret
}
func (r *userRoleRepository) Find(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserRole) {
cnd.Find(db, &list)
return
}
func (r *userRoleRepository) FindOne(db *gorm.DB, cnd *sqls.Cnd) *models.UserRole {
ret := &models.UserRole{}
if err := cnd.FindOne(db, &ret); err != nil {
return nil
}
return ret
}
func (r *userRoleRepository) FindPageByParams(db *gorm.DB, params *params.QueryParams) (list []models.UserRole, paging *sqls.Paging) {
return r.FindPageByCnd(db, &params.Cnd)
}
func (r *userRoleRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.UserRole, paging *sqls.Paging) {
cnd.Find(db, &list)
count := cnd.Count(db, &models.UserRole{})
paging = &sqls.Paging{
Page: cnd.Paging.Page,
Limit: cnd.Paging.Limit,
Total: count,
}
return
}
func (r *userRoleRepository) FindBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (list []models.UserRole) {
db.Raw(sqlStr, paramArr...).Scan(&list)
return
}
func (r *userRoleRepository) CountBySql(db *gorm.DB, sqlStr string, paramArr ...interface{}) (count int64) {
db.Raw(sqlStr, paramArr...).Count(&count)
return
}
func (r *userRoleRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
return cnd.Count(db, &models.UserRole{})
}
func (r *userRoleRepository) Create(db *gorm.DB, t *models.UserRole) (err error) {
err = db.Create(t).Error
return
}
func (r *userRoleRepository) Update(db *gorm.DB, t *models.UserRole) (err error) {
err = db.Save(t).Error
return
}
func (r *userRoleRepository) Updates(db *gorm.DB, id int64, columns map[string]interface{}) (err error) {
err = db.Model(&models.UserRole{}).Where("id = ?", id).Updates(columns).Error
return
}
func (r *userRoleRepository) UpdateColumn(db *gorm.DB, id int64, name string, value interface{}) (err error) {
err = db.Model(&models.UserRole{}).Where("id = ?", id).UpdateColumn(name, value).Error
return
}
func (r *userRoleRepository) Delete(db *gorm.DB, id int64) {
db.Delete(&models.UserRole{}, "id = ?", id)
}