Files
erp/service/data/live_account_service.go

134 lines
3.4 KiB
Go
Raw Normal View History

package data
import (
"context"
consts "erp/consts/data"
dao "erp/dao/data"
dto "erp/model/dto/data"
entity "erp/model/entity/data"
"errors"
"github.com/gogf/gf/v2/util/gconv"
)
type liveAccountService struct{}
// LiveAccount 直播账号服务
var LiveAccount = new(liveAccountService)
// Create 创建直播账号
func (s *liveAccountService) Create(ctx context.Context, req *dto.CreateLiveAccountReq) (res *dto.CreateLiveAccountRes, err error) {
// 检查账号ID是否重复
existAccount, err := dao.LiveAccount.GetByAccountId(ctx, req.AccountId)
if err != nil {
return nil, err
}
if existAccount != nil {
return nil, errors.New("账号ID已存在")
}
// 插入数据库
id, err := dao.LiveAccount.Insert(ctx, req)
if err != nil {
return
}
res = &dto.CreateLiveAccountRes{
Id: id,
}
return
}
// List 获取直播账号列表
func (s *liveAccountService) List(ctx context.Context, req *dto.ListLiveAccountReq) (res *dto.ListLiveAccountRes, err error) {
accountList, total, err := dao.LiveAccount.List(ctx, req)
if err != nil {
return
}
// 组装响应数据
list := make([]dto.LiveAccountItem, 0, len(accountList))
for _, item := range accountList {
list = append(list, dto.LiveAccountItem{
Id: item.Id,
Platform: item.Platform,
AccountName: item.AccountName,
AccountId: item.AccountId,
Status: item.Status,
StatusName: consts.AnchorStatus(item.Status).String(),
Remark: item.Remark,
CreatedAt: item.CreatedAt.Unix(),
UpdatedAt: item.UpdatedAt.Unix(),
})
}
res = &dto.ListLiveAccountRes{
List: list,
Total: total,
}
return
}
// GetOne 获取单个直播账号
func (s *liveAccountService) GetOne(ctx context.Context, req *dto.GetLiveAccountReq) (res *dto.GetLiveAccountRes, err error) {
account, err := dao.LiveAccount.GetOne(ctx, req)
if err != nil {
return
}
var accountEntity *entity.LiveAccount
if err = gconv.Struct(account, &accountEntity); err != nil {
return
}
return &dto.GetLiveAccountRes{
LiveAccount: accountEntity,
}, nil
}
// Update 更新直播账号
func (s *liveAccountService) Update(ctx context.Context, req *dto.UpdateLiveAccountReq) (err error) {
// 检查直播账号是否存在
exist, err := dao.LiveAccount.GetOne(ctx, &dto.GetLiveAccountReq{Id: req.Id})
if err != nil || exist == nil {
return errors.New("直播账号不存在")
}
// 如果修改了账号ID检查新账号ID是否重复
if req.AccountId != "" && req.AccountId != exist.AccountId {
existAccount, err := dao.LiveAccount.GetByAccountId(ctx, req.AccountId)
if err != nil {
return err
}
if existAccount != nil {
return errors.New("账号ID已存在")
}
}
_, err = dao.LiveAccount.Update(ctx, req)
return
}
// UpdateStatus 更新直播账号状态
func (s *liveAccountService) UpdateStatus(ctx context.Context, req *dto.UpdateLiveAccountStatusReq) (err error) {
_, err = dao.LiveAccount.UpdateStatus(ctx, req.Id, int(req.Status))
return
}
// Delete 删除直播账号
func (s *liveAccountService) Delete(ctx context.Context, req *dto.DeleteLiveAccountReq) (err error) {
// 检查是否存在关联的排班
accountId := int(req.Id)
schedules, _, err := dao.Schedule.List(ctx, &dto.ListScheduleReq{
AccountId: &accountId,
})
if err != nil {
return err
}
if len(schedules) > 0 {
return errors.New("该账号存在排班记录,无法删除")
}
_, err = dao.LiveAccount.Delete(ctx, req)
return
}