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

91 lines
3.0 KiB
Go
Raw Permalink Normal View History

2022-01-12 14:34:31 +08:00
package model
import (
"database/sql/driver"
"encoding/json"
"fmt"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type (
ints []int
strs []string
)
type Scripts struct {
ScriptName string `json:"script_name" gorm:"column:script_name"`
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
ScriptTag ints `json:"script_tag" gorm:"column:script_tag"`
ScriptScore float64 `json:"script_score" gorm:"column:script_score"`
GroupDuration int `json:"group_duration" gorm:"column:group_duration"`
ScriptCoverUrl string `json:"script_cover_url" gorm:"column:script_cover_url"`
ScriptTextContext string `json:"script_text_context" gorm:"column:script_text_context"`
ScriptPlotScore float64 `json:"script_plot_score" gorm:"column:script_plot_score"`
ScriptImageContent strs `json:"script_image_content" gorm:"column:script_image_content"`
ScriptMalePlayer int `json:"script_male_player" gorm:"column:script_male_player"`
ScriptFemalePlayer int `json:"script_female_player" gorm:"column:script_female_player"`
ScriptDifficultDegree string `json:"script_difficult_degree" gorm:"column:script_difficult_degree"`
ScriptPlayerLimit int `json:"script_player_limit" gorm:"column:script_player_limit"`
Uuid string `json:"uuid" gorm:"column:uuid" valid:"no_empty"`
ScriptComplexScore float64 `json:"script_complex_score" gorm:"column:script_complex_score"`
Qid string `json:"qid" gorm:"column:qid"`
IsDel int `json:"-" gorm:"column:is_del"`
}
func (m *Scripts) TableName() string {
return "scripts"
}
func (p ints) Value() (driver.Value, error) {
return json.Marshal(p)
}
func (p *ints) Scan(input interface{}) error {
return json.Unmarshal(input.([]byte), &p)
}
func (p strs) Value() (driver.Value, error) {
return json.Marshal(p)
}
func (p *strs) Scan(input interface{}) error {
return json.Unmarshal(input.([]byte), &p)
}
func GetScriptsM(page, size int, query QueryMap, db *gorm.DB) ([]Scripts, int64, error) {
i := new([]Scripts)
num := int64(0)
for k, v := range query {
if v.rule == normal {
db = db.Where(fmt.Sprintf("%v = ?", k), v.value)
}
if v.rule == jsonArray {
db = db.Where(v.value)
}
}
if num = db.Find(i).RowsAffected; num < 0 {
return nil, 0, error_process.GetErr(errors.New(error_process.GormError), query)
}
if size > 0 {
db = db.Limit(size).Offset((page - 1) * size)
}
db.Find(i)
return *i, num, nil
}
func UpdateScriptsM(scripts *Scripts, db *gorm.DB) error {
if num := db.Model(Scripts{}).Where("uuid = ?", scripts.Uuid).Updates(scripts).RowsAffected; num != 1 {
return error_process.UpdateErr(scripts, scripts.Uuid)
}
return nil
}
func CreateScriptsM(script *Scripts, db *gorm.DB) error {
if err := db.Create(script).Error; err != nil {
return error_process.CreateErr(err, script)
}
return nil
}