wangchuanjin 1 anno fa
parent
commit
30107b22f0
2 ha cambiato i file con 123 aggiunte e 13 eliminazioni
  1. 94 0
      api/internal/service/CoopHistoryService.go
  2. 29 13
      api/internal/service/owner.go

+ 94 - 0
api/internal/service/CoopHistoryService.go

@@ -71,6 +71,11 @@ type ProjectTmp struct {
 	PropertyForm []string `ch:"property_form"`
 }
 
+type Cooperate struct {
+	Near   bool
+	ZbTime int64
+}
+
 func GetPrList(req *types.CoopHistoryReq) (result []*ResultData, size_1, size_2, size_3, size_4 int64) {
 	pTmp := ProjectTmp{}
 	err := T.ClickhouseConn.QueryRow(context.TODO(), sql_2_0, req.Pid).ScanStruct(&pTmp)
@@ -323,3 +328,92 @@ func LastTimeCoop(buyerId, ent, stype string) (bool, int64) {
 	}
 	return near, zbtime
 }
+
+//
+func LastTimeCoopBath(buyerIds, winners, agencys []string) (map[string]map[string]*Cooperate, map[string]map[string]*Cooperate) {
+	adiffb, agency := map[string]map[string]*Cooperate{}, map[string]map[string]*Cooperate{}
+	if len(buyerIds) == 0 {
+		return adiffb, agency
+	}
+	var toSearch = func(tp int, query string, args []interface{}) {
+		rows, err := T.ClickhouseConn.Query(context.Background(), query, args...)
+		if err != nil {
+			logx.Error(err)
+			return
+		}
+		for rows.Next() {
+			var (
+				buyer_id string
+				one      string
+				zbtime   int64
+			)
+			if err := rows.Scan(&buyer_id, &one, &zbtime); err != nil {
+				logx.Error(err)
+				continue
+			}
+			c := &Cooperate{
+				ZbTime: zbtime,
+			}
+			timestamp := time.Now().AddDate(-3, 0, 0).Unix()
+			if timestamp <= c.ZbTime {
+				c.Near = true
+			}
+			if tp == 1 {
+				if adiffb[buyer_id] == nil {
+					adiffb[buyer_id] = map[string]*Cooperate{}
+				}
+				adiffb[buyer_id][one] = c
+			} else {
+				if agency[buyer_id] == nil {
+					agency[buyer_id] = map[string]*Cooperate{}
+				}
+				agency[buyer_id][one] = c
+			}
+		}
+		rows.Close()
+		if err := rows.Err(); err != nil {
+			logx.Error(err)
+		}
+	}
+	if len(winners) > 0 {
+		sql := `SELECT buyer_id,winner_one,max(zbtime) FROM information.transaction_info ARRAY JOIN winner AS winner_one WHERE %s group by buyer_id,winner_one`
+		text := `(buyer_id=? AND has(winner,?))`
+		array := []string{}
+		args := []interface{}{}
+		for _, v := range buyerIds {
+			for _, vv := range winners {
+				args = append(args, v, vv)
+				array = append(array, text)
+				if len(array) == 200 {
+					toSearch(1, fmt.Sprintf(sql, strings.Join(array, " or ")), args)
+					array = []string{}
+					args = []interface{}{}
+				}
+			}
+		}
+		if len(array) > 0 {
+			toSearch(1, fmt.Sprintf(sql, strings.Join(array, " or ")), args)
+		}
+	}
+	if len(agencys) > 0 {
+		sql := `SELECT buyer_id,agency,max(zbtime) FROM information.transaction_info WHERE %s group by buyer_id,agency`
+		text := `(buyer_id=? AND agency=?)`
+		array := []string{}
+		args := []interface{}{}
+		for _, v := range buyerIds {
+			for _, vv := range winners {
+				args = append(args, v, vv)
+				array = append(array, text)
+				if len(array) == 200 {
+					toSearch(2, fmt.Sprintf(sql, strings.Join(array, " or ")), args)
+					array = []string{}
+					args = []interface{}{}
+				}
+			}
+		}
+		if len(array) > 0 {
+			toSearch(2, fmt.Sprintf(sql, strings.Join(array, " or ")), args)
+		}
+	}
+	return adiffb, agency
+}

+ 29 - 13
api/internal/service/owner.go

@@ -1102,13 +1102,27 @@ func (t *OwnerService) CandidateChannel() (*ResultDatas, int, int, int, int) {
 		for buyerId := range *dataMap {
 			buyerIds = append(buyerIds, buyerId)
 		}
+		if len(buyerIds) > 300 {
+			buyerIds = buyerIds[:300]
+		}
 		rs1, rs2 := GetData(propertyForm, strings.Join(buyerIds, ","))
+		winners, agency := []string{}, []string{}
+		for buyerId := range *dataMap {
+			for _, m := range rs1[buyerId] {
+				winners = append(winners, common.ObjToString(m["name"]))
+			}
+			for _, m := range rs2[buyerId] {
+				agency = append(agency, common.ObjToString(m["name"]))
+			}
+		}
+		ltcs1, ltcs2 := LastTimeCoopBath(buyerIds, winners, agency)
 		for buyerId := range *dataMap {
-			r1 := rs1[buyerId]
-			r2 := rs2[buyerId]
-			if r1 != nil && len(r1) > 0 {
-				for _, m := range r1 {
-					near, zbtime := LastTimeCoop(buyerId, common.ObjToString(m["name"]), "adiffb")
+			if ltcs1[buyerId] != nil {
+				for _, m := range rs1[buyerId] {
+					ltc := ltcs1[buyerId][common.ObjToString(m["name"])]
+					if ltc == nil {
+						continue
+					}
 					tmp := ResultData{
 						SourceType:  "adiffb",
 						EntId:       common.ObjToString(m["nameId"]),
@@ -1116,8 +1130,8 @@ func (t *OwnerService) CandidateChannel() (*ResultDatas, int, int, int, int) {
 						ProjectNum:  common.IntAll(m["coop_size"]),
 						TotalAmount: common.Float64All(m["coop_amount"]),
 						BuyerId:     buyerId,
-						RecentTime:  zbtime,
-						NearlyYears: near,
+						RecentTime:  ltc.ZbTime,
+						NearlyYears: ltc.Near,
 					}
 					if tmp.EntId == "" || tmp.BuyerId == "" {
 						continue
@@ -1126,16 +1140,19 @@ func (t *OwnerService) CandidateChannel() (*ResultDatas, int, int, int, int) {
 					returnData = append(returnData, &tmp)
 				}
 			}
-			if r2 != nil && len(r2) > 0 {
-				for _, m := range r2 {
-					near, zbtime := LastTimeCoop(buyerId, common.ObjToString(m["name"]), "agency")
+			if ltcs2[buyerId] != nil {
+				for _, m := range rs2[buyerId] {
+					ltc := ltcs1[buyerId][common.ObjToString(m["name"])]
+					if ltc == nil {
+						continue
+					}
 					tmp := ResultData{
 						SourceType:  "agency",
 						EntName:     common.ObjToString(m["name"]),
 						ProjectNum:  common.IntAll(m["coop_size"]),
 						TotalAmount: common.Float64All(m["coop_amount"]),
-						RecentTime:  zbtime,
-						NearlyYears: near,
+						RecentTime:  ltc.ZbTime,
+						NearlyYears: ltc.Near,
 						EntId:       common.ObjToString(m["nameId"]),
 						BuyerId:     buyerId,
 					}
@@ -1146,7 +1163,6 @@ func (t *OwnerService) CandidateChannel() (*ResultDatas, int, int, int, int) {
 					returnData = append(returnData, &tmp)
 				}
 			}
-
 			// 中间人可介绍业主
 			var r3 []map[string]interface{}
 			r3 = FindMiddleman([]string{fmt.Sprintf("'%s'", buyerId)}, t.PositionId, r3)