network.go 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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. key := fmt.Sprintf(NetworkManageAllProjectKey, in.PositionId)
  316. rbt, rerr := redis.GetNewBytes("newother", key)
  317. if rerr == nil && rbt != nil {
  318. json.Unmarshal(*rbt, &reply)
  319. return
  320. }
  321. businessType := strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  322. var count int64
  323. var list []*networkTree
  324. firstpartyChild := map[string][]*firstpartyNetwork{}
  325. if in.Id != "" {
  326. if in.Type == "firstparty" {
  327. fpn := n.FirstpartyNetwork(in.Name, []string{in.Id})
  328. if fpn[in.Id] != nil {
  329. firstparty := n.Introduce_Firstparty(fpn, businessType)
  330. nameIndex := map[string]int{}
  331. for _, v := range fpn[in.Id] {
  332. if _, ok := nameIndex[v.Name]; !ok {
  333. nameIndex[v.Name] = len(list)
  334. list = append(list, &networkTree{
  335. Name: v.Name,
  336. })
  337. }
  338. ntc := &networkTreeChild{
  339. CompanyName: v.CompanyName,
  340. CompanyId: v.CompanyId,
  341. Type: "firstparty",
  342. }
  343. if firstparty[v.CompanyId] != nil {
  344. ntc.Count = firstparty[v.CompanyId].ProjectCount
  345. }
  346. count++
  347. list[nameIndex[v.Name]].Count += ntc.Count
  348. list[nameIndex[v.Name]].Children = append(list[nameIndex[v.Name]].Children, ntc)
  349. }
  350. }
  351. }
  352. } else {
  353. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  354. sqlAppend1 := ""
  355. if in.Name != "" {
  356. sqlAppend1 += ` and a.company_name like ?`
  357. args = append(args, "%"+in.Name+"%")
  358. }
  359. aio := n.AllIntroduceOwner(sqlAppend1, "", args, true, businessType)
  360. firstpartyChild = aio.FirstpartyNetwork
  361. list = []*networkTree{
  362. &networkTree{
  363. Name: "甲方",
  364. Children: []*networkTreeChild{},
  365. },
  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. }
  383. //
  384. for _, v := range aio.Networks {
  385. if v.Itype <= 0 || v.Itype > int64(len(list)) {
  386. return
  387. }
  388. ntc := &networkTreeChild{
  389. Id: v.Id,
  390. CompanyName: v.Company_name,
  391. CompanyId: v.Company_id,
  392. Type: n.TypeIntConvert(v.Itype),
  393. CreateTime: v.Create_time,
  394. }
  395. switch v.Itype {
  396. case 1:
  397. if aio.Firstparty[v.Company_id] != nil {
  398. ntc.Count = aio.Firstparty[v.Company_id].ProjectCount
  399. }
  400. case 2:
  401. if aio.Supplier[v.Company_id] != nil {
  402. ntc.Count = aio.Supplier[v.Company_id].ProjectCount
  403. ntc.IdNames = aio.Supplier[v.Company_id].IdNames
  404. }
  405. case 3:
  406. if aio.Adiffb[v.Company_id] != nil {
  407. ntc.Count = aio.Adiffb[v.Company_id].ProjectCount
  408. ntc.IdNames = aio.Adiffb[v.Company_id].IdNames
  409. }
  410. case 4:
  411. if v.Relate_buyer_id != "" {
  412. for _, rbi := range strings.Split(v.Relate_buyer_id, ",") {
  413. ntc.IdNames = append(ntc.IdNames, &idName{Id: rbi})
  414. if aio.Middleman[rbi] != nil {
  415. ntc.Count++
  416. }
  417. }
  418. }
  419. case 5:
  420. if aio.Agency[v.Company_id] != nil {
  421. ntc.Count = aio.Agency[v.Company_id].ProjectCount
  422. ntc.IdNames = aio.Agency[v.Company_id].IdNames
  423. }
  424. }
  425. count += ntc.Count
  426. list[v.Itype-1].Count += ntc.Count
  427. list[v.Itype-1].Children = append(list[v.Itype-1].Children, ntc)
  428. }
  429. }
  430. checkCode := []string{}
  431. parentConvList := &nodeTrees{}
  432. convList := &nodeTrees{}
  433. hasDefaultCheck := false
  434. if in.ClickId != "" {
  435. hasDefaultCheck = true
  436. }
  437. for _, v := range list {
  438. if len(v.Children) == 0 {
  439. continue
  440. }
  441. parentStartCheckCodeLen := len(checkCode)
  442. pm := &nodeTree{
  443. CODE: v.Name,
  444. NAME: v.Name,
  445. SZ_PID0: v.Name,
  446. SZ_LEVEL: 0,
  447. DATACOUNT: 0,
  448. }
  449. if len(v.Children) > 0 {
  450. pm.SZ_LEAF = 0
  451. } else {
  452. pm.SZ_LEAF = 1
  453. }
  454. sort.Sort(v)
  455. id, pType, pMyType, pMyId := "", "", "", ""
  456. for _, vv := range v.Children {
  457. childStartCheckCodeLen := len(checkCode)
  458. myId := vv.CompanyId
  459. if vv.Type == "middleman" {
  460. myId = fmt.Sprint(vv.Id)
  461. }
  462. if pMyId != "" {
  463. pMyId += ","
  464. }
  465. pMyId += myId
  466. if pMyType != "" {
  467. pMyType += ","
  468. }
  469. pMyType += vv.Type
  470. myChildIds, myChildTypes := "", ""
  471. compareId := fmt.Sprint(vv.Id)
  472. if in.Id == "" && v.Name == "甲方" {
  473. cccNodes := []*nodeTree{}
  474. cccIndexMap := map[string]int{}
  475. for _, vvv := range firstpartyChild[vv.CompanyId] {
  476. if in.CheckCode != "" {
  477. compareId = vvv.CompanyId
  478. }
  479. cccIndex, cccOk := cccIndexMap[vvv.Name]
  480. if cccOk {
  481. cccNodes[cccIndex].ID += "," + vvv.CompanyId
  482. cccNodes[cccIndex].MYID += "," + vvv.CompanyId
  483. cccNodes[cccIndex].TYPE += ",firstparty"
  484. cccNodes[cccIndex].MYTYPE += ",firstparty"
  485. } else {
  486. nt := &nodeTree{
  487. NAME: vvv.Name,
  488. ID: vvv.CompanyId,
  489. SZ_PID0: v.Name,
  490. SZ_PID1: v.Name + ":" + myId,
  491. SZ_PID2: v.Name + ":" + myId + ":" + vvv.Name,
  492. CODE: v.Name + ":" + myId + ":" + vvv.Name,
  493. PCODE: v.Name + ":" + myId,
  494. SZ_LEVEL: 2,
  495. SZ_LEAF: 0,
  496. TYPE: "firstparty",
  497. MYID: vvv.CompanyId,
  498. MYTYPE: "firstparty",
  499. }
  500. cccNodes = append(cccNodes, nt)
  501. cccIndexMap[vvv.Name] = len(cccNodes) - 1
  502. if (!hasDefaultCheck && vv.Count > 0) || compareId == in.ClickId {
  503. hasDefaultCheck = true
  504. checkCode = append(checkCode, nt.CODE)
  505. }
  506. }
  507. if id != "" {
  508. id += ","
  509. }
  510. id += vvv.CompanyId
  511. if pType != "" {
  512. pType += ","
  513. }
  514. pType += vv.Type
  515. if myChildIds != "" {
  516. myChildIds += ","
  517. }
  518. myChildIds += vvv.CompanyId
  519. if myChildTypes != "" {
  520. myChildTypes += ","
  521. }
  522. myChildTypes += vv.Type
  523. nt := &nodeTree{
  524. NAME: vvv.CompanyName,
  525. ID: vvv.CompanyId,
  526. SZ_PID0: v.Name,
  527. SZ_PID1: v.Name + ":" + myId,
  528. SZ_PID2: cccNodes[cccIndex].SZ_PID2,
  529. SZ_PID3: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  530. CODE: v.Name + ":" + myId + ":" + vvv.Name + ":" + vvv.CompanyId,
  531. PCODE: cccNodes[cccIndex].CODE,
  532. SZ_LEVEL: 3,
  533. SZ_LEAF: 1,
  534. TYPE: "firstparty",
  535. MYID: vvv.CompanyId,
  536. MYTYPE: "firstparty",
  537. }
  538. if (!hasDefaultCheck && vv.Count > 0) || compareId == in.ClickId {
  539. hasDefaultCheck = true
  540. checkCode = append(checkCode, nt.CODE)
  541. }
  542. *convList = append(*convList, nt)
  543. }
  544. for _, cccNode := range cccNodes {
  545. *convList = append(*convList, cccNode)
  546. }
  547. } else {
  548. if id != "" {
  549. id += ","
  550. }
  551. id += myId
  552. if pType != "" {
  553. pType += ","
  554. }
  555. pType += vv.Type
  556. myChildIds = myId
  557. myChildTypes = vv.Type
  558. if in.CheckCode != "" {
  559. if len(vv.IdNames) == 0 {
  560. compareId = "-1"
  561. }
  562. for in_k, in_v := range vv.IdNames {
  563. if in_v.Id == in.ClickId {
  564. break
  565. } else if in_k == len(vv.IdNames)-1 {
  566. compareId = "-1"
  567. }
  568. }
  569. }
  570. }
  571. cm := &nodeTree{
  572. NAME: vv.CompanyName,
  573. ID: myChildIds,
  574. SZ_PID0: v.Name,
  575. SZ_PID1: v.Name + ":" + myId,
  576. CODE: v.Name + ":" + myId,
  577. PCODE: v.Name,
  578. DATACOUNT: vv.Count,
  579. SZ_LEVEL: 1,
  580. SZ_LEAF: If(myChildIds == "", 1, 0).(int),
  581. TYPE: myChildTypes,
  582. MYID: myId,
  583. MYTYPE: vv.Type,
  584. }
  585. //如果甲方的子节点被选中了,父节点跟着被选中,非甲方的要对比子节点可介绍业主
  586. if (!hasDefaultCheck && vv.Count > 0) || childStartCheckCodeLen != len(checkCode) || (in.ClickId != "" && compareId == fmt.Sprint(vv.Id)) {
  587. hasDefaultCheck = true
  588. checkCode = append(checkCode, cm.CODE)
  589. }
  590. *convList = append(*convList, cm)
  591. }
  592. pm.ID = id
  593. pm.TYPE = pType
  594. pm.MYTYPE = pMyType
  595. pm.MYID = pMyId
  596. *parentConvList = append(*parentConvList, pm)
  597. if parentStartCheckCodeLen != len(checkCode) {
  598. checkCode = append(checkCode, pm.CODE)
  599. }
  600. }
  601. sort.Sort(convList)
  602. *parentConvList = append(*parentConvList, *convList...)
  603. var newCheckCode []string
  604. if in.CheckCode != "" {
  605. m := map[string]bool{}
  606. for _, v := range strings.Split(in.CheckCode, ",") {
  607. m[v] = true
  608. }
  609. for _, v := range checkCode {
  610. if m[v] {
  611. newCheckCode = append(newCheckCode, v)
  612. }
  613. }
  614. } else {
  615. newCheckCode = checkCode
  616. }
  617. reply = &types.Reply{
  618. Data: map[string]interface{}{
  619. "count": count,
  620. "list": parentConvList,
  621. "checked": checkCode,
  622. },
  623. }
  624. redis.Put("newother", key, reply, C.CacheTimeOut)
  625. return reply
  626. }
  627. // 人脉库-列表
  628. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  629. if in.Page_size <= 0 {
  630. in.Page_size = 10
  631. }
  632. if in.Current_page <= 0 {
  633. in.Current_page = 1
  634. }
  635. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Order_amount == 0
  636. sqlAppendArgs := []interface{}{}
  637. sqlAppend1 := ""
  638. if dbPaging && in.Type != "" {
  639. sqlAppend1 += ` and a.itype=?`
  640. sqlAppendArgs = append(sqlAppendArgs, n.TypeStrConvert(in.Type))
  641. }
  642. comSqlAppend := ""
  643. comSqlArgs := []interface{}{}
  644. if in.Starttime != "" {
  645. comSqlAppend += ` and a.create_time>=?`
  646. comSqlArgs = append(comSqlArgs, in.Starttime+" 00:00:00")
  647. }
  648. if in.Endtime != "" {
  649. comSqlAppend += ` and a.create_time<=?`
  650. if in.Starttime == in.Endtime {
  651. in.Endtime += " 23:59:59"
  652. } else {
  653. in.Endtime += " 00:00:00"
  654. }
  655. comSqlArgs = append(comSqlArgs, in.Endtime)
  656. }
  657. if in.Name != "" {
  658. comSqlAppend += ` and a.company_name like ?`
  659. comSqlArgs = append(comSqlArgs, "%"+in.Name+"%")
  660. }
  661. sqlAppend1 += comSqlAppend
  662. sqlAppendArgs = append(sqlAppendArgs, comSqlArgs...)
  663. var count int64
  664. start := (in.Current_page - 1) * in.Page_size
  665. args := []interface{}{in.PositionId, in.PositionId, in.PositionId}
  666. args = append(args, sqlAppendArgs...)
  667. sqlAppend2 := ""
  668. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  669. if dbPaging {
  670. newArgs := []interface{}{in.PositionId}
  671. newArgs = append(newArgs, sqlAppendArgs...)
  672. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend1, newArgs...)
  673. sqlAppend2 = ` limit ?,?`
  674. args = append(args, start, in.Page_size)
  675. itemArgs := []interface{}{in.PositionId}
  676. itemArgs = append(itemArgs, comSqlArgs...)
  677. items := CrmMysql.SelectBySql(`SELECT itype,SUM(1) AS sum FROM crm.connection a WHERE a.position_id=?`+comSqlAppend+` GROUP BY a.itype`, itemArgs...)
  678. if items != nil {
  679. for _, v := range *items {
  680. switch Int64All(v["itype"]) {
  681. case 1:
  682. firstparty_count = IntAll(v["sum"])
  683. case 2:
  684. supplier_count = IntAll(v["sum"])
  685. case 3:
  686. adiffb_count = IntAll(v["sum"])
  687. case 4:
  688. middleman_count = IntAll(v["sum"])
  689. case 5:
  690. agency_count = IntAll(v["sum"])
  691. }
  692. }
  693. }
  694. }
  695. //
  696. list := []*map[string]interface{}{}
  697. isTjProject := true
  698. businessType := []string{}
  699. businessType = strings.Split(FindBusiness(in.EntId, in.MgoUserId), ",")
  700. if len(businessType) == 0 {
  701. isTjProject = false
  702. }
  703. redisKey := fmt.Sprintf(NetworkManageList, in.PositionId, GetMd5String(fmt.Sprintf("%+v", in)))
  704. aio := &introduceOwnerProject{}
  705. aioByte, aioErr := redis.GetNewBytes("newother", redisKey)
  706. if aioErr == nil && aioByte != nil {
  707. json.Unmarshal(*aioByte, &aio)
  708. } else {
  709. aio = n.AllIntroduceOwner(sqlAppend1, sqlAppend2, args, isTjProject, businessType)
  710. redis.Put("newother", redisKey, aio, C.CacheTimeOut)
  711. }
  712. entMonitor := NetworkCom.EntMonitor(in.UserId)
  713. for _, v := range aio.Networks {
  714. itype := ""
  715. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  716. export_id := []string{}
  717. jump_type, jump_id := "", ""
  718. switch v.Itype {
  719. case 1:
  720. itype = "甲方"
  721. jump_type = "firstparty"
  722. for _, vv := range aio.FirstpartyNetwork[v.Company_id] {
  723. if jump_id != "" {
  724. jump_id += ","
  725. }
  726. jump_id += vv.CompanyId
  727. if entMonitor[vv.CompanyName] {
  728. monitor_count++
  729. }
  730. }
  731. if aio.Firstparty[v.Company_id] != nil {
  732. buyer_count = aio.Firstparty[v.Company_id].BuyerCount
  733. project_count = aio.Firstparty[v.Company_id].ProjectCount
  734. expect_amount = aio.Firstparty[v.Company_id].ProjectAmount
  735. export_id = aio.Firstparty[v.Company_id].ExportId
  736. }
  737. case 2:
  738. itype = "供应商/渠道"
  739. jump_type = "supplier"
  740. jump_id = v.Company_id
  741. if aio.Supplier[v.Company_id] != nil {
  742. buyer_count = aio.Supplier[v.Company_id].BuyerCount
  743. project_count = aio.Supplier[v.Company_id].ProjectCount
  744. expect_amount = aio.Supplier[v.Company_id].ProjectAmount
  745. export_id = aio.Supplier[v.Company_id].ExportId
  746. for _, vv := range aio.Supplier[v.Company_id].IdNames {
  747. if entMonitor[vv.Name] {
  748. monitor_count++
  749. }
  750. }
  751. }
  752. case 3:
  753. itype = "同甲异业渠道"
  754. jump_type = "adiffb"
  755. jump_id = v.Company_id
  756. if aio.Adiffb[v.Company_id] != nil {
  757. buyer_count = aio.Adiffb[v.Company_id].BuyerCount
  758. project_count = aio.Adiffb[v.Company_id].ProjectCount
  759. expect_amount = aio.Adiffb[v.Company_id].ProjectAmount
  760. export_id = aio.Adiffb[v.Company_id].ExportId
  761. for _, vv := range aio.Adiffb[v.Company_id].IdNames {
  762. if entMonitor[vv.Name] {
  763. monitor_count++
  764. }
  765. }
  766. }
  767. case 4:
  768. itype = "中间人"
  769. jump_type = "middleman"
  770. jump_id = fmt.Sprint(v.Id)
  771. buyer_count = v.Buyer_count
  772. if v.Relate_buyer_name != "" {
  773. for _, v := range strings.Split(v.Relate_buyer_name, ",") {
  774. if v == "" {
  775. continue
  776. }
  777. if entMonitor[v] {
  778. monitor_count++
  779. }
  780. }
  781. }
  782. if v.Relate_buyer_id != "" {
  783. for _, v := range strings.Split(v.Relate_buyer_id, ",") {
  784. if aio.Middleman[v] != nil {
  785. project_count++
  786. expect_amount += aio.Middleman[v].ProjectAmount
  787. export_id = append(export_id, aio.Middleman[v].ExportId...)
  788. }
  789. }
  790. }
  791. case 5:
  792. itype = "招标代理机构"
  793. jump_type = "agency"
  794. jump_id = v.Company_id
  795. if aio.Agency[v.Company_id] != nil {
  796. buyer_count = aio.Agency[v.Company_id].BuyerCount
  797. project_count = aio.Agency[v.Company_id].ProjectCount
  798. expect_amount = aio.Agency[v.Company_id].ProjectAmount
  799. export_id = aio.Agency[v.Company_id].ExportId
  800. for _, vv := range aio.Agency[v.Company_id].IdNames {
  801. if entMonitor[vv.Name] {
  802. monitor_count++
  803. }
  804. }
  805. }
  806. }
  807. if buyer_count < in.Buyercount_start {
  808. continue
  809. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  810. continue
  811. } else if in.Monitor == 1 && monitor_count <= 0 {
  812. continue
  813. } else if in.Monitor == -1 && monitor_count > 0 {
  814. continue
  815. } else if monitor_count < in.Monitorcount_start {
  816. continue
  817. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  818. continue
  819. }
  820. if !dbPaging {
  821. switch v.Itype {
  822. case 1:
  823. firstparty_count++
  824. case 2:
  825. supplier_count++
  826. case 3:
  827. adiffb_count++
  828. case 4:
  829. middleman_count++
  830. case 5:
  831. agency_count++
  832. }
  833. if in.Type != "" && n.TypeStrConvert(in.Type) != v.Itype {
  834. continue
  835. }
  836. }
  837. export_url := ""
  838. if len(export_id) > 0 {
  839. exportIdRepeat := map[string]bool{}
  840. exportId := ""
  841. for _, v := range export_id {
  842. if exportIdRepeat[v] {
  843. continue
  844. }
  845. exportIdRepeat[v] = true
  846. if exportId != "" {
  847. exportId += ","
  848. }
  849. exportId += v
  850. }
  851. md5Id := GetMd5String(exportId)
  852. redis.Put("newother", fmt.Sprintf("network_export_%s", md5Id), exportId, 259200)
  853. export_url = "/subscribepay/network/projectExport?version=1&export_id=" + md5Id
  854. }
  855. url := ""
  856. if v.Qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  857. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(v.Qyxy_id)
  858. } else if jump_type == "firstparty" && v.Company_name != "" {
  859. url = "/swordfish/page_big_pc/unit_portrayal/" + v.Company_name
  860. }
  861. list = append(list, &map[string]interface{}{
  862. "company_id": v.Company_id,
  863. "company_name": v.Company_name,
  864. "type": itype,
  865. "jump_type": jump_type,
  866. "jump_id": jump_id,
  867. "person": v.Person,
  868. "phone": v.Phone,
  869. "buyer_count": buyer_count,
  870. "monitor_count": monitor_count,
  871. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  872. "project_count": project_count,
  873. "create_time": v.Create_time,
  874. "export_url": export_url,
  875. "url": url,
  876. "id": v.Id,
  877. })
  878. }
  879. csList := &ComSortList{
  880. SortKeys: []*ComSortKey{
  881. &ComSortKey{
  882. Keys: []string{"expect_amount"},
  883. Order: 1,
  884. Type: "float",
  885. },
  886. },
  887. List: list,
  888. }
  889. if in.Order_amount == -1 {
  890. csList.SortKeys[0].Order = -1
  891. }
  892. if in.Order_amount != 0 {
  893. sort.Sort(csList)
  894. }
  895. finalList := []*map[string]interface{}{}
  896. if !dbPaging {
  897. count = int64(len(csList.List))
  898. if count > 0 && start < count {
  899. end := start + in.Page_size
  900. if end > count {
  901. end = count
  902. }
  903. finalList = csList.List[start:end]
  904. }
  905. } else {
  906. finalList = csList.List
  907. }
  908. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  909. return &types.Reply{
  910. Data: map[string]interface{}{
  911. "count": count,
  912. "total_page": total_page,
  913. "firstparty_count": firstparty_count,
  914. "supplier_count": supplier_count,
  915. "adiffb_count": adiffb_count,
  916. "middleman_count": middleman_count,
  917. "agency_count": agency_count,
  918. "list": finalList,
  919. },
  920. }
  921. }
  922. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  923. result := map[string][]*firstpartyNetwork{}
  924. if len(values) == 0 {
  925. return result
  926. }
  927. wh, args := WhArgs(values)
  928. 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
  929. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  930. if name != "" {
  931. q += ` where c.company_name like ?`
  932. args = append(args, "%"+name+"%")
  933. }
  934. q += ` order by b.name,a.a_id,a.b_name`
  935. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  936. if err != nil {
  937. logx.Error(err)
  938. return result
  939. }
  940. for rows.Next() {
  941. var (
  942. a_id string
  943. company_id string
  944. company_name string
  945. name string
  946. )
  947. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  948. logx.Error(err)
  949. continue
  950. }
  951. if company_id == "" || company_name == "" {
  952. continue
  953. }
  954. result[a_id] = append(result[a_id], &firstpartyNetwork{
  955. CompanyId: company_id,
  956. CompanyName: company_name,
  957. Name: name,
  958. })
  959. }
  960. return result
  961. }
  962. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, businessType []string) map[string]*projectInfo {
  963. values := []string{}
  964. vm := map[string]*projectInfo{}
  965. for _, v := range fpn {
  966. for _, vv := range v {
  967. vm[vv.CompanyId] = &projectInfo{}
  968. values = append(values, vv.CompanyId)
  969. }
  970. }
  971. result := map[string]*projectInfo{}
  972. if len(values) == 0 {
  973. return result
  974. }
  975. wh, args := WhArgs(values)
  976. 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`
  977. args = append(args, NetworkCom.ProjectYearLimit())
  978. if len(businessType) > 0 {
  979. newWh, newArgs := WhArgs(businessType)
  980. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  981. args = append(args, newArgs...)
  982. }
  983. q += ` group by buyer_id`
  984. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  985. if err != nil {
  986. logx.Error(err)
  987. return nil
  988. }
  989. for rows.Next() {
  990. var (
  991. buyer_id string
  992. project_count uint64
  993. project_amount decimal.Decimal
  994. project_id []string
  995. )
  996. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  997. logx.Error(err)
  998. continue
  999. }
  1000. if vm[buyer_id] == nil {
  1001. continue
  1002. }
  1003. vm[buyer_id].ProjectCount += int64(project_count)
  1004. pf, _ := project_amount.Float64()
  1005. vm[buyer_id].ProjectAmount += pf
  1006. vm[buyer_id].ExportId = project_id
  1007. }
  1008. rows.Close()
  1009. if err := rows.Err(); err != nil {
  1010. logx.Error(err)
  1011. }
  1012. for k, v := range fpn {
  1013. if result[k] == nil {
  1014. result[k] = &projectInfo{}
  1015. }
  1016. result[k].BuyerCount = int64(len(v))
  1017. for _, vv := range v {
  1018. if vm[vv.CompanyId] == nil {
  1019. continue
  1020. }
  1021. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  1022. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  1023. result[k].ExportId = append(result[k].ExportId, vm[vv.CompanyId].ExportId...)
  1024. }
  1025. }
  1026. return result
  1027. }
  1028. func (n *network) Introduce_Supplier(values []string, businessType []string) map[string]*projectInfo {
  1029. if len(values) == 0 {
  1030. return map[string]*projectInfo{}
  1031. }
  1032. wh, args := WhArgs(values)
  1033. repeat := map[string]bool{}
  1034. whRepeat := map[string]map[string]bool{}
  1035. buyers := []string{}
  1036. q := `select winner_id,buyer_id,buyer from information.transaction_info_all where hasAny(winner_id,[` + wh + `]) and buyer_id<>''`
  1037. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1038. if err != nil {
  1039. logx.Error(err)
  1040. return map[string]*projectInfo{}
  1041. }
  1042. vbs := map[string][]*idName{}
  1043. for rows.Next() {
  1044. var (
  1045. winner_id []string
  1046. buyer_id string
  1047. buyer string
  1048. )
  1049. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  1050. logx.Error(err)
  1051. continue
  1052. }
  1053. if buyer_id == "" || buyer == "" {
  1054. continue
  1055. }
  1056. if !repeat[buyer_id] {
  1057. repeat[buyer_id] = true
  1058. buyers = append(buyers, buyer_id)
  1059. }
  1060. for _, v := range winner_id {
  1061. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  1062. continue
  1063. }
  1064. if whRepeat[v] == nil {
  1065. whRepeat[v] = map[string]bool{}
  1066. }
  1067. whRepeat[v][buyer_id] = true
  1068. vbs[v] = append(vbs[v], &idName{
  1069. Id: buyer_id,
  1070. Name: buyer,
  1071. })
  1072. }
  1073. }
  1074. rows.Close()
  1075. if err := rows.Err(); err != nil {
  1076. logx.Error(err)
  1077. }
  1078. return n.MakeProjectInfo(nil, values, vbs, businessType)
  1079. }
  1080. func (n *network) Introduce_Agency(values []string, businessType []string) map[string]*projectInfo {
  1081. if len(values) == 0 {
  1082. return map[string]*projectInfo{}
  1083. }
  1084. wh, args := WhArgs(values)
  1085. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info_all where agency_id in (` + wh + `) and buyer_id<>''`
  1086. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1087. if err != nil {
  1088. logx.Error(err)
  1089. return map[string]*projectInfo{}
  1090. }
  1091. vbs := map[string][]*idName{}
  1092. buyers := []string{}
  1093. repeat := map[string]bool{}
  1094. for rows.Next() {
  1095. var (
  1096. agency_id string
  1097. buyer_id string
  1098. buyer string
  1099. )
  1100. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  1101. logx.Error(err)
  1102. continue
  1103. }
  1104. if buyer_id == "" || buyer == "" {
  1105. continue
  1106. }
  1107. vbs[agency_id] = append(vbs[agency_id], &idName{
  1108. Id: buyer_id,
  1109. Name: buyer,
  1110. })
  1111. if !repeat[buyer_id] {
  1112. repeat[buyer_id] = true
  1113. buyers = append(buyers, buyer_id)
  1114. }
  1115. }
  1116. rows.Close()
  1117. if err := rows.Err(); err != nil {
  1118. logx.Error(err)
  1119. }
  1120. return n.MakeProjectInfo(values, nil, vbs, businessType)
  1121. }
  1122. func (n *network) Introduce_Middleman_Buyer(values []string, businessType []string) map[string]*projectInfo {
  1123. return n.BuyerProjectInfo(values, businessType)
  1124. }
  1125. func (n *network) Introduce_Middleman_Project(values []string, businessType []string) map[string]*projectInfo {
  1126. result := map[string]*projectInfo{}
  1127. if len(values) == 0 {
  1128. return result
  1129. }
  1130. wh, args := WhArgs(values)
  1131. q := `select DISTINCT project_id,project_money from information.transaction_info_all where project_id in (` + wh + `)`
  1132. if len(businessType) > 0 {
  1133. newWh, newArgs := WhArgs(businessType)
  1134. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1135. args = append(args, newArgs...)
  1136. }
  1137. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1138. if err != nil {
  1139. logx.Error(err)
  1140. return nil
  1141. }
  1142. for rows.Next() {
  1143. var (
  1144. project_id string
  1145. project_money decimal.Decimal
  1146. )
  1147. if err := rows.Scan(&project_id, &project_money); err != nil {
  1148. logx.Error(err)
  1149. continue
  1150. }
  1151. pf, _ := project_money.Float64()
  1152. result[project_id] = &projectInfo{
  1153. ProjectAmount: pf,
  1154. }
  1155. }
  1156. rows.Close()
  1157. if err := rows.Err(); err != nil {
  1158. logx.Error(err)
  1159. }
  1160. return result
  1161. }
  1162. func (n *network) TypeStrConvert(itype string) int64 {
  1163. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1164. switch itype {
  1165. case "firstparty":
  1166. return 1
  1167. case "supplier":
  1168. return 2
  1169. case "adiffb":
  1170. return 3
  1171. case "middleman":
  1172. return 4
  1173. case "agency":
  1174. return 5
  1175. }
  1176. return 0
  1177. }
  1178. func (n *network) TypeIntConvert(itype int64) string {
  1179. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  1180. switch itype {
  1181. case 1:
  1182. return "firstparty"
  1183. case 2:
  1184. return "supplier"
  1185. case 3:
  1186. return "adiffb"
  1187. case 4:
  1188. return "middleman"
  1189. case 5:
  1190. return "agency"
  1191. }
  1192. return ""
  1193. }
  1194. func (n *network) GetQyxyId(ids []string) map[string]string {
  1195. m := map[string]string{}
  1196. if len(ids) == 0 {
  1197. return m
  1198. }
  1199. wh, args := WhArgs(ids)
  1200. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  1201. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1202. if err != nil {
  1203. logx.Error(err)
  1204. } else {
  1205. for rows.Next() {
  1206. var (
  1207. id string
  1208. company_id string
  1209. )
  1210. if err := rows.Scan(&id, &company_id); err != nil {
  1211. logx.Error(err)
  1212. continue
  1213. }
  1214. m[id] = company_id
  1215. }
  1216. rows.Close()
  1217. if err := rows.Err(); err != nil {
  1218. logx.Error(err)
  1219. }
  1220. }
  1221. return m
  1222. }
  1223. func (n *network) BuyerProjectInfo(ids []string, businessType []string) map[string]*projectInfo {
  1224. vm := map[string]*projectInfo{}
  1225. m := map[string]bool{}
  1226. newIds := []string{}
  1227. for _, v := range ids {
  1228. if m[v] {
  1229. continue
  1230. }
  1231. m[v] = true
  1232. newIds = append(newIds, v)
  1233. if len(newIds) == C.BuyerBatch {
  1234. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1235. newIds = []string{}
  1236. }
  1237. }
  1238. if len(newIds) > 0 {
  1239. n.BuyerProjectInfoBatch(newIds, businessType, vm)
  1240. }
  1241. return vm
  1242. }
  1243. func (n *network) BuyerProjectInfoBatch(ids []string, businessType []string, vm map[string]*projectInfo) {
  1244. if len(ids) == 0 {
  1245. return
  1246. }
  1247. wh, args := WhArgs(ids)
  1248. 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`
  1249. args = append(args, NetworkCom.ProjectYearLimit())
  1250. if len(businessType) > 0 {
  1251. newWh, newArgs := WhArgs(businessType)
  1252. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1253. args = append(args, newArgs...)
  1254. }
  1255. q += ` group by buyer_id`
  1256. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1257. if err != nil {
  1258. logx.Error(err)
  1259. return
  1260. }
  1261. for rows.Next() {
  1262. var (
  1263. buyer_id string
  1264. project_count uint64
  1265. project_amount decimal.Decimal
  1266. project_id []string
  1267. )
  1268. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1269. logx.Error(err)
  1270. continue
  1271. }
  1272. pf, _ := project_amount.Float64()
  1273. if vm[buyer_id] == nil {
  1274. vm[buyer_id] = &projectInfo{
  1275. ProjectCount: int64(project_count),
  1276. ExportId: project_id,
  1277. ProjectAmount: pf,
  1278. }
  1279. } else {
  1280. vm[buyer_id].ProjectAmount += pf
  1281. vm[buyer_id].ProjectCount += int64(project_count)
  1282. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1283. }
  1284. }
  1285. rows.Close()
  1286. if err := rows.Err(); err != nil {
  1287. logx.Error(err)
  1288. }
  1289. return
  1290. }
  1291. func (n *network) TjProject(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1292. vm := map[string]*projectInfo{}
  1293. if len(agencyIds) == 0 && len(winnerIds) == 0 {
  1294. return vm
  1295. }
  1296. yearLimit := NetworkCom.ProjectYearLimit()
  1297. allArgs := []interface{}{}
  1298. 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 `
  1299. if len(agencyIds) > 0 {
  1300. wh, args := WhArgs(agencyIds)
  1301. allArgs = append(allArgs, args...)
  1302. allArgs = append(allArgs, yearLimit)
  1303. q += `agency_id in (` + wh + `) and buyer_id<>'' and zbtime>?`
  1304. } else if len(winnerIds) > 0 {
  1305. wh, args := WhArgs(winnerIds)
  1306. allArgs = append(allArgs, args...)
  1307. allArgs = append(allArgs, yearLimit)
  1308. q += `hasAny(winner_id,[` + wh + `]) and buyer_id<>'' and zbtime>?`
  1309. }
  1310. q += `) and zbtime>? and project_bidstatus>1`
  1311. allArgs = append(allArgs, yearLimit)
  1312. if len(businessType) > 0 {
  1313. newWh, newArgs := WhArgs(businessType)
  1314. q += ` and hasAny(topscopeclass,[` + newWh + `])`
  1315. allArgs = append(allArgs, newArgs...)
  1316. }
  1317. q += ` group by buyer_id`
  1318. logx.Info("全部人脉项目sql", q, allArgs)
  1319. rows, err := ClickhouseConn.Query(context.Background(), q, allArgs...)
  1320. if err != nil {
  1321. logx.Error(err)
  1322. return vm
  1323. }
  1324. for rows.Next() {
  1325. var (
  1326. buyer_id string
  1327. project_count uint64
  1328. project_amount decimal.Decimal
  1329. project_id []string
  1330. )
  1331. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1332. logx.Error(err)
  1333. continue
  1334. }
  1335. pf, _ := project_amount.Float64()
  1336. if vm[buyer_id] == nil {
  1337. vm[buyer_id] = &projectInfo{
  1338. ProjectCount: int64(project_count),
  1339. ExportId: project_id,
  1340. ProjectAmount: pf,
  1341. }
  1342. } else {
  1343. vm[buyer_id].ProjectAmount += pf
  1344. vm[buyer_id].ProjectCount += int64(project_count)
  1345. vm[buyer_id].ExportId = append(vm[buyer_id].ExportId, project_id...)
  1346. }
  1347. }
  1348. rows.Close()
  1349. if err := rows.Err(); err != nil {
  1350. logx.Error(err)
  1351. }
  1352. return vm
  1353. }
  1354. func (n *network) MakeProjectInfo(agencyIds, winnerIds []string, vbs map[string][]*idName, businessType []string) map[string]*projectInfo {
  1355. pis := n.TjProject(agencyIds, winnerIds, vbs, businessType)
  1356. vm := map[string]*projectInfo{}
  1357. for k, v := range vbs {
  1358. pi := &projectInfo{
  1359. BuyerCount: int64(len(v)),
  1360. IdNames: v,
  1361. }
  1362. re := map[string]bool{}
  1363. for _, vv := range v {
  1364. if pis[vv.Id] != nil {
  1365. pi.ProjectCount += pis[vv.Id].ProjectCount
  1366. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1367. for _, vvv := range pis[vv.Id].ExportId {
  1368. if re[vvv] {
  1369. continue
  1370. }
  1371. pi.ExportId = append(pi.ExportId, vvv)
  1372. re[vvv] = true
  1373. }
  1374. }
  1375. }
  1376. vm[k] = pi
  1377. }
  1378. return vm
  1379. }
  1380. func (n *network) AllIntroduceOwner(sqlAppend1, sqlAppend2 string, args []interface{}, isTjProject bool, businessType []string) *introduceOwnerProject {
  1381. 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
  1382. 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
  1383. listTemp := CrmMysql.SelectBySql(q, args...)
  1384. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_buyer_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}, []string{}
  1385. myNetworks := []*myNetwork{}
  1386. for _, v := range *listTemp {
  1387. myNetwork := &myNetwork{
  1388. Id: Int64All(v["id"]),
  1389. Company_id: ObjToString(v["company_id"]),
  1390. Company_name: ObjToString(v["company_name"]),
  1391. Qyxy_id: ObjToString(v["qyxy_id"]),
  1392. Itype: Int64All(v["itype"]),
  1393. Person: ObjToString(v["person"]),
  1394. Phone: ObjToString(v["phone"]),
  1395. Buyer_count: Int64All(v["buyer_count"]),
  1396. Project_count: Int64All(v["project_count"]),
  1397. Relate_buyer_id: ObjToString(v["relate_buyer_id"]),
  1398. Relate_buyer_name: ObjToString(v["relate_buyer_name"]),
  1399. Relate_project_id: ObjToString(v["relate_project_id"]),
  1400. Create_time: ObjToString(v["create_time"]),
  1401. }
  1402. switch myNetwork.Itype {
  1403. case 1:
  1404. firstparty_array = append(firstparty_array, myNetwork.Company_id)
  1405. case 2:
  1406. supplier_array = append(supplier_array, myNetwork.Company_id)
  1407. case 3:
  1408. adiffb_array = append(adiffb_array, myNetwork.Company_id)
  1409. case 4:
  1410. if myNetwork.Relate_buyer_id != "" {
  1411. middleman_buyer_array = append(middleman_buyer_array, strings.Split(myNetwork.Relate_buyer_id, ",")...)
  1412. }
  1413. if myNetwork.Relate_project_id != "" {
  1414. middleman_project_array = append(middleman_project_array, strings.Split(myNetwork.Relate_project_id, ",")...)
  1415. }
  1416. case 5:
  1417. agency_array = append(agency_array, myNetwork.Company_id)
  1418. }
  1419. myNetworks = append(myNetworks, myNetwork)
  1420. }
  1421. iop := &introduceOwnerProject{
  1422. Networks: myNetworks,
  1423. FirstpartyNetwork: map[string][]*firstpartyNetwork{},
  1424. Firstparty: map[string]*projectInfo{},
  1425. Supplier: map[string]*projectInfo{},
  1426. Adiffb: map[string]*projectInfo{},
  1427. Agency: map[string]*projectInfo{},
  1428. Middleman: map[string]*projectInfo{},
  1429. }
  1430. if isTjProject {
  1431. iop.FirstpartyNetwork = n.FirstpartyNetwork("", firstparty_array)
  1432. iop.Firstparty = n.Introduce_Firstparty(iop.FirstpartyNetwork, businessType)
  1433. iop.Supplier = n.Introduce_Supplier(supplier_array, businessType)
  1434. iop.Adiffb = n.Introduce_Supplier(adiffb_array, businessType)
  1435. iop.Agency = n.Introduce_Agency(agency_array, businessType)
  1436. iop.Middleman = n.Introduce_Middleman_Buyer(middleman_buyer_array, businessType)
  1437. }
  1438. return iop
  1439. }
  1440. func (n *network) DeleteCache(positionId int64) {
  1441. redis.Del("newother", fmt.Sprintf(NetworkManageAllProjectKey, positionId))
  1442. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageList, positionId, "*"))
  1443. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageCoopHistory, positionId, "*"))
  1444. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageOwnerlList, positionId, "*"))
  1445. redis.DelByCodePattern("newother", fmt.Sprintf(NetworkManageProjectList, positionId, "*"))
  1446. }