network.go 30 KB

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