network.go 27 KB

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