initNetwork.go 7.1 KB

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