123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package main
- import (
- "context"
- "encoding/json"
- "fmt"
- elastic "qfw/common/src/qfw/util/elastic"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo/options"
- "gopkg.in/mgo.v2/bson"
- "log"
- "sort"
- "strings"
- "time"
- )
- //定时任务
- //1.存异常表
- //2.合并原始库新增
- func TimedTask() {
- t2 := time.NewTimer(time.Second * 5)
- for range t2.C {
- tmpLast := map[string]interface{}{}
- if err := FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").FindOne(context.TODO(), bson.M{}, options.FindOne().SetSort(bson.M{"_id": -1})).Decode(&tmpLast); err != nil {
- //临时表无数据
- log.Println("临时表无数据:", err)
- t2.Reset(time.Minute * 5)
- continue
- } else {
- //临时表有数据
- log.Println("临时表有数据:", tmpLast)
- cursor, err := FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").Find(context.TODO(), bson.M{
- "_id": bson.M{
- "$lte": tmpLast["_id"],
- },
- }, options.Find().SetSort(bson.M{"_id": 1}))
- if err != nil {
- log.Println(err)
- t2.Reset(time.Second * 5)
- continue
- }
- //遍历临时表数据,匹配不到原始库存入异常表
- for cursor.Next(context.TODO()) {
- tmp := make(map[string]interface{})
- if err := cursor.Decode(&tmp); err == nil {
- resulttmp := make(map[string]interface{})
- r := FClient.Database(Config["mgodb_enterprise"]).Collection(Config["mgodb_enterprise_c"]).FindOne(context.TODO(), bson.M{"company_name": tmp["winner"]}).Decode(&resulttmp)
- if r != nil {
- //log.Println(r)
- //匹配不到原始库,存入异常表删除临时表
- FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_err").InsertOne(context.TODO(), tmp)
- FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").DeleteOne(context.TODO(), tmp)
- continue
- } else {
- //log.Println(123)
- //匹配到原始库,新增 resulttmp
- if resulttmp["credit_no"] != nil {
- if credit_no, ok := resulttmp["credit_no"].(string); ok && strings.TrimSpace(credit_no) != "" &&
- len(strings.TrimSpace(credit_no)) > 8 {
- dataNo := strings.TrimSpace(credit_no)[2:8]
- if Addrs[dataNo] != nil {
- if v, ok := Addrs[dataNo].(map[string]interface{}); ok {
- if resulttmp["province"] == nil || resulttmp["province"] == "" {
- resulttmp["province"] = v["province"]
- }
- resulttmp["city"] = v["city"]
- resulttmp["district"] = v["district"]
- }
- }
- }
- }
- contacts := make([]map[string]interface{}, 0)
- contact := make(map[string]interface{}, 0)
- if resulttmp["legal_person"] != nil {
- contact["contact_person"] = resulttmp["legal_person"] //联系人
- } else {
- contact["contact_person"] = "" //联系人
- }
- contact["contact_type"] = "法定代表人" //法定代表人
- //log.Println(1)
- if resulttmp["annual_reports"] != nil {
- bytes, err := json.Marshal(resulttmp["annual_reports"])
- if err != nil {
- log.Println("annual_reports err:", err)
- }
- phonetmp := make([]map[string]interface{}, 0)
- err = json.Unmarshal(bytes, &phonetmp)
- if err != nil {
- log.Println("Unmarshal err:", err)
- }
- for _, vv := range phonetmp {
- if vv["company_phone"] != nil {
- if vv["company_phone"] == "" {
- continue
- } else {
- contact["phone"] = vv["company_phone"] //联系电话
- break
- }
- } else {
- contact["phone"] = "" //联系电话
- }
- }
- }
- //log.Println(k, contact["phone"], resulttmp["_id"])
- //time.Sleep(10 * time.Second)
- if contact["phone"] == nil {
- contact["phone"] = "" //联系电话
- }
- contact["topscopeclass"] = "企业公示" //项目类型
- contact["updatetime"] = time.Now().Unix() //更新时间
- contacts = append(contacts, contact)
- resulttmp["contact"] = contacts
- savetmp := make(map[string]interface{}, 0)
- for _, sk := range Fields {
- if sk == "establish_date" {
- if resulttmp[sk] != nil {
- savetmp[sk] = resulttmp[sk].(primitive.DateTime).Time().UTC().Unix()
- continue
- }
- } else if sk == "capital" {
- //log.Println(sk, resulttmp[sk])
- savetmp[sk] = ObjToMoney([]interface{}{resulttmp[sk], ""})[0]
- continue
- } else if sk == "partners" {
- //log.Println(sk, resulttmp[sk], )
- //fmt.Println(reflect.TypeOf(resulttmp[sk]))
- if resulttmp[sk] != nil {
- if ppms, ok := resulttmp[sk].(primitive.A); ok {
- for i, _ := range ppms {
- if ppms[i].(map[string]interface{})["stock_type"] != nil {
- ppms[i].(map[string]interface{})["stock_type"] = "企业公示"
- }
- delete(ppms[i].(map[string]interface{}), "identify_type")
- }
- savetmp[sk] = ppms
- }
- }else {
- savetmp[sk] = []interface{}{}
- }
- continue
- } else if sk == "_id" {
- savetmp["tmp"+sk] = resulttmp[sk]
- continue
- } else if sk == "area_code" {
- //行政区划代码
- savetmp[sk] = fmt.Sprint(resulttmp[sk])
- continue
- } else if sk == "report_websites" {
- //网址
- if resulttmp["report_websites"] == nil {
- savetmp["website"] = ""
- } else {
- report_websitesArr := []string{}
- if ppms, ok := resulttmp[sk].(primitive.A); ok {
- for _, v := range ppms {
- if vvv, ok := v.(map[string]interface{}); ok {
- if rv, ok := vvv["website_url"].(string); ok {
- report_websitesArr = append(report_websitesArr, rv)
- }
- }
- }
- }
- sort.Strings(report_websitesArr)
- savetmp["website"] = strings.Join(report_websitesArr, ";")
- }
- continue
- } else if sk == "wechat_accounts" {
- savetmp[sk] = []interface{}{}
- continue
- }
- if resulttmp[sk] == nil && sk != "history_name" &&sk != "wechat_accounts" && sk != "establish_date" && sk != "capital" && sk != "partners" && sk != "contact" && sk != "report_websites" {
- savetmp[sk] = ""
- } else {
- savetmp[sk] = resulttmp[sk]
- }
- }
- //tmps = append(tmps, savetmp)
- savetmp["updatatime"] =time.Now().Unix()
- //保存mongo
- result, err := FClient.Database(Config["mgodb_extract_kf"]).Collection(Config["mgo_qyk_c"]).
- InsertOne(context.TODO(), savetmp)
- if err == nil {
- //保存redis
- rc := RedisPool.Get()
- defer rc.Close()
- var _id string
- if v, ok := result.InsertedID.(primitive.ObjectID); ok {
- _id = v.Hex()
- }
- if _, err := rc.Do("SET", savetmp["company_name"], result.InsertedID.(primitive.ObjectID).Hex()); err != nil {
- log.Println("save redis err:", tmp["_id"], savetmp["_id"], savetmp["company_name"], err)
- } else {
- //保存es
- delete(savetmp, "_id")
- esConn := elastic.GetEsConn()
- defer elastic.DestoryEsConn(esConn)
- if _, err := esConn.Index().Index(Config["elasticsearch_index"]).Type(Config["elasticsearch_type"]).Id(_id).BodyJson(savetmp).Refresh(true).Do(); err != nil {
- log.Println("save es err :", tmp["_id"], savetmp["_id"], err)
- } else {
- //删除临时表
- FClient.Database(Config["mgodb_extract_kf"]).Collection("winner_new").DeleteOne(context.TODO(), tmp)
- }
- }
- } else {
- log.Println("save mongo err:", err, tmp["_id"])
- }
- }
- }
- }
- defer cursor.Close(context.TODO())
- }
- t2.Reset(time.Minute)
- }
- }
|