326 lines
9.2 KiB
Go
Executable File
326 lines
9.2 KiB
Go
Executable File
package ai_text_to_image_ser
|
|
|
|
import (
|
|
"91porn-server/common"
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/common/stderr"
|
|
"91porn-server/models/v/aiimagetovideomod"
|
|
"91porn-server/models/v/aiplazamod"
|
|
"91porn-server/models/v/txnmod"
|
|
"91porn-server/models/v/walletmod"
|
|
"91porn-server/web/webg"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"91porn-server/models/cache/aitexttoimagedata"
|
|
"91porn-server/models/commod"
|
|
"91porn-server/models/v/aitexttoimagemod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
type WebListReq struct {
|
|
ID *string `json:"id" form:"id"` // ID
|
|
UID *uint64 `json:"uid" form:"uid"` // 用户ID
|
|
Status *int `json:"status" form:"status"` // 状态
|
|
commod.Page
|
|
}
|
|
|
|
func (q *WebListReq) Filter() primitive.M {
|
|
filter := bson.M{}
|
|
if q.ID != nil {
|
|
id, _ := primitive.ObjectIDFromHex(*q.ID)
|
|
filter["_id"] = id
|
|
}
|
|
if q.Status != nil {
|
|
filter["status"] = q.Status
|
|
}
|
|
if q.UID != nil {
|
|
filter["uid"] = q.UID
|
|
}
|
|
return filter
|
|
}
|
|
|
|
type WebListRes struct {
|
|
Total int64 `json:"total"`
|
|
HasNext bool `json:"hasNext"`
|
|
List []aitexttoimagemod.AiTextToImage `json:"list"`
|
|
}
|
|
|
|
// GetList 获取列表
|
|
func (q *WebListReq) GetList() (res WebListRes, err error) {
|
|
res.List, res.Total, res.HasNext, err = aitexttoimagemod.GetList(q.Filter(), int64(q.Skip()), int64(q.Limit()), q.GetSort())
|
|
|
|
return
|
|
}
|
|
|
|
type WebUpdateReq struct {
|
|
ID string `json:"id" binding:"required"` // ID
|
|
Status *int `json:"status"` // 状态
|
|
Remark *string `json:"remark"` // 备注
|
|
}
|
|
|
|
func (p *WebUpdateReq) UpdateCond() primitive.M {
|
|
data := make(map[string]interface{})
|
|
if p.Status != nil {
|
|
data["status"] = *p.Status
|
|
}
|
|
if p.Remark != nil {
|
|
data["remark"] = *p.Remark
|
|
}
|
|
data["updatedAt"] = time.Now()
|
|
return data
|
|
}
|
|
|
|
// Update 更新数据
|
|
func (p *WebUpdateReq) Update() error {
|
|
id, _ := primitive.ObjectIDFromHex(p.ID)
|
|
data, err := aitexttoimagemod.GetInfo(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if data.ID.IsZero() {
|
|
return errors.New("data is null")
|
|
}
|
|
|
|
if p.Status != nil {
|
|
if data.Status == int(aitexttoimagemod.StatusRefunded) || data.Status == int(aiimagetovideomod.StatusGenerationFailed) {
|
|
return errors.New("Please do not refund repeatedly.")
|
|
}
|
|
|
|
switch aitexttoimagemod.AiTextToImageStatus(*p.Status) {
|
|
case aitexttoimagemod.StatusRefunded:
|
|
if err = webg.VideoDB.Trans(func(t *db.MongoTool) error {
|
|
if _, err = aitexttoimagemod.UpdateByID(t, id, p.UpdateCond()); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 退款
|
|
if data.DebitAmountCoin == 0 && data.DebitIncomeCoin == 0 { // 总金额为0则不需要退款
|
|
return nil
|
|
}
|
|
|
|
// 退款进入钱包余额
|
|
wallet, err := walletmod.ReturnAmountAndIncome(t, -data.DebitAmountCoin, -data.DebitIncomeCoin, data.UID)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("Handle Edit walletmod.DebitAmount error:%+v:", err), log.Any("uid", data.UID))
|
|
return err
|
|
}
|
|
|
|
var tl []txnmod.TransactionLog
|
|
if data.DebitAmountCoin > 0 {
|
|
tl = append(tl, txnmod.TransactionLog{
|
|
TransNo: primitive.NewObjectID(),
|
|
UID: data.UID,
|
|
Amount: -data.DebitAmountCoin,
|
|
ActualAmount: float64(-data.DebitAmountCoin),
|
|
TranType: txnmod.AiTextToImageDebitGoldReturn.Key(),
|
|
TranTypeInt: int64(txnmod.AiTextToImageDebitGoldReturn),
|
|
Desc: fmt.Sprintf("退还AI绘图金币-%d", data.DebitAmountCoin),
|
|
RealAmount: walletmod.GetRealAmount(wallet),
|
|
})
|
|
}
|
|
if data.DebitIncomeCoin > 0 {
|
|
tl = append(tl, txnmod.TransactionLog{
|
|
TransNo: primitive.NewObjectID(),
|
|
UID: data.UID,
|
|
Amount: -data.DebitIncomeCoin,
|
|
ActualAmount: float64(-data.DebitIncomeCoin),
|
|
TranType: txnmod.AiTextToImageDebitIncomeGoldReturn.Key(),
|
|
TranTypeInt: int64(txnmod.AiTextToImageDebitIncomeGoldReturn),
|
|
Desc: fmt.Sprintf("退还AI绘图收益金币-%d", data.DebitIncomeCoin),
|
|
RealAmount: walletmod.GetRealAmount(wallet),
|
|
})
|
|
}
|
|
|
|
if len(tl) > 0 {
|
|
txnErr := txnmod.InsertManyTransactionLog(t, tl)
|
|
if txnErr != nil {
|
|
log.Error(fmt.Sprintf("Handle imagetovideo Edit txnmod.InsertTransactionLog error:%+v:", txnErr), log.Any("uid", data.UID))
|
|
return stderr.ErrDbQueryError
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
if e, ok := err.(stderr.Code); ok {
|
|
return e
|
|
}
|
|
return stderr.ErrDbQueryError
|
|
}
|
|
default:
|
|
}
|
|
}
|
|
if _, err = aitexttoimagemod.UpdateByID(nil, id, p.UpdateCond()); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type WebDeleteReq struct {
|
|
ID primitive.ObjectID `json:"id" binding:"required"`
|
|
}
|
|
|
|
// Delete 删除数据
|
|
func (p *WebDeleteReq) Delete() error {
|
|
data, err := aitexttoimagemod.GetInfo(p.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = validateDeleteStatus(data.Status); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := aitexttoimagedata.DeleteData(nil, p.ID); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func validateDeleteStatus(status int) error {
|
|
if status == int(aitexttoimagemod.StatusOrderSuccess) ||
|
|
status == int(aitexttoimagemod.StatusSubmitted) {
|
|
return stderr.AiGenningDelForbidden
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CallbackReq 换脸订单回调请求
|
|
type CallbackReq struct {
|
|
ImgUrl string `json:"imgUrl"` // AI图生视频图片地址
|
|
AppOrderNum string `json:"appOrderNum"` // app订单号
|
|
Status int `json:"status"` // 订单状态 1:成功,2:失败
|
|
Msg string `json:"msg"` // 消息
|
|
}
|
|
|
|
func (p *CallbackReq) Callback() error {
|
|
id, _ := primitive.ObjectIDFromHex(p.AppOrderNum)
|
|
// 查询该笔订单是否存在
|
|
data, err := aitexttoimagemod.GetInfo(id)
|
|
if err != nil {
|
|
return stderr.ErrDbQueryError
|
|
}
|
|
|
|
if data.ID.IsZero() {
|
|
return errors.New("ai imagetovideo data is null")
|
|
}
|
|
|
|
if data.Status != int(aitexttoimagemod.StatusSubmitted) {
|
|
log.Error("AI文生图-重复回调或者状态不正确", log.Any("id", data.ID.Hex()), log.Any("status", data.Status))
|
|
return nil
|
|
}
|
|
|
|
var status = aitexttoimagemod.StatusGenerationSuccess
|
|
|
|
// 处理金币退款以及后续操作
|
|
set := bson.M{}
|
|
if p.ImgUrl != "" {
|
|
set["newImgUrl"] = strings.TrimSpace(p.ImgUrl)
|
|
}
|
|
if p.Status != 1 {
|
|
set["remark"] = p.Msg
|
|
set["newImgUrl"] = ""
|
|
status = aitexttoimagemod.StatusGenerationFailed
|
|
}
|
|
set["status"] = status
|
|
set["updatedAt"] = time.Now()
|
|
|
|
switch status {
|
|
case aitexttoimagemod.StatusGenerationSuccess:
|
|
if _, err = aitexttoimagemod.UpdateByID(nil, id, set); err != nil {
|
|
return stderr.ErrDbUpdateError
|
|
}
|
|
common.Go(func() {
|
|
if data.ShareStatus != 1 {
|
|
return
|
|
}
|
|
// 新增一个分享
|
|
alPlaza := aiplazamod.AiPlaza{
|
|
Type: 5,
|
|
OrderId: data.ID,
|
|
Template: "",
|
|
Uid: data.UID,
|
|
Title: data.ShareTitle,
|
|
OriginalImage: "",
|
|
OriginalVideo: "",
|
|
OriginContent: data.Text,
|
|
GenerateImage: p.ImgUrl,
|
|
GenerateVideo: "",
|
|
Status: aiplazamod.DefaultStatus,
|
|
}
|
|
_, err = aiplazamod.Insert(nil, alPlaza)
|
|
if err != nil {
|
|
log.Error("新增ai广场帖子失败", log.Any("alPlaza", alPlaza), log.E(err))
|
|
return
|
|
}
|
|
})
|
|
return nil
|
|
case aitexttoimagemod.StatusGenerationFailed: // 退款
|
|
if err = webg.VideoDB.Trans(func(t *db.MongoTool) error {
|
|
if _, err = aitexttoimagemod.UpdateByID(t, id, set); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 退款
|
|
if data.DebitAmountCoin == 0 && data.DebitIncomeCoin == 0 { // 总金额为0则不需要退款
|
|
return nil
|
|
}
|
|
|
|
// 退款进入钱包余额
|
|
wallet, err := walletmod.ReturnAmountAndIncome(t, -data.DebitAmountCoin, -data.DebitIncomeCoin, data.UID)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("Handle Callback Edit walletmod.DebitAmount error:%+v:", err), log.Any("uid", data.UID))
|
|
return err
|
|
}
|
|
|
|
var tl []txnmod.TransactionLog
|
|
if data.DebitAmountCoin > 0 {
|
|
tl = append(tl, txnmod.TransactionLog{
|
|
TransNo: primitive.NewObjectID(),
|
|
UID: data.UID,
|
|
Amount: -data.DebitAmountCoin,
|
|
ActualAmount: float64(-data.DebitAmountCoin),
|
|
TranType: txnmod.AiTextToImageDebitGoldReturn.Key(),
|
|
TranTypeInt: int64(txnmod.AiTextToImageDebitGoldReturn),
|
|
Desc: fmt.Sprintf("退还AI绘图金币-%d", data.DebitAmountCoin),
|
|
RealAmount: walletmod.GetRealAmount(wallet),
|
|
})
|
|
}
|
|
if data.DebitIncomeCoin > 0 {
|
|
tl = append(tl, txnmod.TransactionLog{
|
|
TransNo: primitive.NewObjectID(),
|
|
UID: data.UID,
|
|
Amount: -data.DebitIncomeCoin,
|
|
ActualAmount: float64(-data.DebitIncomeCoin),
|
|
TranType: txnmod.AiTextToImageDebitIncomeGoldReturn.Key(),
|
|
TranTypeInt: int64(txnmod.AiTextToImageDebitIncomeGoldReturn),
|
|
Desc: fmt.Sprintf("退还AI绘图收益金币-%d", data.DebitIncomeCoin),
|
|
RealAmount: walletmod.GetRealAmount(wallet),
|
|
})
|
|
}
|
|
|
|
if len(tl) > 0 {
|
|
txnErr := txnmod.InsertManyTransactionLog(t, tl)
|
|
if txnErr != nil {
|
|
log.Error(fmt.Sprintf("Handle ai text to image Callback txnmod.InsertTransactionLog error:%+v:", txnErr), log.Any("uid", data.UID))
|
|
return stderr.ErrDbQueryError
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
if e, ok := err.(stderr.Code); ok {
|
|
return e
|
|
}
|
|
return stderr.ErrDbQueryError
|
|
}
|
|
return nil
|
|
default:
|
|
}
|
|
return nil
|
|
}
|