dengbiao 1 miesiąc temu
rodzic
commit
1c19fb6099
5 zmienionych plików z 122 dodań i 3 usunięć
  1. +112
    -0
      app/db/db.go
  2. +1
    -1
      consume/init.go
  3. +1
    -1
      consume/md/consume_key.go
  4. +7
    -0
      go.mod
  5. +1
    -1
      main.go

+ 112
- 0
app/db/db.go Wyświetl plik

@@ -0,0 +1,112 @@
package db

import (
"database/sql"
"fmt"
"os"
"time"

_ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic
"xorm.io/xorm"
"xorm.io/xorm/log"

"applet/app/cfg"
"applet/app/utils/logx"
)

var Db *xorm.Engine

//根据DB配置文件初始化数据库
func InitDB(c *cfg.DBCfg) error {
var (
err error
f *os.File
)
//创建Orm引擎
if Db, err = xorm.NewEngine("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4", c.User, c.Psw, c.Host, c.Name)); err != nil {
return err
}
Db.SetConnMaxLifetime(c.MaxLifetime * time.Second) //设置最长连接时间
Db.SetMaxOpenConns(c.MaxOpenConns) //设置最大打开连接数
Db.SetMaxIdleConns(c.MaxIdleConns) //设置连接池的空闲数大小
if err = Db.Ping(); err != nil { //尝试ping数据库
return err
}
if c.ShowLog { //根据配置文件设置日志
Db.ShowSQL(true) //设置是否打印sql
Db.Logger().SetLevel(0) //设置日志等级
//修改日志文件存放路径文件名是%s.log
path := fmt.Sprintf(c.Path, c.Name)
f, err = os.OpenFile(path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777)
if err != nil {
os.RemoveAll(c.Path)
if f, err = os.OpenFile(c.Path, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0777); err != nil {
return err
}
}
logger := log.NewSimpleLogger(f)
logger.ShowSQL(true)
Db.SetLogger(logger)
}
return nil
}

/********************************************* 公用方法 *********************************************/

// 数据批量插入
func DbInsertBatch(Db *xorm.Engine, m ...interface{}) error {
if len(m) == 0 {
return nil
}
id, err := Db.Insert(m...)
if id == 0 || err != nil {
return logx.Warn("cannot insert data :", err)
}
return nil
}

// QueryNativeString 查询原生sql
func QueryNativeString(Db *xorm.Engine, sql string, args ...interface{}) ([]map[string]string, error) {
results, err := Db.SQL(sql, args...).QueryString()
return results, err
}

// UpdateComm common update
func UpdateComm(Db *xorm.Engine, id interface{}, model interface{}) (int64, error) {
row, err := Db.ID(id).Update(model)
return row, err
}

// InsertComm common insert
func InsertComm(Db *xorm.Engine, model interface{}) (int64, error) {
row, err := Db.InsertOne(model)
return row, err
}

// ExecuteOriginalSql 执行原生sql
func ExecuteOriginalSql(session *xorm.Session, sql string) (sql.Result, error) {
result, err := session.Exec(sql)
if err != nil {
_ = logx.Warn(err)
return nil, err
}
return result, nil
}

// GetComm
// payload *model
// return *model,has,err
func GetComm(Db *xorm.Engine, model interface{}) (interface{}, bool, error) {
has, err := Db.Get(model)
if err != nil {
_ = logx.Warn(err)
return nil, false, err
}
return model, has, nil
}

// InsertCommWithSession common insert
func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) {
row, err := session.InsertOne(model)
return row, err
}

+ 1
- 1
consume/init.go Wyświetl plik

@@ -17,7 +17,7 @@ func Init() {

// 增加消费任务队列
func initConsumes() {
jobs[consumeMd.JudgePackageOrdStateQueueConsumeFunName] = JudgePackageOrdStateQueueConsume
jobs[consumeMd.EggEnergyStartLevelDividendFunName] = EggEnergyStartLevelDividendConsume
}

func Run() {


+ 1
- 1
consume/md/consume_key.go Wyświetl plik

@@ -18,7 +18,7 @@ var RabbitMqQueueKeyList = []*MqQueue{
IsPersistent: false,
RoutKey: "star_level_dividend",
BindKey: "",
ConsumeFunName: "JudgePackageOrdStateQueueConsume",
ConsumeFunName: "EggEnergyStartLevelDividendConsume",
},
}



+ 7
- 0
go.mod Wyświetl plik

@@ -32,13 +32,16 @@ require (
require (
code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241114063419-cb68a0ed34ee // indirect
code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.2 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/gookit/color v1.3.6 // indirect
github.com/json-iterator/go v1.1.12 // indirect
@@ -52,6 +55,8 @@ require (
github.com/onsi/gomega v1.19.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/syndtr/goleveldb v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/atomic v1.7.0 // indirect
@@ -68,4 +73,6 @@ require (
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.0.1-2020.1.4 // indirect
xorm.io/builder v0.3.13 // indirect
xorm.io/xorm v1.3.1 // indirect
)

+ 1
- 1
main.go Wyświetl plik

@@ -2,10 +2,10 @@ package main

import (
"applet/app/cfg"
"applet/app/db"
"applet/app/utils"
"applet/app/utils/logx"
"applet/consume"
db "code.fnuoos.com/EggPlanet/egg_models.git/src"
"fmt"
"os"
"os/signal"


Ładowanie…
Anuluj
Zapisz