238 lines
5.3 KiB
Go
238 lines
5.3 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"psi/database"
|
|
"psi/models"
|
|
systemReq "psi/models/request"
|
|
systemRes "psi/models/response"
|
|
"time"
|
|
)
|
|
|
|
type ShopService struct{}
|
|
|
|
// GetShopList 获取店铺列表
|
|
func (s *ShopService) GetShopList(req systemReq.QueryShopRequest, db ...*gorm.DB) ([]systemRes.ShopResponse, int64, error) {
|
|
databaseConn := database.OptionalDB(db...)
|
|
|
|
if req.Page < 1 {
|
|
req.Page = 1
|
|
}
|
|
if req.PageSize < 1 || req.PageSize > 100 {
|
|
req.PageSize = 20
|
|
}
|
|
|
|
query := databaseConn.Model(&models.Shop{}).Where("del_flag = ?", 0)
|
|
|
|
if req.Keyword != "" {
|
|
query = query.Where("shop_name like ? OR shop_alias_name like ? OR shop_nike like ?",
|
|
"%"+req.Keyword+"%", "%"+req.Keyword+"%", "%"+req.Keyword+"%")
|
|
}
|
|
|
|
if req.ShopType != nil {
|
|
query = query.Where("shop_type = ?", *req.ShopType)
|
|
}
|
|
|
|
if req.Status != nil {
|
|
query = query.Where("status = ?", *req.Status)
|
|
}
|
|
|
|
if req.TenantID != "" {
|
|
query = query.Where("tenant_id = ?", req.TenantID)
|
|
}
|
|
|
|
var total int64
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, errors.New("查询店铺总数失败")
|
|
}
|
|
|
|
var shops []models.Shop
|
|
offset := (req.Page - 1) * req.PageSize
|
|
if err := query.Order("id DESC").Offset(offset).Limit(req.PageSize).Find(&shops).Error; err != nil {
|
|
return nil, 0, errors.New("查询店铺列表失败")
|
|
}
|
|
|
|
responses := make([]systemRes.ShopResponse, 0, len(shops))
|
|
for _, shop := range shops {
|
|
resp := systemRes.ConvertShopToResponse(shop)
|
|
responses = append(responses, resp)
|
|
}
|
|
|
|
return responses, total, nil
|
|
}
|
|
|
|
func (s *ShopService) GetShopByID(id int64, db ...*gorm.DB) (*systemRes.ShopResponse, error) {
|
|
databaseConn := database.OptionalDB(db...)
|
|
|
|
var shop models.Shop
|
|
if err := databaseConn.Where("id = ? AND del_flag = ?", id, 0).First(&shop).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, errors.New("店铺不存在")
|
|
}
|
|
return nil, errors.New("查询店铺失败")
|
|
}
|
|
|
|
resp := systemRes.ConvertShopToResponse(shop)
|
|
return &resp, nil
|
|
}
|
|
|
|
func (s *ShopService) CreateShop(req systemReq.CreateShopRequest, db ...*gorm.DB) (int64, error) {
|
|
databaseConn := database.OptionalDB(db...)
|
|
|
|
now := time.Now().Unix()
|
|
shop := models.Shop{
|
|
MallID: req.MallID,
|
|
ShopType: req.ShopType,
|
|
ShopAliasName: req.ShopAliasName,
|
|
Status: 0,
|
|
DelFlag: 0,
|
|
CreateTime: now,
|
|
}
|
|
|
|
if req.ShopType == 0 {
|
|
shop.ShopType = 1
|
|
}
|
|
|
|
if err := databaseConn.Create(&shop).Error; err != nil {
|
|
return 0, errors.New("创建店铺失败")
|
|
}
|
|
|
|
return shop.ID, nil
|
|
}
|
|
|
|
func (s *ShopService) UpdateShop(req systemReq.UpdateShopRequest, db ...*gorm.DB) error {
|
|
databaseConn := database.OptionalDB(db...)
|
|
|
|
var shop models.Shop
|
|
if err := databaseConn.Where("id = ? AND del_flag = ?", req.ID, 0).First(&shop).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.New("店铺不存在")
|
|
}
|
|
return errors.New("查询店铺失败")
|
|
}
|
|
|
|
updates := make(map[string]interface{})
|
|
updates["update_time"] = time.Now().Unix()
|
|
|
|
if req.MallID != nil {
|
|
updates["mall_id"] = *req.MallID
|
|
}
|
|
|
|
if req.ShopNike != "" {
|
|
updates["shop_nike"] = req.ShopNike
|
|
}
|
|
|
|
if req.ShopType != nil {
|
|
updates["shop_type"] = *req.ShopType
|
|
}
|
|
|
|
if req.ShopGroup != "" {
|
|
updates["shop_group"] = req.ShopGroup
|
|
}
|
|
|
|
if req.ShopName != "" {
|
|
updates["shop_name"] = req.ShopName
|
|
}
|
|
|
|
if req.ShopAliasName != "" {
|
|
updates["shop_alias_name"] = req.ShopAliasName
|
|
}
|
|
|
|
if req.ShopAuthorize != "" {
|
|
updates["shop_authorize"] = req.ShopAuthorize
|
|
}
|
|
|
|
if req.ExpirationTime != nil {
|
|
updates["expiration_time"] = *req.ExpirationTime
|
|
}
|
|
|
|
if req.ShopKey != "" {
|
|
updates["shop_key"] = req.ShopKey
|
|
}
|
|
|
|
if req.Token != "" {
|
|
updates["token"] = req.Token
|
|
}
|
|
|
|
if req.RefreshToken != "" {
|
|
updates["refresh_token"] = req.RefreshToken
|
|
}
|
|
|
|
if req.Status != nil {
|
|
updates["status"] = *req.Status
|
|
}
|
|
|
|
if req.TenantID != "" {
|
|
updates["tenant_id"] = req.TenantID
|
|
}
|
|
|
|
if req.UserID != nil {
|
|
updates["user_id"] = *req.UserID
|
|
}
|
|
|
|
if req.Account != "" {
|
|
updates["account"] = req.Account
|
|
}
|
|
|
|
if req.Password != "" {
|
|
updates["password"] = req.Password
|
|
}
|
|
|
|
if req.IsSynOrder != nil {
|
|
updates["is_syn_order"] = *req.IsSynOrder
|
|
}
|
|
|
|
if req.SkuSpec != "" {
|
|
updates["sku_spec"] = req.SkuSpec
|
|
}
|
|
|
|
if req.ShopExpirationTime != nil {
|
|
updates["shop_expiration_time"] = *req.ShopExpirationTime
|
|
}
|
|
|
|
if req.IsExpiration != nil {
|
|
updates["is_expiration"] = *req.IsExpiration
|
|
}
|
|
|
|
if req.PublishType != nil {
|
|
updates["publish_type"] = *req.PublishType
|
|
}
|
|
|
|
if req.Deregulation != nil {
|
|
updates["deregulation"] = *req.Deregulation
|
|
}
|
|
|
|
if err := databaseConn.Model(&shop).Updates(updates).Error; err != nil {
|
|
return errors.New("更新店铺失败")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *ShopService) DeleteShop(id int64, db ...*gorm.DB) error {
|
|
databaseConn := database.OptionalDB(db...)
|
|
|
|
if id == 0 {
|
|
return errors.New("店铺ID不能为空")
|
|
}
|
|
|
|
var shop models.Shop
|
|
if err := databaseConn.Where("id = ? AND del_flag = ?", id, 0).First(&shop).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return errors.New("店铺不存在")
|
|
}
|
|
return errors.New("查询店铺失败")
|
|
}
|
|
|
|
now := time.Now().Unix()
|
|
if err := databaseConn.Model(&shop).Updates(map[string]interface{}{
|
|
"del_flag": 1,
|
|
"update_time": now,
|
|
}).Error; err != nil {
|
|
return errors.New("删除店铺失败")
|
|
}
|
|
|
|
return nil
|
|
}
|