refactor(ticket): rename TicketNoService to TicketNoSequenceService and update references

This commit is contained in:
mlogclub
2026-05-03 12:20:44 +08:00
parent d75f9f05a7
commit ac0f25f10c
5 changed files with 87 additions and 93 deletions
@@ -1,73 +0,0 @@
package services
import (
"time"
"cs-agent/internal/models"
"cs-agent/internal/repositories"
"github.com/mlogclub/simple/sqls"
"github.com/mlogclub/simple/web/params"
"gorm.io/gorm"
)
var TicketNoSequenceService = newTicketNoSequenceService()
func newTicketNoSequenceService() *ticketNoSequenceService {
return &ticketNoSequenceService{}
}
type ticketNoSequenceService struct {
}
func (s *ticketNoSequenceService) Get(id int64) *models.TicketNoSequence {
return repositories.TicketNoSequenceRepository.Get(sqls.DB(), id)
}
func (s *ticketNoSequenceService) Take(where ...any) *models.TicketNoSequence {
return repositories.TicketNoSequenceRepository.Take(sqls.DB(), where...)
}
func (s *ticketNoSequenceService) Find(cnd *sqls.Cnd) []models.TicketNoSequence {
return repositories.TicketNoSequenceRepository.Find(sqls.DB(), cnd)
}
func (s *ticketNoSequenceService) FindOne(cnd *sqls.Cnd) *models.TicketNoSequence {
return repositories.TicketNoSequenceRepository.FindOne(sqls.DB(), cnd)
}
func (s *ticketNoSequenceService) FindPageByParams(params *params.QueryParams) (list []models.TicketNoSequence, paging *sqls.Paging) {
return repositories.TicketNoSequenceRepository.FindPageByParams(sqls.DB(), params)
}
func (s *ticketNoSequenceService) FindPageByCnd(cnd *sqls.Cnd) (list []models.TicketNoSequence, paging *sqls.Paging) {
return repositories.TicketNoSequenceRepository.FindPageByCnd(sqls.DB(), cnd)
}
func (s *ticketNoSequenceService) Count(cnd *sqls.Cnd) int64 {
return repositories.TicketNoSequenceRepository.Count(sqls.DB(), cnd)
}
func (s *ticketNoSequenceService) Create(t *models.TicketNoSequence) error {
return repositories.TicketNoSequenceRepository.Create(sqls.DB(), t)
}
func (s *ticketNoSequenceService) Update(t *models.TicketNoSequence) error {
return repositories.TicketNoSequenceRepository.Update(sqls.DB(), t)
}
func (s *ticketNoSequenceService) Updates(id int64, columns map[string]any) error {
return repositories.TicketNoSequenceRepository.Updates(sqls.DB(), id, columns)
}
func (s *ticketNoSequenceService) UpdateColumn(id int64, name string, value any) error {
return repositories.TicketNoSequenceRepository.UpdateColumn(sqls.DB(), id, name, value)
}
func (s *ticketNoSequenceService) Delete(id int64) {
repositories.TicketNoSequenceRepository.Delete(sqls.DB(), id)
}
func (s *ticketNoSequenceService) Next(db *gorm.DB, now time.Time) (string, error) {
return TicketNoService.Next(db, now)
}
+10 -10
View File
@@ -11,29 +11,29 @@ import (
"gorm.io/gorm"
)
var TicketNoService = newTicketNoService()
var TicketNoSequenceService = newTicketNoSequenceService()
var ticketNoSQLiteMu sync.Mutex
func newTicketNoService() *ticketNoService {
return &ticketNoService{}
func newTicketNoSequenceService() *ticketNoSequenceService {
return &ticketNoSequenceService{}
}
type ticketNoService struct{}
type ticketNoSequenceService struct {
ticketNoSQLiteMu sync.Mutex
}
func (s *ticketNoService) Next(tx *gorm.DB, now time.Time) (string, error) {
func (s *ticketNoSequenceService) Next(tx *gorm.DB, now time.Time) (string, error) {
if tx == nil {
return "", fmt.Errorf("ticket number transaction is required")
}
if tx.Dialector.Name() == "sqlite" {
ticketNoSQLiteMu.Lock()
defer ticketNoSQLiteMu.Unlock()
s.ticketNoSQLiteMu.Lock()
defer s.ticketNoSQLiteMu.Unlock()
return s.nextWithRetry(tx, now)
}
return s.nextWithRetry(tx, now)
}
func (s *ticketNoService) nextWithRetry(tx *gorm.DB, now time.Time) (string, error) {
func (s *ticketNoSequenceService) nextWithRetry(tx *gorm.DB, now time.Time) (string, error) {
dateKey := now.Format("20060102")
for attempt := 0; attempt < 100; attempt++ {
current, err := repositories.TicketNoSequenceRepository.GetByDateKeyForUpdate(tx, dateKey)
+1 -1
View File
@@ -199,7 +199,7 @@ func (s *ticketService) CreateTicket(req request.CreateTicketRequest, operator *
if err := withSQLiteTicketCreateLock(sqls.DB(), func() error {
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
ticketNo, err := TicketNoService.nextWithRetry(ctx.Tx, now)
ticketNo, err := TicketNoSequenceService.nextWithRetry(ctx.Tx, now)
if err != nil {
return err
}
+1 -1
View File
@@ -391,7 +391,7 @@ func TestTicketServiceTicketNoNextConcurrent(t *testing.T) {
go func() {
defer wg.Done()
err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
ticketNo, err := services.TicketNoService.Next(ctx.Tx, time.Now())
ticketNo, err := services.TicketNoSequenceService.Next(ctx.Tx, time.Now())
if err != nil {
return err
}