initNetwork.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strings"
  6. util "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/date"
  8. . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type ContactInfo struct {
  12. Name string
  13. Phone string
  14. }
  15. type Connection struct {
  16. Company_id string
  17. Company_name string
  18. Itype int
  19. QyxyId string
  20. *ContactInfo
  21. }
  22. type InitNetwork struct {
  23. PositionId int64
  24. EntId int64
  25. DeptId int64
  26. UserId int64
  27. EntName string
  28. BusinessType string
  29. }
  30. func (i *InitNetwork) Init() {
  31. logx.Info("进行初始化设置", fmt.Sprintf("%+v", i))
  32. array := []*Connection{}
  33. ids := []string{}
  34. //①业主人脉:当前企业曾经合作过物业项目的采购单位的联系人作为业主人脉
  35. func() {
  36. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT buyer,buyer_id from information.transaction_info_all where has(winner,?)`, i.EntName)
  37. if err != nil {
  38. logx.Error(err)
  39. return
  40. }
  41. for rows.Next() {
  42. var (
  43. buyer string
  44. buyer_id string
  45. )
  46. if err := rows.Scan(&buyer, &buyer_id); err != nil {
  47. logx.Error(err)
  48. continue
  49. }
  50. if buyer == "" || buyer_id == "" {
  51. continue
  52. }
  53. ids = append(ids, buyer_id)
  54. array = append(array, &Connection{
  55. Company_id: buyer_id,
  56. Company_name: buyer,
  57. Itype: 1,
  58. })
  59. }
  60. }()
  61. //②甲异业渠道(人脉):“当前企业曾经合作过物业项目的采购单位的中标物业企业中”与当前企业不一样业态的企业的联系人列表
  62. func() {
  63. if i.BusinessType == "" {
  64. return
  65. }
  66. args := []interface{}{i.EntName, i.EntName}
  67. wh, newArgs := util.WhArgs(strings.Split(i.BusinessType, ","))
  68. args = append(args, newArgs...)
  69. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT winner,winner_id from information.transaction_info_all ARRAY JOIN winner_id,winner WHERE buyer_id IN (SELECT buyer_id from information.transaction_info_all WHERE has(winner,?) and buyer_id<>'') and winner<>? AND hasAny(topscopeclass,[`+wh+`])=0`, args...)
  70. if err != nil {
  71. logx.Error(err)
  72. return
  73. }
  74. for rows.Next() {
  75. var (
  76. winner string
  77. winner_id string
  78. )
  79. if err := rows.Scan(&winner, &winner_id); err != nil {
  80. logx.Error(err)
  81. continue
  82. }
  83. if winner == "" || winner_id == "" {
  84. continue
  85. }
  86. ids = append(ids, winner_id)
  87. array = append(array, &Connection{
  88. Company_id: winner_id,
  89. Company_name: winner,
  90. Itype: 3,
  91. })
  92. }
  93. }()
  94. //③招标代理机构(人脉):当前企业曾经合作过的招标代理机构联系人信息
  95. func() {
  96. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT agency,agency_id from information.transaction_info_all where buyer=? or has(winner,?)`, i.EntName, i.EntName)
  97. if err != nil {
  98. logx.Error(err)
  99. return
  100. }
  101. for rows.Next() {
  102. var (
  103. agency string
  104. agency_id string
  105. )
  106. if err := rows.Scan(&agency, &agency_id); err != nil {
  107. logx.Error(err)
  108. continue
  109. }
  110. if agency == "" || agency_id == "" {
  111. continue
  112. }
  113. ids = append(ids, agency_id)
  114. array = append(array, &Connection{
  115. Company_id: agency_id,
  116. Company_name: agency,
  117. Itype: 5,
  118. })
  119. }
  120. }()
  121. cis := i.GetContactInfo(ids)
  122. cids := i.GetCompanyId(ids)
  123. for _, v := range array {
  124. if cis[v.Company_id] != nil {
  125. v.ContactInfo = cis[v.Company_id]
  126. }
  127. v.QyxyId = cids[v.Company_id]
  128. }
  129. //
  130. nowFormat := NowFormat(Date_Full_Layout)
  131. values := []interface{}{}
  132. 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"}
  133. index := 0
  134. typeLimit := map[int]int{}
  135. for _, v := range array {
  136. if v.Company_name == i.EntName || v.ContactInfo == nil || v.ContactInfo.Phone == "" || v.ContactInfo.Name == "" {
  137. continue
  138. } else if typeLimit[v.Itype] >= 100 {
  139. continue
  140. }
  141. 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 {
  142. typeLimit[v.Itype] = typeLimit[v.Itype] + 1
  143. logx.Info("保存人脉", fmt.Sprintf("%+v", v))
  144. 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)
  145. } else {
  146. logx.Info("过滤掉已存在的人脉", fmt.Sprintf("%+v", v))
  147. }
  148. index++
  149. if index == 200 {
  150. CrmMysql.InsertBatch("crm.connection", fields, values)
  151. values = []interface{}{}
  152. }
  153. }
  154. if len(values) > 0 {
  155. CrmMysql.InsertBatch("crm.connection", fields, values)
  156. values = []interface{}{}
  157. }
  158. Network.DeleteCache(i.PositionId)
  159. }
  160. //
  161. func (i *InitNetwork) GetCompanyId(ids []string) map[string]string {
  162. m := map[string]string{}
  163. if len(ids) == 0 {
  164. return m
  165. }
  166. wh, args := util.WhArgs(ids)
  167. rows, err := ClickhouseConn.Query(context.Background(), `select id,company_id from information.ent_info where id in(`+wh+`)`, args...)
  168. if err != nil {
  169. logx.Error(err)
  170. return m
  171. }
  172. for rows.Next() {
  173. var (
  174. id string
  175. company_id string
  176. )
  177. if err := rows.Scan(&id, &company_id); err != nil {
  178. logx.Error(err)
  179. continue
  180. }
  181. m[id] = company_id
  182. }
  183. return m
  184. }
  185. //
  186. func (i *InitNetwork) GetContactInfo(ids []string) map[string]*ContactInfo {
  187. m := map[string]*ContactInfo{}
  188. if len(ids) == 0 {
  189. return m
  190. }
  191. wh, args := util.WhArgs(ids)
  192. rows, err := ClickhouseConn.Query(context.Background(), `select id,phone,name from information.ent_contact where id in (`+wh+`)`, args...)
  193. if err != nil {
  194. logx.Error(err)
  195. return m
  196. }
  197. for rows.Next() {
  198. var (
  199. id string
  200. phone string
  201. name string
  202. )
  203. if err := rows.Scan(&id, &phone, &name); err != nil {
  204. logx.Error(err)
  205. continue
  206. }
  207. if phone == "" || name == "" {
  208. continue
  209. }
  210. m[id] = &ContactInfo{
  211. Name: name,
  212. Phone: phone,
  213. }
  214. }
  215. return m
  216. }