network.go 25 KB

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