network.go 39 KB

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