initNetwork.go 5.9 KB

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