142 lines
4.1 KiB
Go
142 lines
4.1 KiB
Go
package models
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
type MysqlConnection struct {
|
|
Database *sql.DB
|
|
}
|
|
|
|
var connection *sql.DB
|
|
|
|
// 初始化数据库连接
|
|
func (m *MysqlConnection) Init() (err error) {
|
|
user := Viper.Get("mysql.user").(string)
|
|
password := Viper.Get("mysql.password").(string)
|
|
host := Viper.Get("mysql.host").(string)
|
|
port := Viper.Get("mysql.port").(int)
|
|
database := Viper.Get("mysql.database").(string)
|
|
sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local"
|
|
connection, err = sql.Open("mysql", sqlconf) // 连接数据库
|
|
if err != nil {
|
|
log.Println("连接数据库失败", err)
|
|
return
|
|
}
|
|
m.Database = connection
|
|
return
|
|
}
|
|
|
|
// 根据图片 id 获取图片的 content
|
|
func (m *MysqlConnection) GetImageContent(group string, id string) (content string, err error) {
|
|
switch group {
|
|
case "video":
|
|
err = m.Database.QueryRow("SELECT image FROM web_sort_video WHERE id=" + id).Scan(&content)
|
|
case "article":
|
|
err = m.Database.QueryRow("SELECT image FROM web_article WHERE id=" + id).Scan(&content)
|
|
case "article_attribute":
|
|
err = m.Database.QueryRow("SELECT image FROM web_article_attribute WHERE id=" + id).Scan(&content)
|
|
case "ad":
|
|
err = m.Database.QueryRow("SELECT image FROM web_ad WHERE id=" + id).Scan(&content)
|
|
case "avatar":
|
|
err = m.Database.QueryRow("SELECT avatar FROM web_member WHERE id=" + id).Scan(&content)
|
|
case "image":
|
|
err = m.Database.QueryRow("SELECT content FROM web_images WHERE id=" + id).Scan(&content)
|
|
default:
|
|
err = errors.New("group 参数错误")
|
|
}
|
|
return content, err
|
|
}
|
|
|
|
// 获取图片列表
|
|
func (m *MysqlConnection) GetImages(page int, size int) (images []byte, err error) {
|
|
rows, err := m.Database.Query("SELECT id, group, content FROM web_images LIMIT ?, ?", (page-1)*size, size)
|
|
if err != nil {
|
|
log.Println("获取图片列表失败", err)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
images = []byte("[")
|
|
for rows.Next() {
|
|
var id int
|
|
var group string
|
|
var content string
|
|
err = rows.Scan(&id, &group, &content)
|
|
if err != nil {
|
|
log.Println("获取图片列表失败", err)
|
|
return
|
|
}
|
|
images = append(images, []byte("{\"id\":"+strconv.Itoa(id)+",\"group\":\""+group+"\",\"content\":\""+content+"\"},")...)
|
|
}
|
|
images = images[:len(images)-1]
|
|
images = append(images, []byte("]")...)
|
|
return
|
|
}
|
|
|
|
type User struct {
|
|
Id int `json:"id"`
|
|
UserName string `json:"user_name"`
|
|
Avatar string `json:"avatar"`
|
|
CreateTime time.Time `json:"create_time"`
|
|
UpdateTime time.Time `json:"update_time"`
|
|
}
|
|
|
|
// 获取一组用户信息
|
|
func QueryUserList(id_list []int) (users []User) {
|
|
count := len(id_list)
|
|
if count == 0 {
|
|
return
|
|
}
|
|
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
|
|
rows, err := connection.Query("SELECT id, user_name, avatar, update_time, create_time FROM web_member WHERE id IN (" + idstr + ") LIMIT " + strconv.Itoa(count))
|
|
if err != nil {
|
|
log.Println("获取用户列表失败", err)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var user User
|
|
rows.Scan(&user.Id, &user.UserName, &user.Avatar, &user.UpdateTime, &user.CreateTime)
|
|
users = append(users, user)
|
|
}
|
|
return
|
|
}
|
|
|
|
type Article struct {
|
|
Id int `json:"id"`
|
|
Title string `json:"title"`
|
|
Tags string `json:"tags"`
|
|
CreateTime time.Time `json:"create_time"`
|
|
UpdateTime time.Time `json:"update_time"`
|
|
}
|
|
|
|
// 获取一组文章信息
|
|
func QueryArticleList(id_list []int) (articles []Article) {
|
|
count := len(id_list)
|
|
if count == 0 {
|
|
return
|
|
}
|
|
idstr := strings.Trim(strings.Replace(fmt.Sprint(id_list), " ", ",", -1), "[]")
|
|
rows, err := connection.Query("SELECT id, title, tags, update_time, create_time FROM web_article WHERE id IN (" + idstr + ")")
|
|
if err != nil {
|
|
log.Println("获取文章列表失败", err)
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var article Article
|
|
rows.Scan(&article.Id, &article.Title, &article.Tags, &article.UpdateTime, &article.CreateTime)
|
|
articles = append(articles, article)
|
|
}
|
|
return
|
|
}
|