init
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"affiliate_dash/internal/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SkinService struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewSkinService(db *gorm.DB) *SkinService {
|
||||
return &SkinService{db: db}
|
||||
}
|
||||
|
||||
type SkinListQuery struct {
|
||||
Page int
|
||||
Size int
|
||||
Keyword string
|
||||
Game string
|
||||
Category string
|
||||
Status *int
|
||||
}
|
||||
|
||||
func (s *SkinService) List(q SkinListQuery) ([]model.Skin, int64, error) {
|
||||
if q.Page < 1 {
|
||||
q.Page = 1
|
||||
}
|
||||
if q.Size < 1 || q.Size > 100 {
|
||||
q.Size = 20
|
||||
}
|
||||
tx := s.db.Model(&model.Skin{})
|
||||
if q.Keyword != "" {
|
||||
tx = tx.Where("name LIKE ?", "%"+q.Keyword+"%")
|
||||
}
|
||||
if q.Game != "" {
|
||||
tx = tx.Where("game = ?", q.Game)
|
||||
}
|
||||
if q.Category != "" {
|
||||
tx = tx.Where("category = ?", q.Category)
|
||||
}
|
||||
if q.Status != nil {
|
||||
tx = tx.Where("status = ?", *q.Status)
|
||||
}
|
||||
var total int64
|
||||
if err := tx.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var list []model.Skin
|
||||
err := tx.Order("id DESC").Offset((q.Page - 1) * q.Size).Limit(q.Size).Find(&list).Error
|
||||
return list, total, err
|
||||
}
|
||||
|
||||
func (s *SkinService) Get(id uint) (*model.Skin, error) {
|
||||
var skin model.Skin
|
||||
if err := s.db.First(&skin, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("皮肤不存在")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &skin, nil
|
||||
}
|
||||
|
||||
func (s *SkinService) Create(skin *model.Skin) error {
|
||||
return s.db.Create(skin).Error
|
||||
}
|
||||
|
||||
func (s *SkinService) Update(id uint, updates map[string]interface{}) error {
|
||||
res := s.db.Model(&model.Skin{}).Where("id = ?", id).Updates(updates)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
return errors.New("皮肤不存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SkinService) Delete(id uint) error {
|
||||
res := s.db.Delete(&model.Skin{}, id)
|
||||
if res.Error != nil {
|
||||
return res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
return errors.New("皮肤不存在")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SkinService) SeedDemo() error {
|
||||
var count int64
|
||||
s.db.Model(&model.Skin{}).Count(&count)
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
demos := []model.Skin{
|
||||
{Name: "龙之觉醒", Game: "王者荣耀", Category: "史诗", Price: 88, CostPrice: 50, Commission: 0.15, Stock: -1, Status: 1, Description: "史诗皮肤示例"},
|
||||
{Name: "星空旅人", Game: "和平精英", Category: "限定", Price: 128, CostPrice: 80, Commission: 0.12, Stock: 100, Status: 1, Description: "限定皮肤示例"},
|
||||
{Name: "暗夜骑士", Game: "英雄联盟", Category: "传说", Price: 199, CostPrice: 120, Commission: 0.10, Stock: 50, Status: 1, Description: "传说皮肤示例"},
|
||||
}
|
||||
return s.db.Create(&demos).Error
|
||||
}
|
||||
Reference in New Issue
Block a user