initNetwork.go 5.9 KB

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