network.go 27 KB

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