实现多商户履约平台基础

This commit is contained in:
yml2213
2026-07-30 12:02:32 +08:00
parent dde6f2c269
commit 83429456a6
41 changed files with 5487 additions and 504 deletions
+27 -14
View File
@@ -17,12 +17,13 @@ func NewSkinService(db *gorm.DB) *SkinService {
}
type SkinListQuery struct {
Page int
Size int
Keyword string
Game string
Category string
Status *int
MerchantID uint
Page int
Size int
Keyword string
Game string
Category string
Status *int
}
func (s *SkinService) List(q SkinListQuery) ([]model.Skin, int64, error) {
@@ -33,6 +34,9 @@ func (s *SkinService) List(q SkinListQuery) ([]model.Skin, int64, error) {
q.Size = 20
}
tx := s.db.Model(&model.Skin{})
if q.MerchantID != 0 {
tx = tx.Where("merchant_id = ?", q.MerchantID)
}
if q.Keyword != "" {
like := "%" + q.Keyword + "%"
tx = tx.Where("name LIKE ? OR sku LIKE ?", like, like)
@@ -55,9 +59,13 @@ func (s *SkinService) List(q SkinListQuery) ([]model.Skin, int64, error) {
return list, total, err
}
func (s *SkinService) Get(id uint) (*model.Skin, error) {
func (s *SkinService) Get(merchantID, id uint) (*model.Skin, error) {
var skin model.Skin
if err := s.db.First(&skin, id).Error; err != nil {
tx := s.db.Where("id = ?", id)
if merchantID != 0 {
tx = tx.Where("merchant_id = ?", merchantID)
}
if err := tx.First(&skin).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("皮肤不存在")
}
@@ -70,8 +78,8 @@ 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)
func (s *SkinService) Update(merchantID, id uint, updates map[string]interface{}) error {
res := s.db.Model(&model.Skin{}).Where("id = ? AND merchant_id = ?", id, merchantID).Updates(updates)
if res.Error != nil {
return res.Error
}
@@ -81,8 +89,8 @@ func (s *SkinService) Update(id uint, updates map[string]interface{}) error {
return nil
}
func (s *SkinService) Delete(id uint) error {
res := s.db.Delete(&model.Skin{}, id)
func (s *SkinService) Delete(merchantID, id uint) error {
res := s.db.Where("merchant_id = ?", merchantID).Delete(&model.Skin{}, id)
if res.Error != nil {
return res.Error
}
@@ -94,12 +102,16 @@ func (s *SkinService) Delete(id uint) error {
// SeedCatalog 按 sku 幂等导入商品目录(已存在则跳过)
func (s *SkinService) SeedCatalog() error {
var merchant model.Merchant
if err := s.db.Where("code = ?", "self-operated").First(&merchant).Error; err != nil {
return err
}
// 清理早期无 sku 的演示数据,避免唯一索引冲突
_ = s.db.Where("sku = ? OR sku IS NULL", "").Delete(&model.Skin{}).Error
_ = s.db.Where("merchant_id = ? AND (sku = ? OR sku IS NULL)", merchant.ID, "").Delete(&model.Skin{}).Error
for _, item := range peaceEliteCatalog {
var existing model.Skin
err := s.db.Where("sku = ?", item.SKU).First(&existing).Error
err := s.db.Where("merchant_id = ? AND sku = ?", merchant.ID, item.SKU).First(&existing).Error
if err == nil {
// 已存在:仅同步默认佣金为 0(不改价格等业务字段)
if existing.Commission != 0 {
@@ -111,6 +123,7 @@ func (s *SkinService) SeedCatalog() error {
return err
}
skin := model.Skin{
MerchantID: merchant.ID,
Name: item.Name,
SKU: item.SKU,
Game: "和平精英",