network.go 26 KB

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