低空智控平台 后端go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

105 lines
3.6 KiB

package service
import (
"errors"
"time"
"gorm.io/gorm"
"laic-backend/common"
"laic-backend/logger"
"laic-backend/model"
"laic-backend/vo"
)
type AccountService struct{}
var DefaultAccountService = &AccountService{}
func (s *AccountService) GetProfile(userID int64) (*vo.ProfileVO, *common.BusiError) {
var user model.User
if err := common.DB.Select("id", "name", "phone", "email", "role", "status", "created_at", "updated_at").First(&user, userID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, common.ErrUserNotFound
}
return nil, common.ErrInternal
}
return profileVO(&user), nil
}
func profileVO(user *model.User) *vo.ProfileVO {
return &vo.ProfileVO{
ID: user.ID, Name: user.Name, Phone: user.Phone, Email: user.Email,
Role: user.Role, Status: user.Status, CreatedAt: user.CreatedAt, UpdatedAt: user.UpdatedAt,
}
}
// UpdateProfile 更新个人资料(姓名/邮箱)
func (s *AccountService) UpdateProfile(userID int64, req *vo.ProfileUpdateReq) (*vo.ProfileVO, *common.BusiError) {
var user model.User
if err := common.DB.First(&user, userID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, common.ErrUserNotFound
}
return nil, common.ErrInternal
}
updates := map[string]any{"updated_at": time.Now()}
if req.Name != "" {
updates["name"] = req.Name
}
if req.Email != "" {
updates["email"] = req.Email
}
if err := common.DB.Model(&user).Updates(updates).Error; err != nil {
logger.ERROR("更新个人资料失败", err)
return nil, common.ErrInternal
}
if err := common.DB.First(&user, userID).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, common.ErrUserNotFound
}
return nil, common.ErrInternal
}
return profileVO(&user), nil
}
// GetTrafficBalance 查询云媒体流量余额
func (s *AccountService) GetTrafficBalance(userID int64) (*vo.TrafficBalanceVO, *common.BusiError) {
return DefaultBillingService.GetBalance(userID)
}
// GetUsagePage 流量消费流水分页
func (s *AccountService) GetUsagePage(userID int64, req *vo.UsagePageReq) (*common.PageResponse[model.TrafficUsageLog], *common.BusiError) {
return DefaultBillingService.GetUsagePage(userID, req)
}
// GetOrderPage 流量订单分页
func (s *AccountService) GetOrderPage(userID int64, req *vo.OrderPageReq) (*common.PageResponse[model.TrafficOrder], *common.BusiError) {
return DefaultBillingService.GetOrderPage(userID, req)
}
// CreateTrafficOrder 创建流量充值订单
func (s *AccountService) CreateTrafficOrder(userID int64, req *vo.TrafficOrderCreateReq) (*model.TrafficOrder, *common.BusiError) {
return DefaultBillingService.CreateOrder(userID, req.AmountGb)
}
// PayTrafficOrder 订单支付入账(admin)
func (s *AccountService) PayTrafficOrder(orderID int64) (*model.TrafficOrder, *common.BusiError) {
return DefaultBillingService.PayOrder(orderID)
}
// ListSimCards SIM 卡列表
func (s *AccountService) ListSimCards(userID int64, req *vo.SimCardPageReq) (*common.PageResponse[model.SimCard], *common.BusiError) {
return DefaultBillingService.ListSimCards(userID, req)
}
// GetSimRechargeLogPage SIM 卡充值记录分页
func (s *AccountService) GetSimRechargeLogPage(userID int64, req *vo.SimRechargeLogPageReq) (*common.PageResponse[model.SimRechargeLog], *common.BusiError) {
return DefaultBillingService.GetSimRechargeLogPage(userID, req)
}
// RechargeSimCard SIM 卡充值
func (s *AccountService) RechargeSimCard(userID, simCardID int64, req *vo.SimRechargeReq) (*model.SimRechargeLog, *common.BusiError) {
return DefaultBillingService.RechargeSimCard(userID, simCardID, req.AmountGb)
}