123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package service
- import (
- "context"
- "database/sql"
- "fmt"
- "math"
- "strings"
- "sync"
- . "app.yhyue.com/moapp/jybase/common"
- . "app.yhyue.com/moapp/jybase/date"
- . "app.yhyue.com/moapp/jybase/es"
- . "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- var Network = &network{}
- type network struct {
- }
- type networkTree struct {
- Count int64
- Name string
- Children []*networkTreeChild
- }
- type networkTreeChild struct {
- Count int64
- Name string
- Id string
- Type int
- }
- //人脉库-添加/修改人脉
- func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
- reply := &types.Reply{}
- nowFormat := NowFormat(Date_Full_Layout)
- var saveIntroduce = func(tx *sql.Tx, cid int64, isUpdate bool) bool {
- if in.Type != "middleman" {
- return true
- }
- values := []interface{}{}
- if in.Introduce_owner_id != "" {
- for k, v := range strings.Split(in.Introduce_owner_id, ",") {
- values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_owner_name, ",")[k], 1, nowFormat)
- }
- }
- if in.Introduce_project_id != "" {
- for k, v := range strings.Split(in.Introduce_project_id, ",") {
- values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_project_name, ",")[k], 2, nowFormat)
- }
- }
- if len(values) == 0 {
- return false
- }
- 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
- }
- if in.Id > 0 {
- if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
- 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 {
- reply.Data = map[string]interface{}{
- "status": 0,
- }
- }
- } else {
- itype := n.TypeConvert(in.Type)
- 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,
- }
- } else {
- reply.Data = map[string]interface{}{
- "status": 0,
- }
- }
- }
- return reply
- }
- //人脉库-业主名称联想
- func (n *network) Associate(in *types.AssociateReq) *types.Reply {
- //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
- res := []map[string]interface{}{}
- 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 b.winner,b.winner_id from information.transaction_info a
- inner join information.transaction_info b on (has(a.winner, ?) and a.buyer_id=b.buyer_id and hasAny(b.property_form,[` + wh + `])`
- if in.Name != "" {
- q += ` and b.winner like ?`
- args = append(args, "%"+in.Name+"%")
- }
- q += `) order by b.winner`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- } else {
- for rows.Next() {
- var (
- company_name string
- company_id string
- )
- if err := rows.Scan(&company_name, &company_id); err != nil {
- logx.Error(err)
- continue
- }
- res = append(res, map[string]interface{}{
- "company_name": company_name,
- "company_id": company_id,
- })
- }
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- }
- }
- } 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}}`)
- }
- datas := VarEs.Get("ent_info", "ent_info", fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":10,"_source":["id","company_name"]}`, strings.Join(must, ",")))
- if datas != nil {
- for _, v := range *datas {
- res = append(res, map[string]interface{}{
- "company_name": ObjToString(v["company_name"]),
- "company_id": ObjToString(v["id"]),
- })
- }
- }
- }
- return &types.Reply{
- Data: res,
- }
- }
- //人脉库-全部人脉项目
- 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))
- if in.Id != "" {
- if in.Type == 1 {
- q := `select c.id as company_id,c.company_name,b.name from information.ent_map_code a
- inner join information.ent_code b on (a.a_id=? and b.pcode in ('0100','0200') and a.code=b.code)
- inner join information.ent_info c on (a.b_id=c.id)`
- args := []interface{}{in.Id}
- if in.Name != "" {
- q += ` where c.company_name like ?`
- args = append(args, "%"+in.Name+"%")
- }
- q += ` order by b.name,c.company_name`
- rows, err := ClickhouseConn.Query(context.Background(), q, args...)
- if err != nil {
- logx.Error(err)
- return
- }
- nameIndex := map[string]int{}
- list := []*networkTree{}
- for rows.Next() {
- var (
- company_id string
- company_name string
- name string
- )
- if err := rows.Scan(&company_id, &company_name, &name); err != nil {
- logx.Error(err)
- continue
- }
- if _, ok := nameIndex[name]; !ok {
- nameIndex[name] = len(list)
- list = append(list, &networkTree{
- Name: 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: 1,
- }
- 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()
- list[cIndex].Count += ntc.Count
- list[cIndex].Children = append(list[cIndex].Children, ntc)
- lock.Unlock()
- }(nameIndex[name], company_id, company_name)
- }
- wait.Wait()
- rows.Close()
- if err := rows.Err(); err != nil {
- logx.Error(err)
- }
- }
- } else {
- q := `SELECT a.company_id,a.company_name,b.itype AS TYPE,a.contact_person AS person,a.contact_phone AS phone,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...)
- var count int64
- list := []*networkTree{
- &networkTree{
- Name: "甲方",
- },
- &networkTree{
- Name: "供应商",
- },
- &networkTree{
- Name: "同甲异业渠道",
- },
- &networkTree{
- Name: "中间人",
- },
- &networkTree{
- Name: "招标代理",
- },
- }
- //
- 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
- }
- ntc := &networkTreeChild{
- Name: ObjToString(v["company_name"]),
- Id: ObjToString(v["company_id"]),
- Type: IntAll(v["itype"]),
- }
- 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(b.property_form,[`+wh+`])`, thisArgs)
- } else if itype == 2 || itype == 3 {
- ntc.Count = NetworkCom.Count(`select count(1) from information.transaction_info where winner_id=? and hasAny(b.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(b.property_form,[`+wh+`])`, thisArgs)
- }
- }
- lock.Lock()
- count += ntc.Count
- list[itype].Count += ntc.Count
- list[itype].Children = append(list[itype].Children, ntc)
- lock.Unlock()
- }(vt)
- }
- wait.Wait()
- reply = &types.Reply{
- Data: map[string]interface{}{
- "count": count,
- "list": list,
- },
- }
- }
- return reply
- }
- //人脉库-列表
- func (n *network) List(in *types.NetWorkListReq) *types.Reply {
- q := `select company_id,company_name,itype as type,contact_person as person,contact_phone as phone from crm.connection where position_id=?`
- args := []interface{}{in.PositionId}
- if in.Type != "" {
- q += ` and itype=?`
- args = append(args, n.TypeConvert(in.Type))
- }
- if in.Starttime != "" {
- q += ` and create_time>=?`
- args = append(args, in.Starttime)
- }
- if in.Endtime != "" {
- q += ` and create_time<=?`
- args = append(args, in.Endtime)
- }
- if in.Name != "" {
- q += ` and company_name like ?`
- args = append(args, "%"+in.Name+"%")
- }
- q += ` order by create_time desc`
- list := CrmMysql.SelectBySql(q, args...)
- finalList := []map[string]interface{}{}
- length := int64(len(*list))
- var pageSize int64 = 10
- total_page := int64(math.Ceil(float64(length) / float64(pageSize)))
- firstparty_count, supplier_count, adiffb_count, middleman_count, agency_count := 0, 0, 0, 0, 0
- if length > 0 {
- for _, v := range *list {
- switch Int64All(v["itype"]) {
- case 1:
- firstparty_count++
- case 2:
- supplier_count++
- case 3:
- adiffb_count++
- case 4:
- middleman_count++
- case 5:
- agency_count++
- }
- }
- 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) * pageSize
- end := start + pageSize
- if end > length {
- end = length
- }
- finalList = (*list)[start:end]
- }
- return &types.Reply{
- Data: map[string]interface{}{
- "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) TypeConvert(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
- }
|