134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
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
|
|
}
|