144 lines
3.8 KiB
Go
144 lines
3.8 KiB
Go
package biz
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"geo/internal/entitys"
|
||
"geo/pkg"
|
||
"gitea.cdlsxd.cn/self-tools/l_request"
|
||
"github.com/gofiber/fiber/v2/log"
|
||
"time"
|
||
|
||
"geo/internal/config"
|
||
"geo/internal/data/impl"
|
||
"geo/internal/data/model"
|
||
"xorm.io/builder"
|
||
)
|
||
|
||
type PublishBiz struct {
|
||
cfg *config.Config
|
||
publishImpl *impl.PublishImpl
|
||
userImpl *impl.UserImpl
|
||
platImpl *impl.PlatImpl
|
||
tokenImpl *impl.TokenImpl
|
||
loginRelationImpl *impl.LoginRelationImpl
|
||
}
|
||
|
||
func NewPublishBiz(
|
||
cfg *config.Config,
|
||
publishImpl *impl.PublishImpl,
|
||
userImpl *impl.UserImpl,
|
||
platImpl *impl.PlatImpl,
|
||
tokenImpl *impl.TokenImpl,
|
||
loginRelationImpl *impl.LoginRelationImpl,
|
||
|
||
) *PublishBiz {
|
||
return &PublishBiz{
|
||
cfg: cfg,
|
||
publishImpl: publishImpl,
|
||
loginRelationImpl: loginRelationImpl,
|
||
userImpl: userImpl,
|
||
platImpl: platImpl,
|
||
tokenImpl: tokenImpl,
|
||
}
|
||
}
|
||
|
||
func (b *PublishBiz) BatchInsertPublish(ctx context.Context, list []*model.Publish) error {
|
||
|
||
return b.publishImpl.Add(ctx, list)
|
||
}
|
||
|
||
func (b *PublishBiz) GetPendingPublish(ctx context.Context, tokenID int) (map[string]interface{}, error) {
|
||
|
||
currentTime := time.Now()
|
||
cond := builder.NewCond().
|
||
And(builder.Eq{"p.token_id": tokenID}).
|
||
And(builder.Eq{"p.status": 1}).
|
||
And(builder.Lte{"p.publish_time": currentTime})
|
||
|
||
return b.publishImpl.GetOneWithPlat(ctx, &cond)
|
||
}
|
||
|
||
func (b *PublishBiz) GetTaskByRequestID(ctx context.Context, requestID string) (map[string]interface{}, error) {
|
||
cond := builder.NewCond().
|
||
And(builder.Eq{"p.request_id": requestID})
|
||
return b.publishImpl.GetOneWithPlat(ctx, &cond)
|
||
}
|
||
|
||
func (b *PublishBiz) GetTaskByPublishId(ctx context.Context, id int) (*model.Publish, error) {
|
||
var data model.Publish
|
||
cond := builder.NewCond().
|
||
And(builder.Eq{"id": id})
|
||
err := b.publishImpl.GetOneBySearchStruct(ctx, &cond, &data)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &data, err
|
||
}
|
||
|
||
func (b *PublishBiz) UpdatePublishStatus(ctx context.Context, requestID string, status int, msg string) error {
|
||
return b.publishImpl.UpdateStatus(ctx, requestID, status, msg)
|
||
}
|
||
|
||
func (b *PublishBiz) GetPublishList(ctx context.Context, tokenID int32, page, pageSize int, filters map[string]interface{}) ([]map[string]interface{}, int64, error) {
|
||
return b.publishImpl.GetListWithUser(ctx, tokenID, page, pageSize, filters)
|
||
}
|
||
|
||
func (b *PublishBiz) GetPlatInfo(ctx context.Context, platIndex string) (*model.Plat, error) {
|
||
cond := builder.NewCond().
|
||
And(builder.Eq{"`index`": platIndex}).
|
||
And(builder.Eq{"status": 1})
|
||
plat := &model.Plat{}
|
||
err := b.platImpl.GetOneBySearchStruct(ctx, &cond, plat)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return plat, nil
|
||
}
|
||
|
||
func (b *PublishBiz) UpdateLoginStatus(ctx context.Context, userIndex, platIndex string, loginStatus int32) error {
|
||
cond := builder.NewCond().
|
||
And(builder.Eq{"user_index": userIndex}).
|
||
And(builder.Eq{"plat_index": platIndex})
|
||
return b.loginRelationImpl.UpdateByCond(ctx, &cond, &model.LoginRelation{LoginStatus: loginStatus})
|
||
}
|
||
|
||
func (b *PublishBiz) Notify(data *entitys.NotifyData) (err error) {
|
||
if data.NotifyUrl != "" {
|
||
if data.TokenId == 0 {
|
||
return
|
||
}
|
||
cond := builder.NewCond().
|
||
And(builder.Eq{"id": data.TokenId}).
|
||
And(builder.Eq{"status": 1})
|
||
|
||
tokenInfo := &model.Token{}
|
||
err = b.tokenImpl.GetOneBySearchStructWithOutCtx(&cond, tokenInfo)
|
||
if err != nil {
|
||
return
|
||
}
|
||
if tokenInfo.ID == 0 {
|
||
return fmt.Errorf("未找到用户信息")
|
||
}
|
||
data.NotifyUrl = tokenInfo.Notifyurl
|
||
}
|
||
if data.NotifyUrl == "" {
|
||
return
|
||
}
|
||
sendData, _ := pkg.StructToMap(data)
|
||
req := l_request.Request{
|
||
Method: "POST",
|
||
Url: data.NotifyUrl,
|
||
Json: sendData,
|
||
Headers: map[string]string{
|
||
"Content-Type": "application/json",
|
||
},
|
||
}
|
||
res, err := req.Send()
|
||
log.Infof("回调完成:%v", res)
|
||
if err != nil {
|
||
return fmt.Errorf("请求失败,err: %v", err)
|
||
}
|
||
return nil
|
||
}
|