initNetwork.go 7.9 KB

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