1
This commit is contained in:
@@ -21,6 +21,7 @@ func init() {
|
||||
panic("设置时区失败")
|
||||
}
|
||||
Httpserver.SetOpenApiPath("/api.json")
|
||||
// 全局 panic 恢复(最先注册,作为最外层包裹)
|
||||
Httpserver.BindMiddlewareDefault(ghttp.MiddlewareHandlerResponse)
|
||||
// CORS - allow all origins
|
||||
Httpserver.BindMiddlewareDefault(func(r *ghttp.Request) {
|
||||
|
||||
Binary file not shown.
@@ -3,13 +3,9 @@ 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"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
type agent struct{}
|
||||
@@ -20,6 +16,11 @@ type createAgentRes struct {
|
||||
Id int64 `json:"id"`
|
||||
}
|
||||
|
||||
type getAgentDetailRes struct {
|
||||
User *entity.User `json:"user"`
|
||||
Profile *entity.AgentProfile `json:"profile"`
|
||||
}
|
||||
|
||||
func (c *agent) Create(ctx context.Context, req *dto.CreateAgentReq) (res *createAgentRes, err error) {
|
||||
user, err := service.AgentService.CreateAgent(ctx, req.Username, req.Password, req.Phone, req.Name, req.Province, req.Region, req.RegionProtected)
|
||||
if err != nil {
|
||||
@@ -29,63 +30,11 @@ func (c *agent) Create(ctx context.Context, req *dto.CreateAgentReq) (res *creat
|
||||
}
|
||||
|
||||
func (c *agent) List(ctx context.Context, req *dto.ListAgentReq) (res *dto.ListAgentRes, err error) {
|
||||
rows, total, err := dao.AgentProfile.ListAgentWithProfile(ctx, req.Keyword, req.Phone, req.Province, req.Region, req.ExpiredAtFrom, req.ExpiredAtTo, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make([]*dto.ListAgentItem, len(rows))
|
||||
for i, r := range rows {
|
||||
list[i] = &dto.ListAgentItem{
|
||||
User: &entity.User{
|
||||
Id: r.Id,
|
||||
Username: r.Username,
|
||||
Phone: r.Phone,
|
||||
Name: r.Name,
|
||||
Province: r.Province,
|
||||
Region: r.Region,
|
||||
ExpiredAt: r.ExpiredAt,
|
||||
},
|
||||
RegionProtected: r.RegionProtected,
|
||||
MaxCustomers: r.MaxCustomers,
|
||||
}
|
||||
}
|
||||
return &dto.ListAgentRes{List: list, Total: total, Page: req.Page, PageSize: req.PageSize}, nil
|
||||
return service.AgentService.ListAgents(ctx, req)
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
if req.Phone != "" {
|
||||
m["phone"] = req.Phone
|
||||
}
|
||||
if req.Province != "" {
|
||||
m["province"] = req.Province
|
||||
}
|
||||
if err := dao.User.UpdateFields(ctx, req.Id, m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// 确保 agent_profile 存在
|
||||
profile, _ := dao.AgentProfile.Get(ctx, req.Id)
|
||||
if profile == nil {
|
||||
maxCustomers := 0
|
||||
if pricing, _ := dao.RegionPricing.GetByRegion(ctx, req.Region, 0); pricing != nil {
|
||||
maxCustomers = pricing.MaxCustomers
|
||||
}
|
||||
_ = dao.AgentProfile.Save(ctx, &entity.AgentProfile{
|
||||
UserId: req.Id,
|
||||
MaxCustomers: maxCustomers,
|
||||
Renewals: 0,
|
||||
ExpiredAt: gtime.Now().AddDate(1, 0, 0),
|
||||
RegionProtected: false,
|
||||
})
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
return nil, service.AgentService.UpdateAgent(ctx, req)
|
||||
}
|
||||
|
||||
func (c *agent) Renew(ctx context.Context, req *dto.RenewAgentReq) (res *struct{}, err error) {
|
||||
@@ -107,45 +56,14 @@ func (c *agent) CreateRenewOrder(ctx context.Context, req *dto.CreateRenewOrderR
|
||||
}, nil
|
||||
}
|
||||
|
||||
type getAgentDetailRes struct {
|
||||
User *entity.User `json:"user"`
|
||||
Profile *entity.AgentProfile `json:"profile"`
|
||||
}
|
||||
|
||||
func (c *agent) ListRenewals(ctx context.Context, req *dto.ListRenewalsReq) (res *dto.ListRenewalsRes, err error) {
|
||||
orders, err := dao.PaymentOrder.ListByUserAndType(ctx, req.AgentId, "renewal")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make([]*dto.RenewalRecord, len(orders))
|
||||
for i, o := range orders {
|
||||
paidAt := ""
|
||||
if o.PaidAt != nil {
|
||||
paidAt = o.PaidAt.Format("Y-m-d H:i:s")
|
||||
}
|
||||
createdAt := ""
|
||||
if o.CreatedAt != nil {
|
||||
createdAt = o.CreatedAt.Format("Y-m-d H:i:s")
|
||||
}
|
||||
list[i] = &dto.RenewalRecord{
|
||||
Id: o.Id,
|
||||
OrderNo: o.OrderNo,
|
||||
Amount: o.Amount,
|
||||
Channel: o.Channel,
|
||||
Status: o.Status,
|
||||
Subject: o.Subject,
|
||||
PaidAt: paidAt,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
}
|
||||
return &dto.ListRenewalsRes{List: list}, nil
|
||||
return service.AgentService.ListAgentRenewals(ctx, req.AgentId)
|
||||
}
|
||||
|
||||
func (c *agent) Get(ctx context.Context, req *struct{ Id int64 }) (res *getAgentDetailRes, err error) {
|
||||
user, err := dao.User.GetOne(ctx, req.Id)
|
||||
user, profile, err := service.AgentService.GetAgentDetail(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
|
||||
}
|
||||
|
||||
@@ -2,15 +2,12 @@ package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"video-factory/shortdrama/dao"
|
||||
"video-factory/shortdrama/middleware"
|
||||
"video-factory/shortdrama/model/dto"
|
||||
"video-factory/shortdrama/service"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
type customer struct{}
|
||||
@@ -35,68 +32,15 @@ func (c *customer) Create(ctx context.Context, req *dto.CreateCustomerReq) (res
|
||||
}
|
||||
|
||||
func (c *customer) List(ctx context.Context, req *dto.ListCustomerReq) (res *dto.ListCustomerRes, err error) {
|
||||
rows, total, err := dao.CustomerProfile.ListCustomerWithAgent(ctx, req.AgentId, req.Keyword, req.Phone, req.Province, req.Region, req.AgentName, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]*dto.ListCustomerItem, len(rows))
|
||||
for i, r := range rows {
|
||||
createdAt := ""
|
||||
if r.CreatedAt != nil {
|
||||
createdAt = r.CreatedAt.Format("Y-m-d H:i:s")
|
||||
}
|
||||
items[i] = &dto.ListCustomerItem{
|
||||
Id: r.Id,
|
||||
Phone: r.Phone,
|
||||
Name: r.Name,
|
||||
Province: r.Province,
|
||||
Region: r.Region,
|
||||
Address: r.Address,
|
||||
AgentId: r.AgentId,
|
||||
AgentName: r.AgentName,
|
||||
Balance: r.Balance,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
}
|
||||
return &dto.ListCustomerRes{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, nil
|
||||
return service.CustomerService.ListCustomers(ctx, req)
|
||||
}
|
||||
|
||||
func (c *customer) Detail(ctx context.Context, req *struct{ Id int64 }) (res *dto.CustomerDetail, err error) {
|
||||
user, err := dao.User.GetOne(ctx, req.Id)
|
||||
if err != nil || user == nil {
|
||||
return nil, err
|
||||
}
|
||||
profile, _ := dao.CustomerProfile.Get(ctx, req.Id)
|
||||
balance := service.CustomerService.GetBalance(ctx, req.Id)
|
||||
return &dto.CustomerDetail{User: user, Profile: profile, Balance: balance}, nil
|
||||
return service.CustomerService.GetCustomerDetail(ctx, req.Id)
|
||||
}
|
||||
|
||||
func (c *customer) Update(ctx context.Context, req *dto.UpdateCustomerReq) (res *struct{}, err error) {
|
||||
r := g.RequestFromCtx(ctx)
|
||||
agentId := middleware.GetUserId(r)
|
||||
if ap, _ := dao.AgentProfile.Get(ctx, agentId); ap != nil && ap.ExpiredAt != nil && ap.ExpiredAt.Before(gtime.Now()) {
|
||||
return nil, errors.New("代理商已过期,无法编辑客户")
|
||||
}
|
||||
|
||||
m := g.Map{}
|
||||
if req.Phone != "" {
|
||||
m["phone"] = req.Phone
|
||||
}
|
||||
if req.Name != "" {
|
||||
m["name"] = req.Name
|
||||
}
|
||||
if req.Address != "" {
|
||||
m["address"] = req.Address
|
||||
// re-extract province and region from the updated address
|
||||
province, region, err := service.AgentService.ExtractRegionFromAddress(ctx, req.Address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m["province"] = province
|
||||
m["region"] = region
|
||||
}
|
||||
if len(m) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, dao.User.UpdateFields(ctx, req.Id, m)
|
||||
return nil, service.CustomerService.UpdateCustomer(ctx, agentId, req)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"video-factory/shortdrama/dao"
|
||||
"video-factory/shortdrama/middleware"
|
||||
"video-factory/shortdrama/model/dto"
|
||||
"video-factory/shortdrama/service"
|
||||
@@ -22,7 +21,7 @@ func (c *transaction) List(ctx context.Context, req *dto.ListTransactionReq) (re
|
||||
if userId == 0 {
|
||||
userId = middleware.GetUserId(r)
|
||||
}
|
||||
list, total, err := dao.AccountTransaction.ListByUser(ctx, userId, req.Type, req.Page, req.PageSize)
|
||||
list, total, err := service.TransactionService.ListByUser(ctx, userId, req.Type, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"video-factory/shortdrama/consts/public"
|
||||
"video-factory/shortdrama/dao"
|
||||
"video-factory/shortdrama/model/dto"
|
||||
"video-factory/shortdrama/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
@@ -192,6 +193,106 @@ func (s *agentService) RenewAgentByOrder(ctx context.Context, agentId int64, raw
|
||||
return s.RenewAgent(ctx, agentId, params.Duration)
|
||||
}
|
||||
|
||||
// ListAgents 分页查询代理商列表
|
||||
func (s *agentService) ListAgents(ctx context.Context, req *dto.ListAgentReq) (*dto.ListAgentRes, error) {
|
||||
rows, total, err := dao.AgentProfile.ListAgentWithProfile(ctx, req.Keyword, req.Phone, req.Province, req.Region, req.ExpiredAtFrom, req.ExpiredAtTo, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make([]*dto.ListAgentItem, len(rows))
|
||||
for i, r := range rows {
|
||||
list[i] = &dto.ListAgentItem{
|
||||
User: &entity.User{
|
||||
Id: r.Id,
|
||||
Username: r.Username,
|
||||
Phone: r.Phone,
|
||||
Name: r.Name,
|
||||
Province: r.Province,
|
||||
Region: r.Region,
|
||||
ExpiredAt: r.ExpiredAt,
|
||||
},
|
||||
RegionProtected: r.RegionProtected,
|
||||
MaxCustomers: r.MaxCustomers,
|
||||
}
|
||||
}
|
||||
return &dto.ListAgentRes{List: list, Total: total, Page: req.Page, PageSize: req.PageSize}, nil
|
||||
}
|
||||
|
||||
// UpdateAgent 更新代理商信息
|
||||
func (s *agentService) UpdateAgent(ctx context.Context, req *dto.UpdateAgentReq) error {
|
||||
if req.Name != "" {
|
||||
m := g.Map{
|
||||
"name": req.Name,
|
||||
"region": req.Region,
|
||||
}
|
||||
if req.Phone != "" {
|
||||
m["phone"] = req.Phone
|
||||
}
|
||||
if req.Province != "" {
|
||||
m["province"] = req.Province
|
||||
}
|
||||
if err := dao.User.UpdateFields(ctx, req.Id, m); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
profile, _ := dao.AgentProfile.Get(ctx, req.Id)
|
||||
if profile == nil {
|
||||
maxCustomers := 0
|
||||
if pricing, _ := dao.RegionPricing.GetByRegion(ctx, req.Region, 0); pricing != nil {
|
||||
maxCustomers = pricing.MaxCustomers
|
||||
}
|
||||
_ = dao.AgentProfile.Save(ctx, &entity.AgentProfile{
|
||||
UserId: req.Id,
|
||||
MaxCustomers: maxCustomers,
|
||||
Renewals: 0,
|
||||
ExpiredAt: gtime.Now().AddDate(1, 0, 0),
|
||||
RegionProtected: false,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAgentDetail 获取代理商详情
|
||||
func (s *agentService) GetAgentDetail(ctx context.Context, id int64) (*entity.User, *entity.AgentProfile, error) {
|
||||
user, err := dao.User.GetOne(ctx, id)
|
||||
if err != nil || user == nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
profile, _ := dao.AgentProfile.Get(ctx, id)
|
||||
return user, profile, nil
|
||||
}
|
||||
|
||||
// ListAgentRenewals 查询代理商续费记录
|
||||
func (s *agentService) ListAgentRenewals(ctx context.Context, agentId int64) (*dto.ListRenewalsRes, error) {
|
||||
orders, err := dao.PaymentOrder.ListByUserAndType(ctx, agentId, "renewal")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
list := make([]*dto.RenewalRecord, len(orders))
|
||||
for i, o := range orders {
|
||||
paidAt := ""
|
||||
if o.PaidAt != nil {
|
||||
paidAt = o.PaidAt.Format("Y-m-d H:i:s")
|
||||
}
|
||||
createdAt := ""
|
||||
if o.CreatedAt != nil {
|
||||
createdAt = o.CreatedAt.Format("Y-m-d H:i:s")
|
||||
}
|
||||
list[i] = &dto.RenewalRecord{
|
||||
Id: o.Id,
|
||||
OrderNo: o.OrderNo,
|
||||
Amount: o.Amount,
|
||||
Channel: o.Channel,
|
||||
Status: o.Status,
|
||||
Subject: o.Subject,
|
||||
PaidAt: paidAt,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
}
|
||||
return &dto.ListRenewalsRes{List: list}, nil
|
||||
}
|
||||
|
||||
// bcryptGenerate 辅助函数:生成 bcrypt 密码哈希
|
||||
func bcryptGenerate(password string) ([]byte, error) {
|
||||
return bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"video-factory/shortdrama/dao"
|
||||
"video-factory/shortdrama/model/dto"
|
||||
"video-factory/shortdrama/model/entity"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
@@ -119,3 +120,70 @@ func (s *customerService) DeductBalance(ctx context.Context, customerId int64, a
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// ListCustomers 分页查询客户列表
|
||||
func (s *customerService) ListCustomers(ctx context.Context, req *dto.ListCustomerReq) (*dto.ListCustomerRes, error) {
|
||||
rows, total, err := dao.CustomerProfile.ListCustomerWithAgent(ctx, req.AgentId, req.Keyword, req.Phone, req.Province, req.Region, req.AgentName, req.Page, req.PageSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items := make([]*dto.ListCustomerItem, len(rows))
|
||||
for i, r := range rows {
|
||||
createdAt := ""
|
||||
if r.CreatedAt != nil {
|
||||
createdAt = r.CreatedAt.Format("Y-m-d H:i:s")
|
||||
}
|
||||
items[i] = &dto.ListCustomerItem{
|
||||
Id: r.Id,
|
||||
Phone: r.Phone,
|
||||
Name: r.Name,
|
||||
Province: r.Province,
|
||||
Region: r.Region,
|
||||
Address: r.Address,
|
||||
AgentId: r.AgentId,
|
||||
AgentName: r.AgentName,
|
||||
Balance: r.Balance,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
}
|
||||
return &dto.ListCustomerRes{List: items, Total: total, Page: req.Page, PageSize: req.PageSize}, nil
|
||||
}
|
||||
|
||||
// GetCustomerDetail 获取客户详情
|
||||
func (s *customerService) GetCustomerDetail(ctx context.Context, id int64) (*dto.CustomerDetail, error) {
|
||||
user, err := dao.User.GetOne(ctx, id)
|
||||
if err != nil || user == nil {
|
||||
return nil, err
|
||||
}
|
||||
profile, _ := dao.CustomerProfile.Get(ctx, id)
|
||||
balance := s.GetBalance(ctx, id)
|
||||
return &dto.CustomerDetail{User: user, Profile: profile, Balance: balance}, nil
|
||||
}
|
||||
|
||||
// UpdateCustomer 更新客户信息
|
||||
func (s *customerService) UpdateCustomer(ctx context.Context, agentId int64, req *dto.UpdateCustomerReq) error {
|
||||
if ap, _ := dao.AgentProfile.Get(ctx, agentId); ap != nil && ap.ExpiredAt != nil && ap.ExpiredAt.Before(gtime.Now()) {
|
||||
return errors.New("代理商已过期,无法编辑客户")
|
||||
}
|
||||
|
||||
m := g.Map{}
|
||||
if req.Phone != "" {
|
||||
m["phone"] = req.Phone
|
||||
}
|
||||
if req.Name != "" {
|
||||
m["name"] = req.Name
|
||||
}
|
||||
if req.Address != "" {
|
||||
m["address"] = req.Address
|
||||
province, region, err := AgentService.ExtractRegionFromAddress(ctx, req.Address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m["province"] = province
|
||||
m["region"] = region
|
||||
}
|
||||
if len(m) == 0 {
|
||||
return nil
|
||||
}
|
||||
return dao.User.UpdateFields(ctx, req.Id, m)
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user