diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9a8f686 --- /dev/null +++ b/.gitignore @@ -0,0 +1,46 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +# Test binary, built with `go test -c` +*.test +# Output of the go coverage tool, specifically when used with LiteIDE +*.out +.idea +.vscode +*.log +.DS_Store +Thumbs.db +*.swp +*.swn +*.swo +*.swm +*.7z +*.zip +*.rar +*.tar +*.tar.gz +go.sum +/etc/cfg.yaml +images +test/test.json +etc/cfg.yml +t.json +t1.json +t2.json +t3.json +t.go +wait-for-it.sh +test.go +xorm +test.csv +nginx.conf +.devcontainer +.devcontainer/Dockerfile +.devcontainer/sources.list +/t1.go +/tmp/* +.idea/* +/.idea/modules.xml diff --git a/db/db.go b/db/db.go new file mode 100644 index 0000000..317445a --- /dev/null +++ b/db/db.go @@ -0,0 +1,84 @@ +package db + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.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/model/pay_channel.go b/db/model/pay_channel.go new file mode 100644 index 0000000..702a641 --- /dev/null +++ b/db/model/pay_channel.go @@ -0,0 +1,18 @@ +package model + +import "time" + +type PayChannel struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + Name string `json:"name" xorm:"not null comment('渠道名称') VARCHAR(255)"` + IsUse int `json:"is_use" xorm:"not null default 1 comment('是否开启;1:开启;0:关闭') TINYINT(1)"` + ThirdPartyId string `json:"third_party_id" xorm:"not null comment('第三方聚合支付的渠道id') VARCHAR(20)"` + Kind string `json:"kind" xorm:"not null comment('渠道种类(self:自有支付 zhiYing:智莺支付 thirdParty 第三方聚合支付 )') VARCHAR(255)"` + ModuleIdentifier string `json:"module_identifier" xorm:"not null comment('模块标识符') VARCHAR(255)"` + ModuleName string `json:"module_name" xorm:"not null comment('模块名称') VARCHAR(255)"` + CommissionRate string `json:"commission_rate" xorm:"not null comment('抽成比例(千分之)') VARCHAR(255)"` + Memo string `json:"memo" xorm:"not null comment('备注') VARCHAR(255)"` + Ext string `json:"ext" xorm:"comment('拓展字段(json存储)') TEXT"` + CreateAt time.Time `json:"create_at" xorm:"default 'CURRENT_TIMESTAMP' comment('创建时间') TIMESTAMP"` + UpdateAt time.Time `json:"update_at" xorm:"default 'CURRENT_TIMESTAMP' comment('更新时间') TIMESTAMP"` +} diff --git a/db/model/pay_channel_master.go b/db/model/pay_channel_master.go new file mode 100644 index 0000000..8fc86cc --- /dev/null +++ b/db/model/pay_channel_master.go @@ -0,0 +1,10 @@ +package model + +type PayChannelMaster struct { + Id int `json:"id" xorm:"not null pk autoincr INT(11)"` + ChannelId int `json:"channel_id" xorm:"not null default 1 comment('支付通道id') INT(11)"` + Identifier string `json:"identifier" xorm:"not null comment('场景标识符') VARCHAR(255)"` + MasterId int `json:"master_id" xorm:"not null default 1 comment('站长id') INT(11)"` + MasterNickname string `json:"master_nickname" xorm:"not null comment('站长名称') VARCHAR(255)"` + AppId int `json:"app_id" xorm:"not null default 1 comment('应用id(mid)') INT(20)"` +} diff --git a/go.mod b/go.mod index da0dc62..31b77c9 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git go 1.16 + +require ( + github.com/go-sql-driver/mysql v1.6.0 + xorm.io/builder v0.3.10 // indirect + xorm.io/xorm v1.3.0 +) diff --git a/md/cfg_app.go b/md/cfg_app.go new file mode 100644 index 0000000..c380303 --- /dev/null +++ b/md/cfg_app.go @@ -0,0 +1,16 @@ +package md + +import "time" + +//数据库配置结构体 +type DBCfg struct { + Host string `yaml:"host"` //ip及端口 + Name string `yaml:"name"` //库名 + User string `yaml:"user"` //用户 + Psw string `yaml:"psw"` //密码 + ShowLog bool `yaml:"show_log"` //是否显示SQL语句 + MaxLifetime time.Duration `yaml:"max_lifetime"` + MaxOpenConns int `yaml:"max_open_conns"` + MaxIdleConns int `yaml:"max_idle_conns"` + Path string `yaml:"path"` //日志文件存放路径 +} diff --git a/pay/init.go b/pay/init.go new file mode 100644 index 0000000..6ebe556 --- /dev/null +++ b/pay/init.go @@ -0,0 +1,34 @@ +package pay + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.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 + cfg.Path = "tmp/%s.log" + if err := db.InitDB(&cfg); err != nil { + return err + } + return nil +} diff --git a/pay/pay.go b/pay/pay.go new file mode 100644 index 0000000..4b2b2e5 --- /dev/null +++ b/pay/pay.go @@ -0,0 +1,20 @@ +package pay + +import ( + "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db" + "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db/model" + "errors" +) + +func JudgePayChannel(mid, paySceneIdentifier string) (model.PayChannel, error) { + var payChannelMaster = model.PayChannelMaster{} + var payChannel = model.PayChannel{} + _, err := db.Db.Where("app_id = ?", mid).And("identifier = ?", paySceneIdentifier).Get(&payChannelMaster) + if err != nil { + return payChannel, err + } + if payChannelMaster.ChannelId == 0 { + return payChannel, errors.New("请联系服务商绑定支付渠道") + } + return payChannel, nil +} diff --git a/pay/pay_test.go b/pay/pay_test.go new file mode 100644 index 0000000..f3158c0 --- /dev/null +++ b/pay/pay_test.go @@ -0,0 +1,17 @@ +package pay + +import ( + "testing" +) + +func TestPay(t *testing.T) { + err := Init("119.23.182.117:3306", "zyos_website", "root", "Fnuo123com@") + if err != nil { + t.Errorf("Reverse == %s", err) + } + channel, err := JudgePayChannel("35618318", "mall") + if err != nil { + t.Errorf("Reverse == %s", err) + } + t.Log(channel.Name) +}