|
@@ -0,0 +1,225 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "fmt"
|
|
|
+ "strings"
|
|
|
+
|
|
|
+ util "app.yhyue.com/moapp/jybase/common"
|
|
|
+ . "app.yhyue.com/moapp/jybase/date"
|
|
|
+ . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
|
|
|
+ "github.com/zeromicro/go-zero/core/logx"
|
|
|
+)
|
|
|
+
|
|
|
+type ContactInfo struct {
|
|
|
+ Name string
|
|
|
+ Phone string
|
|
|
+}
|
|
|
+type Connection struct {
|
|
|
+ Company_id string
|
|
|
+ Company_name string
|
|
|
+ Itype int
|
|
|
+ QyxyId string
|
|
|
+ *ContactInfo
|
|
|
+}
|
|
|
+type InitNetwork struct {
|
|
|
+ PositionId int64
|
|
|
+ EntId int64
|
|
|
+ DeptId int64
|
|
|
+ UserId int64
|
|
|
+ EntName string
|
|
|
+ BusinessType string
|
|
|
+}
|
|
|
+
|
|
|
+func (i *InitNetwork) Init() {
|
|
|
+ array := []*Connection{}
|
|
|
+ ids := []string{}
|
|
|
+ //①业主人脉:当前企业曾经合作过物业项目的采购单位的联系人作为业主人脉
|
|
|
+ func() {
|
|
|
+ rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT buyer,buyer_id from information.transaction_info where has(winner,?)`, i.EntName)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for rows.Next() {
|
|
|
+ var (
|
|
|
+ buyer string
|
|
|
+ buyer_id string
|
|
|
+ )
|
|
|
+ if err := rows.Scan(&buyer, &buyer_id); err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if buyer == "" || buyer_id == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cn := &Connection{
|
|
|
+ Company_id: buyer_id,
|
|
|
+ Company_name: buyer,
|
|
|
+ Itype: 1,
|
|
|
+ }
|
|
|
+ ids = append(ids, buyer_id)
|
|
|
+ array = append(array, cn)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ //②甲异业渠道(人脉):“当前企业曾经合作过物业项目的采购单位的中标物业企业中”与当前企业不一样业态的企业的联系人列表
|
|
|
+ func() {
|
|
|
+ if i.BusinessType == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ m := map[string]*Connection{}
|
|
|
+ args := []interface{}{i.EntName}
|
|
|
+ wh, newArgs := util.WhArgs(strings.Split(i.BusinessType, ","))
|
|
|
+ args = append(args, newArgs...)
|
|
|
+ rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT winner_id,winner from information.transaction_info WHERE buyer_id IN (SELECT buyer_id from information.transaction_info WHERE has(winner,?) and buyer_id<>'') AND hasAny(property_form,[`+wh+`])`, args...)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for rows.Next() {
|
|
|
+ var (
|
|
|
+ winners []string
|
|
|
+ winner_ids []string
|
|
|
+ )
|
|
|
+ if err := rows.Scan(&winners, &winner_ids); err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for k, winner_id := range winner_ids {
|
|
|
+ if k >= len(winners) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cn := &Connection{
|
|
|
+ Company_id: winner_id,
|
|
|
+ Company_name: winners[k],
|
|
|
+ Itype: 3,
|
|
|
+ }
|
|
|
+ if m[winner_id] != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ ids = append(ids, winner_id)
|
|
|
+ array = append(array, cn)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ //③招标代理机构(人脉):当前企业曾经合作过的招标代理机构联系人信息
|
|
|
+ func() {
|
|
|
+ rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT agency,agency_id from information.transaction_info where buyer=? or has(winner,?)`, i.EntName, i.EntName)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for rows.Next() {
|
|
|
+ var (
|
|
|
+ agency string
|
|
|
+ agency_id string
|
|
|
+ )
|
|
|
+ if err := rows.Scan(&agency, &agency_id); err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if agency == "" || agency_id == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ cn := &Connection{
|
|
|
+ Company_id: agency_id,
|
|
|
+ Company_name: agency,
|
|
|
+ Itype: 5,
|
|
|
+ }
|
|
|
+ ids = append(ids, agency_id)
|
|
|
+ array = append(array, cn)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ cis := i.GetContactInfo(ids)
|
|
|
+ cids := i.GetCompanyId(ids)
|
|
|
+ for _, v := range array {
|
|
|
+ if cis[v.Company_id] != nil {
|
|
|
+ v.ContactInfo = cis[v.Company_id]
|
|
|
+ }
|
|
|
+ v.QyxyId = cids[v.Company_id]
|
|
|
+ }
|
|
|
+ //
|
|
|
+ nowFormat := NowFormat(Date_Full_Layout)
|
|
|
+ values := []interface{}{}
|
|
|
+ fields := []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_id", "qyxy_id", "contact_person", "contact_phone", "status", "create_time", "update_time"}
|
|
|
+ index := 0
|
|
|
+ for _, v := range array {
|
|
|
+ if v.ContactInfo == nil || v.ContactInfo.Phone == "" || v.ContactInfo.Name == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if CrmMysql.CountBySql(`select count(1) from crm.connection where position_id=? and company_id=? and itype=?`, i.PositionId, v.Company_id, v.Itype) == 0 {
|
|
|
+ logx.Info("保存人脉", fmt.Sprintf("%+v", v))
|
|
|
+ values = append(values, i.PositionId, i.EntId, i.DeptId, i.UserId, v.Itype, v.Company_name, v.Company_id, v.QyxyId, v.ContactInfo.Name, v.ContactInfo.Phone, 1, nowFormat, nowFormat)
|
|
|
+ } else {
|
|
|
+ logx.Info("过滤掉已存在的人脉", fmt.Sprintf("%+v", v))
|
|
|
+ }
|
|
|
+ index++
|
|
|
+ if index == 200 {
|
|
|
+ CrmMysql.InsertBatch("crm.connection", fields, values)
|
|
|
+ values = []interface{}{}
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(values) > 0 {
|
|
|
+ CrmMysql.InsertBatch("crm.connection", fields, values)
|
|
|
+ values = []interface{}{}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (i *InitNetwork) GetCompanyId(ids []string) map[string]string {
|
|
|
+ m := map[string]string{}
|
|
|
+ if len(ids) == 0 {
|
|
|
+ return m
|
|
|
+ }
|
|
|
+ wh, args := util.WhArgs(ids)
|
|
|
+ rows, err := ClickhouseConn.Query(context.Background(), `select id,company_id from information.ent_info where id in(`+wh+`)`, args...)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ return m
|
|
|
+ }
|
|
|
+ for rows.Next() {
|
|
|
+ var (
|
|
|
+ id string
|
|
|
+ company_id string
|
|
|
+ )
|
|
|
+ if err := rows.Scan(&id, &company_id); err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ m[id] = company_id
|
|
|
+ }
|
|
|
+ return m
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+func (i *InitNetwork) GetContactInfo(ids []string) map[string]*ContactInfo {
|
|
|
+ m := map[string]*ContactInfo{}
|
|
|
+ if len(ids) == 0 {
|
|
|
+ return m
|
|
|
+ }
|
|
|
+ wh, args := util.WhArgs(ids)
|
|
|
+ rows, err := ClickhouseConn.Query(context.Background(), `select id,phone,name from information.ent_contact where id in (`+wh+`)`, args...)
|
|
|
+ if err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ return m
|
|
|
+ }
|
|
|
+ for rows.Next() {
|
|
|
+ var (
|
|
|
+ id string
|
|
|
+ phone string
|
|
|
+ name string
|
|
|
+ )
|
|
|
+ if err := rows.Scan(&id, &phone, &name); err != nil {
|
|
|
+ logx.Error(err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ if phone == "" || name == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ m[id] = &ContactInfo{
|
|
|
+ Name: name,
|
|
|
+ Phone: phone,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return m
|
|
|
+}
|