排班管理、主播管理、直播账号管理
This commit is contained in:
133
service/data/anchor_service.go
Normal file
133
service/data/anchor_service.go
Normal file
@@ -0,0 +1,133 @@
|
||||
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 anchorService struct{}
|
||||
|
||||
// Anchor 主播服务
|
||||
var Anchor = new(anchorService)
|
||||
|
||||
// Create 创建主播
|
||||
func (s *anchorService) Create(ctx context.Context, req *dto.CreateAnchorReq) (res *dto.CreateAnchorRes, err error) {
|
||||
// 检查工号是否重复
|
||||
existAnchor, err := dao.Anchor.GetByCode(ctx, req.Code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if existAnchor != nil {
|
||||
return nil, errors.New("工号已存在")
|
||||
}
|
||||
|
||||
// 插入数据库
|
||||
id, err := dao.Anchor.Insert(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = &dto.CreateAnchorRes{
|
||||
Id: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取主播列表
|
||||
func (s *anchorService) List(ctx context.Context, req *dto.ListAnchorReq) (res *dto.ListAnchorRes, err error) {
|
||||
anchorList, total, err := dao.Anchor.List(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装响应数据
|
||||
list := make([]dto.AnchorItem, 0, len(anchorList))
|
||||
for _, item := range anchorList {
|
||||
list = append(list, dto.AnchorItem{
|
||||
Id: item.Id,
|
||||
Name: item.Name,
|
||||
Phone: item.Phone,
|
||||
Code: item.Code,
|
||||
Status: item.Status,
|
||||
StatusName: consts.AnchorStatus(item.Status).String(),
|
||||
Remark: item.Remark,
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
UpdatedAt: item.UpdatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
res = &dto.ListAnchorRes{
|
||||
List: list,
|
||||
Total: total,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个主播
|
||||
func (s *anchorService) GetOne(ctx context.Context, req *dto.GetAnchorReq) (res *dto.GetAnchorRes, err error) {
|
||||
anchor, err := dao.Anchor.GetOne(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var anchorEntity *entity.Anchor
|
||||
if err = gconv.Struct(anchor, &anchorEntity); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return &dto.GetAnchorRes{
|
||||
Anchor: anchorEntity,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新主播
|
||||
func (s *anchorService) Update(ctx context.Context, req *dto.UpdateAnchorReq) (err error) {
|
||||
// 检查主播是否存在
|
||||
exist, err := dao.Anchor.GetOne(ctx, &dto.GetAnchorReq{Id: req.Id})
|
||||
if err != nil || exist == nil {
|
||||
return errors.New("主播不存在")
|
||||
}
|
||||
|
||||
// 如果修改了工号,检查新工号是否重复
|
||||
if req.Code != "" && req.Code != exist.Code {
|
||||
existAnchor, err := dao.Anchor.GetByCode(ctx, req.Code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if existAnchor != nil {
|
||||
return errors.New("工号已存在")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dao.Anchor.Update(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新主播状态
|
||||
func (s *anchorService) UpdateStatus(ctx context.Context, req *dto.UpdateAnchorStatusReq) (err error) {
|
||||
_, err = dao.Anchor.UpdateStatus(ctx, req.Id, int(req.Status))
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除主播
|
||||
func (s *anchorService) Delete(ctx context.Context, req *dto.DeleteAnchorReq) (err error) {
|
||||
// 检查是否存在关联的排班
|
||||
anchorId := int(req.Id)
|
||||
schedules, _, err := dao.Schedule.List(ctx, &dto.ListScheduleReq{
|
||||
AnchorId: &anchorId,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(schedules) > 0 {
|
||||
return errors.New("该主播存在排班记录,无法删除")
|
||||
}
|
||||
|
||||
_, err = dao.Anchor.Delete(ctx, req)
|
||||
return
|
||||
}
|
||||
133
service/data/live_account_service.go
Normal file
133
service/data/live_account_service.go
Normal file
@@ -0,0 +1,133 @@
|
||||
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
|
||||
}
|
||||
226
service/data/schedule_service.go
Normal file
226
service/data/schedule_service.go
Normal file
@@ -0,0 +1,226 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"context"
|
||||
consts "erp/consts/data"
|
||||
dao "erp/dao/data"
|
||||
dto "erp/model/dto/data"
|
||||
entity "erp/model/entity/data"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
type scheduleService struct{}
|
||||
|
||||
// Schedule 排班服务
|
||||
var Schedule = new(scheduleService)
|
||||
|
||||
// Create 创建排班
|
||||
func (s *scheduleService) Create(ctx context.Context, req *dto.CreateScheduleReq) (res *dto.CreateScheduleRes, err error) {
|
||||
// 检查开始时间是否小于结束时间
|
||||
if req.StartTime.After(req.EndTime) || req.StartTime.Equal(req.EndTime) {
|
||||
return nil, errors.New("开始时间必须早于结束时间")
|
||||
}
|
||||
|
||||
// 检查主播是否存在
|
||||
_, err = dao.Anchor.GetOne(ctx, &dto.GetAnchorReq{Id: int64(req.AnchorId)})
|
||||
if err != nil {
|
||||
return nil, errors.New("主播不存在")
|
||||
}
|
||||
|
||||
// 检查直播账号是否存在
|
||||
_, err = dao.LiveAccount.GetOne(ctx, &dto.GetLiveAccountReq{Id: int64(req.AccountId)})
|
||||
if err != nil {
|
||||
return nil, errors.New("直播账号不存在")
|
||||
}
|
||||
|
||||
// 检查时间冲突
|
||||
conflictCount, err := dao.Schedule.CheckTimeConflict(ctx, req.AnchorId, req.StartTime, req.EndTime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if conflictCount > 0 {
|
||||
return nil, errors.New("该主播在此时间段已有排班")
|
||||
}
|
||||
|
||||
// 插入数据库
|
||||
id, err := dao.Schedule.Insert(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
res = &dto.CreateScheduleRes{
|
||||
Id: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// List 获取排班列表
|
||||
func (s *scheduleService) List(ctx context.Context, req *dto.ListScheduleReq) (res *dto.ListScheduleRes, err error) {
|
||||
scheduleList, total, err := dao.Schedule.List(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装响应数据
|
||||
list := make([]dto.ScheduleItem, 0, len(scheduleList))
|
||||
for _, item := range scheduleList {
|
||||
// 获取主播信息
|
||||
anchorName := ""
|
||||
anchor, _ := dao.Anchor.GetOne(ctx, &dto.GetAnchorReq{Id: int64(item.AnchorId)})
|
||||
if anchor != nil {
|
||||
anchorName = anchor.Name
|
||||
}
|
||||
|
||||
// 获取账号信息
|
||||
accountName := ""
|
||||
platform := ""
|
||||
account, _ := dao.LiveAccount.GetOne(ctx, &dto.GetLiveAccountReq{Id: int64(item.AccountId)})
|
||||
if account != nil {
|
||||
accountName = account.AccountName
|
||||
platform = account.Platform
|
||||
}
|
||||
|
||||
list = append(list, dto.ScheduleItem{
|
||||
Id: item.Id,
|
||||
AnchorId: item.AnchorId,
|
||||
AnchorName: anchorName,
|
||||
AccountId: item.AccountId,
|
||||
AccountName: accountName,
|
||||
Platform: platform,
|
||||
StartTime: item.StartTime.Unix(),
|
||||
EndTime: item.EndTime.Unix(),
|
||||
Status: item.Status,
|
||||
StatusName: consts.ScheduleStatus(item.Status).String(),
|
||||
ProductId: item.ProductId,
|
||||
OrderId: item.OrderId,
|
||||
Remark: item.Remark,
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
UpdatedAt: item.UpdatedAt.Unix(),
|
||||
})
|
||||
}
|
||||
|
||||
res = &dto.ListScheduleRes{
|
||||
List: list,
|
||||
Total: total,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetOne 获取单个排班
|
||||
func (s *scheduleService) GetOne(ctx context.Context, req *dto.GetScheduleReq) (res *dto.GetScheduleRes, err error) {
|
||||
schedule, err := dao.Schedule.GetOne(ctx, req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var scheduleEntity *entity.Schedule
|
||||
if err = gconv.Struct(schedule, &scheduleEntity); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return &dto.GetScheduleRes{
|
||||
Schedule: scheduleEntity,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Update 更新排班
|
||||
func (s *scheduleService) Update(ctx context.Context, req *dto.UpdateScheduleReq) (err error) {
|
||||
// 检查排班是否存在
|
||||
exist, err := dao.Schedule.GetOne(ctx, &dto.GetScheduleReq{Id: req.Id})
|
||||
if err != nil || exist == nil {
|
||||
return errors.New("排班不存在")
|
||||
}
|
||||
|
||||
// 如果修改了时间,检查时间合法性
|
||||
startTime := exist.StartTime
|
||||
endTime := exist.EndTime
|
||||
if req.StartTime != nil {
|
||||
startTime = *req.StartTime
|
||||
}
|
||||
if req.EndTime != nil {
|
||||
endTime = *req.EndTime
|
||||
}
|
||||
if startTime.After(endTime) || startTime.Equal(endTime) {
|
||||
return errors.New("开始时间必须早于结束时间")
|
||||
}
|
||||
|
||||
// 如果修改了主播或时间,检查时间冲突
|
||||
anchorId := exist.AnchorId
|
||||
if req.AnchorId != nil {
|
||||
anchorId = *req.AnchorId
|
||||
// 检查主播是否存在
|
||||
_, err = dao.Anchor.GetOne(ctx, &dto.GetAnchorReq{Id: int64(anchorId)})
|
||||
if err != nil {
|
||||
return errors.New("主播不存在")
|
||||
}
|
||||
}
|
||||
|
||||
conflictCount, err := dao.Schedule.CheckTimeConflict(ctx, anchorId, startTime, endTime, req.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if conflictCount > 0 {
|
||||
return errors.New("该主播在此时间段已有排班")
|
||||
}
|
||||
|
||||
// 如果修改了账号,检查账号是否存在
|
||||
if req.AccountId != nil {
|
||||
_, err = dao.LiveAccount.GetOne(ctx, &dto.GetLiveAccountReq{Id: int64(*req.AccountId)})
|
||||
if err != nil {
|
||||
return errors.New("直播账号不存在")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dao.Schedule.Update(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateStatus 更新排班状态
|
||||
func (s *scheduleService) UpdateStatus(ctx context.Context, req *dto.UpdateScheduleStatusReq) (err error) {
|
||||
_, err = dao.Schedule.UpdateStatus(ctx, req.Id, int(req.Status))
|
||||
return
|
||||
}
|
||||
|
||||
// Delete 删除排班
|
||||
func (s *scheduleService) Delete(ctx context.Context, req *dto.DeleteScheduleReq) (err error) {
|
||||
// 检查排班是否存在且未开始
|
||||
schedule, err := dao.Schedule.GetOne(ctx, &dto.GetScheduleReq{Id: req.Id})
|
||||
if err != nil || schedule == nil {
|
||||
return errors.New("排班不存在")
|
||||
}
|
||||
|
||||
// 如果已经开始或结束,不允许删除
|
||||
if schedule.Status != 0 {
|
||||
return errors.New("只能删除待直播的排班")
|
||||
}
|
||||
|
||||
_, err = dao.Schedule.Delete(ctx, req)
|
||||
return
|
||||
}
|
||||
|
||||
// GetTodaySchedules 获取今日排班
|
||||
func (s *scheduleService) GetTodaySchedules(ctx context.Context) (res *dto.ListScheduleRes, err error) {
|
||||
now := time.Now()
|
||||
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
endOfDay := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location())
|
||||
|
||||
req := &dto.ListScheduleReq{
|
||||
StartDate: startOfDay,
|
||||
EndDate: endOfDay,
|
||||
}
|
||||
|
||||
return s.List(ctx, req)
|
||||
}
|
||||
|
||||
// GetAnchorSchedules 获取主播排班
|
||||
func (s *scheduleService) GetAnchorSchedules(ctx context.Context, anchorId int, startDate, endDate time.Time) (res *dto.ListScheduleRes, err error) {
|
||||
req := &dto.ListScheduleReq{
|
||||
AnchorId: &anchorId,
|
||||
StartDate: startDate,
|
||||
EndDate: endDate,
|
||||
}
|
||||
|
||||
return s.List(ctx, req)
|
||||
}
|
||||
Reference in New Issue
Block a user