initNetwork.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "sync"
  8. util "app.yhyue.com/moapp/jybase/common"
  9. . "app.yhyue.com/moapp/jybase/date"
  10. . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type ContactInfo struct {
  14. Name string
  15. Phone string
  16. }
  17. type Connection struct {
  18. Company_id string
  19. Company_name string
  20. Itype int
  21. QyxyId string
  22. *ContactInfo
  23. }
  24. type InitNetwork struct {
  25. PositionId int64
  26. EntId int64
  27. DeptId int64
  28. UserId int64
  29. EntName string
  30. BusinessType string
  31. }
  32. func (i *InitNetwork) Init() {
  33. logx.Info("进行初始化设置", fmt.Sprintf("%+v", i))
  34. array := []*Connection{}
  35. ids := []string{}
  36. idMap := map[string]bool{}
  37. yearLimit := NetworkCom.ProjectYearLimit()
  38. //①业主人脉:当前企业曾经合作过物业项目的采购单位的联系人作为业主人脉
  39. index1 := 0
  40. func() {
  41. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT buyer,buyer_id from information.transaction_info_all prewhere has(winner,?) and zbtime>? order by zbtime desc limit ?`, i.EntName, yearLimit, C.InitNetworkMaxSize)
  42. if err != nil {
  43. logx.Error(err)
  44. return
  45. }
  46. for rows.Next() {
  47. var (
  48. buyer string
  49. buyer_id string
  50. )
  51. if err := rows.Scan(&buyer, &buyer_id); err != nil {
  52. logx.Error(err)
  53. continue
  54. }
  55. if buyer == "" || buyer_id == "" {
  56. continue
  57. }
  58. if !idMap[buyer_id] {
  59. ids = append(ids, buyer_id)
  60. idMap[buyer_id] = true
  61. }
  62. array = append(array, &Connection{
  63. Company_id: buyer_id,
  64. Company_name: buyer,
  65. Itype: 1,
  66. })
  67. index1++
  68. if index1 == C.InitNetworkMaxSize {
  69. break
  70. }
  71. }
  72. }()
  73. logx.Info("初始化设置业主人脉over。。。", index1)
  74. //②甲异业渠道(人脉):“当前企业曾经合作过物业项目的采购单位的中标物业企业中”与当前企业不一样业态的企业的联系人列表
  75. index2 := 0
  76. func() {
  77. if i.BusinessType == "" {
  78. return
  79. }
  80. args := []interface{}{i.EntName, i.EntName}
  81. wh, newArgs := util.WhArgs(strings.Split(i.BusinessType, ","))
  82. args = append(args, newArgs...)
  83. args = append(args, yearLimit)
  84. args = append(args, C.InitNetworkMaxSize)
  85. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT wr,wr_id from information.transaction_info_all ARRAY JOIN winner_id as wr_id,winner as wr where buyer_id IN (SELECT buyer_id from information.transaction_info_all prewhere has(winner,?) and buyer_id<>'') and wr<>? and LENGTH(winner)=LENGTH(winner_id) and hasAny(topscopeclass,[`+wh+`])=0 and wr_id<>'' AND zbtime>? order by zbtime desc limit ?`, args...)
  86. if err != nil {
  87. logx.Error(err)
  88. return
  89. }
  90. for rows.Next() {
  91. var (
  92. winner string
  93. winner_id string
  94. )
  95. if err := rows.Scan(&winner, &winner_id); err != nil {
  96. logx.Error(err)
  97. continue
  98. }
  99. if winner == "" || winner_id == "" {
  100. continue
  101. }
  102. if !idMap[winner_id] {
  103. ids = append(ids, winner_id)
  104. idMap[winner_id] = true
  105. }
  106. array = append(array, &Connection{
  107. Company_id: winner_id,
  108. Company_name: winner,
  109. Itype: 3,
  110. })
  111. index2++
  112. if index2 == C.InitNetworkMaxSize {
  113. break
  114. }
  115. }
  116. }()
  117. logx.Info("初始化设置甲异业渠道人脉over。。。", index2)
  118. //③招标代理机构(人脉):当前企业曾经合作过的招标代理机构联系人信息
  119. index3 := 0
  120. func() {
  121. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT agency,agency_id from information.transaction_info_all prewhere (buyer=? or has(winner,?)) and agency<>'' and agency_id<>'' and zbtime>? order by zbtime desc limit ?`, i.EntName, i.EntName, yearLimit, C.InitNetworkMaxSize)
  122. if err != nil {
  123. logx.Error(err)
  124. return
  125. }
  126. for rows.Next() {
  127. var (
  128. agency string
  129. agency_id string
  130. )
  131. if err := rows.Scan(&agency, &agency_id); err != nil {
  132. logx.Error(err)
  133. continue
  134. }
  135. if agency == "" || agency_id == "" {
  136. continue
  137. }
  138. if !idMap[agency_id] {
  139. ids = append(ids, agency_id)
  140. idMap[agency_id] = true
  141. }
  142. array = append(array, &Connection{
  143. Company_id: agency_id,
  144. Company_name: agency,
  145. Itype: 5,
  146. })
  147. index3++
  148. if index3 == C.InitNetworkMaxSize {
  149. break
  150. }
  151. }
  152. }()
  153. logx.Info("初始化设置招标代理机构人脉over。。。", index3)
  154. newIds := []string{}
  155. cis := map[string]*ContactInfo{}
  156. cids := map[string]string{}
  157. logx.Info("初始化设置总共企业数量", len(ids))
  158. for _, v := range ids {
  159. newIds = append(newIds, v)
  160. if len(newIds) == C.SelectBatchSize {
  161. for kk, vv := range i.GetContactInfo(newIds) {
  162. cis[kk] = vv
  163. }
  164. for kk, vv := range i.GetCompanyId(newIds) {
  165. cids[kk] = vv
  166. }
  167. newIds = []string{}
  168. }
  169. }
  170. if len(newIds) > 0 {
  171. for kk, vv := range i.GetContactInfo(newIds) {
  172. cis[kk] = vv
  173. }
  174. for kk, vv := range i.GetCompanyId(newIds) {
  175. cids[kk] = vv
  176. }
  177. newIds = []string{}
  178. }
  179. for _, v := range array {
  180. if cis[v.Company_id] != nil {
  181. v.ContactInfo = cis[v.Company_id]
  182. }
  183. v.QyxyId = cids[v.Company_id]
  184. }
  185. //
  186. nowFormat := NowFormat(Date_Full_Layout)
  187. newArray := []*Connection{}
  188. 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"}
  189. if CrmMysql.ExecTx("初始化人脉", func(tx *sql.Tx) bool {
  190. if CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection where position_id=? and source=1`, i.PositionId) < 0 {
  191. return false
  192. }
  193. pool := make(chan bool, C.PoolSize)
  194. lock := &sync.Mutex{}
  195. wait := &sync.WaitGroup{}
  196. for _, vt := range array {
  197. if vt.Company_name == i.EntName || vt.ContactInfo == nil || vt.ContactInfo.Phone == "" || vt.ContactInfo.Name == "" {
  198. continue
  199. }
  200. pool <- true
  201. wait.Add(1)
  202. go func(v *Connection) {
  203. defer func() {
  204. <-pool
  205. wait.Done()
  206. }()
  207. 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 {
  208. logx.Info("过滤掉已存在的人脉", fmt.Sprintf("%+v", v))
  209. return
  210. }
  211. lock.Lock()
  212. defer lock.Unlock()
  213. logx.Info("保存人脉", fmt.Sprintf("%+v", v))
  214. newArray = append(newArray, v)
  215. }(vt)
  216. }
  217. wait.Wait()
  218. values := []interface{}{}
  219. for _, v := range newArray {
  220. 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)
  221. if len(values) == 200 {
  222. if r1, r2 := CrmMysql.InsertBatchByTx(tx, "crm.connection", fields, values); r1 <= 0 || r2 <= 0 {
  223. return false
  224. }
  225. values = []interface{}{}
  226. }
  227. }
  228. if len(values) > 0 {
  229. if r1, r2 := CrmMysql.InsertBatchByTx(tx, "crm.connection", fields, values); r1 <= 0 || r2 <= 0 {
  230. return false
  231. }
  232. values = []interface{}{}
  233. }
  234. return true
  235. }) {
  236. Network.DeleteCache(i.PositionId)
  237. }
  238. }
  239. //
  240. func (i *InitNetwork) GetCompanyId(ids []string) map[string]string {
  241. m := map[string]string{}
  242. if len(ids) == 0 {
  243. return m
  244. }
  245. wh, args := util.WhArgs(ids)
  246. rows, err := ClickhouseConn.Query(context.Background(), `select id,company_id from information.ent_info prewhere id in(`+wh+`)`, args...)
  247. if err != nil {
  248. logx.Error(err)
  249. return m
  250. }
  251. for rows.Next() {
  252. var (
  253. id string
  254. company_id string
  255. )
  256. if err := rows.Scan(&id, &company_id); err != nil {
  257. logx.Error(err)
  258. continue
  259. }
  260. m[id] = company_id
  261. }
  262. return m
  263. }
  264. //
  265. func (i *InitNetwork) GetContactInfo(ids []string) map[string]*ContactInfo {
  266. m := map[string]*ContactInfo{}
  267. if len(ids) == 0 {
  268. return m
  269. }
  270. wh, args := util.WhArgs(ids)
  271. rows, err := ClickhouseConn.Query(context.Background(), `select id,phone,name from information.ent_contact prewhere id in (`+wh+`)`, args...)
  272. if err != nil {
  273. logx.Error(err)
  274. return m
  275. }
  276. for rows.Next() {
  277. var (
  278. id string
  279. phone string
  280. name string
  281. )
  282. if err := rows.Scan(&id, &phone, &name); err != nil {
  283. logx.Error(err)
  284. continue
  285. }
  286. if phone == "" || name == "" {
  287. continue
  288. }
  289. m[id] = &ContactInfo{
  290. Name: name,
  291. Phone: phone,
  292. }
  293. }
  294. return m
  295. }