initNetwork.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(property_form,[`+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. for _, v := range array {
  143. if v.ContactInfo == nil || v.ContactInfo.Phone == "" || v.ContactInfo.Name == "" {
  144. continue
  145. }
  146. 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 {
  147. logx.Info("保存人脉", fmt.Sprintf("%+v", v))
  148. 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)
  149. } else {
  150. logx.Info("过滤掉已存在的人脉", fmt.Sprintf("%+v", v))
  151. }
  152. index++
  153. if index == 200 {
  154. CrmMysql.InsertBatch("crm.connection", fields, values)
  155. values = []interface{}{}
  156. }
  157. }
  158. if len(values) > 0 {
  159. CrmMysql.InsertBatch("crm.connection", fields, values)
  160. values = []interface{}{}
  161. }
  162. }
  163. //
  164. func (i *InitNetwork) GetCompanyId(ids []string) map[string]string {
  165. m := map[string]string{}
  166. if len(ids) == 0 {
  167. return m
  168. }
  169. wh, args := util.WhArgs(ids)
  170. rows, err := ClickhouseConn.Query(context.Background(), `select id,company_id from information.ent_info where id in(`+wh+`)`, args...)
  171. if err != nil {
  172. logx.Error(err)
  173. return m
  174. }
  175. for rows.Next() {
  176. var (
  177. id string
  178. company_id string
  179. )
  180. if err := rows.Scan(&id, &company_id); err != nil {
  181. logx.Error(err)
  182. continue
  183. }
  184. m[id] = company_id
  185. }
  186. return m
  187. }
  188. //
  189. func (i *InitNetwork) GetContactInfo(ids []string) map[string]*ContactInfo {
  190. m := map[string]*ContactInfo{}
  191. if len(ids) == 0 {
  192. return m
  193. }
  194. wh, args := util.WhArgs(ids)
  195. rows, err := ClickhouseConn.Query(context.Background(), `select id,phone,name from information.ent_contact where id in (`+wh+`)`, args...)
  196. if err != nil {
  197. logx.Error(err)
  198. return m
  199. }
  200. for rows.Next() {
  201. var (
  202. id string
  203. phone string
  204. name string
  205. )
  206. if err := rows.Scan(&id, &phone, &name); err != nil {
  207. logx.Error(err)
  208. continue
  209. }
  210. if phone == "" || name == "" {
  211. continue
  212. }
  213. m[id] = &ContactInfo{
  214. Name: name,
  215. Phone: phone,
  216. }
  217. }
  218. return m
  219. }