123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946 |
- package service
- import (
- "context"
- "database/sql"
- "fmt"
- "math"
- "sort"
- "strings"
- "sync"
- "time"
- . "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/date"
- "app.yhyue.com/moapp/jybase/encrypt"
- . "app.yhyue.com/moapp/jybase/es"
- . "app.yhyue.com/moapp/jybase/sort"
- . "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
- "github.com/shopspring/decimal"
- "github.com/zeromicro/go-zero/core/logx"
- )
- var Network = &network{}
- type network struct {
- }
- type networkTree struct {
- Count int64 `json:"count"`
- Name string `json:"name"`
- Children []*networkTreeChild `json:"children"`
- }
- type networkTreeChild struct {
- Count int64 `json:"count"`
- Name string `json:"name"`
- Id string `json:"id"`
- Type string `json:"type"`
- CreateTime string `json:"createTime"`
- }
- func (s *networkTree) Len() int {
- return len(s.Children)
- }
- func (s *networkTree) Less(i, j int) bool {
- if s.Name == "甲方" {
- return s.Children[i].Name < s.Children[j].Name
- }
- it, err1 := time.ParseInLocation(Date_Full_Layout, s.Children[i].CreateTime, time.Local)
- if err1 != nil {
- return true
- }
- jt, err2 := time.ParseInLocation(Date_Full_Layout, s.Children[j].CreateTime, time.Local)
- if err2 != nil {
- return true
- }
- return it.Unix() < jt.Unix()
- }
- func (s *networkTree) Swap(i, j int) {
- s.Children[i], s.Children[j] = s.Children[j], s.Children[i]
- }
- type projectInfo struct {
- BuyerCount int64
- ProjectCount int64
- ProjectAmount float64
- MonitorCount int64
- ExportId []string
- }
- type firstpartyNetwork struct {
- CompanyId string
- CompanyName string
- Name string
- }
- //人脉库-添加/修改人脉
- func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
- reply := &types.Reply{Data: map[string]interface{}{
- "status": 0,
- }}
- if in.Type != "middleman" && in.Company_id == "" {
- return reply
- }
- nowFormat := NowFormat(Date_Full_Layout)
- var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
- if in.Type != "middleman" {
- return true
- } else if in.Introduce_owner_id == "" || in.Introduce_project_id == "" {
- return false
- }
- values := []interface{}{}
- ioi := strings.Split(in.Introduce_owner_id, ",")
- ion := strings.Split(in.Introduce_owner_name, ",")
- if len(ioi) != len(ion) {
- return false
- }
- for k, v := range ioi {
- values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, ion[k], 1, nowFormat)
- }
- ipi := strings.Split(in.Introduce_project_id, ",")
- ipn := strings.Split(in.Introduce_project_name, ",")
- if len(ipi) != len(ipn) {
- return false
- }
- for k, v := range ipi {
- values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, ipn[k], 2, nowFormat)
- }
- var r2 int64
- if isUpdate {
- r2 = CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
- }
- r3, _ := CrmMysql.InsertBatchByTx(tx, "crm.connection_introduce", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "connection_id", "relate_id", "relate_name", "itype", "create_time"}, values)
- return r2 >= 0 && r3 > 0
- }
- itype := n.TypeStrConvert(in.Type)
- if in.Id > 0 {
- if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
- if in.Company_id != "" {
- 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)
- if count == -1 {
- return false
- } else if count > 0 {
- reply.Data = map[string]interface{}{
- "status": -1,
- }
- return false
- }
- }
- 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)
- return r1 >= 0 && saveIntroduce(tx, in.Id, true)
- }) {
- reply.Data = map[string]interface{}{
- "status": 1,
- }
- }
- } else {
- if in.Company_id != "" {
- 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)
- if count == -1 {
- return reply
- } else if count > 0 {
- reply.Data = map[string]interface{}{
- "status": -1,
- }
- return reply
- }
- }
- var r1 int64
- if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
- _, r1 = CrmMysql.InsertBatchByTx(tx, "crm.connection", []string{"position_id", "ent_id", "ent_dept_id", "ent_user_id", "itype", "company_name", "company_id", "contact_person", "contact_phone", "status", "create_time", "update_time"}, []interface{}{in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, itype, in.Company_name, in.Company_id, in.Contact_person, in.Contact_phone, 1, nowFormat, nowFormat})
- return r1 > 0 && saveIntroduce(tx, r1, false)
- }) {
- reply.Data = map[string]interface{}{
- "status": 1,
- "id": r1,
- }
- }
- }
- return reply
- }
- //人脉库-业主名称联想
- func (n *network) Associate(in *types.AssociateReq) (reply *types.Reply) {
- //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
- res := []map[string]interface{}{}
- reply = &types.Reply{Data: res}
- in.Name = strings.TrimSpace(in.Name)
- if in.Name == "" {
- return
- }
- pageSize := 10
- if in.Type == "adiffb" {
- probusfors := NetworkCom.GetMyProbusfor(in.EntAccountId)
- if len(probusfors) > 0 {
- args := []interface{}{in.EntName}
- wh, newArgs := NetworkCom.WhArgs(probusfors)
- args = append(args, newArgs...)
- q := `select DISTINCT b.winner_id,b.winner from information.transaction_info a
- inner join information.transaction_info b on (has(a.winner, ?) and a.buyer_id<>'' and a.buyer_id=b.buyer_id and hasAny(b.property_form,[` + wh + `])=0) ORDER BY b.project_id`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- } else {
- repeat := map[string]bool{}
- for rows.Next() {
- var (
- winner_id []string
- winner []string
- )
- if err := rows.Scan(&winner_id, &winner); err != nil {
- logx.Error(err)
- continue
- }
- for k, v := range winner {
- if repeat[v] || !strings.Contains(v, in.Name) {
- continue
- }
- repeat[v] = true
- if k >= len(winner_id) {
- continue
- }
- res = append(res, map[string]interface{}{
- "company_name": v,
- "company_id": winner_id[k],
- })
- }
- }
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- if len(res) > pageSize {
- res = res[:pageSize]
- }
- }
- }
- } else {
- must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
- switch in.Type {
- case "firstparty":
- must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
- must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
- case "supplier":
- must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
- must = append(must, `{"term":{"company_unit_type":3}}`)
- case "middleman_owner":
- must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
- case "agency":
- must = append(must, `{"term":{"company_unit_type":4}}`)
- }
- q := fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":%d,"_source":["id","company_name"]}`, strings.Join(must, ","), pageSize)
- logx.Info("人脉库-业主名称联想", q)
- datas := VarEs.Get("ent_info", "ent_info", q)
- if datas != nil {
- for _, v := range *datas {
- res = append(res, map[string]interface{}{
- "company_name": ObjToString(v["company_name"]),
- "company_id": ObjToString(v["id"]),
- })
- }
- }
- }
- reply.Data = res
- return
- }
- //人脉库-全部人脉项目
- func (n *network) AllProject(in *types.AllprojectReq) (reply *types.Reply) {
- pool := make(chan bool, 5)
- wait := &sync.WaitGroup{}
- lock := &sync.Mutex{}
- reply = &types.Reply{}
- wh, newArgs := NetworkCom.WhArgs(NetworkCom.GetMyProbusfor(in.EntAccountId))
- var count int64
- var list []*networkTree
- if in.Id != "" {
- if in.Type == 1 {
- result := n.FirstpartyNetwork(in.Name, []string{in.Id})
- if result[in.Id] != nil {
- nameIndex := map[string]int{}
- for _, v := range result[in.Id] {
- if _, ok := nameIndex[v.Name]; !ok {
- nameIndex[v.Name] = len(list)
- list = append(list, &networkTree{
- Name: v.Name,
- })
- }
- pool <- true
- wait.Add(1)
- go func(cIndex int, cId, cName string) {
- defer func() {
- <-pool
- wait.Done()
- }()
- ntc := &networkTreeChild{
- Name: cName,
- Id: cId,
- Type: "firstparty",
- }
- if wh != "" {
- thisArgs := []interface{}{ntc.Id}
- thisArgs = append(thisArgs, newArgs...)
- ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
- }
- lock.Lock()
- count++
- list[cIndex].Count += ntc.Count
- list[cIndex].Children = append(list[cIndex].Children, ntc)
- lock.Unlock()
- }(nameIndex[v.Name], v.CompanyId, v.CompanyName)
- }
- wait.Wait()
- }
- }
- } else {
- q := `SELECT a.company_id,a.company_name,a.itype,a.create_time,COUNT(b.id) AS ipc FROM crm.connection a
- LEFT JOIN crm.connection_introduce b ON (b.position_id=? AND b.itype=2 AND a.id=b.connection_id) WHERE a.position_id=?`
- args := []interface{}{in.PositionId, in.PositionId}
- if in.Name != "" {
- q += ` and company_name like ?`
- args = append(args, "%"+in.Name+"%")
- }
- q += ` GROUP BY a.id ORDER BY a.create_time DESC`
- datas := CrmMysql.SelectBySql(q, args...)
- list = []*networkTree{
- &networkTree{
- Name: "甲方",
- Children: []*networkTreeChild{},
- },
- &networkTree{
- Name: "供应商",
- Children: []*networkTreeChild{},
- },
- &networkTree{
- Name: "同甲异业渠道",
- Children: []*networkTreeChild{},
- },
- &networkTree{
- Name: "中间人",
- Children: []*networkTreeChild{},
- },
- &networkTree{
- Name: "招标代理",
- Children: []*networkTreeChild{},
- },
- }
- //
- for _, vt := range *datas {
- pool <- true
- wait.Add(1)
- go func(v map[string]interface{}) {
- defer func() {
- <-pool
- wait.Done()
- }()
- itype := IntAll(v["itype"])
- if itype <= 0 || itype > len(list) {
- return
- }
- company_name := ObjToString(v["company_name"])
- if company_name == "" {
- company_name = "未填写"
- }
- ntc := &networkTreeChild{
- Name: company_name,
- Id: ObjToString(v["company_id"]),
- Type: n.TypeIntConvert(Int64All(v["itype"])),
- CreateTime: ObjToString(v["create_time"]),
- }
- if wh != "" {
- thisArgs := []interface{}{ntc.Id}
- thisArgs = append(thisArgs, newArgs...)
- if itype == 1 {
- ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where buyer_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
- } else if itype == 2 || itype == 3 {
- ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where has(winner_id,?) and hasAny(property_form,[`+wh+`])`, thisArgs...)
- } else if itype == 4 {
- ntc.Count = Int64All(v["ipc"])
- } else if itype == 5 {
- ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where agency_id=? and hasAny(property_form,[`+wh+`])`, thisArgs...)
- }
- }
- lock.Lock()
- count += ntc.Count
- list[itype-1].Count += ntc.Count
- list[itype-1].Children = append(list[itype-1].Children, ntc)
- lock.Unlock()
- }(vt)
- }
- wait.Wait()
- }
- convList := []map[string]interface{}{}
- for _, v := range list {
- pm := map[string]interface{}{
- "CODE": v.Name,
- "NAME": v.Name,
- "SZ_PID0": v.Name,
- "SZ_LEVEL": 0,
- "datacount": 0,
- }
- if len(v.Children) > 0 {
- pm["SZ_LEAF"] = 0
- } else {
- pm["SZ_LEAF"] = 1
- }
- ID := ""
- pType := ""
- sort.Sort(v)
- for _, vv := range v.Children {
- if ID != "" {
- ID += ","
- }
- if pType != "" {
- pType += ","
- }
- ID += vv.Id
- pType += vv.Type
- cm := map[string]interface{}{
- "NAME": v.Name,
- "ID": vv.Id,
- "SZ_PID0": v.Name,
- "SZ_PID1": v.Name + ":" + vv.Id,
- "CODE": v.Name + ":" + vv.Id,
- "PCODE": v.Name,
- "datacount": vv.Count,
- "SZ_LEVEL": 1,
- "SZ_LEAF": 1,
- "type": vv.Type,
- }
- convList = append(convList, cm)
- }
- pm["ID"] = ID
- pm["type"] = pType
- convList = append(convList, pm)
- }
- reply = &types.Reply{
- Data: map[string]interface{}{
- "count": count,
- "list": convList,
- },
- }
- return reply
- }
- //人脉库-列表
- func (n *network) List(in *types.NetWorkListReq) *types.Reply {
- q := `select a.company_id,a.company_name,a.itype,a.contact_person as person,a.contact_phone as phone,count(DISTINCT if(b.itype=1,b.relate_id,null)) as buyer_count,count(DISTINCT if(b.itype=2,b.relate_id,null)) as project_count,GROUP_CONCAT(IF(b.itype=1,b.relate_id,NULL)) AS relate_buyer_id,GROUP_CONCAT(IF(b.itype=2,b.relate_id,NULL)) AS relate_project_id,a.create_time from crm.connection a
- left join crm.connection_introduce b on (a.id=b.connection_id) where a.position_id=?`
- args := []interface{}{in.PositionId}
- if in.Type != "" {
- q += ` and a.itype=?`
- args = append(args, n.TypeStrConvert(in.Type))
- }
- if in.Starttime != "" {
- q += ` and a.create_time>=?`
- args = append(args, in.Starttime+" 00:00:00")
- }
- if in.Endtime != "" {
- q += ` and a.create_time<=?`
- if in.Starttime == in.Endtime {
- in.Endtime += " 23:59:59"
- } else {
- in.Endtime += " 00:00:00"
- }
- args = append(args, in.Endtime)
- }
- if in.Name != "" {
- q += ` and a.company_name like ?`
- args = append(args, "%"+in.Name+"%")
- }
- q += ` GROUP BY a.id order by a.create_time desc`
- listTemp := CrmMysql.SelectBySql(q, args...)
- firstparty_array, supplier_array, adiffb_array, agency_array, middleman_project_array := []string{}, []string{}, []string{}, []string{}, []string{}
- for _, v := range *listTemp {
- switch Int64All(v["itype"]) {
- case 1:
- firstparty_array = append(firstparty_array, ObjToString(v["company_id"]))
- case 2:
- supplier_array = append(supplier_array, ObjToString(v["company_id"]))
- case 3:
- adiffb_array = append(adiffb_array, ObjToString(v["company_id"]))
- case 4:
- if relate_project_id := ObjToString(v["relate_project_id"]); relate_project_id != "" {
- middleman_project_array = append(middleman_project_array, strings.Split(relate_project_id, ",")...)
- }
- case 5:
- agency_array = append(agency_array, ObjToString(v["company_id"]))
- }
- }
- //
- firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
- list := []*map[string]interface{}{}
- isGoNextSetp := true
- probusfors := []string{}
- if in.Project_matchme == 1 {
- probusfors = NetworkCom.GetMyProbusfor(in.EntAccountId)
- if len(probusfors) == 0 {
- isGoNextSetp = false
- }
- }
- if isGoNextSetp {
- entMonitor := NetworkCom.EntMonitor(in.UserId)
- fpn := n.FirstpartyNetwork("", firstparty_array)
- firstparty_project := n.Introduce_Firstparty(fpn, entMonitor, in.Project_matchme, probusfors)
- supplier_project := n.Introduce_Supplier(supplier_array, entMonitor, in.Project_matchme, probusfors)
- adiffb_project := n.Introduce_Supplier(adiffb_array, entMonitor, in.Project_matchme, probusfors)
- agency_project := n.Introduce_Agency(agency_array, entMonitor, in.Project_matchme, probusfors)
- middleman_project := n.Introduce_Middleman(middleman_project_array, entMonitor, in.Project_matchme, probusfors)
- for _, v := range *listTemp {
- itype := ""
- buyer_count, project_count, expect_amount, monitor_count := int64(0), int64(0), float64(0), int64(0)
- company_id := ObjToString(v["company_id"])
- export_id := []string{}
- jump_type, jump_id := "", ""
- switch Int64All(v["itype"]) {
- case 1:
- itype = "甲方"
- jump_type = "firstparty"
- for _, vv := range fpn[company_id] {
- if jump_id != "" {
- jump_id += ","
- }
- jump_id += vv.CompanyId
- }
- firstparty_count++
- if firstparty_project[company_id] != nil {
- buyer_count = firstparty_project[company_id].BuyerCount
- project_count = firstparty_project[company_id].ProjectCount
- expect_amount = firstparty_project[company_id].ProjectAmount
- monitor_count = firstparty_project[company_id].MonitorCount
- export_id = firstparty_project[company_id].ExportId
- }
- case 2:
- itype = "供应商"
- jump_type = "supplier"
- jump_id = company_id
- supplier_count++
- if supplier_project[company_id] != nil {
- buyer_count = supplier_project[company_id].BuyerCount
- project_count = supplier_project[company_id].ProjectCount
- expect_amount = supplier_project[company_id].ProjectAmount
- monitor_count = supplier_project[company_id].MonitorCount
- export_id = supplier_project[company_id].ExportId
- }
- case 3:
- itype = "同甲异业渠道"
- jump_type = "adiffb"
- jump_id = company_id
- adiffb_count++
- if adiffb_project[company_id] != nil {
- buyer_count = adiffb_project[company_id].BuyerCount
- project_count = adiffb_project[company_id].ProjectCount
- expect_amount = adiffb_project[company_id].ProjectAmount
- monitor_count = adiffb_project[company_id].MonitorCount
- export_id = adiffb_project[company_id].ExportId
- }
- case 4:
- itype = "中间人"
- jump_type = "middleman"
- jump_id = company_id
- middleman_count++
- buyer_count = Int64All(v["buyer_count"])
- project_count = Int64All(v["project_count"])
- if relate_buyer_id := ObjToString(v["relate_buyer_id"]); relate_buyer_id != "" {
- for _, v := range strings.Split(relate_buyer_id, ",") {
- if v == "" {
- continue
- }
- if entMonitor[v] {
- monitor_count++
- }
- }
- }
- if relate_project_id := ObjToString(v["relate_project_id"]); relate_project_id != "" {
- export_id = strings.Split(relate_project_id, ",")
- for _, v := range export_id {
- if middleman_project[v] != nil {
- expect_amount += middleman_project[v].ProjectAmount
- }
- }
- }
- case 5:
- itype = "招标代理机构"
- jump_type = "agency"
- jump_id = company_id
- agency_count++
- if agency_project[company_id] != nil {
- buyer_count = agency_project[company_id].BuyerCount
- project_count = agency_project[company_id].ProjectCount
- expect_amount = agency_project[company_id].ProjectAmount
- monitor_count = agency_project[company_id].MonitorCount
- export_id = agency_project[company_id].ExportId
- }
- }
- if buyer_count < in.Buyercount_start {
- continue
- } else if in.Buyercount_end > 0 && buyer_count > in.Buyercount_end {
- continue
- } else if in.Monitor == 1 && monitor_count <= 0 {
- continue
- } else if in.Monitor == -1 && monitor_count > 0 {
- continue
- } else if monitor_count < in.Monitorcount_start {
- continue
- } else if in.Monitorcount_end > 0 && monitor_count > in.Monitorcount_end {
- continue
- } else if in.Project_matchme == 1 && project_count == 0 {
- continue
- }
- export_url := ""
- if len(export_id) > 0 {
- export_url = "/subscribepay/network/projectExport?export_id=" + encrypt.SE.EncodeStringByCheck(strings.Join(export_id, ","))
- }
- company_name, _ := v["company_name"].(string)
- url := "/swordfish/page_big_pc/unit_portrayal/" + company_name
- if company_name == "" {
- company_name = "未填写"
- url = ""
- }
- list = append(list, &map[string]interface{}{
- "company_id": company_id,
- "company_name": company_name,
- "type": itype,
- "jump_type": jump_type,
- "jump_id": jump_id,
- "person": v["person"],
- "phone": v["phone"],
- "buyer_count": buyer_count,
- "monitor_count": 0,
- "expect_amount": RetainDecimal(expect_amount/10000, 2),
- "project_count": project_count,
- "create_time": v["create_time"],
- "export_url": export_url,
- "url": url,
- })
- }
- }
- csList := &ComSortList{
- SortKeys: []*ComSortKey{
- &ComSortKey{
- Keys: []string{"expect_amount"},
- Order: 1,
- Type: "float",
- },
- },
- List: list,
- }
- if in.Order_amount == -1 {
- csList.SortKeys[0].Order = -1
- }
- if in.Order_amount != 0 {
- sort.Sort(csList)
- }
- length := int64(len(csList.List))
- if in.Page_size <= 0 {
- in.Page_size = 10
- }
- total_page := int64(math.Ceil(float64(length) / float64(in.Page_size)))
- finalList := []*map[string]interface{}{}
- if length > 0 {
- if in.Current_page <= 0 {
- in.Current_page = 1
- }
- if in.Current_page > total_page {
- in.Current_page = total_page
- }
- start := (in.Current_page - 1) * in.Page_size
- end := start + in.Page_size
- if end > length {
- end = length
- }
- finalList = csList.List[start:end]
- }
- return &types.Reply{
- Data: map[string]interface{}{
- "count": length,
- "total_page": total_page,
- "firstparty_count": firstparty_count,
- "supplier_count": supplier_count,
- "adiffb_count": adiffb_count,
- "middleman_count": middleman_count,
- "agency_count": agency_count,
- "list": finalList,
- },
- }
- }
- //
- func (n *network) FirstpartyNetwork(name string, values []string) map[string][]*firstpartyNetwork {
- result := map[string][]*firstpartyNetwork{}
- wh, args := NetworkCom.WhArgs(values)
- 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
- inner join information.ent_code b on (a.a_id in (` + wh + `) and b.pcode in ('0100','0200') and a.code=b.code)`
- if name != "" {
- q += ` where c.company_name like ?`
- args = append(args, "%"+name+"%")
- }
- q += ` order by b.name,a.a_id,a.b_name`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- return result
- }
- for rows.Next() {
- var (
- a_id string
- company_id string
- company_name string
- name string
- )
- if err := rows.Scan(&a_id, &company_id, &company_name, &name); err != nil {
- logx.Error(err)
- continue
- }
- result[a_id] = append(result[a_id], &firstpartyNetwork{
- CompanyId: company_id,
- CompanyName: company_name,
- Name: name,
- })
- }
- return result
- }
- //
- func (n *network) Introduce_Firstparty(fpn map[string][]*firstpartyNetwork, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
- values := []string{}
- vm := map[string]*projectInfo{}
- for _, v := range fpn {
- for _, vv := range v {
- vm[vv.CompanyId] = &projectInfo{}
- values = append(values, vv.CompanyId)
- }
- }
- wh, args := NetworkCom.WhArgs(values)
- 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 + `)`
- if matchme == 1 {
- newWh, newArgs := NetworkCom.WhArgs(probusfors)
- q += ` hasAny(property_form,[` + newWh + `])`
- args = append(args, newArgs...)
- }
- q += ` group by buyer_id`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- return nil
- }
- for rows.Next() {
- var (
- buyer_id string
- project_count uint64
- project_amount decimal.Decimal
- project_id []string
- )
- if err := rows.Scan(&buyer_id, &project_count, &project_amount, &project_id); err != nil {
- logx.Error(err)
- continue
- }
- if vm[buyer_id] == nil {
- continue
- }
- vm[buyer_id].ProjectCount += int64(project_count)
- pf, _ := project_amount.Float64()
- vm[buyer_id].ProjectAmount += pf
- vm[buyer_id].ExportId = project_id
- }
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- result := map[string]*projectInfo{}
- for k, v := range fpn {
- if result[k] == nil {
- result[k] = &projectInfo{}
- }
- result[k].BuyerCount = int64(len(v))
- for _, vv := range v {
- if entMonitor[vv.CompanyName] {
- result[k].MonitorCount++
- }
- if vm[vv.CompanyId] == nil {
- continue
- }
- result[k].ProjectCount += vm[vv.CompanyId].ProjectCount
- result[k].ProjectAmount += vm[vv.CompanyId].ProjectAmount
- }
- }
- return result
- }
- //
- func (n *network) Introduce_Supplier(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
- vm := map[string]*projectInfo{}
- for _, v := range values {
- vm[v] = &projectInfo{}
- }
- wh, args := NetworkCom.WhArgs(values)
- 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
- inner join information.transaction_info b on (hasAny(a.winner_id,[` + wh + `]) and a.buyer_id<>'' and a.buyer_id=b.buyer_id`
- if matchme == 1 {
- newWh, newArgs := NetworkCom.WhArgs(probusfors)
- q += ` hasAny(b.property_form,[` + newWh + `])`
- args = append(args, newArgs...)
- }
- q += `) group by a.winner_id`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- return nil
- }
- for rows.Next() {
- var (
- winner_id []string
- buyer_count uint64
- project_count uint64
- project_amount decimal.Decimal
- buyers []string
- project_id []string
- )
- if err := rows.Scan(&winner_id, &buyer_count, &project_count, &project_amount, &buyers, &project_id); err != nil {
- logx.Error(err)
- continue
- }
- for _, v := range winner_id {
- if vm[v] == nil {
- continue
- }
- vm[v].BuyerCount += int64(buyer_count)
- vm[v].ProjectCount += int64(project_count)
- pf, _ := project_amount.Float64()
- vm[v].ProjectAmount += pf
- vm[v].ExportId = project_id
- for _, v := range buyers {
- if entMonitor[v] {
- vm[v].MonitorCount++
- }
- }
- }
- }
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- return vm
- }
- //
- func (n *network) Introduce_Agency(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
- vm := map[string]*projectInfo{}
- for _, v := range values {
- vm[v] = &projectInfo{}
- }
- wh, args := NetworkCom.WhArgs(values)
- 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
- inner join information.transaction_info b on (a.agency_id in (` + wh + `) and a.buyer_id<>'' and a.buyer_id=b.buyer_id`
- if matchme == 1 {
- newWh, newArgs := NetworkCom.WhArgs(probusfors)
- q += ` hasAny(b.property_form,[` + newWh + `])`
- args = append(args, newArgs...)
- }
- q += `) group by a.agency_id`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- return nil
- }
- for rows.Next() {
- var (
- agency_id string
- buyer_count uint64
- project_count uint64
- project_amount decimal.Decimal
- buyers []string
- project_id []string
- )
- if err := rows.Scan(&agency_id, &buyer_count, &project_count, &project_amount, &buyers, &project_id); err != nil {
- logx.Error(err)
- continue
- }
- if vm[agency_id] == nil {
- continue
- }
- vm[agency_id].BuyerCount += int64(buyer_count)
- vm[agency_id].ProjectCount += int64(project_count)
- pf, _ := project_amount.Float64()
- vm[agency_id].ProjectAmount += pf
- vm[agency_id].ExportId = project_id
- for _, v := range buyers {
- if entMonitor[v] {
- vm[agency_id].MonitorCount++
- }
- }
- }
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- return vm
- }
- //
- func (n *network) Introduce_Middleman(values []string, entMonitor map[string]bool, matchme int64, probusfors []string) map[string]*projectInfo {
- vm := map[string]*projectInfo{}
- wh, newArgs := NetworkCom.WhArgs(values)
- rows, err := ClickhouseConn.Query(context.Background(), `select project_id,project_money from information.transaction_info where project_id in (`+wh+`)`, newArgs...)
- if err != nil {
- logx.Error(err)
- return nil
- }
- for rows.Next() {
- var (
- project_id string
- project_money decimal.Decimal
- )
- if err := rows.Scan(&project_id, &project_money); err != nil {
- logx.Error(err)
- continue
- }
- pf, _ := project_money.Float64()
- vm[project_id] = &projectInfo{
- ProjectAmount: pf,
- }
- }
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- return vm
- }
- //
- func (n *network) TypeStrConvert(itype string) int {
- //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
- switch itype {
- case "firstparty":
- return 1
- case "supplier":
- return 2
- case "adiffb":
- return 3
- case "middleman":
- return 4
- case "agency":
- return 5
- }
- return 0
- }
- //
- func (n *network) TypeIntConvert(itype int64) string {
- //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
- switch itype {
- case 1:
- return "firstparty"
- case 2:
- return "supplier"
- case 3:
- return "adiffb"
- case 4:
- return "middleman"
- case 5:
- return "agency"
- }
- return ""
- }
|