network.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. package service
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "math"
  7. "sort"
  8. "strings"
  9. "sync"
  10. . "app.yhyue.com/moapp/jybase/common"
  11. . "app.yhyue.com/moapp/jybase/date"
  12. . "app.yhyue.com/moapp/jybase/es"
  13. . "app.yhyue.com/moapp/jybase/sort"
  14. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  15. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  16. "github.com/shopspring/decimal"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. var Network = &network{}
  20. type network struct {
  21. }
  22. type networkTree struct {
  23. Count int64 `json:"count"`
  24. Name string `json:"name"`
  25. Children []*networkTreeChild `json:"children"`
  26. }
  27. type networkTreeChild struct {
  28. Count int64 `json:"count"`
  29. Name string `json:"name"`
  30. Id string `json:"id"`
  31. Type int `json:"type"`
  32. }
  33. type projectInfo struct {
  34. BuyerCount int
  35. ProjectCount int
  36. ProjectAmount float64
  37. }
  38. type firstpartyNetwork struct {
  39. CompanyId string
  40. CompanyName string
  41. Name string
  42. }
  43. //人脉库-添加/修改人脉
  44. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  45. reply := &types.Reply{}
  46. nowFormat := NowFormat(Date_Full_Layout)
  47. var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
  48. if in.Type != "middleman" {
  49. return true
  50. }
  51. values := []interface{}{}
  52. if in.Introduce_owner_id != "" {
  53. for k, v := range strings.Split(in.Introduce_owner_id, ",") {
  54. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_owner_name, ",")[k], 1, nowFormat)
  55. }
  56. }
  57. if in.Introduce_project_id != "" {
  58. for k, v := range strings.Split(in.Introduce_project_id, ",") {
  59. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_project_name, ",")[k], 2, nowFormat)
  60. }
  61. }
  62. if len(values) == 0 {
  63. return false
  64. }
  65. var r2 int64
  66. if isUpdate {
  67. r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  68. }
  69. r3, _ := CrmMysql.InsertBatchByTx(tx, "crm.connection_introduce", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "connection_id", "relate_id", "relate_name", "itype", "create_time"}, values)
  70. return r2 >= 0 && r3 > 0
  71. }
  72. if in.Id > 0 {
  73. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  74. 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)
  75. return r1 >= 0 && saveIntroduce(tx, in.Id, true)
  76. }) {
  77. reply.Data = map[string]interface{}{
  78. "status": 1,
  79. }
  80. } else {
  81. reply.Data = map[string]interface{}{
  82. "status": 0,
  83. }
  84. }
  85. } else {
  86. itype := n.TypeConvert(in.Type)
  87. var r1 int64
  88. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  89. _, r1 = CrmMysql.InsertBatchByTx(tx, "crm.connection", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_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.Contact_person, in.Contact_phone, 1, nowFormat, nowFormat})
  90. return r1 > 0 && saveIntroduce(tx, r1, false)
  91. }) {
  92. reply.Data = map[string]interface{}{
  93. "status": 1,
  94. "id": r1,
  95. }
  96. } else {
  97. reply.Data = map[string]interface{}{
  98. "status": 0,
  99. }
  100. }
  101. }
  102. return reply
  103. }
  104. //人脉库-业主名称联想
  105. func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
  106. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  107. res := []map[string]interface{}{}
  108. reply = &types.Reply{Data: res}
  109. in.Name = strings.TrimSpace(in.Name)
  110. if in.Name == "" {
  111. return
  112. }
  113. pageSize := 10
  114. if in.Type == "adiffb" {
  115. probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
  116. if len(probusfors) > 0 {
  117. args := []interface{}{in.EntName}
  118. wh, newArgs := NetworkCom.WhArgs(probusfors)
  119. args = append(args, newArgs...)
  120. q := `select DISTINCT b.winner_id,b.winner from information.transaction_info a
  121. inner join information.transaction_info b on (has(a.winner, ?) and a.buyer_id<>'' and a.buyer_id=b.buyer_id and hasAny(b.property_form,[` + wh + `])) ORDER BY b.project_id`
  122. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  123. if err != nil {
  124. logx.Error(err)
  125. } else {
  126. repeat := map[string]bool{}
  127. for rows.Next() {
  128. var (
  129. winner_id []string
  130. winner []string
  131. )
  132. if err := rows.Scan(&winner_id, &winner); err != nil {
  133. logx.Error(err)
  134. continue
  135. }
  136. for k, v := range winner {
  137. if repeat[v] || !strings.Contains(v, in.Name) {
  138. continue
  139. }
  140. repeat[v] = true
  141. if k >= len(winner_id) {
  142. continue
  143. }
  144. res = append(res, map[string]interface{}{
  145. "company_name": v,
  146. "company_id": winner_id[k],
  147. })
  148. }
  149. }
  150. rows.Close()
  151. if err := rows.Err(); err != nil {
  152. logx.Error(err)
  153. }
  154. if len(res) > pageSize {
  155. res = res[:pageSize]
  156. }
  157. }
  158. }
  159. } else {
  160. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  161. switch in.Type {
  162. case "firstparty":
  163. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  164. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  165. case "supplier":
  166. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  167. must = append(must, `{"term":{"company_unit_type":3}}`)
  168. case "middleman_owner":
  169. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  170. case "agency":
  171. must = append(must, `{"term":{"company_unit_type":4}}`)
  172. }
  173. q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_name"]}`, strings.Join(must, ","), pageSize)
  174. logx.Info("人脉库-业主名称联想", q)
  175. datas := VarEs.Get("ent_info", "ent_info", q)
  176. if datas != nil {
  177. for _, v := range *datas {
  178. res = append(res, map[string]interface{}{
  179. "company_name": ObjToString(v["company_name"]),
  180. "company_id": ObjToString(v["id"]),
  181. })
  182. }
  183. }
  184. }
  185. reply.Data = res
  186. return
  187. }
  188. //人脉库-全部人脉项目
  189. func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
  190. pool := make(chan bool, 5)
  191. wait := &sync.WaitGroup{}
  192. lock := &sync.Mutex{}
  193. reply = &types.Reply{}
  194. wh, newArgs := NetworkCom.WhArgs(NetworkCom.GetMyProbusfor(in.EntAccountId))
  195. var count int64
  196. var list []*networkTree
  197. if in.Id != "" {
  198. if in.Type == 1 {
  199. result := n.FirstpartyNetwork(in.Name, []string{in.Id})
  200. if result[in.Id] != nil {
  201. nameIndex := map[string]int{}
  202. for _, v := range result[in.Id] {
  203. if _, ok := nameIndex[v.Name]; !ok {
  204. nameIndex[v.Name] = len(list)
  205. list = append(list, &networkTree{
  206. Name: v.Name,
  207. })
  208. }
  209. pool <- true
  210. wait.Add(1)
  211. go func(cIndex int, cId, cName string) {
  212. defer func() {
  213. <-pool
  214. wait.Done()
  215. }()
  216. ntc := &networkTreeChild{
  217. Name: cName,
  218. Id: cId,
  219. Type: 1,
  220. }
  221. if wh != "" {
  222. thisArgs := []interface{}{ntc.Id}
  223. thisArgs = append(thisArgs, newArgs...)
  224. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  225. }
  226. lock.Lock()
  227. count++
  228. list[cIndex].Count += ntc.Count
  229. list[cIndex].Children = append(list[cIndex].Children, ntc)
  230. lock.Unlock()
  231. }(nameIndex[v.Name], v.CompanyId, v.CompanyName)
  232. }
  233. wait.Wait()
  234. }
  235. }
  236. } else {
  237. q := `SELECT a.company_id,a.company_name,a.itype,COUNT(b.id) AS ipc FROM crm.connection a
  238. LEFT JOIN crm.connection_introduce b ON (b.position_id=? AND b.itype=2 AND a.id=b.connection_id) WHERE a.position_id=?`
  239. args := []interface{}{in.PositionId, in.PositionId}
  240. if in.Name != "" {
  241. q += ` and company_name like ?`
  242. args = append(args, "%"+in.Name+"%")
  243. }
  244. q += ` GROUP BY a.id ORDER BY a.create_time DESC`
  245. datas := CrmMysql.SelectBySql(q, args...)
  246. list = []*networkTree{
  247. &networkTree{
  248. Name: "甲方",
  249. Children: []*networkTreeChild{},
  250. },
  251. &networkTree{
  252. Name: "供应商",
  253. Children: []*networkTreeChild{},
  254. },
  255. &networkTree{
  256. Name: "同甲异业渠道",
  257. Children: []*networkTreeChild{},
  258. },
  259. &networkTree{
  260. Name: "中间人",
  261. Children: []*networkTreeChild{},
  262. },
  263. &networkTree{
  264. Name: "招标代理",
  265. Children: []*networkTreeChild{},
  266. },
  267. }
  268. //
  269. for _, vt := range *datas {
  270. pool <- true
  271. wait.Add(1)
  272. go func(v map[string]interface{}) {
  273. defer func() {
  274. <-pool
  275. wait.Done()
  276. }()
  277. itype := IntAll(v["itype"])
  278. if itype <= 0 || itype > len(list) {
  279. return
  280. }
  281. ntc := &networkTreeChild{
  282. Name: ObjToString(v["company_name"]),
  283. Id: ObjToString(v["company_id"]),
  284. Type: IntAll(v["itype"]),
  285. }
  286. if wh != "" {
  287. thisArgs := []interface{}{ntc.Id}
  288. thisArgs = append(thisArgs, newArgs...)
  289. if itype == 1 {
  290. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  291. } else if itype == 2 || itype == 3 {
  292. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where has(winner_id,?) and hasAny(property_form,[`+wh+`])`, thisArgs...)
  293. } else if itype == 4 {
  294. ntc.Count = Int64All(v["ipc"])
  295. } else if itype == 5 {
  296. ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where agency_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
  297. }
  298. }
  299. lock.Lock()
  300. count += ntc.Count
  301. list[itype-1].Count += ntc.Count
  302. list[itype-1].Children = append(list[itype-1].Children, ntc)
  303. lock.Unlock()
  304. }(vt)
  305. }
  306. wait.Wait()
  307. }
  308. reply = &types.Reply{
  309. Data: map[string]interface{}{
  310. "count": count,
  311. "list": list,
  312. },
  313. }
  314. return reply
  315. }
  316. //人脉库-列表
  317. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  318. q := `select a.company_id,a.company_name,a.itype,a.contact_person as person,a.contact_phone as phone,count(if(b.itype=1,1,null)) as buyer_count,count(if(b.itype=2,1,null)) as project_count,GROUP_CONCAT(b.relate_id) as relate_id,a.create_time from crm.connection a
  319. left join crm.connection_introduce b on (a.id=b.connection_id) where a.position_id=?`
  320. args := []interface{}{in.PositionId}
  321. if in.Type != "" {
  322. q += ` and a.itype=?`
  323. args = append(args, n.TypeConvert(in.Type))
  324. }
  325. if in.Starttime != "" {
  326. q += ` and a.create_time>=?`
  327. args = append(args, in.Starttime)
  328. }
  329. if in.Endtime != "" {
  330. q += ` and a.create_time<=?`
  331. args = append(args, in.Endtime)
  332. }
  333. if in.Name != "" {
  334. q += ` and a.company_name like ?`
  335. args = append(args, "%"+in.Name+"%")
  336. }
  337. q += ` GROUP BY a.id order by a.create_time desc`
  338. listTemp := CrmMysql.SelectBySql(q, args...)
  339. firstparty_array, supplier_array, adiffb_array, agency_array, middleman_array := []string{}, []string{}, []string{}, []string{}, []string{}
  340. for _, v := range *listTemp {
  341. switch Int64All(v["itype"]) {
  342. case 1:
  343. firstparty_array = append(firstparty_array, ObjToString(v["company_id"]))
  344. case 2:
  345. supplier_array = append(supplier_array, ObjToString(v["company_id"]))
  346. case 3:
  347. adiffb_array = append(adiffb_array, ObjToString(v["company_id"]))
  348. case 4:
  349. if relate_id := ObjToString(v["relate_id"]); relate_id != "" {
  350. middleman_array = append(middleman_array, strings.Split(relate_id, ",")...)
  351. }
  352. case 5:
  353. agency_array = append(agency_array, ObjToString(v["company_id"]))
  354. }
  355. }
  356. //
  357. firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
  358. list := []*map[string]interface{}{}
  359. firstparty_project := n.Introduce_Firstparty(n.FirstpartyNetwork("", firstparty_array))
  360. supplier_project := n.Introduce_Supplier(supplier_array)
  361. adiffb_project := n.Introduce_Supplier(adiffb_array)
  362. agency_project := n.Introduce_Agency(agency_array)
  363. middleman_project := n.Introduce_Middleman(middleman_array)
  364. for _, v := range *listTemp {
  365. itype := ""
  366. buyer_count, project_count, expect_amount := 0, 0, float64(0)
  367. company_id := ObjToString(v["company_id"])
  368. switch Int64All(v["itype"]) {
  369. case 1:
  370. itype = "甲方"
  371. firstparty_count++
  372. if firstparty_project[company_id] != nil {
  373. buyer_count = firstparty_project[company_id].BuyerCount
  374. project_count = firstparty_project[company_id].ProjectCount
  375. expect_amount = firstparty_project[company_id].ProjectAmount
  376. }
  377. case 2:
  378. itype = "供应商"
  379. supplier_count++
  380. if supplier_project[company_id] != nil {
  381. buyer_count = supplier_project[company_id].BuyerCount
  382. project_count = supplier_project[company_id].ProjectCount
  383. expect_amount = supplier_project[company_id].ProjectAmount
  384. }
  385. case 3:
  386. itype = "同甲异业渠道"
  387. adiffb_count++
  388. if adiffb_project[company_id] != nil {
  389. buyer_count = adiffb_project[company_id].BuyerCount
  390. project_count = adiffb_project[company_id].ProjectCount
  391. expect_amount = adiffb_project[company_id].ProjectAmount
  392. }
  393. case 4:
  394. itype = "中间人"
  395. middleman_count++
  396. buyer_count = IntAll(v["buyer_count"])
  397. project_count = IntAll(v["project_count"])
  398. if relate_id := ObjToString(v["relate_id"]); relate_id != "" {
  399. for _, v := range strings.Split(relate_id, ",") {
  400. if middleman_project[v] != nil {
  401. expect_amount += middleman_project[v].ProjectAmount
  402. }
  403. }
  404. }
  405. if middleman_project[company_id] != nil {
  406. expect_amount = middleman_project[company_id].ProjectAmount
  407. }
  408. case 5:
  409. itype = "招标代理机构"
  410. agency_count++
  411. if agency_project[company_id] != nil {
  412. buyer_count = agency_project[company_id].BuyerCount
  413. project_count = agency_project[company_id].ProjectCount
  414. expect_amount = agency_project[company_id].ProjectAmount
  415. }
  416. }
  417. list = append(list, &map[string]interface{}{
  418. "company_id": company_id,
  419. "company_name": v["company_name"],
  420. "type": itype,
  421. "person": v["person"],
  422. "phone": v["phone"],
  423. "buyer_count": buyer_count,
  424. "monitor_count": 0,
  425. "expect_amount": RetainDecimal(expect_amount/10000, 2),
  426. "project_count": project_count,
  427. "create_time": v["create_time"],
  428. })
  429. }
  430. csList := &ComSortList{
  431. SortKeys: []*ComSortKey{
  432. &ComSortKey{
  433. Keys: []string{"expect_amount"},
  434. Order: -1,
  435. Type: "float",
  436. },
  437. },
  438. List: list,
  439. }
  440. if in.Order_amount == -1 {
  441. csList.SortKeys[0].Order = -1
  442. }
  443. if in.Order_amount != 0 {
  444. sort.Sort(csList)
  445. }
  446. length := int64(len(csList.List))
  447. var pageSize int64 = 100
  448. total_page := int64(math.Ceil(float64(length) / float64(pageSize)))
  449. finalList := []*map[string]interface{}{}
  450. if length > 0 {
  451. if in.Current_page <= 0 {
  452. in.Current_page = 1
  453. }
  454. if in.Current_page > total_page {
  455. in.Current_page = total_page
  456. }
  457. start := (in.Current_page - 1) * pageSize
  458. end := start + pageSize
  459. if end > length {
  460. end = length
  461. }
  462. finalList = csList.List[start:end]
  463. }
  464. return &types.Reply{
  465. Data: map[string]interface{}{
  466. "total_page": total_page,
  467. "firstparty_count": firstparty_count,
  468. "supplier_count": supplier_count,
  469. "adiffb_count": adiffb_count,
  470. "middleman_count": middleman_count,
  471. "agency_count": agency_count,
  472. "list": finalList,
  473. },
  474. }
  475. }
  476. //
  477. func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
  478. result := map[string][]*firstpartyNetwork{}
  479. wh, args := NetworkCom.WhArgs(values)
  480. 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
  481. inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
  482. if name != "" {
  483. q += ` where c.company_name like ?`
  484. args = append(args, "%"+name+"%")
  485. }
  486. q += ` order by a.a_id,b.name,a.b_name`
  487. rows, err := ClickhouseConn.Query(context.Background(), q, args...)
  488. if err != nil {
  489. logx.Error(err)
  490. return result
  491. }
  492. for rows.Next() {
  493. var (
  494. a_id string
  495. company_id string
  496. company_name string
  497. name string
  498. )
  499. if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
  500. logx.Error(err)
  501. continue
  502. }
  503. result[a_id] = append(result[a_id], &firstpartyNetwork{
  504. CompanyId: company_id,
  505. CompanyName: company_name,
  506. Name: name,
  507. })
  508. }
  509. return result
  510. }
  511. //
  512. func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork) map[string]*projectInfo {
  513. values := []string{}
  514. vm := map[string]*projectInfo{}
  515. for _, v := range fpn {
  516. for _, vv := range v {
  517. vm[vv.CompanyId] = &projectInfo{}
  518. values = append(values, vv.CompanyId)
  519. }
  520. }
  521. wh, args := NetworkCom.WhArgs(values)
  522. rows, err := ClickhouseConn.Query(context.Background(), `select buyer_id,count(project_id) AS project_count,sum(project_money) AS project_amount from information.transaction_info where buyer_id in (`+wh+`) group by buyer_id`, args...)
  523. if err != nil {
  524. logx.Error(err)
  525. return nil
  526. }
  527. for rows.Next() {
  528. var (
  529. buyer_id string
  530. project_count uint64
  531. project_amount decimal.Decimal
  532. )
  533. if err := rows.Scan(&buyer_id, &project_count, &project_amount); err != nil {
  534. logx.Error(err)
  535. continue
  536. }
  537. if vm[buyer_id] == nil {
  538. continue
  539. }
  540. vm[buyer_id].ProjectCount += int(project_count)
  541. pf, _ := project_amount.Float64()
  542. vm[buyer_id].ProjectAmount += pf
  543. }
  544. rows.Close()
  545. if err := rows.Err(); err != nil {
  546. logx.Error(err)
  547. }
  548. result := map[string]*projectInfo{}
  549. for k, v := range fpn {
  550. if result[k] == nil {
  551. result[k] = &projectInfo{}
  552. }
  553. result[k].BuyerCount = len(v)
  554. for _, vv := range v {
  555. if vm[vv.CompanyId] == nil {
  556. continue
  557. }
  558. result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
  559. result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
  560. }
  561. }
  562. return result
  563. }
  564. //
  565. func (n *network) Introduce_Supplier(values []string) map[string]*projectInfo {
  566. vm := map[string]*projectInfo{}
  567. for _, v := range values {
  568. vm[v] = &projectInfo{}
  569. }
  570. wh, newArgs := NetworkCom.WhArgs(values)
  571. rows, err := ClickhouseConn.Query(context.Background(), `select a.winner_id,count(a.buyer_id) AS buyer_count,count(b.project_id) AS project_count,sum(b.project_money) AS project_amount from information.transaction_info a
  572. inner join information.transaction_info b on (hasAny(a.winner_id,[`+wh+`]) and a.buyer_id<>'' and a.buyer_id=b.buyer_id)
  573. group by a.winner_id`, newArgs...)
  574. if err != nil {
  575. logx.Error(err)
  576. return nil
  577. }
  578. for rows.Next() {
  579. var (
  580. winner_id []string
  581. buyer_count uint64
  582. project_count uint64
  583. project_amount decimal.Decimal
  584. )
  585. if err := rows.Scan(&winner_id, &buyer_count, &project_count, &project_amount); err != nil {
  586. logx.Error(err)
  587. continue
  588. }
  589. for _, v := range winner_id {
  590. if vm[v] == nil {
  591. continue
  592. }
  593. vm[v].BuyerCount += int(buyer_count)
  594. vm[v].ProjectCount += int(project_count)
  595. pf, _ := project_amount.Float64()
  596. vm[v].ProjectAmount += pf
  597. }
  598. }
  599. rows.Close()
  600. if err := rows.Err(); err != nil {
  601. logx.Error(err)
  602. }
  603. return vm
  604. }
  605. //
  606. func (n *network) Introduce_Agency(values []string) map[string]*projectInfo {
  607. vm := map[string]*projectInfo{}
  608. for _, v := range values {
  609. vm[v] = &projectInfo{}
  610. }
  611. wh, newArgs := NetworkCom.WhArgs(values)
  612. rows, err := ClickhouseConn.Query(context.Background(), `select a.agency_id,count(a.buyer_id) AS buyer_count,count(b.project_id) AS project_count,sum(b.project_money) AS project_amount from information.transaction_info a
  613. inner join information.transaction_info b on (a.agency_id in (`+wh+`) and a.buyer_id<>'' and a.buyer_id=b.buyer_id)
  614. group by a.agency_id`, newArgs...)
  615. if err != nil {
  616. logx.Error(err)
  617. return nil
  618. }
  619. for rows.Next() {
  620. var (
  621. agency_id string
  622. buyer_count uint64
  623. project_count uint64
  624. project_amount decimal.Decimal
  625. )
  626. if err := rows.Scan(&agency_id, &buyer_count, &project_count, &project_amount); err != nil {
  627. logx.Error(err)
  628. continue
  629. }
  630. if vm[agency_id] == nil {
  631. continue
  632. }
  633. vm[agency_id].BuyerCount += int(buyer_count)
  634. vm[agency_id].ProjectCount += int(project_count)
  635. pf, _ := project_amount.Float64()
  636. vm[agency_id].ProjectAmount += pf
  637. }
  638. rows.Close()
  639. if err := rows.Err(); err != nil {
  640. logx.Error(err)
  641. }
  642. return vm
  643. }
  644. //
  645. func (n *network) Introduce_Middleman(values []string) map[string]*projectInfo {
  646. vm := map[string]*projectInfo{}
  647. wh, newArgs := NetworkCom.WhArgs(values)
  648. rows, err := ClickhouseConn.Query(context.Background(), `select project_id,project_money from information.transaction_info where project_id in (`+wh+`)`, newArgs...)
  649. if err != nil {
  650. logx.Error(err)
  651. return nil
  652. }
  653. for rows.Next() {
  654. var (
  655. project_id string
  656. project_money decimal.Decimal
  657. )
  658. if err := rows.Scan(&project_id, &project_money); err != nil {
  659. logx.Error(err)
  660. continue
  661. }
  662. pf, _ := project_money.Float64()
  663. vm[project_id] = &projectInfo{
  664. ProjectAmount: pf,
  665. }
  666. }
  667. rows.Close()
  668. if err := rows.Err(); err != nil {
  669. logx.Error(err)
  670. }
  671. return vm
  672. }
  673. //
  674. func (n *network) TypeConvert(itype string) int {
  675. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  676. switch itype {
  677. case "firstparty":
  678. return 1
  679. case "supplier":
  680. return 2
  681. case "adiffb":
  682. return 3
  683. case "middleman":
  684. return 4
  685. case "agency":
  686. return 5
  687. }
  688. return 0
  689. }