generated from pkg/go-template
feat: murder项目整体迁移
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
|
||||
bgm "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/util"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type (
|
||||
Category struct {
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Uuid string `json:"uuid" gorm:"primary_key" valid:"no_empty"`
|
||||
IsDel int `json:"-" gorm:"column:is_del"`
|
||||
Tags *[]Tag `json:"tags,omitempty" gorm:"many2many:c_tag;foreignKey:Uuid;joinForeignKey:CategoryUid;References:Uuid;JoinReferences:TagUid" valid:"no_empty"`
|
||||
}
|
||||
CategoriesTagDto struct {
|
||||
TIds []string `json:"tag_ids" valid:"no_empty"`
|
||||
Cid string `json:"category_id" valid:"no_empty"`
|
||||
}
|
||||
)
|
||||
|
||||
func (m *Category) TableName() string {
|
||||
return "category"
|
||||
}
|
||||
|
||||
func GetCategoryM(page, size int, query bgm.QueryMap, db *gorm.DB) ([]Category, int64, error) {
|
||||
i := new([]Category)
|
||||
num := int64(0)
|
||||
for k, v := range query {
|
||||
db = db.Where(fmt.Sprintf("%v = ?", k), v)
|
||||
}
|
||||
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)
|
||||
}
|
||||
if err := db.Preload("Tags").Find(i).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return *i, num, nil
|
||||
}
|
||||
|
||||
func CreateCategoryM(category *Category, db *gorm.DB) error {
|
||||
if err := db.Create(category).Error; err != nil {
|
||||
return error_process.CreateErr(err, category)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateCategoryM(category *Category, db *gorm.DB) error {
|
||||
if num := db.Model(Category{}).Where("uuid = ?", category.Uuid).Updates(*category).RowsAffected; num != 1 {
|
||||
return error_process.UpdateErr(category, category.Uuid)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CreateTagForCategoryM(cid string, tIds []string, db *gorm.DB) error {
|
||||
c := new(Category)
|
||||
//step1 getCategory
|
||||
if err := db.Where(&Category{
|
||||
Uuid: cid,
|
||||
}).Find(c).Error; err != nil {
|
||||
return error_process.GetErr(err, c)
|
||||
}
|
||||
//step2 get tags and check num is expected
|
||||
var ts []Tag
|
||||
if num := db.Where(tIds).Find(&ts).RowsAffected; num != cast.ToInt64(len(tIds)) {
|
||||
return error_process.RecordNotMatchWithUniqueErr(tIds, len(tIds), cast.ToInt(num))
|
||||
}
|
||||
//step3 add association
|
||||
if err := db.Model(c).Association("Tags").Append(ts); err != nil {
|
||||
return error_process.CommonErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DelTagForCategoryM(cid string, tIds []string, db *gorm.DB) error {
|
||||
c := new(Category)
|
||||
//step1 getCategory
|
||||
if err := db.Where(&Category{
|
||||
Uuid: cid,
|
||||
}).Find(c).Error; err != nil {
|
||||
return error_process.GetErr(err, c)
|
||||
}
|
||||
//step2 get tags and check num is expected
|
||||
var ts []Tag
|
||||
if num := db.Where(tIds).Find(&ts).RowsAffected; num != cast.ToInt64(len(tIds)) {
|
||||
return error_process.RecordNotMatchWithUniqueErr(tIds, len(tIds), cast.ToInt(num))
|
||||
}
|
||||
//step3 add association
|
||||
if err := db.Model(c).Association("Tags").Delete(ts); err != nil {
|
||||
return error_process.CommonErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
|
||||
"github.com/pkg/errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
jsonArray = "jsonArr"
|
||||
normal = "normal"
|
||||
)
|
||||
|
||||
type (
|
||||
//区分query中不同规则
|
||||
ruleType struct {
|
||||
rule string
|
||||
value string
|
||||
}
|
||||
QueryMap map[string]ruleType
|
||||
)
|
||||
|
||||
type GetSqlMapFromQuery interface {
|
||||
// ProcessMap 生成queryMap
|
||||
ProcessMap() (QueryMap, error)
|
||||
}
|
||||
|
||||
type GetQuery struct {
|
||||
Query string
|
||||
}
|
||||
|
||||
type ScriptsDto struct {
|
||||
ScriptName string `json:"script_name" gorm:"column:script_name"`
|
||||
ScriptIntro string `json:"script_intro" gorm:"column:script_intro"`
|
||||
ScriptTag strs `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"`
|
||||
}
|
||||
|
||||
//检查语法
|
||||
func checkGrammar(value string) bool {
|
||||
//如果 = 左右两侧 不是完整的key、value返回错误
|
||||
var pairs []string
|
||||
if pairs = strings.Split(value, "="); len(pairs) != 2 {
|
||||
return false
|
||||
}
|
||||
//如果 value 没有完整闭合括号则返回错误
|
||||
bytes := []byte(pairs[1])
|
||||
if (bytes[0] == '(' && bytes[len(bytes)-1] != ')') || (bytes[len(bytes)-1] == ')' && bytes[0] != '(') {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
//处理json数组
|
||||
func processJsonArr(pair []string) (string, bool) {
|
||||
value := fmt.Sprintf("JSON_CONTAINS(%v,JSON_ARRAY(", pair[0])
|
||||
//处理括号,遍历元素
|
||||
arrs := strings.Split(string([]byte(pair[1])[1:len([]byte(pair[1]))-1]), "|")
|
||||
for _, v := range arrs {
|
||||
//如果大括号里面没有元素
|
||||
if v == "" {
|
||||
return "", false
|
||||
}
|
||||
value = fmt.Sprintf("%v%v,", value, v)
|
||||
}
|
||||
s := []byte(value)
|
||||
value = string(s[0:len(s)-1]) + "))"
|
||||
return value, true
|
||||
|
||||
}
|
||||
|
||||
// ProcessMap 生成查询map
|
||||
func (g GetQuery) ProcessMap() (QueryMap, error) {
|
||||
m := make(QueryMap)
|
||||
//寻找没有软删掉的数据
|
||||
m["is_del"] = ruleType{rule: normal, value: `0`}
|
||||
//不满足最小查询条件直接返回
|
||||
if len([]byte(g.Query)) < 5 {
|
||||
return m, nil
|
||||
}
|
||||
//括号不匹配直接返回
|
||||
if bytes := []byte(g.Query); bytes[0] != '(' || bytes[len(bytes)-1] != ')' {
|
||||
return nil, errors.New(error_process.GrammerError)
|
||||
}
|
||||
//处理query字符串、掐头去尾
|
||||
g.Query = string([]byte(g.Query)[1 : len([]byte(g.Query))-1])
|
||||
//遍历处理过的字符串,根据query规则生成queryMap
|
||||
for _, v := range strings.Split(g.Query, ",") {
|
||||
if !checkGrammar(v) {
|
||||
return nil, errors.New(error_process.GrammerError)
|
||||
}
|
||||
pairs := strings.Split(v, "=")
|
||||
bytes := []byte(pairs[1])
|
||||
//是否是json数组
|
||||
if bytes[0] == '(' && bytes[len(bytes)-1] == ')' {
|
||||
if value, usable := processJsonArr(pairs); usable {
|
||||
m[pairs[0]] = ruleType{
|
||||
rule: jsonArray,
|
||||
value: value,
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, errors.New(error_process.GrammerError)
|
||||
}
|
||||
//普通字段
|
||||
m[pairs[0]] = ruleType{
|
||||
rule: normal,
|
||||
value: pairs[1],
|
||||
}
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
|
||||
bgm "git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/util"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cast"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type (
|
||||
Tag struct {
|
||||
Value string `json:"value" gorm:"column:value"`
|
||||
Uuid int `json:"uuid" gorm:"primary_key" valid:"no_empty"`
|
||||
IsDel int `json:"-" gorm:"column:is_del" value:"1|0"`
|
||||
Cs *[]Category `json:"categories,omitempty" gorm:"many2many:c_tag;foreignKey:Uuid;joinForeignKey:TagUid;References:Uuid;JoinReferences:CategoryUid" valid:"no_empty"`
|
||||
}
|
||||
// TagCategoriesDto 为标签新增分组
|
||||
TagCategoriesDto struct {
|
||||
CIds []string `json:"category_ids" valid:"no_empty"`
|
||||
Tid int `json:"tag_id" valid:"no_empty"`
|
||||
}
|
||||
)
|
||||
|
||||
func (m *Tag) TableName() string {
|
||||
return "tag"
|
||||
}
|
||||
|
||||
func CreateTagM(tag *Tag, db *gorm.DB) error {
|
||||
if err := db.Create(tag).Error; err != nil {
|
||||
return error_process.CreateErr(err, tag)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateTagM(tag *Tag, db *gorm.DB) error {
|
||||
if num := db.Model(Tag{}).Where("uuid = ?", tag.Uuid).Updates(*tag).RowsAffected; num != 1 {
|
||||
return error_process.UpdateErr(tag, fmt.Sprintf("uuid:%v", tag.Uuid))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetTagM(page, size int, query bgm.QueryMap, db *gorm.DB) ([]Tag, int64, error) {
|
||||
t := new([]Tag)
|
||||
num := int64(0)
|
||||
for k, v := range query {
|
||||
db = db.Where(fmt.Sprintf("%v = ?", k), v)
|
||||
}
|
||||
if num = db.Find(t).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)
|
||||
}
|
||||
if err := db.Preload("Cs").Find(t).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return *t, num, nil
|
||||
}
|
||||
|
||||
func CreateCategoryForTagM(tid int, cIds []string, db *gorm.DB) error {
|
||||
t := new(Tag)
|
||||
//step1 getTag
|
||||
if err := db.Where(&Tag{
|
||||
Uuid: tid,
|
||||
}).Find(t).Error; err != nil {
|
||||
return error_process.GetErr(err, t)
|
||||
}
|
||||
//step2 get categories and check num is expected
|
||||
var cs []Category
|
||||
if num := db.Where(cIds).Find(&cs).RowsAffected; num != cast.ToInt64(len(cIds)) {
|
||||
return error_process.RecordNotMatchWithUniqueErr(cIds, len(cIds), cast.ToInt(num))
|
||||
}
|
||||
//step3 add association
|
||||
if err := db.Model(t).Association("Cs").Append(cs); err != nil {
|
||||
return error_process.CommonErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DelCategoryForTagM(tid int, cIds []string, db *gorm.DB) error {
|
||||
t := new(Tag)
|
||||
//step1 checkTagNum
|
||||
if err := db.Where(&Tag{
|
||||
Uuid: tid,
|
||||
}).Find(t).Error; err != nil {
|
||||
return error_process.GetErr(err, t)
|
||||
}
|
||||
//step2 check cIdsNum
|
||||
var cs []Category
|
||||
if num := db.Where(cIds).Find(&cs).RowsAffected; num != cast.ToInt64(len(cs)) {
|
||||
return error_process.RecordNotMatchWithUniqueErr(cIds, len(cIds), cast.ToInt(num))
|
||||
}
|
||||
//step3 add association
|
||||
err := db.Model(t).Association("Cs").Delete(cs)
|
||||
if err != nil {
|
||||
return error_process.CommonErr(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user