203 lines
4.9 KiB
Go
203 lines
4.9 KiB
Go
package main
|
||
|
||
import (
|
||
"database/sql"
|
||
"fmt"
|
||
|
||
_ "github.com/go-sql-driver/mysql"
|
||
)
|
||
|
||
// 全局变量
|
||
var (
|
||
// 数据库连接配置
|
||
DBUsername = "root" // 用户名
|
||
DBPassword = "Long6166@@" // 密码
|
||
DBHost = "nj-cynosdbmysql-grp-1v6vxn5f.sql.tencentcdb.com" // 主机
|
||
DBPort = 26247 // 端口
|
||
DBDataBase = "book_center" // 数据库
|
||
DBCharset = "utf8mb4" // 字符集
|
||
|
||
//连接池配置
|
||
MaxOpenConns = 20
|
||
MaxIdleConns = 10
|
||
)
|
||
|
||
// 连接数据库
|
||
func connectDB() (*sql.DB, error) {
|
||
// 数据源名称格式
|
||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local",
|
||
DBUsername,
|
||
DBPassword,
|
||
DBHost,
|
||
DBPort,
|
||
DBDataBase,
|
||
DBCharset)
|
||
// 连接数据库
|
||
db, err := sql.Open("mysql", dsn)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("打开数据库连接失败: %v", err)
|
||
}
|
||
// 设置连接池参数
|
||
db.SetMaxOpenConns(MaxOpenConns) // 最大打开连接数
|
||
db.SetMaxIdleConns(MaxIdleConns) // 最大空闲连接数
|
||
// 测试链接
|
||
err = db.Ping()
|
||
if err != nil {
|
||
return nil, fmt.Errorf("数据库连接测试失败: %v", err)
|
||
}
|
||
return db, nil
|
||
}
|
||
|
||
func selectBookIsbn(db *sql.DB, bookId int) error {
|
||
//// 执行语句
|
||
//query := `SELECT isbn, MAX(create_time) as max_create_time
|
||
//FROM shop_goods_rejection
|
||
//WHERE isbn IS NOT NULL
|
||
//GROUP BY isbn`
|
||
//
|
||
//exec, err := db.Exec(query)
|
||
//fmt.Println(exec, err)
|
||
return nil
|
||
}
|
||
|
||
//func main() {
|
||
//db, err := connectDB()
|
||
//if err != nil {
|
||
// log.Fatal("数据库连接失败:", err)
|
||
//}
|
||
//
|
||
//books, err := GetAllCategoryBooks()
|
||
//if err != nil {
|
||
// fmt.Printf("", err)
|
||
//}
|
||
//if len(books) == 0 {
|
||
// fmt.Printf("数组是空!")
|
||
//}
|
||
//// 查询数据库中不存在的ISBN
|
||
//missingISBNs, err := findMissingISBNs(db, books)
|
||
//if err != nil {
|
||
// log.Fatal("查询失败:", err)
|
||
//}
|
||
//log.Printf("数据库中不存在的ISBN数量: %d", len(missingISBNs))
|
||
//fmt.Println("数据库中不存在的ISBN数组:")
|
||
//for i, isbn := range missingISBNs {
|
||
// fmt.Printf("%d. %s\n", i+1, isbn)
|
||
//}
|
||
//
|
||
//missingISBNsString := strings.Join(missingISBNs, "\n")
|
||
//
|
||
//// 这里您可以将 missingISBNs 数组用于后续处理
|
||
//// 比如保存到文件、插入到另一个表等
|
||
//fmt.Printf("\n新的missingISBNs字符串: %v\n", missingISBNsString)
|
||
//}
|
||
|
||
// 查询数据库中不存在的ISBN
|
||
func findMissingISBNs(db *sql.DB, books []string) ([]string, error) {
|
||
if len(books) == 0 {
|
||
return []string{}, nil
|
||
}
|
||
|
||
// 查询数据库中存在的ISBN
|
||
existingISBNs, err := queryExistingISBNs(db, books)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 创建现有ISBN的映射,用于快速查找
|
||
existingMap := make(map[string]bool)
|
||
for _, isbn := range existingISBNs {
|
||
existingMap[isbn] = true
|
||
}
|
||
|
||
// 创建新数组,存储数据库中不存在的ISBN
|
||
missingISBNs := make([]string, 0)
|
||
for _, isbn := range books {
|
||
if !existingMap[isbn] {
|
||
missingISBNs = append(missingISBNs, isbn)
|
||
}
|
||
}
|
||
|
||
return missingISBNs, nil
|
||
}
|
||
|
||
// 查询数据库中存在的ISBN
|
||
func queryExistingISBNs(db *sql.DB, isbns []string) ([]string, error) {
|
||
if len(isbns) == 0 {
|
||
return []string{}, nil
|
||
}
|
||
|
||
// 分批处理,避免SQL语句过长
|
||
batchSize := 1000
|
||
allExistingISBNs := make([]string, 0)
|
||
|
||
for i := 0; i < len(isbns); i += batchSize {
|
||
end := i + batchSize
|
||
if end > len(isbns) {
|
||
end = len(isbns)
|
||
}
|
||
|
||
batchISBNs := isbns[i:end]
|
||
batchExisting, err := queryBatchISBNs(db, batchISBNs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
allExistingISBNs = append(allExistingISBNs, batchExisting...)
|
||
}
|
||
|
||
return allExistingISBNs, nil
|
||
}
|
||
|
||
// 批量查询ISBN
|
||
func queryBatchISBNs(db *sql.DB, isbns []string) ([]string, error) {
|
||
if len(isbns) == 0 {
|
||
return []string{}, nil
|
||
}
|
||
// 构建IN查询的占位符
|
||
placeholders := make([]string, len(isbns))
|
||
args := make([]interface{}, len(isbns))
|
||
for i, isbn := range isbns {
|
||
placeholders[i] = "?"
|
||
args[i] = isbn
|
||
}
|
||
query := fmt.Sprintf(`
|
||
SELECT isbn
|
||
FROM book_center
|
||
WHERE isbn IN (%s)
|
||
AND del_flag = 0
|
||
`, joinPlaceholders(placeholders, ","))
|
||
|
||
rows, err := db.Query(query, args...)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("查询数据库失败: %v", err)
|
||
}
|
||
defer rows.Close()
|
||
|
||
var existingISBNs []string
|
||
for rows.Next() {
|
||
var isbn string
|
||
if err := rows.Scan(&isbn); err != nil {
|
||
return nil, fmt.Errorf("扫描结果失败: %v", err)
|
||
}
|
||
existingISBNs = append(existingISBNs, isbn)
|
||
}
|
||
|
||
if err := rows.Err(); err != nil {
|
||
return nil, fmt.Errorf("遍历结果失败: %v", err)
|
||
}
|
||
|
||
return existingISBNs, nil
|
||
}
|
||
|
||
// joinPlaceholders 连接占位符
|
||
func joinPlaceholders(placeholders []string, sep string) string {
|
||
if len(placeholders) == 0 {
|
||
return ""
|
||
}
|
||
|
||
result := placeholders[0]
|
||
for i := 1; i < len(placeholders); i++ {
|
||
result += sep + placeholders[i]
|
||
}
|
||
return result
|
||
}
|