2026-04-17 16:28:31 +08:00
|
|
|
package data
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
consts "erp/consts/public"
|
|
|
|
|
dto "erp/model/dto/data"
|
|
|
|
|
entity "erp/model/entity/data"
|
|
|
|
|
|
2026-06-10 15:59:11 +08:00
|
|
|
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
|
2026-04-17 16:28:31 +08:00
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var Anchor = new(anchorDao)
|
|
|
|
|
|
|
|
|
|
type anchorDao struct{}
|
|
|
|
|
|
|
|
|
|
// Insert 插入主播
|
|
|
|
|
func (d *anchorDao) Insert(ctx context.Context, req *dto.CreateAnchorReq) (id int64, err error) {
|
|
|
|
|
var res *entity.Anchor
|
|
|
|
|
if err = gconv.Struct(req, &res); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).Data(&res).Insert()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return r.LastInsertId()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update 更新主播
|
|
|
|
|
func (d *anchorDao) Update(ctx context.Context, req *dto.UpdateAnchorReq) (rows int64, err error) {
|
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).Data(&req).OmitEmpty().Where(entity.AnchorCols.Id, req.Id).Update()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return r.RowsAffected()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete 删除主播
|
|
|
|
|
func (d *anchorDao) Delete(ctx context.Context, req *dto.DeleteAnchorReq) (rows int64, err error) {
|
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).Where(entity.AnchorCols.Id, req.Id).Delete()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return r.RowsAffected()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetOne 获取单个主播
|
|
|
|
|
func (d *anchorDao) GetOne(ctx context.Context, req *dto.GetAnchorReq) (res *entity.Anchor, err error) {
|
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).Where(entity.AnchorCols.Id, req.Id).One()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = r.Struct(&res)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Count 获取主播数量
|
|
|
|
|
func (d *anchorDao) Count(ctx context.Context, req *dto.ListAnchorReq) (count int, err error) {
|
|
|
|
|
return d.buildListFilter(ctx, req).Count()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List 获取主播列表
|
|
|
|
|
func (d *anchorDao) List(ctx context.Context, req *dto.ListAnchorReq) (res []entity.Anchor, total int, err error) {
|
|
|
|
|
model := d.buildListFilter(ctx, req)
|
|
|
|
|
model.OrderDesc(entity.AnchorCols.CreatedAt)
|
|
|
|
|
if req.Page != nil {
|
|
|
|
|
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
|
|
|
|
}
|
|
|
|
|
r, total, err := model.AllAndCount(false)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = r.Structs(&res)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buildListFilter 构建列表查询的过滤条件
|
|
|
|
|
func (d *anchorDao) buildListFilter(ctx context.Context, req *dto.ListAnchorReq) *gdb.Model {
|
|
|
|
|
model := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).Model
|
|
|
|
|
if !g.IsEmpty(req.Keyword) {
|
|
|
|
|
model.WhereLike(entity.AnchorCols.Name, "%"+req.Keyword+"%").
|
|
|
|
|
WhereOrLike(entity.AnchorCols.Phone, "%"+req.Keyword+"%").
|
|
|
|
|
WhereOrLike(entity.AnchorCols.Code, "%"+req.Keyword+"%")
|
|
|
|
|
}
|
2026-04-21 13:31:10 +08:00
|
|
|
if !g.IsEmpty(req.Name) {
|
|
|
|
|
model.WhereLike(entity.AnchorCols.Name, "%"+req.Name+"%")
|
|
|
|
|
}
|
|
|
|
|
if !g.IsEmpty(req.Phone) {
|
|
|
|
|
model.WhereLike(entity.AnchorCols.Phone, "%"+req.Phone+"%")
|
|
|
|
|
}
|
|
|
|
|
if !g.IsEmpty(req.Code) {
|
|
|
|
|
model.WhereLike(entity.AnchorCols.Code, "%"+req.Code+"%")
|
|
|
|
|
}
|
2026-04-17 16:28:31 +08:00
|
|
|
if req.Status != nil {
|
2026-04-21 13:31:10 +08:00
|
|
|
model.Where("status = ?", *req.Status)
|
2026-04-17 16:28:31 +08:00
|
|
|
}
|
|
|
|
|
model.OmitEmptyWhere()
|
|
|
|
|
return model
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateStatus 更新主播状态
|
|
|
|
|
func (d *anchorDao) UpdateStatus(ctx context.Context, id int64, status int) (rows int64, err error) {
|
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).
|
|
|
|
|
Data(map[string]interface{}{"status": status}).
|
|
|
|
|
Where(entity.AnchorCols.Id, id).
|
|
|
|
|
Update()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return r.RowsAffected()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetByCode 根据工号获取主播
|
|
|
|
|
func (d *anchorDao) GetByCode(ctx context.Context, code string) (res *entity.Anchor, err error) {
|
|
|
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.AnchorTable).
|
|
|
|
|
Where(entity.AnchorCols.Code, code).
|
|
|
|
|
One()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = r.Struct(&res)
|
|
|
|
|
return
|
|
|
|
|
}
|