initNetwork.go 6.4 KB

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