|
@@ -0,0 +1,264 @@
|
|
|
+package data
|
|
|
+
|
|
|
+import (
|
|
|
+ . "app.yhyue.com/moapp/jybase/encrypt"
|
|
|
+ elastic "app.yhyue.com/moapp/jybase/es"
|
|
|
+ "app.yhyue.com/moapp/jybase/mongodb"
|
|
|
+ "context"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gogf/gf/v2/frame/g"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
+ "strings"
|
|
|
+ "sync"
|
|
|
+ "time"
|
|
|
+ "workTasks/common"
|
|
|
+)
|
|
|
+
|
|
|
+type (
|
|
|
+ ProjectMsg struct {
|
|
|
+ Id string `json:"id" doc:"项目ID"`
|
|
|
+ Name string `json:"name" doc:"项目名字"`
|
|
|
+ Code string `json:"code" doc:"项目编号"`
|
|
|
+ Scope string `json:"scope" doc:"项目范围"`
|
|
|
+ }
|
|
|
+ EntMsg struct {
|
|
|
+ Person string `json:"person" doc:"联系人"`
|
|
|
+ Tel string `json:"tel" doc:"电话"`
|
|
|
+ CreditNo string `json:"creditNo" doc:"社会统一信用代码"`
|
|
|
+ Mail string `json:"mail" doc:"邮箱"`
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+func RangeBidding(ctx context.Context, st, ed time.Time, callBack func([]g.Map)) (int64, error) {
|
|
|
+ sess := common.MG.DB("bidding").GetMgoConn()
|
|
|
+ defer common.MG.DB("bidding").DestoryMongoConn(sess)
|
|
|
+
|
|
|
+ var (
|
|
|
+ selectMap = map[string]interface{}{}
|
|
|
+ cut = g.Cfg().MustGet(ctx, "fileDataCut").Int()
|
|
|
+ )
|
|
|
+ for _, key := range []string{"_id", "area", "city", "title", "toptype", "detail", "publishtime", "href", "budget", "bidamount", "bidopentime", "buyer", "buyerperson", "buyertel", "agency", "agencyperson", "agencytel", "bidopentime", "bidendtime", "s_topscopeclass", "s_winner", "winnerperson", "winnertel"} {
|
|
|
+ selectMap[key] = 1
|
|
|
+ }
|
|
|
+ it := sess.DB(common.MG.DB("bidding").DbName).C("bidding").Find(g.Map{
|
|
|
+ "pici": g.Map{
|
|
|
+ "$gte": st.Unix(),
|
|
|
+ "$lt": ed.Unix(),
|
|
|
+ },
|
|
|
+ }).Select(selectMap).Iter()
|
|
|
+ var (
|
|
|
+ index int64
|
|
|
+ bidSet []g.Map
|
|
|
+ )
|
|
|
+ for m := make(map[string]interface{}); it.Next(&m); {
|
|
|
+ index++
|
|
|
+ if index%10e4 == 0 {
|
|
|
+ g.Log().Infof(context.TODO(), "%s load %d数据", st.Format(time.DateOnly), index)
|
|
|
+ }
|
|
|
+ bidSet = append(bidSet, m)
|
|
|
+
|
|
|
+ if len(bidSet)%cut == 0 {
|
|
|
+ callBack(formatData(ctx, bidSet))
|
|
|
+ bidSet = []g.Map{}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(bidSet) > 0 {
|
|
|
+ callBack(formatData(ctx, bidSet))
|
|
|
+ }
|
|
|
+ return index, nil
|
|
|
+}
|
|
|
+
|
|
|
+// formatData 补充其他字段
|
|
|
+func formatData(ctx context.Context, bidList []g.Map) []g.Map {
|
|
|
+ var (
|
|
|
+ projectMsgMapping = map[string]*ProjectMsg{}
|
|
|
+ pLock sync.RWMutex
|
|
|
+ entMsgMapping = map[string]*EntMsg{}
|
|
|
+ eLock sync.RWMutex
|
|
|
+
|
|
|
+ ids []string
|
|
|
+ ent []string
|
|
|
+ )
|
|
|
+ for _, m := range bidList {
|
|
|
+ var (
|
|
|
+ bid = mongodb.BsonIdToSId(m["_id"])
|
|
|
+ buyer = mongodb.BsonIdToSId(m["buyer"])
|
|
|
+ s_winner = mongodb.BsonIdToSId(m["s_winner"])
|
|
|
+ )
|
|
|
+ ids = append(ids, bid)
|
|
|
+ if buyer != "" {
|
|
|
+ ent = append(ent, buyer)
|
|
|
+ }
|
|
|
+ if strings.Index(s_winner, ",") > -1 {
|
|
|
+ for _, s := range strings.Split(s_winner, ",") {
|
|
|
+ if name := strings.TrimSpace(s); name != "" {
|
|
|
+ ent = append(ent, name)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if s_winner != "" {
|
|
|
+ ent = append(ent, s_winner)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //查询项目数据
|
|
|
+ batchQuery(ctx, ids, "projectset", "projectset", `{"query":{"bool":{"must":[{"terms":{"ids":["%s"]}}],"must_not":[],"should":[]}},"size":%d,"_source":["projectcode","projectname","projectscope","id","ids"]}`, func(maps *[]map[string]interface{}) error {
|
|
|
+ if maps != nil && len(*maps) > 0 {
|
|
|
+ pLock.Lock()
|
|
|
+ defer pLock.Unlock()
|
|
|
+ fmt.Println("projectset", len(*maps))
|
|
|
+ for _, m := range *maps {
|
|
|
+ var (
|
|
|
+ projectcode = gconv.String(m["projectcode"])
|
|
|
+ projectname = gconv.String(m["projectname"])
|
|
|
+ projectscope = gconv.String(m["projectscope"])
|
|
|
+ projectId = gconv.String(m["id"])
|
|
|
+ ids = gconv.Strings(m["ids"])
|
|
|
+ )
|
|
|
+ d := ProjectMsg{
|
|
|
+ Id: projectId,
|
|
|
+ Name: projectname,
|
|
|
+ Code: projectcode,
|
|
|
+ Scope: projectscope,
|
|
|
+ }
|
|
|
+ for _, bidId := range ids {
|
|
|
+ projectMsgMapping[bidId] = &d
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+
|
|
|
+ //查询企业数据
|
|
|
+ batchQuery(ctx, ent, "qyxy", "qyxy", `{"query":{"bool":{"must":[{"terms":{"name.ent_name":["%s"]}}]}},"size":%d,"_source":["credit_no","legal_person","company_email","company_phone","company_name"]}`, func(maps *[]map[string]interface{}) error {
|
|
|
+ if maps != nil && len(*maps) > 0 {
|
|
|
+ eLock.Lock()
|
|
|
+ defer eLock.Unlock()
|
|
|
+ fmt.Println("qyxy", len(*maps))
|
|
|
+ for _, m := range *maps {
|
|
|
+ var (
|
|
|
+ credit_no = gconv.String(m["credit_no"])
|
|
|
+ legal_person = gconv.String(m["legal_person"])
|
|
|
+ company_email = gconv.String(m["company_email"])
|
|
|
+ company_phone = gconv.String(m["company_phone"])
|
|
|
+ company_name = gconv.String(m["company_name"])
|
|
|
+ )
|
|
|
+ entMsgMapping[company_name] = &EntMsg{
|
|
|
+ Person: legal_person,
|
|
|
+ Tel: company_phone,
|
|
|
+ CreditNo: credit_no,
|
|
|
+ Mail: company_email,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+ })
|
|
|
+
|
|
|
+ //填充数据
|
|
|
+ for i := 0; i < len(bidList); i++ {
|
|
|
+ var (
|
|
|
+ id = mongodb.BsonIdToSId(bidList[i]["_id"])
|
|
|
+ buyer = gconv.String(bidList[i]["buyer"])
|
|
|
+ s_winner = gconv.String(bidList[i]["s_winner"])
|
|
|
+
|
|
|
+ buyerCreditNo string //社会统一信用代码
|
|
|
+ winnersCreditNo, winnersPhone, winnersMail, winnersPerson string
|
|
|
+ projectName, projectScope, projectCode, projectId string
|
|
|
+ )
|
|
|
+
|
|
|
+ if buyer != "" {
|
|
|
+ if buyerMsg := entMsgMapping[buyer]; buyerMsg != nil {
|
|
|
+ buyerCreditNo = buyerMsg.CreditNo
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if projectMsg := projectMsgMapping[id]; projectMsg != nil {
|
|
|
+ projectName = projectMsg.Name
|
|
|
+ projectScope = projectMsg.Scope
|
|
|
+ projectCode = projectMsg.Code
|
|
|
+ projectId = projectMsg.Id
|
|
|
+ }
|
|
|
+
|
|
|
+ //拼接企业信用代码
|
|
|
+ if s_winner != "" {
|
|
|
+ if strings.Index(s_winner, ",") > -1 {
|
|
|
+ for i, s := range strings.Split(s_winner, ",") {
|
|
|
+ if name := strings.TrimSpace(s); name != "" {
|
|
|
+ if i != 0 {
|
|
|
+ winnersCreditNo += ","
|
|
|
+ winnersPhone += ","
|
|
|
+ winnersMail += ","
|
|
|
+ winnersPerson += ","
|
|
|
+ }
|
|
|
+ if winnerMsg := entMsgMapping[name]; winnerMsg != nil {
|
|
|
+ winnersCreditNo += winnerMsg.CreditNo
|
|
|
+ winnersPhone += winnerMsg.Tel
|
|
|
+ winnersMail += winnerMsg.Mail
|
|
|
+ winnersPerson += winnerMsg.Person
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if winnerMsg := entMsgMapping[s_winner]; winnerMsg != nil {
|
|
|
+ winnersCreditNo = winnerMsg.CreditNo
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ bidList[i]["bid"] = id
|
|
|
+ bidList[i]["buyerCreditNo"] = buyerCreditNo
|
|
|
+ bidList[i]["winnersCreditNo"] = winnersCreditNo
|
|
|
+ bidList[i]["winnersEntTel"] = winnersPhone
|
|
|
+ bidList[i]["winnersEntMail"] = winnersMail
|
|
|
+ bidList[i]["winnersEntPerson"] = winnersPerson
|
|
|
+ bidList[i]["projectName"] = projectName
|
|
|
+ bidList[i]["projectScope"] = projectScope
|
|
|
+ bidList[i]["projectCode"] = projectCode
|
|
|
+ bidList[i]["projectId"] = projectId
|
|
|
+ if id != "" {
|
|
|
+ encodeId := CommonEncodeArticle("content", id)
|
|
|
+ bidList[i]["jyHref"] = "https://www.jianyu360.cn/article/content/" + encodeId + ".html"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ g.Dump("cccc", len(entMsgMapping), len(projectMsgMapping))
|
|
|
+ return bidList
|
|
|
+}
|
|
|
+
|
|
|
+func batchQuery(ctx context.Context, fullData []string, index, itype, query string, callBack func(*[]map[string]interface{}) error) {
|
|
|
+ var (
|
|
|
+ wg sync.WaitGroup
|
|
|
+ batchNum = 100
|
|
|
+ ll = len(fullData)
|
|
|
+ )
|
|
|
+ count := ll / batchNum
|
|
|
+ if ll%batchNum != 0 {
|
|
|
+ count = count + 1
|
|
|
+ }
|
|
|
+ jobs := make(chan []string, count)
|
|
|
+ for i := 0; i < g.Cfg().MustGet(ctx, "esPoolSize", 5).Int(); i++ {
|
|
|
+ wg.Add(1)
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+ //查询企业数据
|
|
|
+ for siteInfo := range jobs {
|
|
|
+ query := fmt.Sprintf(query, strings.Join(siteInfo, "\",\""), batchNum)
|
|
|
+ fmt.Println(query)
|
|
|
+ if err := callBack(elastic.Get(index, itype, query)); err != nil {
|
|
|
+ g.Log().Errorf(ctx, "batchQuery 查询异常%s", err.Error())
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ }
|
|
|
+
|
|
|
+ for i := 0; i < count; i++ {
|
|
|
+ var (
|
|
|
+ s = i * batchNum
|
|
|
+ e = s + batchNum
|
|
|
+ )
|
|
|
+ if e > ll {
|
|
|
+ e = ll
|
|
|
+ }
|
|
|
+ jobs <- fullData[s : e-1]
|
|
|
+ }
|
|
|
+ close(jobs)
|
|
|
+ wg.Wait()
|
|
|
+}
|