network.go 33 KB

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