network.go 29 KB

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