network.go 28 KB

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