Commit 76978df8 by Joneq

提交代码

parent 97dc9094
package scm_data_dao
import (
"database/sql"
"fmt"
"log"
_ "github.com/lib/pq"
"github.com/ichunt2019/cfg/lib"
_"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"log"
)
//初始化数据库
func GetScmDataDb() {
sqlx.Open("mysql")
func GetScmDataDb()*sqlx.DB {
db,err := sqlx.Open("mysql",lib.Instance("db").GetString("scm_data.dns"))
if err != nil {
fmt.Println(err)
log.Fatalln(err)
}
return db
}
package main
import (
"flag"
"fmt"
"github.com/ichunt2019/cfg/lib"
"golang-asynctask/app/dao/scm_data_dao"
"sync"
"time"
"github.com/hprose/hprose-golang/rpc"
......@@ -21,20 +24,36 @@ var (
"SYS_OPERATE_LOG",
}
wg sync.WaitGroup
)
func init(){
var (
configPath string
logPath string
)
flag.StringVar(&configPath, "config", "./config/dev/", "配置文件")
flag.StringVar(&logPath, "logdir", "./logs/", "日志文件存储目录")
flag.Parse()
err := lib.Init(configPath)
if err != nil{
panic(err)
}
return
}
//同步海关信息至中间库
func main() {
fmt.Println(lib.Instance("db").GetString("scm_data"))
panic(1)
//获取当天时间
var currentDate = time.Now().Format("2006-01-02")
currentDate = "2021-05-07"
//循环获取表名,然后获取表内当天信息,并且存入mysql,协程处理
for _,v := range syncTable {
wg.Add(1)
......@@ -50,22 +69,40 @@ func main() {
//处理当前的这个表任务
func handleCurrentTable(tableName,currentDate string) {
defer wg.Done()
fmt.Println(currentDate+"开始同步"+tableName)
currentJson := requestTableInfo(tableName,currentDate)
if currentJson == "" {
return
}
//var sqlInsert string = "insert into "+ tableName
sqlDb := scm_data_dao.GetScmDataDb()
var sqlKeyValue []map[string]interface{}
var currenKeyValue = make(map[string]interface{})
for _, res := range gjson.Parse(currentJson).Array() {
//sqlkey
res.ForEach(func(key, value gjson.Result) bool {
//获取key和val并拼接字符串
currenKeyValue[key.String()] = value.String()
return true
})
sqlKeyValue = append(sqlKeyValue,currenKeyValue)
}
var keystring,valstring string
for k,_ := range sqlKeyValue[0] {
keystring += k+","
valstring += ":"+k+","
}
_, err := sqlDb.NamedExec(`INSERT INTO `+tableName+` (`+keystring[:len(keystring)-1]+`)VALUES (`+valstring[:len(valstring)-1]+`)`, sqlKeyValue)
if err != nil {
fmt.Println(err)
}
}
......
......@@ -13,6 +13,7 @@ require (
github.com/ichunt2019/golang-rbmq-sl v0.0.0-20200515075131-59a37ab77d7d
github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042
github.com/jmoiron/sqlx v1.3.3
github.com/lib/pq v1.7.0
github.com/spf13/viper v1.7.1
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.8
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment