WorldInfo.go 9.73 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package models

import (
	"WorldEpcho/src/datasource"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/go-xorm/xorm"
)

/*
ID 世界名称 简介 角色 背景 标签 世界基数 粉丝量 话题数 平行世界量 交互量 创建时间
*/

type WorldInfo struct {
	ID             int64    `xorm:"'id' pk autoincr" json:"id"`
	Name           string   `xorm:"'name' unique" json:"name"`
	Description    string   `xorm:"'description'" json:"description"`
	Role           string   `xorm:"'role'" json:"role"`
	Background     string   `xorm:"'background'" json:"background"`
	Tags           []string `xorm:"'tags' json" json:"tags"`
	Creator        int64    `xorm:"'creator'" json:"creator" `
	BaseNumber     int      `xorm:"'base_number'" json:"baseNumber"`
	Fans           int      `xorm:"'fans'" json:"fans"`
	TopicCount     int      `xorm:"'topic_count'" json:"topicCount"`
	ParallelWorlds int      `xorm:"'parallel_worlds'" json:"parallelWorlds"`
	Interaction    int      `xorm:"'interaction'" json:"interaction"`
	ShowHome       string   `xorm:"show_home" json:"ShowHome"`                     //首页展示 onTitle bool
	WorldHeader    string   `xorm:"world_header" json:"worldHeader"`               //世界标头 varchar
	WorldParameter string   `xorm:" 'world_parameter' json" json:"worldParameter"` //	显化参量 JSON
	CreatedAt      int64    `xorm:"'created_at' created" json:"createdAt"`
	ConfigData     string   `xorm:" 'config_data' json" json:"configData"` //保存考核定制面板的参数数据json
}

//世界信息增删改查
// AddWorldInfo adds new world information to the datasource and returns the newly added world ID upon success
func AddWorldInfo(session *xorm.Session, worldInfo *WorldInfo) (int64, error) {
	_, err := session.Insert(worldInfo)
	if err != nil {
		return 0, err // Return 0 if there's an error since ID cannot be negative
	}
	return worldInfo.ID, nil // Return the new world's ID

}

46 47 48 49 50 51 52 53 54
func CreateWorldInfo(session *xorm.Session, worldInfo *WorldInfo) (*WorldInfo, error) {
	_, err := session.Insert(worldInfo)
	if err != nil {
		return nil, err // Return 0 if there's an error since ID cannot be negative
	}
	return worldInfo, nil // Return the new world's ID

}

Ford committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
//根据世界id查询世界信息
func GetWorld(session *xorm.Session, id int64) (*WorldInfo, error) {
	world := &WorldInfo{}
	_, err := session.ID(id).Get(world)
	return world, err
}

// GetWorldInfoByName 根据世界名称查询世界对象
func GetWorldInfoByName(worldName string) (*WorldInfo, error) {
	var worldInfo WorldInfo
	has, err := datasource.Engine.Where("name = ?", worldName).Get(&worldInfo)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return &worldInfo, nil
}

//根据世界名称查询世界信息
func GetWorldInfoByName_Tx(session *xorm.Session, worldName string) (*WorldInfo, bool, error) {
	var worldInfo WorldInfo
	has, err := session.Where("name = ?", worldName).Get(&worldInfo)
	if err != nil {
		return nil, false, err
	}
	if !has {
		return nil, false, nil
	}
	return &worldInfo, true, nil
}

//根据世界Id查询世界信息
func GetWorldInfoById_Tx(session *xorm.Session, id int64) (*WorldInfo, bool, error) {
	var worldInfo WorldInfo
	has, err := session.Where("id = ?", id).Get(&worldInfo)
	if err != nil {
		return nil, false, err
	}
	if !has {
		return nil, false, nil
	}
	return &worldInfo, true, nil
}
func GetWorldInfoById(id int64) (*WorldInfo, bool, error) {
	var worldInfo WorldInfo
	has, err := datasource.Engine.Where("id = ?", id).Get(&worldInfo)
	if err != nil {
		return nil, false, err
	}
	if !has {
		return nil, false, nil
	}
	return &worldInfo, true, nil
}

func UpdateWorld(session *xorm.Session, w *WorldInfo) error {
	affected, err := session.ID(w.ID).Update(w)
	return err
	if affected == 0 {
		return errors.New("修改世界信息出错")
	}
	return nil
}

func DeleteWorld(id int64) error {
	_, err := datasource.Engine.ID(id).Delete(&WorldInfo{})
	return err
}

// 查询所有世界信息
func ListWorlds() ([]*WorldInfo, error) {
	var worlds []*WorldInfo
	err := datasource.Engine.Find(&worlds)
	return worlds, err
}

// 根据世界名称模糊查询所有世界信息
func QueryWorldsByLikeName(name string) ([]WorldInfo, error) {
	var worlds []WorldInfo
	err := datasource.Engine.Where("name like ?", "%"+name+"%").Find(&worlds)
	if err != nil {
		return nil, err
	}
	return worlds, nil
}

// WorldInfoAndAsset 结构体用于联表查询的结果封装
type WorldInfoAndAsset struct {
	WorldInfo  `xorm:"extends"`
	WorldAsset `xorm:"extends"`
}

// GetWorldInfoAndAssetByID 根据世界ID获取世界信息和资产信息
func GetWorldInfoAndAssetByID_Tx(session *xorm.Session, worldId int64) (*WorldInfoAndAsset, error) {
	// 定义查询结果的结构体
	var worldInfoAndAsset WorldInfoAndAsset

	// 进行联表查询
	has, err := session.Table("world_info").
		Join("LEFT", "world_asset", "world_info.id = world_asset.world_id").
		Where("world_info.id = ?", worldId).Get(&worldInfoAndAsset)
	if err != nil {
		return nil, err
	}

	if !has {
		return nil, errors.New("没有找到对应的世界信息")
	}

	// 封装查询结果返回
	return &worldInfoAndAsset, nil
}

func GetWorldInfoAndAssetByID(worldId int64) (*WorldInfoAndAsset, error) {
	// 定义查询结果的结构体
	var worldInfoAndAsset WorldInfoAndAsset

	// 进行联表查询
	has, err := datasource.Engine.Table("world_info").
		Join("LEFT", "world_asset", "world_info.id = world_asset.world_id").
		Where("world_info.id = ?", worldId).Get(&worldInfoAndAsset)
	if err != nil {
		return nil, err
	}

	if !has {
		return nil, errors.New("没有找到对应的世界信息")
	}

	// 封装查询结果返回
	return &worldInfoAndAsset, nil
}

// QueryWorldsByLikeName 根据世界名称模糊查询所有世界信息和世界资产信息
func QueryWorldsByLikeName2(name string) ([]WorldInfoAndAsset, error) {
	var worldInfoAndAssets []WorldInfoAndAsset
	// 需要确保在 WorldInfo 和 WorldAsset 中 WorldID 是关联的键
	err := datasource.Engine.Table("world_info").Alias("wi").
		Join("INNER", []string{"world_asset", "wa"}, "wi.id = wa.world_id").
		Where("wi.name like ?", "%"+name+"%").
		Find(&worldInfoAndAssets)
	if err != nil {
		return nil, err
	}
	return worldInfoAndAssets, nil
}

//根据世界id获取世界配置信息
func GetWorldConfigDataByID(id int64) (string, error) {
	var world WorldInfo
	has, err := datasource.Engine.Where("id = ?", id).Get(&world)
	if err != nil {
		return "", err
	}
	if !has {
		return "", fmt.Errorf("no world found with ID %d", id)
	}

	configData := world.ConfigData
	return configData, nil
}

// FindWorldsByTagAndName
func FindWorldsByTagAndName(WorldName string, tag string) (*WorldInfo, string, error) {
	var world WorldInfo
	// 确保 tag 被引号包围,形成有效的 JSON 字符串
	tagJSON := fmt.Sprintf("[\"%s\"]", tag) // 将tag转换为JSON格式的字符串,并确保使用双引号
	has, err := datasource.Engine.Table("world_info").
		Where("name = ? ", WorldName).And("JSON_CONTAINS(tags, ?)", tagJSON).Get(&world)
	if err != nil {
		return nil, "", err // 数据库查询错误
	}
	if !has {
		return nil, "", nil // 未找到记录
	}
	return &world, world.ConfigData, nil
}

// 根据世界名称模糊查询和包含的世界标签查询世界信息
func FindWorldsByTagAndName2(WorldName string, tag string) ([]WorldInfo, string, error) {
	var world []WorldInfo

	// 构建查询条件
	query := datasource.Engine.Table("world_info")
	if WorldName != "" {
		query = query.Where("name LIKE ?", "%"+WorldName+"%")
	}
	if tag != "" {
		tagJSON := fmt.Sprintf("[\"%s\"]", tag) // 将 tag 转换为 JSON 格式的字符串,并确保使用双引号
		query = query.And("JSON_CONTAINS(tags, ?)", tagJSON)
	}

	// 执行查询
	err := query.Find(&world)
	if err != nil {
		return nil, "", err // 数据库查询错误
	}

	if len(world) == 0 {
		return nil, "", nil // 没有找到任何世界信息
	}

	return world, world[0].ConfigData, nil // 假设 ConfigData 存储在每个 WorldInfo 结构中,并假设至少有一个结果
}

func FindWorldsByTagAndName3(WorldName string, tags []string) ([]WorldInfo, string, error) {
	var world []WorldInfo

	// 构建查询条件
	query := datasource.Engine.Table("world_info")
	if WorldName != "" {
		query = query.Where("name LIKE ?", "%"+WorldName+"%")
	}
	if len(tags) > 0 {
		tagsJSON, err := json.Marshal(tags) // 将 tags 列表转换为 JSON 数组
		if err != nil {
			return nil, "", err // JSON 格式化错误
		}
		query = query.And("JSON_CONTAINS(tags, ?)", string(tagsJSON))
	}

	// 执行查询
	err := query.Find(&world)
	if err != nil {
		return nil, "", err // 数据库查询错误
	}

	if len(world) == 0 {
		return nil, "", nil // 没有找到任何世界信息
	}

	return world, world[0].ConfigData, nil // 假设 ConfigData 存储在每个 WorldInfo 结构中,并假设至少有一个结果
}

func FindWorlds(name string, tags []string) ([]WorldInfo, error) {
	var worlds []WorldInfo
	query := datasource.Engine.Table("world_info")
	if name != "" {
		query = query.Where("name = ?", name)
	}

	if len(tags) > 0 {
		for _, tag := range tags {
			tagJSON := fmt.Sprintf("\"%s\"", tag) // 使用双引号确保格式正确
			query = query.And("JSON_CONTAINS(tags, ?)", tagJSON)
		}
	}

	err := query.Find(&worlds)
	if err != nil {
		return nil, err
	}

	return worlds, nil
}

func FindWorlds2(name string, tags []string) ([]WorldInfo, error) {
	var worlds []WorldInfo
	query := datasource.Engine.Table("world_info")
	if name != "" {
		query = query.Where("name = ?", name)
	}

	if len(tags) > 0 {
		for _, tag := range tags {
			tagJSON := fmt.Sprintf("\"%s\"", tag) // 使用双引号确保格式正确
			// 确保只对有有效 JSON 的记录进行查询
			query = query.Where("JSON_VALID(tags) = 1").And("JSON_CONTAINS(tags, ?)", tagJSON)
		}
	}

	err := query.Find(&worlds)
	if err != nil {
		return nil, err
	}

	return worlds, nil
}