network.go 33 KB

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