wangchuanjin 1 год назад
Родитель
Сommit
b175063d3f
2 измененных файлов с 221 добавлено и 117 удалено
  1. 213 111
      api/internal/service/network.go
  2. 8 6
      api/internal/service/network_test.go

+ 213 - 111
api/internal/service/network.go

@@ -74,6 +74,10 @@ type firstpartyNetwork struct {
 	CompanyName string
 	Name        string
 }
+type idName struct {
+	Id   string
+	Name string
+}
 
 //人脉库-添加/修改人脉
 func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
@@ -265,6 +269,7 @@ func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
 	wh, newArgs := NetworkCom.WhArgs(NetworkCom.GetMyProbusfor(in.EntAccountId))
 	var count int64
 	var list []*networkTree
+	firstpartyIds := []string{}
 	if in.Id != "" {
 		if in.Type == "firstparty" {
 			result := n.FirstpartyNetwork(in.Name, []string{in.Id})
@@ -371,6 +376,9 @@ func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
 					}
 				}
 				lock.Lock()
+				if itype == 1 {
+					firstpartyIds = append(firstpartyIds, ntc.CompanyId)
+				}
 				count += ntc.Count
 				list[itype-1].Count += ntc.Count
 				list[itype-1].Children = append(list[itype-1].Children, ntc)
@@ -379,6 +387,10 @@ func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
 		}
 		wait.Wait()
 	}
+	firstpartyChild := map[string][]*firstpartyNetwork{}
+	if len(firstpartyIds) > 0 {
+		firstpartyChild = n.FirstpartyNetwork("", firstpartyIds)
+	}
 	convList := []map[string]interface{}{}
 	for _, v := range list {
 		pm := map[string]interface{}{
@@ -393,25 +405,42 @@ func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
 		} else {
 			pm["SZ_LEAF"] = 1
 		}
-		ID := ""
-		pType := ""
 		sort.Sort(v)
+		id := ""
+		pType := ""
 		for _, vv := range v.Children {
-			if ID != "" {
-				ID += ","
-			}
-			if pType != "" {
-				pType += ","
-			}
 			tempId := vv.CompanyId
 			if vv.Type == "middleman" {
 				tempId = fmt.Sprint(vv.Id)
 			}
-			ID += tempId
+			myChildIds := ""
+			if in.Id == "" && v.Name == "甲方" {
+				for _, vvv := range firstpartyChild[vv.CompanyId] {
+					if id != "" {
+						id += ","
+					}
+					id += vvv.CompanyId
+					if myChildIds != "" {
+						myChildIds += ","
+					}
+					myChildIds += vvv.CompanyId
+				}
+			} else {
+				if id != "" {
+					id += ","
+				}
+				id += tempId
+			}
+			if pType != "" {
+				pType += ","
+			}
 			pType += vv.Type
+			if myChildIds == "" {
+				myChildIds = tempId
+			}
 			cm := map[string]interface{}{
 				"NAME":      vv.CompanyName,
-				"ID":        tempId,
+				"ID":        myChildIds,
 				"SZ_PID0":   v.Name,
 				"SZ_PID1":   v.Name + ":" + tempId,
 				"CODE":      v.Name + ":" + tempId,
@@ -420,10 +449,11 @@ func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
 				"SZ_LEVEL":  1,
 				"SZ_LEAF":   1,
 				"TYPE":      vv.Type,
+				"MYID":      tempId,
 			}
 			convList = append(convList, cm)
 		}
-		pm["ID"] = ID
+		pm["ID"] = id
 		pm["TYPE"] = pType
 		convList = append(convList, pm)
 	}
@@ -441,19 +471,21 @@ func (n *network) List(in *types.NetWorkListReq) *types.Reply {
 	if in.Page_size <= 0 {
 		in.Page_size = 10
 	}
-	q := `select a.id,a.company_id,a.company_name,a.qyxy_id,a.itype,a.contact_person as person,a.contact_phone as phone,count(DISTINCT if(b.itype=1,b.relate_id,null)) as buyer_count,count(DISTINCT if(b.itype=2,b.relate_id,null)) as project_count,GROUP_CONCAT(IF(b.itype=1,b.relate_id,NULL)) AS relate_buyer_id,GROUP_CONCAT(IF(b.itype=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a 
-		left join crm.connection_introduce b on (a.id=b.connection_id) where a.position_id=?`
+	if in.Current_page <= 0 {
+		in.Current_page = 1
+	}
 	args := []interface{}{in.PositionId}
+	sqlAppend := ""
 	if in.Type != "" {
-		q += ` and a.itype=?`
+		sqlAppend += ` and a.itype=?`
 		args = append(args, n.TypeStrConvert(in.Type))
 	}
 	if in.Starttime != "" {
-		q += ` and a.create_time>=?`
+		sqlAppend += ` and a.create_time>=?`
 		args = append(args, in.Starttime+" 00:00:00")
 	}
 	if in.Endtime != "" {
-		q += ` and a.create_time<=?`
+		sqlAppend += ` and a.create_time<=?`
 		if in.Starttime == in.Endtime {
 			in.Endtime += " 23:59:59"
 		} else {
@@ -462,14 +494,20 @@ func (n *network) List(in *types.NetWorkListReq) *types.Reply {
 		args = append(args, in.Endtime)
 	}
 	if in.Name != "" {
-		q += ` and a.company_name like ?`
+		sqlAppend += ` and a.company_name like ?`
 		args = append(args, "%"+in.Name+"%")
 	}
-	q += ` GROUP BY a.id order by a.create_time desc`
-	// if in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Project_matchme == 0 {
-	// 	q += ` limit ?`
-	// 	args = append(args, in.Page_size)
-	// }
+	q := `select a.id,a.company_id,a.company_name,a.qyxy_id,a.itype,a.contact_person as person,a.contact_phone as phone,count(DISTINCT if(b.itype=1,b.relate_id,null)) as buyer_count,count(DISTINCT if(b.itype=2,b.relate_id,null)) as project_count,GROUP_CONCAT(IF(b.itype=1,b.relate_id,NULL)) AS relate_buyer_id,GROUP_CONCAT(IF(b.itype=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a 
+		left join crm.connection_introduce b on (a.id=b.connection_id) where a.position_id=?` + sqlAppend + ` GROUP BY a.id order by a.create_time desc`
+	var count int64
+	start := (in.Current_page - 1) * in.Page_size
+	end := start + in.Page_size
+	dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Project_matchme == 0 && in.Order_amount == 0
+	if dbPaging {
+		count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend, args...)
+		q += ` limit ?,?`
+		args = append(args, start, end)
+	}
 	listTemp := CrmMysql.SelectBySql(q, args...)
 	firstparty_array, supplier_array, adiffb_array, agency_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}
 	for _, v := range *listTemp {
@@ -653,26 +691,22 @@ func (n *network) List(in *types.NetWorkListReq) *types.Reply {
 	if in.Order_amount != 0 {
 		sort.Sort(csList)
 	}
-	length := int64(len(csList.List))
-	total_page := int64(math.Ceil(float64(length) / float64(in.Page_size)))
 	finalList := []*map[string]interface{}{}
-	if length > 0 {
-		if in.Current_page <= 0 {
-			in.Current_page = 1
-		}
-		if in.Current_page > total_page {
-			in.Current_page = total_page
-		}
-		start := (in.Current_page - 1) * in.Page_size
-		end := start + in.Page_size
-		if end > length {
-			end = length
+	if !dbPaging {
+		count = int64(len(csList.List))
+		if count > 0 && start < count {
+			if end > count {
+				end = count
+			}
+			finalList = csList.List[start:end]
 		}
-		finalList = csList.List[start:end]
+	} else {
+		finalList = csList.List
 	}
+	total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
 	return &types.Reply{
 		Data: map[string]interface{}{
-			"count":            length,
+			"count":            count,
 			"total_page":       total_page,
 			"firstparty_count": firstparty_count,
 			"supplier_count":   supplier_count,
@@ -711,6 +745,9 @@ func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*
 			logx.Error(err)
 			continue
 		}
+		if company_id == "" || company_name == "" {
+			continue
+		}
 		result[a_id] = append(result[a_id], &firstpartyNetwork{
 			CompanyId:   company_id,
 			CompanyName: company_name,
@@ -730,6 +767,10 @@ func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entM
 			values = append(values, vv.CompanyId)
 		}
 	}
+	result := map[string]*projectInfo{}
+	if len(values) == 0 {
+		return result
+	}
 	wh, args := NetworkCom.WhArgs(values)
 	q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
 	if matchme == 1 {
@@ -766,7 +807,6 @@ func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entM
 	if err := rows.Err(); err != nil {
 		logx.Error(err)
 	}
-	result := map[string]*projectInfo{}
 	for k, v := range fpn {
 		if result[k] == nil {
 			result[k] = &projectInfo{}
@@ -788,117 +828,108 @@ func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entM
 
 //
 func (n *network) Introduce_Supplier(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
-	vm := map[string]*projectInfo{}
-	for _, v := range values {
-		vm[v] = &projectInfo{}
+	if len(values) == 0 {
+		return map[string]*projectInfo{}
 	}
 	wh, args := NetworkCom.WhArgs(values)
-	q := `select a.winner_id,count(DISTINCT b.buyer_id) AS buyer_count,count(DISTINCT b.project_id) AS project_count,sum(b.project_money) AS project_amount,groupUniqArray(b.buyer),groupUniqArray(b.project_id) from information.transaction_info a 
-		inner join information.transaction_info b on (hasAny(a.winner_id,[` + wh + `]) and a.buyer_id<>'' and a.buyer_id=b.buyer_id`
-	if matchme == 1 {
-		newWh, newArgs := NetworkCom.WhArgs(probusfors)
-		q += ` and hasAny(b.property_form,[` + newWh + `])`
-		args = append(args, newArgs...)
-	}
-	q += `) group by a.winner_id`
+	vbs := map[string][]*idName{}
+	repeat := map[string]bool{}
+	whRepeat := map[string]map[string]bool{}
+	buyers := []string{}
+	q := `select winner_id,buyer_id,buyer from information.transaction_info where hasAny(winner_id,[` + wh + `])`
 	rows, err := ClickhouseConn.Query(context.Background(), q, args...)
 	if err != nil {
 		logx.Error(err)
-		return nil
-	}
-	for rows.Next() {
-		var (
-			winner_id      []string
-			buyer_count    uint64
-			project_count  uint64
-			project_amount decimal.Decimal
-			buyers         []string
-			project_id     []string
-		)
-		if err := rows.Scan(&winner_id, &buyer_count, &project_count, &project_amount, &buyers, &project_id); err != nil {
-			logx.Error(err)
-			continue
-		}
-		for _, v := range winner_id {
-			if vm[v] == nil {
+	} else {
+		for rows.Next() {
+			var (
+				winner_id []string
+				buyer_id  string
+				buyer     string
+			)
+			if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
+				logx.Error(err)
+				continue
+			}
+			if buyer_id == "" || buyer == "" {
 				continue
 			}
-			vm[v].BuyerCount += int64(buyer_count)
-			vm[v].ProjectCount += int64(project_count)
-			pf, _ := project_amount.Float64()
-			vm[v].ProjectAmount += pf
-			vm[v].ExportId = project_id
-			for _, v := range buyers {
-				if entMonitor[v] {
-					vm[v].MonitorCount++
+			if !repeat[buyer_id] {
+				repeat[buyer_id] = true
+				buyers = append(buyers, buyer_id)
+			}
+			for _, v := range winner_id {
+				if whRepeat[v] != nil && whRepeat[v][buyer_id] {
+					continue
+				}
+				if whRepeat[v] == nil {
+					whRepeat[v] = map[string]bool{}
 				}
+				whRepeat[v][buyer_id] = true
+				vbs[v] = append(vbs[v], &idName{
+					Id:   buyer_id,
+					Name: buyer,
+				})
 			}
 		}
+		rows.Close()
+		if err := rows.Err(); err != nil {
+			logx.Error(err)
+		}
 	}
-	rows.Close()
-	if err := rows.Err(); err != nil {
-		logx.Error(err)
-	}
-	return vm
+	return n.MakeProjectInfo(buyers, vbs, matchme, probusfors, entMonitor)
 }
 
 //
 func (n *network) Introduce_Agency(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
-	vm := map[string]*projectInfo{}
-	for _, v := range values {
-		vm[v] = &projectInfo{}
+	if len(values) == 0 {
+		return map[string]*projectInfo{}
 	}
 	wh, args := NetworkCom.WhArgs(values)
-	q := `select a.agency_id,count(DISTINCT b.buyer_id) AS buyer_count,count(DISTINCT b.project_id) AS project_count,sum(b.project_money) AS project_amount,groupUniqArray(b.buyer),groupUniqArray(b.project_id) from information.transaction_info a 
-		inner join information.transaction_info b on (a.agency_id in (` + wh + `) and a.buyer_id<>'' and a.buyer_id=b.buyer_id`
-	if matchme == 1 {
-		newWh, newArgs := NetworkCom.WhArgs(probusfors)
-		q += ` and hasAny(b.property_form,[` + newWh + `])`
-		args = append(args, newArgs...)
-	}
-	q += `) group by a.agency_id`
+	q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info where agency_id in (` + wh + `)`
 	rows, err := ClickhouseConn.Query(context.Background(), q, args...)
 	if err != nil {
 		logx.Error(err)
 		return nil
 	}
+	vbs := map[string][]*idName{}
+	buyers := []string{}
+	repeat := map[string]bool{}
 	for rows.Next() {
 		var (
-			agency_id      string
-			buyer_count    uint64
-			project_count  uint64
-			project_amount decimal.Decimal
-			buyers         []string
-			project_id     []string
+			agency_id string
+			buyer_id  string
+			buyer     string
 		)
-		if err := rows.Scan(&agency_id, &buyer_count, &project_count, &project_amount, &buyers, &project_id); err != nil {
+		if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
 			logx.Error(err)
 			continue
 		}
-		if vm[agency_id] == nil {
+		if buyer_id == "" || buyer == "" {
 			continue
 		}
-		vm[agency_id].BuyerCount += int64(buyer_count)
-		vm[agency_id].ProjectCount += int64(project_count)
-		pf, _ := project_amount.Float64()
-		vm[agency_id].ProjectAmount += pf
-		vm[agency_id].ExportId = project_id
-		for _, v := range buyers {
-			if entMonitor[v] {
-				vm[agency_id].MonitorCount++
-			}
+		vbs[agency_id] = append(vbs[agency_id], &idName{
+			Id:   buyer_id,
+			Name: buyer,
+		})
+		if !repeat[buyer_id] {
+			repeat[buyer_id] = true
+			buyers = append(buyers, buyer_id)
 		}
 	}
 	rows.Close()
 	if err := rows.Err(); err != nil {
 		logx.Error(err)
 	}
-	return vm
+	return n.MakeProjectInfo(buyers, vbs, matchme, probusfors, entMonitor)
 }
 
 //
 func (n *network) Introduce_Middleman(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
-	vm := map[string]*projectInfo{}
+	result := map[string]*projectInfo{}
+	if len(values) == 0 {
+		return result
+	}
 	wh, newArgs := NetworkCom.WhArgs(values)
 	rows, err := ClickhouseConn.Query(context.Background(), `select project_id,project_money from information.transaction_info where project_id in (`+wh+`)`, newArgs...)
 	if err != nil {
@@ -915,7 +946,7 @@ func (n *network) Introduce_Middleman(values []string, entMonitor map[string]boo
 			continue
 		}
 		pf, _ := project_money.Float64()
-		vm[project_id] = &projectInfo{
+		result[project_id] = &projectInfo{
 			ProjectAmount: pf,
 		}
 	}
@@ -923,7 +954,7 @@ func (n *network) Introduce_Middleman(values []string, entMonitor map[string]boo
 	if err := rows.Err(); err != nil {
 		logx.Error(err)
 	}
-	return vm
+	return result
 }
 
 //
@@ -989,3 +1020,74 @@ func (n *network) GetQyxyId(ids []string) map[string]string {
 	}
 	return m
 }
+
+//
+func (n *network) BuyerProjectInfo(ids []string, matchme int64, probusfors []string) map[string]*projectInfo {
+	vm := map[string]*projectInfo{}
+	wh, args := NetworkCom.WhArgs(ids)
+	q := `select buyer_id,count(DISTINCT project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
+	if matchme == 1 {
+		newWh, newArgs := NetworkCom.WhArgs(probusfors)
+		q += ` and hasAny(property_form,[` + newWh + `])`
+		args = append(args, newArgs...)
+	}
+	q += ` group by buyer_id`
+	rows, err := ClickhouseConn.Query(context.Background(), q, args...)
+	if err != nil {
+		logx.Error(err)
+		return nil
+	}
+	for rows.Next() {
+		var (
+			buyer_id       string
+			project_count  uint64
+			project_amount decimal.Decimal
+			project_id     []string
+		)
+		if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
+			logx.Error(err)
+			continue
+		}
+		pf, _ := project_amount.Float64()
+		vm[buyer_id] = &projectInfo{
+			ProjectCount:  int64(project_count),
+			ExportId:      project_id,
+			ProjectAmount: pf,
+		}
+	}
+	rows.Close()
+	if err := rows.Err(); err != nil {
+		logx.Error(err)
+	}
+	return vm
+}
+
+//
+func (n *network) MakeProjectInfo(buyers []string, vbs map[string][]*idName, matchme int64, probusfors []string, entMonitor map[string]bool) map[string]*projectInfo {
+	pis := n.BuyerProjectInfo(buyers, matchme, probusfors)
+	vm := map[string]*projectInfo{}
+	for k, v := range vbs {
+		pi := &projectInfo{
+			BuyerCount: int64(len(v)),
+		}
+		re := map[string]bool{}
+		for _, vv := range v {
+			if pis[vv.Id] != nil {
+				pi.ProjectCount += pis[vv.Id].ProjectCount
+				pi.ProjectAmount += pis[vv.Id].ProjectAmount
+				for _, vvv := range pis[vv.Id].ExportId {
+					if re[vvv] {
+						continue
+					}
+					pi.ExportId = append(pi.ExportId, vvv)
+					re[vvv] = true
+				}
+			}
+			if entMonitor[vv.Name] {
+				pi.MonitorCount++
+			}
+		}
+		vm[k] = pi
+	}
+	return vm
+}

+ 8 - 6
api/internal/service/network_test.go

@@ -105,17 +105,19 @@ func TestNetWorkList(t *testing.T) {
 	initDb()
 	//类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
 	res := Network.List(&types.NetWorkListReq{
-		//PositionId: 1205591997,
+		PositionId:   1205591997,
+		Current_page: 1,
+		Page_size:    100,
 		//PositionId: 935,
-		PositionId: 1205591998,
-		// Order_amount:       1,
+		//PositionId: 1205591998,
+		Order_amount: -1,
 		// Monitorcount_start: 1,
 		// Monitorcount_end:   1,
 		//Monitor:      0,
 		//Project_matchme: 1,
 		//Starttime: "2024-04-23",
 		//Endtime:   "2024-04-23",
-		//Type: "agency",
+		//Type: "supplier",
 		//Name: "三亚市",
 	})
 	for k, v := range res.Data.(map[string]interface{}) {
@@ -133,8 +135,8 @@ func TestAllProject(t *testing.T) {
 	res := Network.AllProject(&types.AllprojectReq{
 		EntAccountId: 64,
 		PositionId:   935,
-		Id:           "wcj_11111111a",
-		Type:         "firstparty",
+		//Id:           "wcj_11111111a",
+		//Type:         "firstparty",
 	})
 	b, _ := json.Marshal(res.Data)
 	log.Println(string(b))