12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package main
- import (
- "fieldproject_common/config"
- "fmt"
- "sync"
- "time"
- "utils"
- "utils/log"
- "utils/mongodb"
- "utils/mysqldb"
- )
- var (
- MongoTool *mongodb.MongodbSim
- MysqlTool *mysqldb.Mysql
- )
- func init() {
- config.Init("./common.toml")
- InitLog()
- InitMgo()
- InitMysql()
- log.Info("init success")
- }
- func main() {
- task()
- }
- func task() {
- sess := MongoTool.GetMgoConn()
- defer MongoTool.DestoryMongoConn(sess)
- ch := make(chan bool, 2)
- wg := &sync.WaitGroup{}
- log.Info(fmt.Sprintf("%d", MongoTool.Count("zktest_mysql_company_info", nil)))
- field := map[string]interface{}{"use_flag": 0, "province_short": 0, "create_time": 0, "update_time": 0}
- query := sess.DB(config.Conf.DB.Mongo.Dbname).C("zktest_mysql_company_info").Find(nil).Select(field).Sort("-_id").Iter()
- count := 0
- for tmp := make(map[string]interface{}); query.Next(tmp); count++ {
- if count%2000 == 0 {
- log.Info(fmt.Sprintf("current --- %d", count))
- }
- ch <- true
- wg.Add(1)
- go func(tmp map[string]interface{}) {
- defer func() {
- <-ch
- wg.Done()
- }()
- delete(tmp, "_id")
- m := make(map[string]interface{})
- if util.ObjToString(tmp["district"]) != "" {
- m["district"] = tmp["district"]
- } else if util.ObjToString(tmp["city"]) != "" {
- m["city"] = tmp["city"]
- } else {
- m["area"] = tmp["area"]
- }
- if len(m) > 0 {
- info := MysqlTool.FindOne("code_area", m, "", "")
- if info != nil && len(*info) > 0 {
- tmp["areacode"] = (*info)["code"]
- } else {
- tmp["areacode"] = "000000"
- }
- } else {
- tmp["areacode"] = "000000"
- }
- delete(tmp, "area")
- delete(tmp, "city")
- delete(tmp, "district")
- tmp["comeintime"] = time.Now()
- tmp["updatetime"] = time.Now()
- tmp["sourcetype"] = 1
- MysqlTool.Insert("company_baseinfo", tmp)
- }(tmp)
- tmp = make(map[string]interface{})
- }
- wg.Wait()
- log.Info(fmt.Sprintf("over --- %d", count))
- }
|