network.go 29 KB

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