From 40986602618b96c26ae64cc343d4be46417db619 Mon Sep 17 00:00:00 2001 From: Hjj <582604932@qq.com> Date: Tue, 10 May 2022 14:33:35 +0800 Subject: [PATCH] test --- coupon_test.go | 29 +++++++++++++ db/db.go | 84 ++++++++++++++++++++++++++++++++++++++ db/db_coupon_comm_check.go | 2 +- init.go | 33 +++++++++++++++ utils/logx/output.go | 5 +-- 5 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 coupon_test.go create mode 100644 db/db.go create mode 100644 init.go diff --git a/coupon_test.go b/coupon_test.go new file mode 100644 index 0000000..025c07c --- /dev/null +++ b/coupon_test.go @@ -0,0 +1,29 @@ +package zyos_go_coupon + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" + "testing" +) + +func TestCouponCommCheck(t *testing.T) { + err := Init("119.23.182.117:3306", "fnuoos_test1", "root", "Fnuo123com@") + if err != nil { + t.Errorf("Reverse == %s", err) + } + req := md.CouponCheckRequest{ + CouponAmount: "", + Uid: "21702", + PvdType: "", + Type: "", + Pvd: "", + Gid: "", + CheckAmount: "", + IsMustReduce: "", + CreateAt: "", + IsChangeState: "", + GoodsTitle: "", + Oid: "", + } + CommCheck(db.Db, req) +} diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..a3266e0 --- /dev/null +++ b/db/db.go @@ -0,0 +1,84 @@ +package db + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" + "database/sql" + "fmt" + _ "github.com/go-sql-driver/mysql" //必须导入mysql驱动,否则会panic + "os" + "time" + "xorm.io/xorm" + "xorm.io/xorm/log" +) + +var Db *xorm.Engine + +//根据DB配置文件初始化数据库 +func InitDB(c *md.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 +} + +/********************************************* 公用方法 *********************************************/ + +// 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 +} + +// InsertCommWithSession common insert +func InsertCommWithSession(session *xorm.Session, model interface{}) (int64, error) { + row, err := session.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 { + return nil, err + } + return result, nil +} diff --git a/db/db_coupon_comm_check.go b/db/db_coupon_comm_check.go index 7419791..d11366a 100644 --- a/db/db_coupon_comm_check.go +++ b/db/db_coupon_comm_check.go @@ -1,4 +1,4 @@ -package db +package db import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" diff --git a/init.go b/init.go new file mode 100644 index 0000000..6e8a427 --- /dev/null +++ b/init.go @@ -0,0 +1,33 @@ +package zyos_go_coupon + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" +) + +var cfg = md.DBCfg{ + Host: "", + Name: "", + User: "", + Psw: "", + ShowLog: false, + MaxLifetime: 0, + MaxOpenConns: 0, + MaxIdleConns: 0, + Path: "", +} + +func Init(host, name, user, psw string) error { + cfg.Host = host + cfg.Name = name + cfg.User = user + cfg.Psw = psw + cfg.ShowLog = true + cfg.MaxLifetime = 30 + cfg.MaxOpenConns = 100 + cfg.MaxIdleConns = 100 + if err := db.InitDB(&cfg); err != nil { + return err + } + return nil +} diff --git a/utils/logx/output.go b/utils/logx/output.go index 6e6810a..2e6fbe7 100644 --- a/utils/logx/output.go +++ b/utils/logx/output.go @@ -1,13 +1,12 @@ -package logx +package logx import ( "bytes" + "gopkg.in/natefinch/lumberjack.v2" "io" "os" "path/filepath" "time" - - "gopkg.in/natefinch/lumberjack.v2" ) // output interface