Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
孙龙
/
golang_asynctask
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
76978df8
authored
May 10, 2021
by
Joneq
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
提交代码
parent
97dc9094
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
14 deletions
app/dao/scm_data_dao/dao.go
cmd/scm/sync_customs/main.go
go.mod
app/dao/scm_data_dao/dao.go
View file @
76978df8
package
scm_data_dao
package
scm_data_dao
import
(
import
(
"database/sql"
"fmt"
"fmt"
"log"
"github.com/ichunt2019/cfg/lib"
_
"github.com/go-sql-driver/mysql"
_
"github.com/lib/pq"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx"
"log"
)
)
//初始化数据库
//初始化数据库
func
GetScmDataDb
()
{
func
GetScmDataDb
()
*
sqlx
.
DB
{
db
,
err
:=
sqlx
.
Open
(
"mysql"
,
lib
.
Instance
(
"db"
)
.
GetString
(
"scm_data.dns"
))
sqlx
.
Open
(
"mysql"
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
log
.
Fatalln
(
err
)
}
return
db
}
}
cmd/scm/sync_customs/main.go
View file @
76978df8
package
main
package
main
import
(
import
(
"flag"
"fmt"
"fmt"
"github.com/ichunt2019/cfg/lib"
"github.com/ichunt2019/cfg/lib"
"golang-asynctask/app/dao/scm_data_dao"
"sync"
"sync"
"time"
"time"
"github.com/hprose/hprose-golang/rpc"
"github.com/hprose/hprose-golang/rpc"
...
@@ -21,20 +24,36 @@ var (
...
@@ -21,20 +24,36 @@ var (
"SYS_OPERATE_LOG"
,
"SYS_OPERATE_LOG"
,
}
}
wg
sync
.
WaitGroup
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
()
{
func
main
()
{
fmt
.
Println
(
lib
.
Instance
(
"db"
)
.
GetString
(
"scm_data"
))
panic
(
1
)
//获取当天时间
//获取当天时间
var
currentDate
=
time
.
Now
()
.
Format
(
"2006-01-02"
)
var
currentDate
=
time
.
Now
()
.
Format
(
"2006-01-02"
)
currentDate
=
"2021-05-07"
//循环获取表名,然后获取表内当天信息,并且存入mysql,协程处理
//循环获取表名,然后获取表内当天信息,并且存入mysql,协程处理
for
_
,
v
:=
range
syncTable
{
for
_
,
v
:=
range
syncTable
{
wg
.
Add
(
1
)
wg
.
Add
(
1
)
...
@@ -50,22 +69,40 @@ func main() {
...
@@ -50,22 +69,40 @@ func main() {
//处理当前的这个表任务
//处理当前的这个表任务
func
handleCurrentTable
(
tableName
,
currentDate
string
)
{
func
handleCurrentTable
(
tableName
,
currentDate
string
)
{
defer
wg
.
Done
()
fmt
.
Println
(
currentDate
+
"开始同步"
+
tableName
)
fmt
.
Println
(
currentDate
+
"开始同步"
+
tableName
)
currentJson
:=
requestTableInfo
(
tableName
,
currentDate
)
currentJson
:=
requestTableInfo
(
tableName
,
currentDate
)
if
currentJson
==
""
{
if
currentJson
==
""
{
return
return
}
}
//var sqlInsert string = "insert into "+ tableName
//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
()
{
for
_
,
res
:=
range
gjson
.
Parse
(
currentJson
)
.
Array
()
{
//sqlkey
//sqlkey
res
.
ForEach
(
func
(
key
,
value
gjson
.
Result
)
bool
{
res
.
ForEach
(
func
(
key
,
value
gjson
.
Result
)
bool
{
//获取key和val并拼接字符串
//获取key和val并拼接字符串
currenKeyValue
[
key
.
String
()]
=
value
.
String
()
return
true
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
)
}
}
}
...
...
go.mod
View file @
76978df8
...
@@ -13,6 +13,7 @@ require (
...
@@ -13,6 +13,7 @@ require (
github.com/ichunt2019/golang-rbmq-sl v0.0.0-20200515075131-59a37ab77d7d
github.com/ichunt2019/golang-rbmq-sl v0.0.0-20200515075131-59a37ab77d7d
github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042
github.com/ichunt2019/lxLog v0.0.0-20210226024426-781becb3c042
github.com/jmoiron/sqlx v1.3.3
github.com/jmoiron/sqlx v1.3.3
github.com/lib/pq v1.7.0
github.com/spf13/viper v1.7.1
github.com/spf13/viper v1.7.1
github.com/syyongx/php2go v0.9.4
github.com/syyongx/php2go v0.9.4
github.com/tidwall/gjson v1.6.8
github.com/tidwall/gjson v1.6.8
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment