network.go 45 KB

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