initNetwork.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. index := 0
  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. allcon := map[string]bool{}
  194. CrmMysql.SelectByBathByTx(1, func(l *[]map[string]interface{}) bool {
  195. allcon[fmt.Sprintf("%s_%d", util.ObjToString((*l)[0]["company_id"]), util.Int64All((*l)[0]["itype"]))] = true
  196. return true
  197. }, tx, `select company_id,itype from crm.connection where position_id=?`, i.PositionId)
  198. for _, v := range array {
  199. if v.Company_name == i.EntName || v.ContactInfo == nil || v.ContactInfo.Phone == "" || v.ContactInfo.Name == "" {
  200. continue
  201. }
  202. if allcon[fmt.Sprintf("%s_%d", v.Company_id, v.Itype)] {
  203. logx.Info("过滤掉已存在的人脉", fmt.Sprintf("%+v", v))
  204. continue
  205. }
  206. logx.Info("保存人脉", fmt.Sprintf("%+v", v))
  207. 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)
  208. index++
  209. if index == 200 {
  210. if r1, r2 := CrmMysql.InsertBatchByTx(tx, "crm.connection", fields, values); r1 <= 0 || r2 <= 0 {
  211. return false
  212. }
  213. index = 0
  214. values = []interface{}{}
  215. }
  216. }
  217. if index > 0 {
  218. if r1, r2 := CrmMysql.InsertBatchByTx(tx, "crm.connection", fields, values); r1 <= 0 || r2 <= 0 {
  219. return false
  220. }
  221. values = []interface{}{}
  222. }
  223. return true
  224. }) {
  225. Network.DeleteCache(i.PositionId)
  226. }
  227. }
  228. //
  229. func (i *InitNetwork) GetCompanyId(ids []string) map[string]string {
  230. m := map[string]string{}
  231. if len(ids) == 0 {
  232. return m
  233. }
  234. wh, args := util.WhArgs(ids)
  235. rows, err := ClickhouseConn.Query(context.Background(), `select id,company_id from information.ent_info prewhere id in(`+wh+`)`, args...)
  236. if err != nil {
  237. logx.Error(err)
  238. return m
  239. }
  240. for rows.Next() {
  241. var (
  242. id string
  243. company_id string
  244. )
  245. if err := rows.Scan(&id, &company_id); err != nil {
  246. logx.Error(err)
  247. continue
  248. }
  249. m[id] = company_id
  250. }
  251. return m
  252. }
  253. //
  254. func (i *InitNetwork) GetContactInfo(ids []string) map[string]*ContactInfo {
  255. m := map[string]*ContactInfo{}
  256. if len(ids) == 0 {
  257. return m
  258. }
  259. wh, args := util.WhArgs(ids)
  260. rows, err := ClickhouseConn.Query(context.Background(), `select id,phone,name from information.ent_contact prewhere id in (`+wh+`)`, args...)
  261. if err != nil {
  262. logx.Error(err)
  263. return m
  264. }
  265. for rows.Next() {
  266. var (
  267. id string
  268. phone string
  269. name string
  270. )
  271. if err := rows.Scan(&id, &phone, &name); err != nil {
  272. logx.Error(err)
  273. continue
  274. }
  275. if phone == "" || name == "" {
  276. continue
  277. }
  278. m[id] = &ContactInfo{
  279. Name: name,
  280. Phone: phone,
  281. }
  282. }
  283. return m
  284. }