network.go 45 KB

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