+359
@@ -0,0 +1,359 @@
|
||||
package ai_image_to_video_ser
|
||||
|
||||
import (
|
||||
"91porn-server/app/appg"
|
||||
"91porn-server/common"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/common/log"
|
||||
"91porn-server/common/stderr"
|
||||
"91porn-server/middleware/ua"
|
||||
"91porn-server/models/cache/aiimagetovideodata"
|
||||
"91porn-server/models/cache/sysconfdata"
|
||||
"91porn-server/models/commod"
|
||||
"91porn-server/models/v/aichangefacevidmod"
|
||||
"91porn-server/models/v/aiimagetovideomod"
|
||||
"91porn-server/models/v/sysconfmod"
|
||||
"91porn-server/models/v/txnmod"
|
||||
"91porn-server/models/v/usermod"
|
||||
"91porn-server/models/v/walletmod"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type AppQueryListReq struct {
|
||||
Status int `json:"status" form:"status"` // 状态 1、排队中 2、生成成功 3、生成失败
|
||||
UID uint64 `json:"-"` // 用户ID
|
||||
commod.Page
|
||||
}
|
||||
|
||||
func (p *AppQueryListReq) Filter() primitive.M {
|
||||
filter := bson.M{}
|
||||
switch p.Status {
|
||||
case 1:
|
||||
filter["status"] = bson.M{"$in": []aiimagetovideomod.AiImageToVideoStatus{aiimagetovideomod.StatusOrderSuccess, aiimagetovideomod.StatusSubmitted}}
|
||||
case 2:
|
||||
filter["status"] = aiimagetovideomod.StatusGenerationSuccess
|
||||
case 3:
|
||||
filter["status"] = bson.M{"$in": []aiimagetovideomod.AiImageToVideoStatus{aiimagetovideomod.StatusGenerationFailed, aiimagetovideomod.StatusRefunded}}
|
||||
}
|
||||
filter["uid"] = p.UID
|
||||
filter["isHide"] = false
|
||||
return filter
|
||||
}
|
||||
|
||||
type AppListRes struct {
|
||||
HasNext bool `json:"hasNext"` // 是否有下一页
|
||||
List []*aiimagetovideomod.AiImageToVideoInfo `json:"list"` // 列表
|
||||
}
|
||||
|
||||
// GetList 获取列表
|
||||
func (p *AppQueryListReq) GetList() (AppListRes, error) {
|
||||
var res AppListRes
|
||||
var err error
|
||||
|
||||
sort := bson.D{{"createdAt", -1}}
|
||||
|
||||
// 获取列表
|
||||
var data []aiimagetovideomod.AiImageToVideo
|
||||
data, _, res.HasNext, err = aiimagetovideomod.GetList(p.Filter(), int64(p.Skip()), int64(p.Limit()), sort)
|
||||
if err != nil {
|
||||
log.Error("获取AI图生视频列表列表数据错误", log.Any("Params", *p), log.E(err))
|
||||
return res, err
|
||||
}
|
||||
res.List = aiimagetovideodata.FormatAppDataList(data)
|
||||
return res, nil
|
||||
}
|
||||
|
||||
type GenerateReq struct {
|
||||
MID string `json:"mid" bson:"mid"` // 模版ID
|
||||
OriginPic string `json:"originPic" bson:"originPic"` // 脱衣原图
|
||||
Coin int64 `json:"-"` // 此次脱衣金币个数
|
||||
UID uint64 `json:"-"` // 用户ID
|
||||
ShareTitle string `json:"shareTitle" bson:"shareTitle"` // 分享标题
|
||||
ShareStatus int `json:"shareStatus" bson:"shareStatus"` // 是否分享 0-不分享 1-分享
|
||||
|
||||
}
|
||||
|
||||
func (p *GenerateReq) GenerateInfo(orderId primitive.ObjectID, orderCreatedAt time.Time, debitCoins, debitIncomeCoins int64, sceneType int) aiimagetovideomod.AiImageToVideo {
|
||||
return aiimagetovideomod.AiImageToVideo{
|
||||
ID: orderId,
|
||||
SceneType: sceneType,
|
||||
Coin: debitIncomeCoins + debitCoins,
|
||||
DebitAmountCoin: debitCoins,
|
||||
DebitIncomeCoin: debitIncomeCoins,
|
||||
IsFreeTimes: false,
|
||||
ImgUrl: p.OriginPic,
|
||||
UID: p.UID,
|
||||
Status: int(aiimagetovideomod.StatusOrderSuccess),
|
||||
IsHide: false,
|
||||
ShareTitle: p.ShareTitle,
|
||||
ShareStatus: p.ShareStatus,
|
||||
UpdatedAt: orderCreatedAt,
|
||||
CreatedAt: orderCreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// Generate 生成订单
|
||||
func (p *GenerateReq) Generate(ua ua.UA, ip string) (stderr.Code, error) {
|
||||
// 默认脱衣
|
||||
sceneType := int(aiimagetovideomod.SceneTagLiftShirt)
|
||||
|
||||
if p.MID != "" {
|
||||
// 获取模版信息
|
||||
id, _ := primitive.ObjectIDFromHex(p.MID)
|
||||
mod, err := aichangefacevidmod.GetModById(id)
|
||||
if err != nil {
|
||||
return stderr.ErrDbQueryError, err
|
||||
}
|
||||
if mod.ID.IsZero() {
|
||||
return stderr.CodeEmptyData, errors.New("mod is null")
|
||||
}
|
||||
sceneType = mod.SceneType
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
user, err := usermod.FindUserByUID(p.UID)
|
||||
if err != nil {
|
||||
return stderr.ErrDbQueryError, err
|
||||
}
|
||||
if user.ID.IsZero() {
|
||||
return stderr.UserIsNotExists, errors.New("user is null")
|
||||
}
|
||||
configure, _ := sysconfdata.GetAllFromCache()
|
||||
p.Coin = configure.GetInt(sysconfmod.VCodeAiImageToVideoPrice)
|
||||
var debitAmountCoins, debitIncomeCoins int64
|
||||
|
||||
if p.Coin > 0 {
|
||||
// 获取用户钱包
|
||||
w, err := walletmod.GetWallet(p.UID)
|
||||
if err != nil {
|
||||
return stderr.ErrDbQueryError, err
|
||||
}
|
||||
if w == nil {
|
||||
return stderr.InsufficientBalance, errors.New("Insufficient balance")
|
||||
}
|
||||
|
||||
// 如果收费的部分余额不够支付
|
||||
if p.Coin > (w.Amount + w.Income) {
|
||||
return stderr.InsufficientBalance, errors.New("Insufficient balance")
|
||||
}
|
||||
|
||||
debitAmountCoins, debitIncomeCoins = TotalDebit(p.Coin, *w)
|
||||
}
|
||||
// 获取用户是否是复购
|
||||
isRepurchase, err := txnmod.CheckRepurchaseByTransTypes(p.UID, []txnmod.TransType{
|
||||
txnmod.AiImageToVideoDebitGold,
|
||||
txnmod.AiImageToVideoDebitInComeGold,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error("txnmod.CheckRepurchaseByTransTypes fail", log.E(err))
|
||||
return stderr.ErrDbQueryError, err
|
||||
}
|
||||
var orderId primitive.ObjectID
|
||||
var orderCreatedAt time.Time
|
||||
if err = appg.VideoDB.Trans(func(tool *db.MongoTool) error {
|
||||
var tl []txnmod.TransactionLog
|
||||
if debitIncomeCoins > 0 || debitAmountCoins > 0 {
|
||||
// 扣除钱包余额
|
||||
wallet, wErr := walletmod.DebitAmountAndIncome(tool, debitAmountCoins, debitIncomeCoins, p.UID)
|
||||
if wErr != nil {
|
||||
log.Error(fmt.Sprintf("Handle imagetovideo Generate walletmod.DebitAmount error:%+v:", wErr), log.Any("uid", p.UID))
|
||||
return stderr.ErrDbQueryError
|
||||
}
|
||||
|
||||
if debitAmountCoins > 0 {
|
||||
tl = append(tl, txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: p.UID,
|
||||
Amount: -debitAmountCoins,
|
||||
ActualAmount: float64(-debitAmountCoins),
|
||||
TranType: txnmod.AiImageToVideoDebitGold.Key(),
|
||||
TranTypeInt: int64(txnmod.AiImageToVideoDebitGold),
|
||||
Desc: fmt.Sprintf("生成AI图生视频金币-%d", debitAmountCoins),
|
||||
RealAmount: walletmod.GetRealAmount(wallet),
|
||||
SysType: user.SysType,
|
||||
IsRepurchase: isRepurchase,
|
||||
})
|
||||
}
|
||||
if debitIncomeCoins > 0 {
|
||||
tl = append(tl, txnmod.TransactionLog{
|
||||
TransNo: primitive.NewObjectID(),
|
||||
UID: p.UID,
|
||||
Amount: -debitIncomeCoins,
|
||||
ActualAmount: float64(-debitIncomeCoins),
|
||||
TranType: txnmod.AiImageToVideoDebitInComeGold.Key(),
|
||||
TranTypeInt: int64(txnmod.AiImageToVideoDebitInComeGold),
|
||||
Desc: fmt.Sprintf("生成AI图生视频收益金币-%d", debitIncomeCoins),
|
||||
RealAmount: walletmod.GetRealAmount(wallet),
|
||||
SysType: user.SysType,
|
||||
IsRepurchase: isRepurchase,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(tl) > 0 {
|
||||
txnErr := txnmod.InsertManyTransactionLog(tool, tl)
|
||||
if txnErr != nil {
|
||||
log.Error(fmt.Sprintf("Handle imagetovideo Generate txnmod.InsertTransactionLog error:%+v:", txnErr), log.Any("uid", p.UID))
|
||||
return stderr.ErrDbQueryError
|
||||
}
|
||||
}
|
||||
|
||||
// 新增AI图生视频记录
|
||||
orderId = primitive.NewObjectID()
|
||||
orderCreatedAt = time.Now()
|
||||
order := p.GenerateInfo(orderId, orderCreatedAt, debitAmountCoins, debitIncomeCoins, sceneType)
|
||||
if _, err = aiimagetovideomod.Insert(tool, order); err != nil {
|
||||
log.Error(fmt.Sprintf("Handle imagetovideo Generate InsertOnes error:%+v:", err), log.Any("uid", p.UID))
|
||||
return stderr.ErrDbInsertError
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
log.Error(fmt.Sprintf("Handle imagetovideo Generate Trans error:%+v;uid:%v;", err, p.UID))
|
||||
return stderr.ErrDbUpdateError, err
|
||||
}
|
||||
|
||||
common.Go(func() {
|
||||
autoUploadOrder(orderId)
|
||||
})
|
||||
return stderr.Success, nil
|
||||
}
|
||||
|
||||
type AutoUploadReq struct {
|
||||
AppID int `json:"appId"` // 应用 ID
|
||||
UID uint64 `json:"uid"` // 用户 ID
|
||||
ImgURL string `json:"imgUrl"` // 图片 URL
|
||||
AppOrderNum string `json:"appOrderNum"` // 应用订单号
|
||||
NotifyURL string `json:"notifyUrl"` // 通知 URL
|
||||
SceneType int `json:"sceneType" bson:"sceneType"` // 模版场景类型
|
||||
}
|
||||
|
||||
func autoUploadOrder(id primitive.ObjectID) error {
|
||||
item, err := aiimagetovideomod.GetInfo(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if item.ID.IsZero() {
|
||||
return errors.New(" aiimagetovideo order is null")
|
||||
}
|
||||
|
||||
// 将请求参数编码为JSON
|
||||
requestBody := AutoUploadReq{
|
||||
AppID: int(commod.KFK_APPID),
|
||||
UID: item.UID,
|
||||
ImgURL: item.ImgUrl,
|
||||
AppOrderNum: item.ID.Hex(),
|
||||
SceneType: item.SceneType,
|
||||
NotifyURL: fmt.Sprintf("%v/api/web/admin/ai/image_to_video/callback", appg.Conf.URL.AiImageToVideoCallbackUrl),
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(requestBody)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apiURL := getAutoUploadUrl()
|
||||
|
||||
// 创建请求对象
|
||||
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建请求失败: %v", err)
|
||||
}
|
||||
|
||||
// 通过http.Client发送请求
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("请求失败: %v", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应内容
|
||||
respBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取响应内容失败: %v", err)
|
||||
}
|
||||
|
||||
// 如果返回状态码非200,可以考虑返回错误
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("请求失败,状态码:%d,内容:%s", resp.StatusCode, string(respBytes))
|
||||
}
|
||||
|
||||
// 更新订单
|
||||
updateCond := bson.M{
|
||||
"status": aiimagetovideomod.StatusSubmitted,
|
||||
"updatedAt": time.Now(),
|
||||
}
|
||||
_, err = aiimagetovideomod.UpdateByID(nil, id, updateCond)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getAutoUploadUrl() string {
|
||||
return fmt.Sprintf("%v/api/comfyui/prd/create_job", appg.Conf.URL.AiImageToVideoUrl)
|
||||
}
|
||||
|
||||
func TotalDebit(price int64, w walletmod.Wallet) (amountCoins, incomeCoins int64) {
|
||||
var (
|
||||
debitAmountCoins int64
|
||||
debitIncomeCoins int64
|
||||
)
|
||||
|
||||
// 计算当日实际扣减的金额
|
||||
debitCoins := price
|
||||
if w.Amount >= debitCoins {
|
||||
debitAmountCoins = debitCoins
|
||||
} else {
|
||||
debitAmountCoins = w.Amount
|
||||
debitIncomeCoins = debitCoins - w.Amount
|
||||
}
|
||||
return debitAmountCoins, debitIncomeCoins
|
||||
}
|
||||
|
||||
type HideReq struct {
|
||||
ID string `json:"id"` // AI订单ID
|
||||
UID uint64 `json:"-"` // 用户ID
|
||||
}
|
||||
|
||||
// Hide 删除订单
|
||||
func (p *HideReq) Hide() error {
|
||||
oid, err := primitive.ObjectIDFromHex(p.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
item, err := aiimagetovideomod.GetInfo(oid)
|
||||
if err != nil {
|
||||
log.Error("获取AI图生视频列表详情数据错误", log.Any("ID", p.ID), log.E(err))
|
||||
return err
|
||||
}
|
||||
|
||||
if item.ID.IsZero() {
|
||||
return errors.New("AI订单不存在")
|
||||
}
|
||||
|
||||
if item.UID != p.UID {
|
||||
return errors.New("只能删除自己的订单")
|
||||
}
|
||||
|
||||
if item.Status == int(aiimagetovideomod.StatusSubmitted) {
|
||||
return errors.New("不能删除排队中的订单")
|
||||
}
|
||||
|
||||
if err = aiimagetovideomod.Hide(p.UID, oid); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user