backend/app/brahma/api/murder/internal/service/scripts.go

100 lines
2.6 KiB
Go

package service
import (
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/gorm_helper"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/model"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/util"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
"strconv"
)
func GetScripts(page, limit int, query model.QueryMap, ctx *fiber.Ctx) ([]model.ScriptsDto, int64, error) {
var (
db *gorm.DB
err error
)
if db, err = gorm_helper.GetContextDB(ctx); err != nil {
return nil, 0, err
}
queryMap := make(util.QueryMap)
m := make(map[int]string)
queryMap["type"] = "script"
tag, _, err := model.GetTagM(-1, -1, queryMap, db)
if err != nil {
return nil, 0, err
}
for _, v := range tag {
m[v.Uuid] = v.Value
}
script, num, err := model.GetScriptsM(page, limit, query, db)
if err != nil {
return nil, 0, err
}
dtos := make([]model.ScriptsDto, len(script))
for k, v := range script {
tags := make([]string, len(v.ScriptTag))
for k1, v1 := range v.ScriptTag {
if tag1, ok := m[v1]; !ok {
return nil, 0, error_process.EmptyValueErr(strconv.Itoa(k1))
} else {
tags[k1] = tag1
}
}
dtos[k] = model.ScriptsDto{
ScriptName: v.ScriptName,
ScriptIntro: v.ScriptIntro,
ScriptTag: tags,
ScriptScore: v.ScriptScore,
GroupDuration: v.GroupDuration,
ScriptCoverUrl: v.ScriptCoverUrl,
ScriptTextContext: v.ScriptTextContext,
ScriptPlotScore: v.ScriptPlotScore,
ScriptImageContent: v.ScriptImageContent,
ScriptMalePlayer: v.ScriptMalePlayer,
ScriptFemalePlayer: v.ScriptFemalePlayer,
ScriptDifficultDegree: v.ScriptDifficultDegree,
ScriptPlayerLimit: v.ScriptPlayerLimit,
Uuid: v.Uuid,
ScriptComplexScore: v.ScriptComplexScore,
}
}
return dtos, num, nil
}
func UpdateScripts(scripts *model.Scripts, ctx *fiber.Ctx) error {
var (
db *gorm.DB
err error
)
if db, err = gorm_helper.GetContextDB(ctx); err != nil {
return err
}
return model.UpdateScriptsM(scripts, db)
}
func CreateScripts(scripts *model.Scripts, ctx *fiber.Ctx) error {
var (
db *gorm.DB
err error
)
if db, err = gorm_helper.GetContextDB(ctx); err != nil {
return err
}
return model.CreateScriptsM(scripts, db)
}
func DeleteScripts(scripts *model.Scripts, ctx *fiber.Ctx) error {
var (
db *gorm.DB
err error
)
if db, err = gorm_helper.GetContextDB(ctx); err != nil {
return err
}
scripts.IsDel = 1
return model.UpdateScriptsM(scripts, db)
}