95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
// Package service 业务逻辑层
|
|
package service
|
|
|
|
import (
|
|
"time"
|
|
|
|
"server/modules/user/dto"
|
|
"server/modules/user/entity"
|
|
"server/modules/user/mapper"
|
|
)
|
|
|
|
type PlatformUserService struct {
|
|
mapper *mapper.PlatformUserMapper
|
|
}
|
|
|
|
func NewPlatformUserService() *PlatformUserService {
|
|
return &PlatformUserService{mapper: mapper.NewPlatformUserMapper()}
|
|
}
|
|
|
|
// List 获取平台用户列表
|
|
func (s *PlatformUserService) List(page, size int) ([]entity.PlatformUser, int64, error) {
|
|
return s.mapper.FindAll(page, size)
|
|
}
|
|
|
|
// GetByID 获取平台用户详情
|
|
func (s *PlatformUserService) GetByID(id int64) (*entity.PlatformUser, error) {
|
|
return s.mapper.FindByID(id)
|
|
}
|
|
|
|
// Create 创建平台用户
|
|
func (s *PlatformUserService) Create(req *dto.CreatePlatformUserRequest) (*entity.PlatformUser, error) {
|
|
item := &entity.PlatformUser{
|
|
UserID: req.UserID,
|
|
PlatformType: req.PlatformType,
|
|
PlatformOpenID: req.PlatformOpenID,
|
|
PlatformUnionID: req.PlatformUnionID,
|
|
PlatformSessionKey: req.PlatformSessionKey,
|
|
PlatformExtra: req.PlatformExtra,
|
|
LastLoginTime: req.LastLoginTime,
|
|
DelFlag: 0,
|
|
}
|
|
if err := s.mapper.Create(item); err != nil {
|
|
return nil, err
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
// Update 更新平台用户
|
|
func (s *PlatformUserService) Update(id int64, req *dto.UpdatePlatformUserRequest) (*entity.PlatformUser, error) {
|
|
fields := make(map[string]interface{})
|
|
if req.UserID != nil {
|
|
fields["user_id"] = *req.UserID
|
|
}
|
|
if req.PlatformType != nil {
|
|
fields["platform_type"] = *req.PlatformType
|
|
}
|
|
if req.PlatformOpenID != nil {
|
|
fields["platform_openid"] = *req.PlatformOpenID
|
|
}
|
|
if req.PlatformUnionID != nil {
|
|
fields["platform_unionid"] = *req.PlatformUnionID
|
|
}
|
|
if req.PlatformSessionKey != nil {
|
|
fields["platform_session_key"] = *req.PlatformSessionKey
|
|
}
|
|
if req.PlatformExtra != nil {
|
|
fields["platform_extra"] = req.PlatformExtra
|
|
}
|
|
if req.LastLoginTime != nil {
|
|
fields["last_login_time"] = *req.LastLoginTime
|
|
}
|
|
if len(fields) == 0 {
|
|
return s.mapper.FindByID(id)
|
|
}
|
|
fields["update_time"] = time.Now()
|
|
if err := s.mapper.UpdateFields(id, fields); err != nil {
|
|
return nil, err
|
|
}
|
|
return s.mapper.FindByID(id)
|
|
}
|
|
|
|
// UpdateFields 动态字段更新
|
|
func (s *PlatformUserService) UpdateFields(id int64, fields map[string]interface{}) error {
|
|
if len(fields) == 0 {
|
|
return nil
|
|
}
|
|
fields["update_time"] = time.Now()
|
|
return s.mapper.UpdateFields(id, fields)
|
|
}
|
|
|
|
// Delete 删除平台用户
|
|
func (s *PlatformUserService) Delete(id int64) error {
|
|
return s.mapper.Delete(id)
|
|
}
|