network.go 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "math"
  8. "sort"
  9. "strings"
  10. "sync"
  11. "time"
  12. . "app.yhyue.com/moapp/jybase/common"
  13. . "app.yhyue.com/moapp/jybase/date"
  14. "app.yhyue.com/moapp/jybase/encrypt"
  15. . "app.yhyue.com/moapp/jybase/es"
  16. "app.yhyue.com/moapp/jybase/redis"
  17. . "app.yhyue.com/moapp/jybase/sort"
  18. . "bp.jydev.jianyu360.cn/CRM/networkManage/api/common"
  19. "bp.jydev.jianyu360.cn/CRM/networkManage/api/internal/types"
  20. "github.com/shopspring/decimal"
  21. "github.com/zeromicro/go-zero/core/logx"
  22. )
  23. const (
  24. NetworkManageAllProjectKey = "networkManage_allProject_%d"
  25. NetworkManageList = "networkManage_networkList_%d_%s"
  26. )
  27. var Network = &network{}
  28. type network struct {
  29. }
  30. type networkTree struct {
  31. Count int64 `json:"count"`
  32. Name string `json:"name"`
  33. Children []*networkTreeChild `json:"children"`
  34. }
  35. type networkTreeChild struct {
  36. Count int64 `json:"count"`
  37. Id int64 `json:"id"`
  38. CompanyId string `json:"companyId"`
  39. CompanyName string `json:"companyName"`
  40. Type string `json:"type"`
  41. CreateTime string `json:"createTime"`
  42. IdNames []*idName `json:"idNames"`
  43. }
  44. func (n *networkTree) Len() int {
  45. return len(n.Children)
  46. }
  47. func (n *networkTree) Less(i, j int) bool {
  48. if n.Name == "甲方" {
  49. return n.Children[i].CompanyName < n.Children[j].CompanyName
  50. }
  51. it, err1 := time.ParseInLocation(Date_Full_Layout, n.Children[i].CreateTime, time.Local)
  52. if err1 != nil {
  53. return true
  54. }
  55. jt, err2 := time.ParseInLocation(Date_Full_Layout, n.Children[j].CreateTime, time.Local)
  56. if err2 != nil {
  57. return true
  58. }
  59. return it.Unix() < jt.Unix()
  60. }
  61. func (n *networkTree) Swap(i, j int) {
  62. n.Children[i], n.Children[j] = n.Children[j], n.Children[i]
  63. }
  64. type projectInfo struct {
  65. BuyerCount int64
  66. ProjectCount int64
  67. ProjectAmount float64
  68. ExportId []string
  69. IdNames []*idName
  70. }
  71. type firstpartyNetwork struct {
  72. CompanyId string
  73. CompanyName string
  74. Name string
  75. }
  76. type idName struct {
  77. Id string
  78. Name string
  79. }
  80. type introduceOwnerProject struct {
  81. Networks []*myNetwork
  82. FirstpartyNetwork map[string][]*firstpartyNetwork
  83. Firstparty map[string]*projectInfo
  84. FirstpartyPio map[string]*projectInfo
  85. Supplier map[string]*projectInfo
  86. Adiffb map[string]*projectInfo
  87. Agency map[string]*projectInfo
  88. Middleman map[string]*projectInfo
  89. }
  90. type myNetwork struct {
  91. Id int64
  92. Company_id string
  93. Company_name string
  94. Qyxy_id string
  95. Itype int64
  96. Person string
  97. Phone string
  98. Buyer_count int64
  99. Project_count int64
  100. Relate_buyer_id string
  101. Relate_buyer_name string
  102. Relate_project_id string
  103. Create_time string
  104. }
  105. type nodeTree struct {
  106. NAME string
  107. ID string
  108. SZ_PID0 string
  109. SZ_PID1 string
  110. SZ_PID2 string
  111. SZ_PID3 string
  112. CODE string
  113. PCODE string
  114. DATACOUNT int64
  115. SZ_LEVEL int
  116. SZ_LEAF int
  117. TYPE string
  118. MYID string
  119. MYTYPE string
  120. }
  121. type nodeTrees []*nodeTree
  122. func (n *nodeTrees) Len() int {
  123. return len(*n)
  124. }
  125. func (n *nodeTrees) Less(i, j int) bool {
  126. if (*n)[i].PCODE == (*n)[j].PCODE {
  127. return (*n)[i].CODE < (*n)[j].CODE
  128. }
  129. return (*n)[i].PCODE < (*n)[j].PCODE
  130. }
  131. func (n *nodeTrees) Swap(i, j int) {
  132. (*n)[i], (*n)[j] = (*n)[j], (*n)[i]
  133. }
  134. // 人脉库-添加/修改人脉
  135. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  136. reply := &types.Reply{Data: map[string]interface{}{
  137. "status": 0,
  138. }}
  139. if in.Type != "middleman" && in.Company_id == "" {
  140. return reply
  141. }
  142. if in.Company_name == "" {
  143. in.Company_name = "未填写"
  144. }
  145. nowFormat := NowFormat(Date_Full_Layout)
  146. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  147. if in.Type != "middleman" {
  148. return true
  149. } else if in.Introduce_owner_id == "" || in.Introduce_project_id == "" {
  150. return false
  151. }
  152. values := []interface{}{}
  153. ioi := strings.Split(in.Introduce_owner_id, ",")
  154. ioqi := strings.Split(in.Introduce_owner_qyxy_id, ",")
  155. ion := strings.Split(in.Introduce_owner_name, ",")
  156. if len(ioi) != len(ion) || len(ioi) != len(ioqi) {
  157. return false
  158. }
  159. for k, v := range ioi {
  160. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, ioqi[k], v, ion[k], 1, nowFormat)
  161. }
  162. ipi := strings.Split(in.Introduce_project_id, ",")
  163. ipn := strings.Split(in.Introduce_project_name, ",")
  164. if len(ipi) != len(ipn) {
  165. return false
  166. }
  167. for k, v := range ipi {
  168. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, nil, v, ipn[k], 2, nowFormat)
  169. }
  170. var r2 int64
  171. if isUpdate {
  172. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  173. }
  174. r3, _ := CrmMysql.InsertBatchByTx(tx, "crm.connection_introduce", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "connection_id", "qyxy_id", "relate_id", "relate_name", "itype", "create_time"}, values)
  175. return r2 >= 0 && r3 > 0
  176. }
  177. itype := n.TypeStrConvert(in.Type)
  178. if in.Id > 0 {
  179. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  180. if in.Company_id != "" {
  181. count := CrmMysql.CountBySql(`select count(1) as count from crm.connection where position_id=? and company_id=? and itype=? and id<>? and status=1`, in.PositionId, in.Company_id, itype, in.Id)
  182. if count == -1 {
  183. return false
  184. } else if count > 0 {
  185. reply.Data = map[string]interface{}{
  186. "status": -1,
  187. }
  188. return false
  189. }
  190. }
  191. r1 := CrmMysql.UpdateOrDeleteBySqlByTx(tx, `update crm.connection set company_name=?,company_id=?,contact_person=?,contact_phone=?,source=1,update_time=? where id=? and position_id=?`, in.Company_name, in.Company_id, in.Contact_person, in.Contact_phone, nowFormat, in.Id, in.PositionId)
  192. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  193. }) {
  194. reply.Data = map[string]interface{}{
  195. "status": 1,
  196. }
  197. }
  198. } else {
  199. if in.Company_id != "" {
  200. count := CrmMysql.CountBySql(`select count(1) as count from crm.connection where position_id=? and company_id=? and itype=? and status=1`, in.PositionId, in.Company_id, itype)
  201. if count == -1 {
  202. return reply
  203. } else if count > 0 {
  204. reply.Data = map[string]interface{}{
  205. "status": -1,
  206. }
  207. return reply
  208. }
  209. }
  210. var r1 int64
  211. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  212. _, r1 = CrmMysql.InsertBatchByTx(tx, "crm.connection", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_id", "qyxy_id", "contact_person", "contact_phone", "status", "create_time", "update_time"}, []interface{}{in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, itype, in.Company_name, in.Company_id, in.Qyxy_id, in.Contact_person, in.Contact_phone, 1, nowFormat, nowFormat})
  213. return r1 > 0 && saveIntroduce(tx, r1, false)
  214. }) {
  215. reply.Data = map[string]interface{}{
  216. "status": 1,
  217. "id": r1,
  218. }
  219. }
  220. }
  221. n.DeleteCache(in.PositionId)
  222. return reply
  223. }
  224. // 人脉库-业主名称联想
  225. func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
  226. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  227. res := []map[string]string{}
  228. reply = &types.Reply{Data: res}
  229. in.Name = strings.TrimSpace(in.Name)
  230. if in.Name == "" {
  231. return
  232. }
  233. pageSize := 10
  234. if in.Type == "adiffb" {
  235. businessType := strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  236. if len(businessType) > 0 {
  237. args := []interface{}{in.EntName}
  238. wh, newArgs := WhArgs(businessType)
  239. args = append(args, newArgs...)
  240. q := `select DISTINCT winner_id,winner from information.transaction_info_all prewhere buyer_id in (select buyer_id from information.transaction_info_all prewhere has(winner, ?) and buyer_id<>'') and hasAny(topscopeclass,[` + wh + `])=0 ORDER BY project_id`
  241. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  242. if err != nil {
  243. logx.Error(err)
  244. } else {
  245. repeat := map[string]bool{}
  246. ids := []string{}
  247. for rows.Next() {
  248. var (
  249. winner_id []string
  250. winner []string
  251. )
  252. if err := rows.Scan(&winner_id, &winner); err != nil {
  253. logx.Error(err)
  254. continue
  255. }
  256. for k, v := range winner {
  257. if repeat[v] || !strings.Contains(v, in.Name) {
  258. continue
  259. }
  260. repeat[v] = true
  261. if k >= len(winner_id) {
  262. continue
  263. }
  264. ids = append(ids, winner_id[k])
  265. res = append(res, map[string]string{
  266. "company_name": v,
  267. "company_id": winner_id[k],
  268. })
  269. if len(res) == pageSize {
  270. break
  271. }
  272. }
  273. }
  274. rows.Close()
  275. if err := rows.Err(); err != nil {
  276. logx.Error(err)
  277. }
  278. qyxyId := n.GetQyxyId(ids)
  279. for _, v := range res {
  280. v["qyxy_id"] = qyxyId[v["company_id"]]
  281. }
  282. }
  283. }
  284. } else {
  285. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  286. switch in.Type {
  287. case "firstparty":
  288. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  289. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  290. case "supplier":
  291. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  292. must = append(must, `{"term":{"company_unit_type":3}}`)
  293. case "middleman_owner":
  294. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  295. case "agency":
  296. must = append(must, `{"term":{"company_unit_type":4}}`)
  297. }
  298. q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_id","company_name"]}`, strings.Join(must, ","), pageSize)
  299. logx.Info("人脉库-业主名称联想", q)
  300. datas := VarEs.Get("ent_info", "ent_info", q)
  301. if datas != nil {
  302. for _, v := range *datas {
  303. res = append(res, map[string]string{
  304. "company_name": ObjToString(v["company_name"]),
  305. "company_id": ObjToString(v["id"]),
  306. "qyxy_id": ObjToString(v["company_id"]),
  307. })
  308. }
  309. }
  310. }
  311. reply.Data = res
  312. return
  313. }
  314. // 人脉库-全部人脉项目
  315. func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
  316. reply = &types.Reply{}
  317. businessType := strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  318. var count int64
  319. var list []*networkTree
  320. firstparty, firstpartyPio := map[string]*projectInfo{}, map[string]*projectInfo{}
  321. firstpartyChild := map[string][]*firstpartyNetwork{}
  322. if in.Id != "" {
  323. if in.Type == "firstparty" {
  324. fpn := n.FirstpartyNetwork(in.Name, []string{in.Id})
  325. if fpn[in.Id] != nil {
  326. firstparty, firstpartyPio = n.Introduce_Firstparty(fpn, businessType)
  327. nameIndex := map[string]int{}
  328. for _, v := range fpn[in.Id] {
  329. if _, ok := nameIndex[v.Name]; !ok {
  330. nameIndex[v.Name] = len(list)
  331. list = append(list, &networkTree{
  332. Name: v.Name,
  333. })
  334. }
  335. ntc := &networkTreeChild{
  336. CompanyName: v.CompanyName,
  337. CompanyId: v.CompanyId,
  338. Type: "firstparty",
  339. }
  340. if firstparty[v.CompanyId] != nil {
  341. ntc.Count = firstparty[v.CompanyId].ProjectCount
  342. }
  343. count++
  344. list[nameIndex[v.Name]].Count += ntc.Count
  345. list[nameIndex[v.Name]].Children = append(list[nameIndex[v.Name]].Children, ntc)
  346. }
  347. }
  348. }
  349. } else {
  350. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  351. sqlAppend1 := ""
  352. if in.Name != "" {
  353. sqlAppend1 += ` and a.company_name like ?`
  354. args = append(args, "%"+in.Name+"%")
  355. }
  356. aio := &introduceOwnerProject{}
  357. key := fmt.Sprintf(NetworkManageAllProjectKey, in.PositionId)
  358. if rbt, rerr := redis.GetNewBytes("newother", key); rerr == nil && rbt != nil {
  359. json.Unmarshal(*rbt, &aio)
  360. } else {
  361. aio = n.AllIntroduceOwner(in.PositionId, sqlAppend1, "", args, true, businessType)
  362. redis.Put("newother", key, aio, NetworkCom.CacheTimeout())
  363. }
  364. firstparty = aio.Firstparty
  365. firstpartyPio = aio.FirstpartyPio
  366. firstpartyChild = aio.FirstpartyNetwork
  367. list = []*networkTree{
  368. &networkTree{
  369. Name: "甲方",
  370. Children: []*networkTreeChild{},
  371. },
  372. &networkTree{
  373. Name: "供应商/渠道",
  374. Children: []*networkTreeChild{},
  375. },
  376. &networkTree{
  377. Name: "同甲异业渠道",
  378. Children: []*networkTreeChild{},
  379. },
  380. &networkTree{
  381. Name: "中间人",
  382. Children: []*networkTreeChild{},
  383. },
  384. &networkTree{
  385. Name: "招标代理",
  386. Children: []*networkTreeChild{},
  387. },
  388. }
  389. //
  390. for _, v := range aio.Networks {
  391. if v.Itype <= 0 || v.Itype > int64(len(list)) {
  392. return
  393. }
  394. ntc := &networkTreeChild{
  395. Id: v.Id,
  396. CompanyName: v.Company_name,
  397. CompanyId: v.Company_id,
  398. Type: n.TypeIntConvert(v.Itype),
  399. CreateTime: v.Create_time,
  400. }
  401. switch v.Itype {
  402. case 1:
  403. if firstparty[v.Company_id] != nil {
  404. ntc.Count = firstparty[v.Company_id].ProjectCount
  405. }
  406. case 2:
  407. if aio.Supplier[v.Company_id] != nil {
  408. ntc.Count = aio.Supplier[v.Company_id].ProjectCount
  409. ntc.IdNames = aio.Supplier[v.Company_id].IdNames
  410. }
  411. case 3:
  412. if aio.Adiffb[v.Company_id] != nil {
  413. ntc.Count = aio.Adiffb[v.Company_id].ProjectCount
  414. ntc.IdNames = aio.Adiffb[v.Company_id].IdNames
  415. }
  416. case 4:
  417. if v.Relate_buyer_id != "" {
  418. for _, rbi := range strings.Split(v.Relate_buyer_id, ",") {
  419. ntc.IdNames = append(ntc.IdNames, &idName{Id: rbi})
  420. if aio.Middleman[rbi] != nil {
  421. ntc.Count++
  422. }
  423. }
  424. }
  425. case 5:
  426. if aio.Agency[v.Company_id] != nil {
  427. ntc.Count = aio.Agency[v.Company_id].ProjectCount
  428. ntc.IdNames = aio.Agency[v.Company_id].IdNames
  429. }
  430. }
  431. count += ntc.Count
  432. list[v.Itype-1].Count += ntc.Count
  433. list[v.Itype-1].Children = append(list[v.Itype-1].Children, ntc)
  434. }
  435. }
  436. checkCode := []*nodeTree{}
  437. parentConvList := &nodeTrees{}
  438. convList := &nodeTrees{}
  439. for _, v := range list {
  440. if len(v.Children) == 0 {
  441. continue
  442. }
  443. pm := &nodeTree{
  444. CODE: v.Name,
  445. NAME: v.Name,
  446. SZ_PID0: v.Name,
  447. SZ_LEVEL: 0,
  448. DATACOUNT: 0,
  449. }
  450. if len(v.Children) > 0 {
  451. pm.SZ_LEAF = 0
  452. } else {
  453. pm.SZ_LEAF = 1
  454. }
  455. sort.Sort(v)
  456. id, pType, pMyType, pMyId := "", "", "", ""
  457. for _, vv := range v.Children {
  458. myId := vv.CompanyId
  459. if vv.Type == "middleman" {
  460. myId = fmt.Sprint(vv.Id)
  461. }
  462. if pMyId != "" {
  463. pMyId += ","
  464. }
  465. pMyId += myId
  466. if pMyType != "" {
  467. pMyType += ","
  468. }
  469. pMyType += vv.Type
  470. myChildIds, myChildTypes := "", ""
  471. compareId := fmt.Sprint(vv.Id)
  472. thisLength := len(checkCode)
  473. if in.Id == "" && v.Name == "甲方" {
  474. cccNodes := []*nodeTree{}
  475. cccIndexMap := map[string]int{}
  476. for _, vvv := range firstpartyChild[vv.CompanyId] {
  477. if in.CheckCode != "" {
  478. compareId = vvv.CompanyId
  479. }
  480. cccIndex, cccOk := cccIndexMap[vvv.Name]
  481. var dataCount int64
  482. if firstpartyPio[vvv.CompanyId] != nil {
  483. dataCount = firstpartyPio[vvv.CompanyId].ProjectCount
  484. }
  485. if cccOk {
  486. cccNodes[cccIndex].ID += "," + vvv.CompanyId
  487. cccNodes[cccIndex].MYID += "," + vvv.CompanyId
  488. cccNodes[cccIndex].TYPE += ",firstparty"
  489. cccNodes[cccIndex].MYTYPE += ",firstparty"
  490. cccNodes[cccIndex].DATACOUNT += dataCount
  491. } else {
  492. nt := &nodeTree{
  493. NAME: vvv.Name,
  494. ID: vvv.CompanyId,
  495. SZ_PID0: v.Name,
  496. SZ_PID1: v.Name + ":" + myId,
  497. SZ_PID2: v.Name + ":" + myId + ":" + vvv.Name,
  498. CODE: v.Name + ":" + myId + ":" + vvv.Name,
  499. PCODE: v.Name + ":" + myId,
  500. SZ_LEVEL: 2,
  501. SZ_LEAF: 0,
  502. TYPE: "firstparty",
  503. MYID: vvv.CompanyId,
  504. MYTYPE: "firstparty",
  505. DATACOUNT: dataCount,
  506. }
  507. cccNodes = append(cccNodes, nt)
  508. cccIndexMap[vvv.Name] = len(cccNodes) - 1
  509. if compareId == in.ClickId {
  510. checkCode = append(checkCode, nt)
  511. }
  512. }
  513. if id != "" {
  514. id += ","
  515. }
  516. id += vvv.CompanyId
  517. if pType != "" {
  518. pType += ","
  519. }
  520. pType += vv.Type
  521. if myChildIds != "" {
  522. myChildIds += ","
  523. }
  524. myChildIds += vvv.CompanyId
  525. if myChildTypes != "" {
  526. myChildTypes += ","
  527. }
  528. myChildTypes += vv.Type
  529. nt := &nodeTree{
  530. NAME: vvv.CompanyName,
  531. ID: vvv.CompanyId,
  532. SZ_PID0: v.Name,
  533. SZ_PID1: v.Name + ":" + myId,
  534. SZ_PID2: cccNodes[cccIndex].SZ_PID2,
  535. SZ_PID3: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  536. CODE: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  537. PCODE: cccNodes[cccIndex].CODE,
  538. SZ_LEVEL: 3,
  539. SZ_LEAF: 1,
  540. TYPE: "firstparty",
  541. MYID: vvv.CompanyId,
  542. MYTYPE: "firstparty",
  543. DATACOUNT: dataCount,
  544. }
  545. if compareId == in.ClickId {
  546. checkCode = append(checkCode, nt)
  547. }
  548. *convList = append(*convList, nt)
  549. }
  550. for _, cccNode := range cccNodes {
  551. *convList = append(*convList, cccNode)
  552. }
  553. if in.CheckCode != "" {
  554. compareId = "-1"
  555. }
  556. } else {
  557. if id != "" {
  558. id += ","
  559. }
  560. id += myId
  561. if pType != "" {
  562. pType += ","
  563. }
  564. pType += vv.Type
  565. myChildIds = myId
  566. myChildTypes = vv.Type
  567. if in.CheckCode != "" {
  568. if len(vv.IdNames) == 0 {
  569. compareId = "-1"
  570. }
  571. for in_k, in_v := range vv.IdNames {
  572. if in_v.Id == in.ClickId {
  573. break
  574. } else if in_k == len(vv.IdNames)-1 {
  575. compareId = "-1"
  576. }
  577. }
  578. }
  579. }
  580. cm := &nodeTree{
  581. NAME: vv.CompanyName,
  582. ID: myChildIds,
  583. SZ_PID0: v.Name,
  584. SZ_PID1: v.Name + ":" + myId,
  585. CODE: v.Name + ":" + myId,
  586. PCODE: v.Name,
  587. DATACOUNT: vv.Count,
  588. SZ_LEVEL: 1,
  589. SZ_LEAF: If(myChildIds == "", 1, 0).(int),
  590. TYPE: myChildTypes,
  591. MYID: myId,
  592. MYTYPE: vv.Type,
  593. }
  594. //如果甲方的子节点被选中了,父节点跟着被选中,非甲方的要对比子节点可介绍业主
  595. if thisLength == len(checkCode) && ((in.ClickId != "" && in.ClickId == fmt.Sprint(vv.Id)) || (in.CheckCode != "" && compareId == fmt.Sprint(vv.Id))) {
  596. checkCode = append(checkCode, cm)
  597. }
  598. *convList = append(*convList, cm)
  599. }
  600. pm.ID = id
  601. pm.TYPE = pType
  602. pm.MYTYPE = pMyType
  603. pm.MYID = pMyId
  604. *parentConvList = append(*parentConvList, pm)
  605. }
  606. sort.Sort(convList)
  607. *parentConvList = append(*parentConvList, *convList...)
  608. newCheckCode := []string{}
  609. if in.CheckCode != "" {
  610. m := map[string]bool{}
  611. for _, v := range strings.Split(in.CheckCode, ",") {
  612. m[v] = true
  613. }
  614. for _, v := range checkCode {
  615. if !m[v.SZ_PID0] && !m[v.SZ_PID1] && !m[v.SZ_PID2] && !m[v.SZ_PID3] {
  616. continue
  617. }
  618. newCheckCode = append(newCheckCode, v.CODE)
  619. }
  620. } else {
  621. for _, v := range checkCode {
  622. newCheckCode = append(newCheckCode, v.CODE)
  623. }
  624. }
  625. if in.CheckCode == "" && in.ClickId == "" && len(newCheckCode) == 0 && len(*parentConvList) > 0 && (*parentConvList)[0].SZ_LEVEL == 0 {
  626. if (*parentConvList)[0].NAME == "甲方" {
  627. var level1 *nodeTree
  628. var level2 *nodeTree
  629. var level3 *nodeTree
  630. for _, v := range *parentConvList {
  631. if (*parentConvList)[0].NAME != v.SZ_PID0 {
  632. continue
  633. }
  634. if level1 == nil && v.SZ_LEVEL == 1 {
  635. level1 = v
  636. }
  637. if level2 == nil && v.SZ_LEVEL == 2 {
  638. level2 = v
  639. }
  640. if level3 == nil && v.SZ_LEVEL == 3 {
  641. level3 = v
  642. }
  643. }
  644. if level3 != nil && level2 != nil && level1 != nil && level3.SZ_PID2 == level2.CODE && level2.SZ_PID1 == level1.CODE {
  645. newCheckCode = append(newCheckCode, level3.CODE)
  646. } else if level2 != nil && level1 != nil && level2.SZ_PID1 == level1.CODE {
  647. newCheckCode = append(newCheckCode, level2.CODE)
  648. } else if level1 != nil {
  649. newCheckCode = append(newCheckCode, level1.CODE)
  650. }
  651. } else {
  652. for _, v := range *parentConvList {
  653. if (*parentConvList)[0].NAME != v.SZ_PID0 || v.SZ_LEVEL == 0 {
  654. continue
  655. }
  656. newCheckCode = append(newCheckCode, v.CODE)
  657. break
  658. }
  659. }
  660. }
  661. reply = &types.Reply{
  662. Data: map[string]interface{}{
  663. "count": count,
  664. "list": parentConvList,
  665. "checked": newCheckCode,
  666. },
  667. }
  668. return reply
  669. }
  670. // 人脉库-列表
  671. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  672. if in.Page_size <= 0 {
  673. in.Page_size = 10
  674. }
  675. if in.Current_page <= 0 {
  676. in.Current_page = 1
  677. }
  678. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Order_amount == 0
  679. sqlAppendArgs := []interface{}{}
  680. sqlAppend1 := ""
  681. if dbPaging && in.Type != "" {
  682. sqlAppend1 += ` and a.itype=?`
  683. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  684. }
  685. comSqlAppend := ""
  686. comSqlArgs := []interface{}{}
  687. if in.Starttime != "" {
  688. comSqlAppend += ` and a.create_time>=?`
  689. comSqlArgs = append(comSqlArgs, in.Starttime+" 00:00:00")
  690. }
  691. if in.Endtime != "" {
  692. comSqlAppend += ` and a.create_time<=?`
  693. if in.Starttime == in.Endtime {
  694. in.Endtime += " 23:59:59"
  695. } else {
  696. in.Endtime += " 00:00:00"
  697. }
  698. comSqlArgs = append(comSqlArgs, in.Endtime)
  699. }
  700. if in.Name != "" {
  701. comSqlAppend += ` and a.company_name like ?`
  702. comSqlArgs = append(comSqlArgs, "%"+in.Name+"%")
  703. }
  704. sqlAppend1 += comSqlAppend
  705. sqlAppendArgs = append(sqlAppendArgs, comSqlArgs...)
  706. var count int64
  707. start := (in.Current_page - 1) * in.Page_size
  708. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  709. args = append(args, sqlAppendArgs...)
  710. sqlAppend2 := ""
  711. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  712. if dbPaging {
  713. newArgs := []interface{}{in.PositionId}
  714. newArgs = append(newArgs, sqlAppendArgs...)
  715. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  716. sqlAppend2 = ` limit ?,?`
  717. args = append(args, start, in.Page_size)
  718. itemArgs := []interface{}{in.PositionId}
  719. itemArgs = append(itemArgs, comSqlArgs...)
  720. items := CrmMysql.SelectBySql(`SELECT itype,SUM(1) AS sum FROM crm.connection a WHERE a.position_id=?`+comSqlAppend+` GROUP BY a.itype`, itemArgs...)
  721. if items != nil {
  722. for _, v := range *items {
  723. switch Int64All(v["itype"]) {
  724. case 1:
  725. firstparty_count = IntAll(v["sum"])
  726. case 2:
  727. supplier_count = IntAll(v["sum"])
  728. case 3:
  729. adiffb_count = IntAll(v["sum"])
  730. case 4:
  731. middleman_count = IntAll(v["sum"])
  732. case 5:
  733. agency_count = IntAll(v["sum"])
  734. }
  735. }
  736. }
  737. }
  738. //
  739. list := []*map[string]interface{}{}
  740. isTjProject := true
  741. businessType := []string{}
  742. businessType = strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  743. if len(businessType) == 0 {
  744. isTjProject = false
  745. }
  746. redisKey := fmt.Sprintf(NetworkManageList, in.PositionId, GetMd5String(fmt.Sprintf("%+v", in)))
  747. aio := &introduceOwnerProject{}
  748. aioByte, aioErr := redis.GetNewBytes("newother", redisKey)
  749. timeOut := NetworkCom.CacheTimeout()
  750. if aioErr == nil && aioByte != nil {
  751. json.Unmarshal(*aioByte, &aio)
  752. } else {
  753. aio = n.AllIntroduceOwner(in.PositionId, sqlAppend1, sqlAppend2, args, isTjProject, businessType)
  754. redis.Put("newother", redisKey, aio, timeOut)
  755. }
  756. entMonitor := NetworkCom.EntMonitor(in.UserId)
  757. for _, v := range aio.Networks {
  758. itype := ""
  759. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  760. export_id := []string{}
  761. jump_type, jump_id := "", ""
  762. switch v.Itype {
  763. case 1:
  764. itype = "甲方"
  765. jump_type = "firstparty"
  766. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  767. if jump_id != "" {
  768. jump_id += ","
  769. }
  770. jump_id += vv.CompanyId
  771. if entMonitor[vv.CompanyName] {
  772. monitor_count++
  773. }
  774. }
  775. if aio.Firstparty[v.Company_id] != nil {
  776. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  777. project_count = aio.Firstparty[v.Company_id].ProjectCount
  778. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  779. export_id = aio.Firstparty[v.Company_id].ExportId
  780. }
  781. case 2:
  782. itype = "供应商/渠道"
  783. jump_type = "supplier"
  784. jump_id = v.Company_id
  785. if aio.Supplier[v.Company_id] != nil {
  786. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  787. project_count = aio.Supplier[v.Company_id].ProjectCount
  788. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  789. export_id = aio.Supplier[v.Company_id].ExportId
  790. for _, vv := range aio.Supplier[v.Company_id].IdNames {
  791. if entMonitor[vv.Name] {
  792. monitor_count++
  793. }
  794. }
  795. }
  796. case 3:
  797. itype = "同甲异业渠道"
  798. jump_type = "adiffb"
  799. jump_id = v.Company_id
  800. if aio.Adiffb[v.Company_id] != nil {
  801. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  802. project_count = aio.Adiffb[v.Company_id].ProjectCount
  803. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  804. export_id = aio.Adiffb[v.Company_id].ExportId
  805. for _, vv := range aio.Adiffb[v.Company_id].IdNames {
  806. if entMonitor[vv.Name] {
  807. monitor_count++
  808. }
  809. }
  810. }
  811. case 4:
  812. itype = "中间人"
  813. jump_type = "middleman"
  814. jump_id = fmt.Sprint(v.Id)
  815. buyer_count = v.Buyer_count
  816. if v.Relate_buyer_name != "" {
  817. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  818. if v == "" {
  819. continue
  820. }
  821. if entMonitor[v] {
  822. monitor_count++
  823. }
  824. }
  825. }
  826. if v.Relate_buyer_id != "" {
  827. for _, v := range strings.Split(v.Relate_buyer_id, ",") {
  828. if aio.Middleman[v] != nil {
  829. project_count++
  830. expect_amount += aio.Middleman[v].ProjectAmount
  831. export_id = append(export_id, aio.Middleman[v].ExportId...)
  832. }
  833. }
  834. }
  835. case 5:
  836. itype = "招标代理机构"
  837. jump_type = "agency"
  838. jump_id = v.Company_id
  839. if aio.Agency[v.Company_id] != nil {
  840. buyer_count = aio.Agency[v.Company_id].BuyerCount
  841. project_count = aio.Agency[v.Company_id].ProjectCount
  842. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  843. export_id = aio.Agency[v.Company_id].ExportId
  844. for _, vv := range aio.Agency[v.Company_id].IdNames {
  845. if entMonitor[vv.Name] {
  846. monitor_count++
  847. }
  848. }
  849. }
  850. }
  851. if buyer_count < in.Buyercount_start {
  852. continue
  853. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  854. continue
  855. } else if in.Monitor == 1 && monitor_count <= 0 {
  856. continue
  857. } else if in.Monitor == -1 && monitor_count > 0 {
  858. continue
  859. } else if monitor_count < in.Monitorcount_start {
  860. continue
  861. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  862. continue
  863. }
  864. if !dbPaging {
  865. switch v.Itype {
  866. case 1:
  867. firstparty_count++
  868. case 2:
  869. supplier_count++
  870. case 3:
  871. adiffb_count++
  872. case 4:
  873. middleman_count++
  874. case 5:
  875. agency_count++
  876. }
  877. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  878. continue
  879. }
  880. }
  881. url := ""
  882. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  883. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  884. } else if jump_type == "firstparty" && v.Company_name != "" {
  885. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  886. }
  887. list = append(list, &map[string]interface{}{
  888. "company_id": v.Company_id,
  889. "company_name": v.Company_name,
  890. "type": itype,
  891. "jump_type": jump_type,
  892. "jump_id": jump_id,
  893. "person": v.Person,
  894. "phone": v.Phone,
  895. "buyer_count": buyer_count,
  896. "monitor_count": monitor_count,
  897. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  898. "project_count": project_count,
  899. "create_time": v.Create_time,
  900. "url": url,
  901. "id": v.Id,
  902. "export_id": strings.Join(export_id, ","),
  903. })
  904. }
  905. csList := &ComSortList{
  906. SortKeys: []*ComSortKey{
  907. &ComSortKey{
  908. Keys: []string{"expect_amount"},
  909. Order: 1,
  910. Type: "float",
  911. },
  912. },
  913. List: list,
  914. }
  915. if in.Order_amount == -1 {
  916. csList.SortKeys[0].Order = -1
  917. }
  918. if in.Order_amount != 0 {
  919. sort.Sort(csList)
  920. }
  921. finalList := []*map[string]interface{}{}
  922. if !dbPaging {
  923. count = int64(len(csList.List))
  924. if count > 0 && start < count {
  925. end := start + in.Page_size
  926. if end > count {
  927. end = count
  928. }
  929. finalList = csList.List[start:end]
  930. }
  931. } else {
  932. finalList = csList.List
  933. }
  934. pool := make(chan bool, C.PoolSize)
  935. wait := &sync.WaitGroup{}
  936. for _, v := range finalList {
  937. exportId := ObjToString((*v)["export_id"])
  938. md5Id := GetMd5String(exportId)
  939. (*v)["export_url"] = "/subscribepay/network/projectExport?version=1&export_id=" + md5Id
  940. delete(*v, "export_id")
  941. pool <- true
  942. wait.Add(1)
  943. go func(md5IdNew, exportIdNew string) {
  944. defer func() {
  945. <-pool
  946. wait.Done()
  947. }()
  948. key := fmt.Sprintf("network_export_%s", md5IdNew)
  949. if ok, _ := redis.Exists("newother", key); !ok {
  950. redis.Put("newother", key, exportIdNew, timeOut)
  951. }
  952. }(md5Id, exportId)
  953. }
  954. wait.Wait()
  955. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  956. return &types.Reply{
  957. Data: map[string]interface{}{
  958. "count": count,
  959. "total_page": total_page,
  960. "firstparty_count": firstparty_count,
  961. "supplier_count": supplier_count,
  962. "adiffb_count": adiffb_count,
  963. "middleman_count": middleman_count,
  964. "agency_count": agency_count,
  965. "list": finalList,
  966. },
  967. }
  968. }
  969. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  970. result := map[string][]*firstpartyNetwork{}
  971. if len(values) == 0 {
  972. return result
  973. }
  974. wh, args := WhArgs(values)
  975. q := `select DISTINCT a.a_id,a.b_id as company_id,a.b_name as company_name,b.name from information.ent_map_code a
  976. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  977. if name != "" {
  978. q += ` prewhere c.company_name like ?`
  979. args = append(args, "%"+name+"%")
  980. }
  981. q += ` order by b.name,a.a_id,a.b_name`
  982. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  983. if err != nil {
  984. logx.Error(err)
  985. return result
  986. }
  987. for rows.Next() {
  988. var (
  989. a_id string
  990. company_id string
  991. company_name string
  992. name string
  993. )
  994. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  995. logx.Error(err)
  996. continue
  997. }
  998. if company_id == "" || company_name == "" {
  999. continue
  1000. }
  1001. result[a_id] = append(result[a_id], &firstpartyNetwork{
  1002. CompanyId: company_id,
  1003. CompanyName: company_name,
  1004. Name: name,
  1005. })
  1006. }
  1007. return result
  1008. }
  1009. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, businessType []string) (map[string]*projectInfo, map[string]*projectInfo) {
  1010. values := []string{}
  1011. vm := map[string]*projectInfo{}
  1012. for _, v := range fpn {
  1013. for _, vv := range v {
  1014. vm[vv.CompanyId] = &projectInfo{}
  1015. values = append(values, vv.CompanyId)
  1016. }
  1017. }
  1018. result := map[string]*projectInfo{}
  1019. if len(values) == 0 {
  1020. return result, vm
  1021. }
  1022. wh, args := WhArgs(values)
  1023. q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all prewhere buyer_id in (` + wh + `) and zbtime>? and project_bidstatus>1`
  1024. args = append(args, NetworkCom.ProjectYearLimit())
  1025. if len(businessType) > 0 {
  1026. newWh, newArgs := WhArgs(businessType)
  1027. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1028. args = append(args, newArgs...)
  1029. }
  1030. q += ` group by buyer_id`
  1031. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1032. if err != nil {
  1033. logx.Error(err)
  1034. return result, vm
  1035. }
  1036. for rows.Next() {
  1037. var (
  1038. buyer_id string
  1039. project_count uint64
  1040. project_amount decimal.Decimal
  1041. project_id []string
  1042. )
  1043. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1044. logx.Error(err)
  1045. continue
  1046. }
  1047. if vm[buyer_id] == nil {
  1048. continue
  1049. }
  1050. vm[buyer_id].ProjectCount += int64(project_count)
  1051. pf, _ := project_amount.Float64()
  1052. vm[buyer_id].ProjectAmount += pf
  1053. vm[buyer_id].ExportId = project_id
  1054. }
  1055. rows.Close()
  1056. if err := rows.Err(); err != nil {
  1057. logx.Error(err)
  1058. }
  1059. for k, v := range fpn {
  1060. if result[k] == nil {
  1061. result[k] = &projectInfo{}
  1062. }
  1063. result[k].BuyerCount = int64(len(v))
  1064. for _, vv := range v {
  1065. if vm[vv.CompanyId] == nil {
  1066. continue
  1067. }
  1068. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  1069. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  1070. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  1071. }
  1072. }
  1073. return result, vm
  1074. }
  1075. func (n *network) Introduce_Supplier(values []string, businessType []string) map[string]*projectInfo {
  1076. if len(values) == 0 {
  1077. return map[string]*projectInfo{}
  1078. }
  1079. wh, args := WhArgs(values)
  1080. repeat := map[string]bool{}
  1081. whRepeat := map[string]map[string]bool{}
  1082. buyers := []string{}
  1083. q := `select winner_id,buyer_id,buyer from information.transaction_info_all prewhere hasAny(winner_id,[` + wh + `]) and buyer_id<>''`
  1084. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1085. if err != nil {
  1086. logx.Error(err)
  1087. return map[string]*projectInfo{}
  1088. }
  1089. vbs := map[string][]*idName{}
  1090. for rows.Next() {
  1091. var (
  1092. winner_id []string
  1093. buyer_id string
  1094. buyer string
  1095. )
  1096. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  1097. logx.Error(err)
  1098. continue
  1099. }
  1100. if buyer_id == "" || buyer == "" {
  1101. continue
  1102. }
  1103. if !repeat[buyer_id] {
  1104. repeat[buyer_id] = true
  1105. buyers = append(buyers, buyer_id)
  1106. }
  1107. for _, v := range winner_id {
  1108. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  1109. continue
  1110. }
  1111. if whRepeat[v] == nil {
  1112. whRepeat[v] = map[string]bool{}
  1113. }
  1114. whRepeat[v][buyer_id] = true
  1115. vbs[v] = append(vbs[v], &idName{
  1116. Id: buyer_id,
  1117. Name: buyer,
  1118. })
  1119. }
  1120. }
  1121. rows.Close()
  1122. if err := rows.Err(); err != nil {
  1123. logx.Error(err)
  1124. }
  1125. return n.MakeProjectInfo(nil, values, vbs, businessType)
  1126. }
  1127. func (n *network) Introduce_Agency(values []string, businessType []string) map[string]*projectInfo {
  1128. if len(values) == 0 {
  1129. return map[string]*projectInfo{}
  1130. }
  1131. wh, args := WhArgs(values)
  1132. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info_all prewhere agency_id in (` + wh + `) and buyer_id<>''`
  1133. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1134. if err != nil {
  1135. logx.Error(err)
  1136. return map[string]*projectInfo{}
  1137. }
  1138. vbs := map[string][]*idName{}
  1139. buyers := []string{}
  1140. repeat := map[string]bool{}
  1141. for rows.Next() {
  1142. var (
  1143. agency_id string
  1144. buyer_id string
  1145. buyer string
  1146. )
  1147. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  1148. logx.Error(err)
  1149. continue
  1150. }
  1151. if buyer_id == "" || buyer == "" {
  1152. continue
  1153. }
  1154. vbs[agency_id] = append(vbs[agency_id], &idName{
  1155. Id: buyer_id,
  1156. Name: buyer,
  1157. })
  1158. if !repeat[buyer_id] {
  1159. repeat[buyer_id] = true
  1160. buyers = append(buyers, buyer_id)
  1161. }
  1162. }
  1163. rows.Close()
  1164. if err := rows.Err(); err != nil {
  1165. logx.Error(err)
  1166. }
  1167. return n.MakeProjectInfo(values, nil, vbs, businessType)
  1168. }
  1169. func (n *network) Introduce_Middleman_Buyer(values []string, businessType []string) map[string]*projectInfo {
  1170. return n.BuyerProjectInfo(values, businessType)
  1171. }
  1172. func (n *network) Introduce_Middleman_Project(values []string, businessType []string) map[string]*projectInfo {
  1173. result := map[string]*projectInfo{}
  1174. if len(values) == 0 {
  1175. return result
  1176. }
  1177. wh, args := WhArgs(values)
  1178. q := `select DISTINCT project_id,project_money from information.transaction_info_all prewhere project_id in (` + wh + `)`
  1179. if len(businessType) > 0 {
  1180. newWh, newArgs := WhArgs(businessType)
  1181. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1182. args = append(args, newArgs...)
  1183. }
  1184. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1185. if err != nil {
  1186. logx.Error(err)
  1187. return nil
  1188. }
  1189. for rows.Next() {
  1190. var (
  1191. project_id string
  1192. project_money decimal.Decimal
  1193. )
  1194. if err := rows.Scan(&project_id, &project_money); err != nil {
  1195. logx.Error(err)
  1196. continue
  1197. }
  1198. pf, _ := project_money.Float64()
  1199. result[project_id] = &projectInfo{
  1200. ProjectAmount: pf,
  1201. }
  1202. }
  1203. rows.Close()
  1204. if err := rows.Err(); err != nil {
  1205. logx.Error(err)
  1206. }
  1207. return result
  1208. }
  1209. func (n *network) TypeStrConvert(itype string) int64 {
  1210. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1211. switch itype {
  1212. case "firstparty":
  1213. return 1
  1214. case "supplier":
  1215. return 2
  1216. case "adiffb":
  1217. return 3
  1218. case "middleman":
  1219. return 4
  1220. case "agency":
  1221. return 5
  1222. }
  1223. return 0
  1224. }
  1225. func (n *network) TypeIntConvert(itype int64) string {
  1226. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1227. switch itype {
  1228. case 1:
  1229. return "firstparty"
  1230. case 2:
  1231. return "supplier"
  1232. case 3:
  1233. return "adiffb"
  1234. case 4:
  1235. return "middleman"
  1236. case 5:
  1237. return "agency"
  1238. }
  1239. return ""
  1240. }
  1241. func (n *network) GetQyxyId(ids []string) map[string]string {
  1242. m := map[string]string{}
  1243. if len(ids) == 0 {
  1244. return m
  1245. }
  1246. wh, args := WhArgs(ids)
  1247. q := `select id,company_id from information.ent_info prewhere id in (` + wh + `)`
  1248. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1249. if err != nil {
  1250. logx.Error(err)
  1251. } else {
  1252. for rows.Next() {
  1253. var (
  1254. id string
  1255. company_id string
  1256. )
  1257. if err := rows.Scan(&id, &company_id); err != nil {
  1258. logx.Error(err)
  1259. continue
  1260. }
  1261. m[id] = company_id
  1262. }
  1263. rows.Close()
  1264. if err := rows.Err(); err != nil {
  1265. logx.Error(err)
  1266. }
  1267. }
  1268. return m
  1269. }
  1270. func (n *network) BuyerProjectInfo(ids []string, businessType []string) map[string]*projectInfo {
  1271. vm := map[string]*projectInfo{}
  1272. m := map[string]bool{}
  1273. newIds := []string{}
  1274. for _, v := range ids {
  1275. if m[v] {
  1276. continue
  1277. }
  1278. m[v] = true
  1279. newIds = append(newIds, v)
  1280. if len(newIds) == C.SelectBatchSize {
  1281. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1282. newIds = []string{}
  1283. }
  1284. }
  1285. if len(newIds) > 0 {
  1286. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1287. }
  1288. return vm
  1289. }
  1290. func (n *network) BuyerProjectInfoBatch(ids []string, businessType []string, vm map[string]*projectInfo) {
  1291. if len(ids) == 0 {
  1292. return
  1293. }
  1294. wh, args := WhArgs(ids)
  1295. q := `select buyer_id,uniqCombined(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all prewhere buyer_id in (` + wh + `) and zbtime>? and project_bidstatus>1`
  1296. args = append(args, NetworkCom.ProjectYearLimit())
  1297. if len(businessType) > 0 {
  1298. newWh, newArgs := WhArgs(businessType)
  1299. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1300. args = append(args, newArgs...)
  1301. }
  1302. q += ` group by buyer_id`
  1303. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1304. if err != nil {
  1305. logx.Error(err)
  1306. return
  1307. }
  1308. for rows.Next() {
  1309. var (
  1310. buyer_id string
  1311. project_count uint64
  1312. project_amount decimal.Decimal
  1313. project_id []string
  1314. )
  1315. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1316. logx.Error(err)
  1317. continue
  1318. }
  1319. pf, _ := project_amount.Float64()
  1320. if vm[buyer_id] == nil {
  1321. vm[buyer_id] = &projectInfo{
  1322. ProjectCount: int64(project_count),
  1323. ExportId: project_id,
  1324. ProjectAmount: pf,
  1325. }
  1326. } else {
  1327. vm[buyer_id].ProjectAmount += pf
  1328. vm[buyer_id].ProjectCount += int64(project_count)
  1329. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1330. }
  1331. }
  1332. rows.Close()
  1333. if err := rows.Err(); err != nil {
  1334. logx.Error(err)
  1335. }
  1336. return
  1337. }
  1338. func (n *network) TjProject(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1339. vm := map[string]*projectInfo{}
  1340. if len(agencyIds) == 0 && len(winnerIds) == 0 {
  1341. return vm
  1342. }
  1343. yearLimit := NetworkCom.ProjectYearLimit()
  1344. allArgs := []interface{}{}
  1345. q := `select buyer_id,uniqCombined(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info_all prewhere buyer_id in (select DISTINCT buyer_id from information.transaction_info_all prewhere `
  1346. if len(agencyIds) > 0 {
  1347. wh, args := WhArgs(agencyIds)
  1348. allArgs = append(allArgs, args...)
  1349. allArgs = append(allArgs, yearLimit)
  1350. q += `agency_id in (` + wh + `) and buyer_id<>'' and zbtime>?`
  1351. } else if len(winnerIds) > 0 {
  1352. wh, args := WhArgs(winnerIds)
  1353. allArgs = append(allArgs, args...)
  1354. allArgs = append(allArgs, yearLimit)
  1355. q += `hasAny(winner_id,[` + wh + `]) and buyer_id<>'' and zbtime>?`
  1356. }
  1357. q += `) and zbtime>? and project_bidstatus>1`
  1358. allArgs = append(allArgs, yearLimit)
  1359. if len(businessType) > 0 {
  1360. newWh, newArgs := WhArgs(businessType)
  1361. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1362. allArgs = append(allArgs, newArgs...)
  1363. }
  1364. q += ` group by buyer_id`
  1365. logx.Info("全部人脉项目sql", q, allArgs)
  1366. rows, err := ClickhouseConn.Query(context.Background(), q, allArgs...)
  1367. if err != nil {
  1368. logx.Error(err)
  1369. return vm
  1370. }
  1371. for rows.Next() {
  1372. var (
  1373. buyer_id string
  1374. project_count uint64
  1375. project_amount decimal.Decimal
  1376. project_id []string
  1377. )
  1378. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1379. logx.Error(err)
  1380. continue
  1381. }
  1382. pf, _ := project_amount.Float64()
  1383. if vm[buyer_id] == nil {
  1384. vm[buyer_id] = &projectInfo{
  1385. ProjectCount: int64(project_count),
  1386. ExportId: project_id,
  1387. ProjectAmount: pf,
  1388. }
  1389. } else {
  1390. vm[buyer_id].ProjectAmount += pf
  1391. vm[buyer_id].ProjectCount += int64(project_count)
  1392. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1393. }
  1394. }
  1395. rows.Close()
  1396. if err := rows.Err(); err != nil {
  1397. logx.Error(err)
  1398. }
  1399. return vm
  1400. }
  1401. func (n *network) MakeProjectInfo(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1402. pis := n.TjProject(agencyIds, winnerIds, vbs, businessType)
  1403. vm := map[string]*projectInfo{}
  1404. for k, v := range vbs {
  1405. pi := &projectInfo{
  1406. BuyerCount: int64(len(v)),
  1407. IdNames: v,
  1408. }
  1409. re := map[string]bool{}
  1410. for _, vv := range v {
  1411. if pis[vv.Id] != nil {
  1412. pi.ProjectCount += pis[vv.Id].ProjectCount
  1413. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1414. for _, vvv := range pis[vv.Id].ExportId {
  1415. if re[vvv] {
  1416. continue
  1417. }
  1418. pi.ExportId = append(pi.ExportId, vvv)
  1419. re[vvv] = true
  1420. }
  1421. }
  1422. }
  1423. vm[k] = pi
  1424. }
  1425. return vm
  1426. }
  1427. func (n *network) AllIntroduceOwner(positionId int64, sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, businessType []string) *introduceOwnerProject {
  1428. logx.Info(positionId, " AllIntroduceOwner start 。。。")
  1429. q := `select a.id,a.company_id,a.company_name,a.qyxy_id,a.itype,a.contact_person as person,a.contact_phone as phone,count(distinct if(b.itype=1,b.relate_id,null)) as buyer_count,count(distinct if(b.itype=2,b.relate_id,null)) as project_count,GROUP_CONCAT(IF(b.itype=1,b.relate_id,NULL)) AS relate_buyer_id,GROUP_CONCAT(IF(b.itype=1,b.relate_name,NULL)) AS relate_buyer_name,GROUP_CONCAT(IF(b.itype=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a
  1430. left join crm.connection_introduce b on (a.position_id=? and b.position_id=? and a.id=b.connection_id) where a.position_id=?` + sqlAppend1 + ` GROUP BY a.id order by a.create_time desc` + sqlAppend2
  1431. listTemp := CrmMysql.SelectBySql(q, args...)
  1432. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_buyer_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
  1433. myNetworks := []*myNetwork{}
  1434. for _, v := range *listTemp {
  1435. myNetwork := &myNetwork{
  1436. Id: Int64All(v["id"]),
  1437. Company_id: ObjToString(v["company_id"]),
  1438. Company_name: ObjToString(v["company_name"]),
  1439. Qyxy_id: ObjToString(v["qyxy_id"]),
  1440. Itype: Int64All(v["itype"]),
  1441. Person: ObjToString(v["person"]),
  1442. Phone: ObjToString(v["phone"]),
  1443. Buyer_count: Int64All(v["buyer_count"]),
  1444. Project_count: Int64All(v["project_count"]),
  1445. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1446. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1447. Relate_project_id: ObjToString(v["relate_project_id"]),
  1448. Create_time: ObjToString(v["create_time"]),
  1449. }
  1450. switch myNetwork.Itype {
  1451. case 1:
  1452. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1453. case 2:
  1454. supplier_array = append(supplier_array, myNetwork.Company_id)
  1455. case 3:
  1456. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1457. case 4:
  1458. if myNetwork.Relate_buyer_id != "" {
  1459. middleman_buyer_array = append(middleman_buyer_array, strings.Split(myNetwork.Relate_buyer_id, ",")...)
  1460. }
  1461. if myNetwork.Relate_project_id != "" {
  1462. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1463. }
  1464. case 5:
  1465. agency_array = append(agency_array, myNetwork.Company_id)
  1466. }
  1467. myNetworks = append(myNetworks, myNetwork)
  1468. }
  1469. iop := &introduceOwnerProject{
  1470. Networks: myNetworks,
  1471. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1472. Firstparty: map[string]*projectInfo{},
  1473. Supplier: map[string]*projectInfo{},
  1474. Adiffb: map[string]*projectInfo{},
  1475. Agency: map[string]*projectInfo{},
  1476. Middleman: map[string]*projectInfo{},
  1477. }
  1478. if isTjProject {
  1479. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1480. logx.Info(positionId, " FirstpartyNetwork over 。。。")
  1481. iop.Firstparty, iop.FirstpartyPio = n.Introduce_Firstparty(iop.FirstpartyNetwork, businessType)
  1482. logx.Info(positionId, " Introduce_Firstparty over 。。。")
  1483. iop.Supplier = n.Introduce_Supplier(supplier_array, businessType)
  1484. logx.Info(positionId, " Introduce_Supplier over 。。。")
  1485. iop.Adiffb = n.Introduce_Supplier(adiffb_array, businessType)
  1486. logx.Info(positionId, " Introduce_Supplier over 。。。")
  1487. iop.Agency = n.Introduce_Agency(agency_array, businessType)
  1488. logx.Info(positionId, " Introduce_Agency over 。。。")
  1489. iop.Middleman = n.Introduce_Middleman_Buyer(middleman_buyer_array, businessType)
  1490. logx.Info(positionId, " Introduce_Middleman_Buyer over 。。。")
  1491. }
  1492. return iop
  1493. }
  1494. func (n *network) DeleteCache(positionId int64) {
  1495. redis.Del("newother", fmt.Sprintf(NetworkManageAllProjectKey, positionId))
  1496. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageList, positionId, "*"))
  1497. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageCoopHistory, positionId, "*"))
  1498. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageOwnerlList, positionId, "*"))
  1499. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageProjectList, positionId, "*"))
  1500. }