initNetwork.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. const (
  13. InitNetworkMaxSize = 100
  14. )
  15. type ContactInfo struct {
  16. Name string
  17. Phone string
  18. }
  19. type Connection struct {
  20. Company_id string
  21. Company_name string
  22. Itype int
  23. QyxyId string
  24. *ContactInfo
  25. }
  26. type InitNetwork struct {
  27. PositionId int64
  28. EntId int64
  29. DeptId int64
  30. UserId int64
  31. EntName string
  32. BusinessType string
  33. }
  34. func (i *InitNetwork) Init() {
  35. logx.Info("进行初始化设置", fmt.Sprintf("%+v", i))
  36. array := []*Connection{}
  37. ids := []string{}
  38. idMap := map[string]bool{}
  39. yearLimit := NetworkCom.ProjectYearLimit()
  40. //①业主人脉:当前企业曾经合作过物业项目的采购单位的联系人作为业主人脉
  41. index1 := 0
  42. func() {
  43. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT buyer,buyer_id from information.transaction_info_all prewhere has(winner,?) and zbtime>?`, i.EntName, yearLimit)
  44. if err != nil {
  45. logx.Error(err)
  46. return
  47. }
  48. for rows.Next() {
  49. var (
  50. buyer string
  51. buyer_id string
  52. )
  53. if err := rows.Scan(&buyer, &buyer_id); err != nil {
  54. logx.Error(err)
  55. continue
  56. }
  57. if buyer == "" || buyer_id == "" {
  58. continue
  59. }
  60. if !idMap[buyer_id] {
  61. ids = append(ids, buyer_id)
  62. idMap[buyer_id] = true
  63. }
  64. array = append(array, &Connection{
  65. Company_id: buyer_id,
  66. Company_name: buyer,
  67. Itype: 1,
  68. })
  69. index1++
  70. if index1 == InitNetworkMaxSize {
  71. break
  72. }
  73. }
  74. }()
  75. logx.Info("初始化设置业主人脉over。。。", index1)
  76. //②甲异业渠道(人脉):“当前企业曾经合作过物业项目的采购单位的中标物业企业中”与当前企业不一样业态的企业的联系人列表
  77. index2 := 0
  78. func() {
  79. if i.BusinessType == "" {
  80. return
  81. }
  82. args := []interface{}{i.EntName, i.EntName}
  83. wh, newArgs := util.WhArgs(strings.Split(i.BusinessType, ","))
  84. args = append(args, newArgs...)
  85. args = append(args, yearLimit)
  86. 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...)
  87. if err != nil {
  88. logx.Error(err)
  89. return
  90. }
  91. for rows.Next() {
  92. var (
  93. winner string
  94. winner_id string
  95. )
  96. if err := rows.Scan(&winner, &winner_id); err != nil {
  97. logx.Error(err)
  98. continue
  99. }
  100. if winner == "" || winner_id == "" {
  101. continue
  102. }
  103. if !idMap[winner_id] {
  104. ids = append(ids, winner_id)
  105. idMap[winner_id] = true
  106. }
  107. array = append(array, &Connection{
  108. Company_id: winner_id,
  109. Company_name: winner,
  110. Itype: 3,
  111. })
  112. index2++
  113. if index2 == InitNetworkMaxSize {
  114. break
  115. }
  116. }
  117. }()
  118. logx.Info("初始化设置甲异业渠道人脉over。。。", index2)
  119. //③招标代理机构(人脉):当前企业曾经合作过的招标代理机构联系人信息
  120. index3 := 0
  121. func() {
  122. rows, err := ClickhouseConn.Query(context.Background(), `select DISTINCT agency,agency_id from information.transaction_info_all prewhere (buyer=? or has(winner,?)) and zbtime>?`, i.EntName, i.EntName, yearLimit)
  123. if err != nil {
  124. logx.Error(err)
  125. return
  126. }
  127. for rows.Next() {
  128. var (
  129. agency string
  130. agency_id string
  131. )
  132. if err := rows.Scan(&agency, &agency_id); err != nil {
  133. logx.Error(err)
  134. continue
  135. }
  136. if agency == "" || agency_id == "" {
  137. continue
  138. }
  139. if !idMap[agency_id] {
  140. ids = append(ids, agency_id)
  141. idMap[agency_id] = true
  142. }
  143. array = append(array, &Connection{
  144. Company_id: agency_id,
  145. Company_name: agency,
  146. Itype: 5,
  147. })
  148. index3++
  149. if index3 == InitNetworkMaxSize {
  150. break
  151. }
  152. }
  153. }()
  154. logx.Info("初始化设置招标代理机构人脉over。。。", index2)
  155. newIds := []string{}
  156. cis := map[string]*ContactInfo{}
  157. cids := map[string]string{}
  158. logx.Info("初始化设置总共企业数量", len(ids))
  159. for _, v := range ids {
  160. newIds = append(newIds, v)
  161. if len(newIds) == C.BuyerBatch {
  162. for kk, vv := range i.GetContactInfo(newIds) {
  163. cis[kk] = vv
  164. }
  165. for kk, vv := range i.GetCompanyId(newIds) {
  166. cids[kk] = vv
  167. }
  168. newIds = []string{}
  169. }
  170. }
  171. if len(newIds) > 0 {
  172. for kk, vv := range i.GetContactInfo(newIds) {
  173. cis[kk] = vv
  174. }
  175. for kk, vv := range i.GetCompanyId(newIds) {
  176. cids[kk] = vv
  177. }
  178. newIds = []string{}
  179. }
  180. for _, v := range array {
  181. if cis[v.Company_id] != nil {
  182. v.ContactInfo = cis[v.Company_id]
  183. }
  184. v.QyxyId = cids[v.Company_id]
  185. }
  186. //
  187. nowFormat := NowFormat(Date_Full_Layout)
  188. values := []interface{}{}
  189. 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"}
  190. index := 0
  191. typeLimit := map[int]int{}
  192. if CrmMysql.ExecTx("初始化人脉", func(tx *sql.Tx) bool {
  193. if CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection where position_id=? and source=1`, i.PositionId) < 0 {
  194. return false
  195. }
  196. for _, v := range array {
  197. if v.Company_name == i.EntName || v.ContactInfo == nil || v.ContactInfo.Phone == "" || v.ContactInfo.Name == "" {
  198. continue
  199. } else if typeLimit[v.Itype] >= 100 {
  200. continue
  201. }
  202. 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 {
  203. typeLimit[v.Itype] = typeLimit[v.Itype] + 1
  204. logx.Info("保存人脉", fmt.Sprintf("%+v", v))
  205. 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)
  206. } else {
  207. logx.Info("过滤掉已存在的人脉", fmt.Sprintf("%+v", v))
  208. }
  209. index++
  210. if index == 200 {
  211. if r1, r2 := CrmMysql.InsertBatchByTx(tx, "crm.connection", fields, values); r1 <= 0 || r2 <= 0 {
  212. return false
  213. }
  214. values = []interface{}{}
  215. }
  216. }
  217. if len(values) > 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. }