84 lines
2.2 KiB
Go
84 lines
2.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
|
|
"video-factory/shortdrama/dao"
|
|
"video-factory/shortdrama/model/dto"
|
|
"video-factory/shortdrama/model/entity"
|
|
"video-factory/shortdrama/service"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
type agent struct{}
|
|
|
|
var Agent = new(agent)
|
|
|
|
type createAgentRes struct {
|
|
Id int64 `json:"id"`
|
|
}
|
|
|
|
func (c *agent) Create(ctx context.Context, req *dto.CreateAgentReq) (res *createAgentRes, err error) {
|
|
user, err := service.AuthService.CreateAgent(ctx, req.Username, req.Password, req.Phone, req.Name, req.Region, req.TierId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &createAgentRes{Id: user.Id}, nil
|
|
}
|
|
|
|
func (c *agent) List(ctx context.Context, req *dto.ListAgentReq) (res *dto.ListAgentRes, err error) {
|
|
list, total, err := dao.User.ListByRole(ctx, "agent", req.Page, req.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.ListAgentRes{List: list, Total: total, Page: req.Page, PageSize: req.PageSize}, nil
|
|
}
|
|
|
|
func (c *agent) Update(ctx context.Context, req *dto.UpdateAgentReq) (res *struct{}, err error) {
|
|
if req.Name != "" {
|
|
m := g.Map{
|
|
"name": req.Name,
|
|
"region": req.Region,
|
|
"status": req.Status,
|
|
}
|
|
if req.Phone != "" {
|
|
m["phone"] = req.Phone
|
|
}
|
|
if err := dao.User.UpdateFields(ctx, req.Id, m); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if req.TierId > 0 {
|
|
ap, _ := dao.AgentProfile.Get(ctx, req.Id)
|
|
tier, err := dao.AgentTier.GetOne(ctx, req.TierId)
|
|
if err != nil || tier == nil {
|
|
return nil, err
|
|
}
|
|
if ap != nil {
|
|
ap.TierId = req.TierId
|
|
ap.MaxCustomers = tier.MaxCustomers
|
|
return nil, dao.AgentProfile.Update(ctx, ap)
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (c *agent) Renew(ctx context.Context, req *dto.RenewAgentReq) (res *struct{}, err error) {
|
|
return nil, service.AuthService.RenewAgent(ctx, req.AgentId, req.TierId)
|
|
}
|
|
|
|
type getAgentDetailRes struct {
|
|
User *entity.User `json:"user"`
|
|
Profile *entity.AgentProfile `json:"profile"`
|
|
}
|
|
|
|
func (c *agent) Get(ctx context.Context, req *struct{ Id int64 }) (res *getAgentDetailRes, err error) {
|
|
user, err := dao.User.GetOne(ctx, req.Id)
|
|
if err != nil || user == nil {
|
|
return nil, err
|
|
}
|
|
profile, _ := dao.AgentProfile.Get(ctx, req.Id)
|
|
return &getAgentDetailRes{User: user, Profile: profile}, nil
|
|
}
|