network.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "math"
  7. "sort"
  8. "strings"
  9. "sync"
  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/sort"
  16. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  17. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  18. "github.com/shopspring/decimal"
  19. "github.com/zeromicro/go-zero/core/logx"
  20. )
  21. var Network = &network{}
  22. type network struct {
  23. }
  24. type networkTree struct {
  25. Count int64 `json:"count"`
  26. Name string `json:"name"`
  27. Children []*networkTreeChild `json:"children"`
  28. }
  29. type networkTreeChild struct {
  30. Count int64 `json:"count"`
  31. Id int64 `json:"id"`
  32. CompanyId string `json:"companyId"`
  33. CompanyName string `json:"companyName"`
  34. Type string `json:"type"`
  35. CreateTime string `json:"createTime"`
  36. }
  37. func (s *networkTree) Len() int {
  38. return len(s.Children)
  39. }
  40. func (s *networkTree) Less(i, j int) bool {
  41. if s.Name == "甲方" {
  42. return s.Children[i].CompanyName < s.Children[j].CompanyName
  43. }
  44. it, err1 := time.ParseInLocation(Date_Full_Layout, s.Children[i].CreateTime, time.Local)
  45. if err1 != nil {
  46. return true
  47. }
  48. jt, err2 := time.ParseInLocation(Date_Full_Layout, s.Children[j].CreateTime, time.Local)
  49. if err2 != nil {
  50. return true
  51. }
  52. return it.Unix() < jt.Unix()
  53. }
  54. func (s *networkTree) Swap(i, j int) {
  55. s.Children[i], s.Children[j] = s.Children[j], s.Children[i]
  56. }
  57. type projectInfo struct {
  58. BuyerCount int64
  59. ProjectCount int64
  60. ProjectAmount float64
  61. MonitorCount int64
  62. ExportId []string
  63. }
  64. type firstpartyNetwork struct {
  65. CompanyId string
  66. CompanyName string
  67. Name string
  68. }
  69. type idName struct {
  70. Id string
  71. Name string
  72. }
  73. //人脉库-添加/修改人脉
  74. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  75. reply := &types.Reply{Data: map[string]interface{}{
  76. "status": 0,
  77. }}
  78. if in.Type != "middleman" && in.Company_id == "" {
  79. return reply
  80. }
  81. if in.Company_name == "" {
  82. in.Company_name = "未填写"
  83. }
  84. nowFormat := NowFormat(Date_Full_Layout)
  85. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  86. if in.Type != "middleman" {
  87. return true
  88. } else if in.Introduce_owner_id == "" || in.Introduce_project_id == "" {
  89. return false
  90. }
  91. values := []interface{}{}
  92. ioi := strings.Split(in.Introduce_owner_id, ",")
  93. ioqi := strings.Split(in.Introduce_owner_qyxy_id, ",")
  94. ion := strings.Split(in.Introduce_owner_name, ",")
  95. if len(ioi) != len(ion) || len(ioi) != len(ioqi) {
  96. return false
  97. }
  98. for k, v := range ioi {
  99. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, ioqi[k], v, ion[k], 1, nowFormat)
  100. }
  101. ipi := strings.Split(in.Introduce_project_id, ",")
  102. ipn := strings.Split(in.Introduce_project_name, ",")
  103. if len(ipi) != len(ipn) {
  104. return false
  105. }
  106. for k, v := range ipi {
  107. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, nil, v, ipn[k], 2, nowFormat)
  108. }
  109. var r2 int64
  110. if isUpdate {
  111. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  112. }
  113. 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)
  114. return r2 >= 0 && r3 > 0
  115. }
  116. itype := n.TypeStrConvert(in.Type)
  117. if in.Id > 0 {
  118. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  119. if in.Company_id != "" {
  120. 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)
  121. if count == -1 {
  122. return false
  123. } else if count > 0 {
  124. reply.Data = map[string]interface{}{
  125. "status": -1,
  126. }
  127. return false
  128. }
  129. }
  130. 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)
  131. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  132. }) {
  133. reply.Data = map[string]interface{}{
  134. "status": 1,
  135. }
  136. }
  137. } else {
  138. if in.Company_id != "" {
  139. 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)
  140. if count == -1 {
  141. return reply
  142. } else if count > 0 {
  143. reply.Data = map[string]interface{}{
  144. "status": -1,
  145. }
  146. return reply
  147. }
  148. }
  149. var r1 int64
  150. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  151. _, 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})
  152. return r1 > 0 && saveIntroduce(tx, r1, false)
  153. }) {
  154. reply.Data = map[string]interface{}{
  155. "status": 1,
  156. "id": r1,
  157. }
  158. }
  159. }
  160. return reply
  161. }
  162. //人脉库-业主名称联想
  163. func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
  164. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  165. res := []map[string]string{}
  166. reply = &types.Reply{Data: res}
  167. in.Name = strings.TrimSpace(in.Name)
  168. if in.Name == "" {
  169. return
  170. }
  171. pageSize := 10
  172. if in.Type == "adiffb" {
  173. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  174. if len(probusfors) > 0 {
  175. args := []interface{}{in.EntName}
  176. wh, newArgs := NetworkCom.WhArgs(probusfors)
  177. args = append(args, newArgs...)
  178. q := `select DISTINCT winner_id,winner from information.transaction_info where buyer_id in (select buyer_id from information.transaction_info where has(winner, ?) and buyer_id<>'') and hasAny(property_form,[` + wh + `])=0 ORDER BY project_id`
  179. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  180. if err != nil {
  181. logx.Error(err)
  182. } else {
  183. repeat := map[string]bool{}
  184. ids := []string{}
  185. for rows.Next() {
  186. var (
  187. winner_id []string
  188. winner []string
  189. )
  190. if err := rows.Scan(&winner_id, &winner); err != nil {
  191. logx.Error(err)
  192. continue
  193. }
  194. for k, v := range winner {
  195. if repeat[v] || !strings.Contains(v, in.Name) {
  196. continue
  197. }
  198. repeat[v] = true
  199. if k >= len(winner_id) {
  200. continue
  201. }
  202. ids = append(ids, winner_id[k])
  203. res = append(res, map[string]string{
  204. "company_name": v,
  205. "company_id": winner_id[k],
  206. })
  207. if len(res) == pageSize {
  208. break
  209. }
  210. }
  211. }
  212. rows.Close()
  213. if err := rows.Err(); err != nil {
  214. logx.Error(err)
  215. }
  216. qyxyId := n.GetQyxyId(ids)
  217. for _, v := range res {
  218. v["qyxy_id"] = qyxyId[v["company_id"]]
  219. }
  220. }
  221. }
  222. } else {
  223. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  224. switch in.Type {
  225. case "firstparty":
  226. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  227. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  228. case "supplier":
  229. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  230. must = append(must, `{"term":{"company_unit_type":3}}`)
  231. case "middleman_owner":
  232. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  233. case "agency":
  234. must = append(must, `{"term":{"company_unit_type":4}}`)
  235. }
  236. q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_id","company_name"]}`, strings.Join(must, ","), pageSize)
  237. logx.Info("人脉库-业主名称联想", q)
  238. datas := VarEs.Get("ent_info", "ent_info", q)
  239. if datas != nil {
  240. for _, v := range *datas {
  241. res = append(res, map[string]string{
  242. "company_name": ObjToString(v["company_name"]),
  243. "company_id": ObjToString(v["id"]),
  244. "qyxy_id": ObjToString(v["company_id"]),
  245. })
  246. }
  247. }
  248. }
  249. reply.Data = res
  250. return
  251. }
  252. //人脉库-全部人脉项目
  253. func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
  254. pool := make(chan bool, 5)
  255. wait := &sync.WaitGroup{}
  256. lock := &sync.Mutex{}
  257. reply = &types.Reply{}
  258. wh, newArgs := NetworkCom.WhArgs(NetworkCom.GetMyProbusfor(in.EntAccountId))
  259. var count int64
  260. var list []*networkTree
  261. firstpartyIds := []string{}
  262. if in.Id != "" {
  263. if in.Type == "firstparty" {
  264. result := n.FirstpartyNetwork(in.Name, []string{in.Id})
  265. if result[in.Id] != nil {
  266. nameIndex := map[string]int{}
  267. for _, v := range result[in.Id] {
  268. if _, ok := nameIndex[v.Name]; !ok {
  269. nameIndex[v.Name] = len(list)
  270. list = append(list, &networkTree{
  271. Name: v.Name,
  272. })
  273. }
  274. pool <- true
  275. wait.Add(1)
  276. go func(cIndex int, cId, cName string) {
  277. defer func() {
  278. <-pool
  279. wait.Done()
  280. }()
  281. ntc := &networkTreeChild{
  282. CompanyName: cName,
  283. CompanyId: cId,
  284. Type: "firstparty",
  285. }
  286. if wh != "" {
  287. thisArgs := []interface{}{ntc.CompanyId}
  288. thisArgs = append(thisArgs, newArgs...)
  289. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  290. }
  291. lock.Lock()
  292. count++
  293. list[cIndex].Count += ntc.Count
  294. list[cIndex].Children = append(list[cIndex].Children, ntc)
  295. lock.Unlock()
  296. }(nameIndex[v.Name], v.CompanyId, v.CompanyName)
  297. }
  298. wait.Wait()
  299. }
  300. }
  301. } else {
  302. q := `SELECT a.id,a.company_id,a.company_name,a.itype,a.create_time,COUNT(b.id) AS ipc FROM crm.connection a
  303. LEFT JOIN crm.connection_introduce b ON (b.position_id=? AND b.itype=2 AND a.id=b.connection_id) WHERE a.position_id=?`
  304. args := []interface{}{in.PositionId, in.PositionId}
  305. if in.Name != "" {
  306. q += ` and company_name like ?`
  307. args = append(args, "%"+in.Name+"%")
  308. }
  309. q += ` GROUP BY a.id ORDER BY a.create_time DESC`
  310. datas := CrmMysql.SelectBySql(q, args...)
  311. list = []*networkTree{
  312. &networkTree{
  313. Name: "甲方",
  314. Children: []*networkTreeChild{},
  315. },
  316. &networkTree{
  317. Name: "供应商",
  318. Children: []*networkTreeChild{},
  319. },
  320. &networkTree{
  321. Name: "同甲异业渠道",
  322. Children: []*networkTreeChild{},
  323. },
  324. &networkTree{
  325. Name: "中间人",
  326. Children: []*networkTreeChild{},
  327. },
  328. &networkTree{
  329. Name: "招标代理",
  330. Children: []*networkTreeChild{},
  331. },
  332. }
  333. //
  334. for _, vt := range *datas {
  335. pool <- true
  336. wait.Add(1)
  337. go func(v map[string]interface{}) {
  338. defer func() {
  339. <-pool
  340. wait.Done()
  341. }()
  342. itype := IntAll(v["itype"])
  343. if itype <= 0 || itype > len(list) {
  344. return
  345. }
  346. company_name := ObjToString(v["company_name"])
  347. ntc := &networkTreeChild{
  348. Id: Int64All(v["id"]),
  349. CompanyName: company_name,
  350. CompanyId: ObjToString(v["company_id"]),
  351. Type: n.TypeIntConvert(Int64All(v["itype"])),
  352. CreateTime: ObjToString(v["create_time"]),
  353. }
  354. if wh != "" {
  355. thisArgs := []interface{}{ntc.CompanyId}
  356. thisArgs = append(thisArgs, newArgs...)
  357. if itype == 1 {
  358. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  359. } else if itype == 2 || itype == 3 {
  360. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where has(winner_id,?) and hasAny(property_form,[`+wh+`])`, thisArgs...)
  361. } else if itype == 4 {
  362. ntc.Count = Int64All(v["ipc"])
  363. } else if itype == 5 {
  364. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where agency_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  365. }
  366. }
  367. lock.Lock()
  368. if itype == 1 {
  369. firstpartyIds = append(firstpartyIds, ntc.CompanyId)
  370. }
  371. count += ntc.Count
  372. list[itype-1].Count += ntc.Count
  373. list[itype-1].Children = append(list[itype-1].Children, ntc)
  374. lock.Unlock()
  375. }(vt)
  376. }
  377. wait.Wait()
  378. }
  379. firstpartyChild := map[string][]*firstpartyNetwork{}
  380. if len(firstpartyIds) > 0 {
  381. firstpartyChild = n.FirstpartyNetwork("", firstpartyIds)
  382. }
  383. convList := []map[string]interface{}{}
  384. for _, v := range list {
  385. pm := map[string]interface{}{
  386. "CODE": v.Name,
  387. "NAME": v.Name,
  388. "SZ_PID0": v.Name,
  389. "SZ_LEVEL": 0,
  390. "DATACOUNT": 0,
  391. }
  392. if len(v.Children) > 0 {
  393. pm["SZ_LEAF"] = 0
  394. } else {
  395. pm["SZ_LEAF"] = 1
  396. }
  397. sort.Sort(v)
  398. id := ""
  399. pType := ""
  400. for _, vv := range v.Children {
  401. tempId := vv.CompanyId
  402. if vv.Type == "middleman" {
  403. tempId = fmt.Sprint(vv.Id)
  404. }
  405. myChildIds := ""
  406. if in.Id == "" && v.Name == "甲方" {
  407. for _, vvv := range firstpartyChild[vv.CompanyId] {
  408. if id != "" {
  409. id += ","
  410. }
  411. id += vvv.CompanyId
  412. if myChildIds != "" {
  413. myChildIds += ","
  414. }
  415. myChildIds += vvv.CompanyId
  416. }
  417. } else {
  418. if id != "" {
  419. id += ","
  420. }
  421. id += tempId
  422. }
  423. if pType != "" {
  424. pType += ","
  425. }
  426. pType += vv.Type
  427. if myChildIds == "" {
  428. myChildIds = tempId
  429. }
  430. cm := map[string]interface{}{
  431. "NAME": vv.CompanyName,
  432. "ID": myChildIds,
  433. "SZ_PID0": v.Name,
  434. "SZ_PID1": v.Name + ":" + tempId,
  435. "CODE": v.Name + ":" + tempId,
  436. "PCODE": v.Name,
  437. "DATACOUNT": vv.Count,
  438. "SZ_LEVEL": 1,
  439. "SZ_LEAF": 1,
  440. "TYPE": vv.Type,
  441. "MYID": tempId,
  442. }
  443. convList = append(convList, cm)
  444. }
  445. pm["ID"] = id
  446. pm["TYPE"] = pType
  447. convList = append(convList, pm)
  448. }
  449. reply = &types.Reply{
  450. Data: map[string]interface{}{
  451. "count": count,
  452. "list": convList,
  453. },
  454. }
  455. return reply
  456. }
  457. //人脉库-列表
  458. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  459. if in.Page_size <= 0 {
  460. in.Page_size = 10
  461. }
  462. if in.Current_page <= 0 {
  463. in.Current_page = 1
  464. }
  465. args := []interface{}{in.PositionId}
  466. sqlAppend := ""
  467. if in.Type != "" {
  468. sqlAppend += ` and a.itype=?`
  469. args = append(args, n.TypeStrConvert(in.Type))
  470. }
  471. if in.Starttime != "" {
  472. sqlAppend += ` and a.create_time>=?`
  473. args = append(args, in.Starttime+" 00:00:00")
  474. }
  475. if in.Endtime != "" {
  476. sqlAppend += ` and a.create_time<=?`
  477. if in.Starttime == in.Endtime {
  478. in.Endtime += " 23:59:59"
  479. } else {
  480. in.Endtime += " 00:00:00"
  481. }
  482. args = append(args, in.Endtime)
  483. }
  484. if in.Name != "" {
  485. sqlAppend += ` and a.company_name like ?`
  486. args = append(args, "%"+in.Name+"%")
  487. }
  488. 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=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a
  489. left join crm.connection_introduce b on (a.id=b.connection_id) where a.position_id=?` + sqlAppend + ` GROUP BY a.id order by a.create_time desc`
  490. var count int64
  491. start := (in.Current_page - 1) * in.Page_size
  492. end := start + in.Page_size
  493. dbPaging := in.Buyercount_start == 0 && in.Buyercount_end == 0 && in.Monitor == 0 && in.Monitorcount_start == 0 && in.Monitorcount_end == 0 && in.Project_matchme == 0 && in.Order_amount == 0
  494. if dbPaging {
  495. count = CrmMysql.CountBySql(`select count(1) as count from crm.connection a where a.position_id=?`+sqlAppend, args...)
  496. q += ` limit ?,?`
  497. args = append(args, start, end)
  498. }
  499. listTemp := CrmMysql.SelectBySql(q, args...)
  500. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}
  501. for _, v := range *listTemp {
  502. switch Int64All(v["itype"]) {
  503. case 1:
  504. firstparty_array = append(firstparty_array, ObjToString(v["company_id"]))
  505. case 2:
  506. supplier_array = append(supplier_array, ObjToString(v["company_id"]))
  507. case 3:
  508. adiffb_array = append(adiffb_array, ObjToString(v["company_id"]))
  509. case 4:
  510. if relate_project_id := ObjToString(v["relate_project_id"]); relate_project_id != "" {
  511. middleman_project_array = append(middleman_project_array, strings.Split(relate_project_id, ",")...)
  512. }
  513. case 5:
  514. agency_array = append(agency_array, ObjToString(v["company_id"]))
  515. }
  516. }
  517. //
  518. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  519. list := []*map[string]interface{}{}
  520. isGoNextSetp := true
  521. probusfors := []string{}
  522. if in.Project_matchme == 1 {
  523. probusfors = NetworkCom.GetMyProbusfor(in.EntAccountId)
  524. if len(probusfors) == 0 {
  525. isGoNextSetp = false
  526. }
  527. }
  528. if isGoNextSetp {
  529. entMonitor := NetworkCom.EntMonitor(in.UserId)
  530. fpn := n.FirstpartyNetwork("", firstparty_array)
  531. firstparty_project := n.Introduce_Firstparty(fpn, entMonitor, in.Project_matchme, probusfors)
  532. supplier_project := n.Introduce_Supplier(supplier_array, entMonitor, in.Project_matchme, probusfors)
  533. adiffb_project := n.Introduce_Supplier(adiffb_array, entMonitor, in.Project_matchme, probusfors)
  534. agency_project := n.Introduce_Agency(agency_array, entMonitor, in.Project_matchme, probusfors)
  535. middleman_project := n.Introduce_Middleman(middleman_project_array, entMonitor, in.Project_matchme, probusfors)
  536. for _, v := range *listTemp {
  537. itype := ""
  538. buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
  539. company_id := ObjToString(v["company_id"])
  540. export_id := []string{}
  541. jump_type, jump_id := "", ""
  542. switch Int64All(v["itype"]) {
  543. case 1:
  544. itype = "甲方"
  545. jump_type = "firstparty"
  546. for _, vv := range fpn[company_id] {
  547. if jump_id != "" {
  548. jump_id += ","
  549. }
  550. jump_id += vv.CompanyId
  551. }
  552. firstparty_count++
  553. if firstparty_project[company_id] != nil {
  554. buyer_count = firstparty_project[company_id].BuyerCount
  555. project_count = firstparty_project[company_id].ProjectCount
  556. expect_amount = firstparty_project[company_id].ProjectAmount
  557. monitor_count = firstparty_project[company_id].MonitorCount
  558. export_id = firstparty_project[company_id].ExportId
  559. }
  560. case 2:
  561. itype = "供应商"
  562. jump_type = "supplier"
  563. jump_id = company_id
  564. supplier_count++
  565. if supplier_project[company_id] != nil {
  566. buyer_count = supplier_project[company_id].BuyerCount
  567. project_count = supplier_project[company_id].ProjectCount
  568. expect_amount = supplier_project[company_id].ProjectAmount
  569. monitor_count = supplier_project[company_id].MonitorCount
  570. export_id = supplier_project[company_id].ExportId
  571. }
  572. case 3:
  573. itype = "同甲异业渠道"
  574. jump_type = "adiffb"
  575. jump_id = company_id
  576. adiffb_count++
  577. if adiffb_project[company_id] != nil {
  578. buyer_count = adiffb_project[company_id].BuyerCount
  579. project_count = adiffb_project[company_id].ProjectCount
  580. expect_amount = adiffb_project[company_id].ProjectAmount
  581. monitor_count = adiffb_project[company_id].MonitorCount
  582. export_id = adiffb_project[company_id].ExportId
  583. }
  584. case 4:
  585. itype = "中间人"
  586. jump_type = "middleman"
  587. jump_id = fmt.Sprint(Int64All(v["id"]))
  588. middleman_count++
  589. buyer_count = Int64All(v["buyer_count"])
  590. project_count = Int64All(v["project_count"])
  591. if relate_buyer_id := ObjToString(v["relate_buyer_id"]); relate_buyer_id != "" {
  592. for _, v := range strings.Split(relate_buyer_id, ",") {
  593. if v == "" {
  594. continue
  595. }
  596. if entMonitor[v] {
  597. monitor_count++
  598. }
  599. }
  600. }
  601. if relate_project_id := ObjToString(v["relate_project_id"]); relate_project_id != "" {
  602. export_id = strings.Split(relate_project_id, ",")
  603. for _, v := range export_id {
  604. if middleman_project[v] != nil {
  605. expect_amount += middleman_project[v].ProjectAmount
  606. }
  607. }
  608. }
  609. case 5:
  610. itype = "招标代理机构"
  611. jump_type = "agency"
  612. jump_id = company_id
  613. agency_count++
  614. if agency_project[company_id] != nil {
  615. buyer_count = agency_project[company_id].BuyerCount
  616. project_count = agency_project[company_id].ProjectCount
  617. expect_amount = agency_project[company_id].ProjectAmount
  618. monitor_count = agency_project[company_id].MonitorCount
  619. export_id = agency_project[company_id].ExportId
  620. }
  621. }
  622. if buyer_count < in.Buyercount_start {
  623. continue
  624. } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
  625. continue
  626. } else if in.Monitor == 1 && monitor_count <= 0 {
  627. continue
  628. } else if in.Monitor == -1 && monitor_count > 0 {
  629. continue
  630. } else if monitor_count < in.Monitorcount_start {
  631. continue
  632. } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
  633. continue
  634. } else if in.Project_matchme == 1 && project_count == 0 {
  635. continue
  636. }
  637. export_url := ""
  638. if len(export_id) > 0 {
  639. export_url = "/subscribepay/network/projectExport?export_id=" + encrypt.SE.EncodeStringByCheck(strings.Join(export_id, ","))
  640. }
  641. url := ""
  642. company_name, _ := v["company_name"].(string)
  643. if qyxy_id := ObjToString(v["qyxy_id"]); qyxy_id != "" && (jump_type == "supplier" || jump_type == "adiffb") {
  644. url = "/swordfish/page_big_pc/ent_portrait/" + encrypt.EncodeArticleId2ByCheck(qyxy_id)
  645. } else if jump_type == "firstparty" && company_name != "" {
  646. url = "/swordfish/page_big_pc/unit_portrayal/" + company_name
  647. }
  648. list = append(list, &map[string]interface{}{
  649. "company_id": company_id,
  650. "company_name": company_name,
  651. "type": itype,
  652. "jump_type": jump_type,
  653. "jump_id": jump_id,
  654. "person": v["person"],
  655. "phone": v["phone"],
  656. "buyer_count": buyer_count,
  657. "monitor_count": 0,
  658. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  659. "project_count": project_count,
  660. "create_time": v["create_time"],
  661. "export_url": export_url,
  662. "url": url,
  663. })
  664. }
  665. }
  666. csList := &ComSortList{
  667. SortKeys: []*ComSortKey{
  668. &ComSortKey{
  669. Keys: []string{"expect_amount"},
  670. Order: 1,
  671. Type: "float",
  672. },
  673. },
  674. List: list,
  675. }
  676. if in.Order_amount == -1 {
  677. csList.SortKeys[0].Order = -1
  678. }
  679. if in.Order_amount != 0 {
  680. sort.Sort(csList)
  681. }
  682. finalList := []*map[string]interface{}{}
  683. if !dbPaging {
  684. count = int64(len(csList.List))
  685. if count > 0 && start < count {
  686. if end > count {
  687. end = count
  688. }
  689. finalList = csList.List[start:end]
  690. }
  691. } else {
  692. finalList = csList.List
  693. }
  694. total_page := int64(math.Ceil(float64(count) / float64(in.Page_size)))
  695. return &types.Reply{
  696. Data: map[string]interface{}{
  697. "count": count,
  698. "total_page": total_page,
  699. "firstparty_count": firstparty_count,
  700. "supplier_count": supplier_count,
  701. "adiffb_count": adiffb_count,
  702. "middleman_count": middleman_count,
  703. "agency_count": agency_count,
  704. "list": finalList,
  705. },
  706. }
  707. }
  708. //
  709. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  710. result := map[string][]*firstpartyNetwork{}
  711. wh, args := NetworkCom.WhArgs(values)
  712. 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
  713. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  714. if name != "" {
  715. q += ` where c.company_name like ?`
  716. args = append(args, "%"+name+"%")
  717. }
  718. q += ` order by b.name,a.a_id,a.b_name`
  719. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  720. if err != nil {
  721. logx.Error(err)
  722. return result
  723. }
  724. for rows.Next() {
  725. var (
  726. a_id string
  727. company_id string
  728. company_name string
  729. name string
  730. )
  731. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  732. logx.Error(err)
  733. continue
  734. }
  735. if company_id == "" || company_name == "" {
  736. continue
  737. }
  738. result[a_id] = append(result[a_id], &firstpartyNetwork{
  739. CompanyId: company_id,
  740. CompanyName: company_name,
  741. Name: name,
  742. })
  743. }
  744. return result
  745. }
  746. //
  747. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
  748. values := []string{}
  749. vm := map[string]*projectInfo{}
  750. for _, v := range fpn {
  751. for _, vv := range v {
  752. vm[vv.CompanyId] = &projectInfo{}
  753. values = append(values, vv.CompanyId)
  754. }
  755. }
  756. result := map[string]*projectInfo{}
  757. if len(values) == 0 {
  758. return result
  759. }
  760. wh, args := NetworkCom.WhArgs(values)
  761. q := `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
  762. if matchme == 1 {
  763. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  764. q += ` and hasAny(property_form,[` + newWh + `])`
  765. args = append(args, newArgs...)
  766. }
  767. q += ` group by buyer_id`
  768. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  769. if err != nil {
  770. logx.Error(err)
  771. return nil
  772. }
  773. for rows.Next() {
  774. var (
  775. buyer_id string
  776. project_count uint64
  777. project_amount decimal.Decimal
  778. project_id []string
  779. )
  780. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  781. logx.Error(err)
  782. continue
  783. }
  784. if vm[buyer_id] == nil {
  785. continue
  786. }
  787. vm[buyer_id].ProjectCount += int64(project_count)
  788. pf, _ := project_amount.Float64()
  789. vm[buyer_id].ProjectAmount += pf
  790. vm[buyer_id].ExportId = project_id
  791. }
  792. rows.Close()
  793. if err := rows.Err(); err != nil {
  794. logx.Error(err)
  795. }
  796. for k, v := range fpn {
  797. if result[k] == nil {
  798. result[k] = &projectInfo{}
  799. }
  800. result[k].BuyerCount = int64(len(v))
  801. for _, vv := range v {
  802. if entMonitor[vv.CompanyName] {
  803. result[k].MonitorCount++
  804. }
  805. if vm[vv.CompanyId] == nil {
  806. continue
  807. }
  808. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  809. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  810. }
  811. }
  812. return result
  813. }
  814. //
  815. func (n *network) Introduce_Supplier(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
  816. if len(values) == 0 {
  817. return map[string]*projectInfo{}
  818. }
  819. wh, args := NetworkCom.WhArgs(values)
  820. vbs := map[string][]*idName{}
  821. repeat := map[string]bool{}
  822. whRepeat := map[string]map[string]bool{}
  823. buyers := []string{}
  824. q := `select winner_id,buyer_id,buyer from information.transaction_info where hasAny(winner_id,[` + wh + `])`
  825. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  826. if err != nil {
  827. logx.Error(err)
  828. } else {
  829. for rows.Next() {
  830. var (
  831. winner_id []string
  832. buyer_id string
  833. buyer string
  834. )
  835. if err := rows.Scan(&winner_id, &buyer_id, &buyer); err != nil {
  836. logx.Error(err)
  837. continue
  838. }
  839. if buyer_id == "" || buyer == "" {
  840. continue
  841. }
  842. if !repeat[buyer_id] {
  843. repeat[buyer_id] = true
  844. buyers = append(buyers, buyer_id)
  845. }
  846. for _, v := range winner_id {
  847. if whRepeat[v] != nil && whRepeat[v][buyer_id] {
  848. continue
  849. }
  850. if whRepeat[v] == nil {
  851. whRepeat[v] = map[string]bool{}
  852. }
  853. whRepeat[v][buyer_id] = true
  854. vbs[v] = append(vbs[v], &idName{
  855. Id: buyer_id,
  856. Name: buyer,
  857. })
  858. }
  859. }
  860. rows.Close()
  861. if err := rows.Err(); err != nil {
  862. logx.Error(err)
  863. }
  864. }
  865. return n.MakeProjectInfo(buyers, vbs, matchme, probusfors, entMonitor)
  866. }
  867. //
  868. func (n *network) Introduce_Agency(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
  869. if len(values) == 0 {
  870. return map[string]*projectInfo{}
  871. }
  872. wh, args := NetworkCom.WhArgs(values)
  873. q := `select DISTINCT agency_id,buyer_id,buyer from information.transaction_info where agency_id in (` + wh + `)`
  874. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  875. if err != nil {
  876. logx.Error(err)
  877. return nil
  878. }
  879. vbs := map[string][]*idName{}
  880. buyers := []string{}
  881. repeat := map[string]bool{}
  882. for rows.Next() {
  883. var (
  884. agency_id string
  885. buyer_id string
  886. buyer string
  887. )
  888. if err := rows.Scan(&agency_id, &buyer_id, &buyer); err != nil {
  889. logx.Error(err)
  890. continue
  891. }
  892. if buyer_id == "" || buyer == "" {
  893. continue
  894. }
  895. vbs[agency_id] = append(vbs[agency_id], &idName{
  896. Id: buyer_id,
  897. Name: buyer,
  898. })
  899. if !repeat[buyer_id] {
  900. repeat[buyer_id] = true
  901. buyers = append(buyers, buyer_id)
  902. }
  903. }
  904. rows.Close()
  905. if err := rows.Err(); err != nil {
  906. logx.Error(err)
  907. }
  908. return n.MakeProjectInfo(buyers, vbs, matchme, probusfors, entMonitor)
  909. }
  910. //
  911. func (n *network) Introduce_Middleman(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
  912. result := map[string]*projectInfo{}
  913. if len(values) == 0 {
  914. return result
  915. }
  916. wh, newArgs := NetworkCom.WhArgs(values)
  917. rows, err := ClickhouseConn.Query(context.Background(), `select project_id,project_money from information.transaction_info where project_id in (`+wh+`)`, newArgs...)
  918. if err != nil {
  919. logx.Error(err)
  920. return nil
  921. }
  922. for rows.Next() {
  923. var (
  924. project_id string
  925. project_money decimal.Decimal
  926. )
  927. if err := rows.Scan(&project_id, &project_money); err != nil {
  928. logx.Error(err)
  929. continue
  930. }
  931. pf, _ := project_money.Float64()
  932. result[project_id] = &projectInfo{
  933. ProjectAmount: pf,
  934. }
  935. }
  936. rows.Close()
  937. if err := rows.Err(); err != nil {
  938. logx.Error(err)
  939. }
  940. return result
  941. }
  942. //
  943. func (n *network) TypeStrConvert(itype string) int {
  944. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  945. switch itype {
  946. case "firstparty":
  947. return 1
  948. case "supplier":
  949. return 2
  950. case "adiffb":
  951. return 3
  952. case "middleman":
  953. return 4
  954. case "agency":
  955. return 5
  956. }
  957. return 0
  958. }
  959. //
  960. func (n *network) TypeIntConvert(itype int64) string {
  961. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  962. switch itype {
  963. case 1:
  964. return "firstparty"
  965. case 2:
  966. return "supplier"
  967. case 3:
  968. return "adiffb"
  969. case 4:
  970. return "middleman"
  971. case 5:
  972. return "agency"
  973. }
  974. return ""
  975. }
  976. //
  977. func (n *network) GetQyxyId(ids []string) map[string]string {
  978. wh, args := NetworkCom.WhArgs(ids)
  979. q := `select id,company_id from information.ent_info where id in (` + wh + `)`
  980. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  981. m := map[string]string{}
  982. if err != nil {
  983. logx.Error(err)
  984. } else {
  985. for rows.Next() {
  986. var (
  987. id string
  988. company_id string
  989. )
  990. if err := rows.Scan(&id, &company_id); err != nil {
  991. logx.Error(err)
  992. continue
  993. }
  994. m[id] = company_id
  995. }
  996. rows.Close()
  997. if err := rows.Err(); err != nil {
  998. logx.Error(err)
  999. }
  1000. }
  1001. return m
  1002. }
  1003. //
  1004. func (n *network) BuyerProjectInfo(ids []string, matchme int64, probusfors []string) map[string]*projectInfo {
  1005. vm := map[string]*projectInfo{}
  1006. wh, args := NetworkCom.WhArgs(ids)
  1007. q := `select buyer_id,count(DISTINCT project_id) AS project_count,sum(project_money) AS project_amount,groupUniqArray(project_id) from information.transaction_info where buyer_id in (` + wh + `)`
  1008. if matchme == 1 {
  1009. newWh, newArgs := NetworkCom.WhArgs(probusfors)
  1010. q += ` and hasAny(property_form,[` + newWh + `])`
  1011. args = append(args, newArgs...)
  1012. }
  1013. q += ` group by buyer_id`
  1014. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  1015. if err != nil {
  1016. logx.Error(err)
  1017. return nil
  1018. }
  1019. for rows.Next() {
  1020. var (
  1021. buyer_id string
  1022. project_count uint64
  1023. project_amount decimal.Decimal
  1024. project_id []string
  1025. )
  1026. if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
  1027. logx.Error(err)
  1028. continue
  1029. }
  1030. pf, _ := project_amount.Float64()
  1031. vm[buyer_id] = &projectInfo{
  1032. ProjectCount: int64(project_count),
  1033. ExportId: project_id,
  1034. ProjectAmount: pf,
  1035. }
  1036. }
  1037. rows.Close()
  1038. if err := rows.Err(); err != nil {
  1039. logx.Error(err)
  1040. }
  1041. return vm
  1042. }
  1043. //
  1044. func (n *network) MakeProjectInfo(buyers []string, vbs map[string][]*idName, matchme int64, probusfors []string, entMonitor map[string]bool) map[string]*projectInfo {
  1045. pis := n.BuyerProjectInfo(buyers, matchme, probusfors)
  1046. vm := map[string]*projectInfo{}
  1047. for k, v := range vbs {
  1048. pi := &projectInfo{
  1049. BuyerCount: int64(len(v)),
  1050. }
  1051. re := map[string]bool{}
  1052. for _, vv := range v {
  1053. if pis[vv.Id] != nil {
  1054. pi.ProjectCount += pis[vv.Id].ProjectCount
  1055. pi.ProjectAmount += pis[vv.Id].ProjectAmount
  1056. for _, vvv := range pis[vv.Id].ExportId {
  1057. if re[vvv] {
  1058. continue
  1059. }
  1060. pi.ExportId = append(pi.ExportId, vvv)
  1061. re[vvv] = true
  1062. }
  1063. }
  1064. if entMonitor[vv.Name] {
  1065. pi.MonitorCount++
  1066. }
  1067. }
  1068. vm[k] = pi
  1069. }
  1070. return vm
  1071. }