network.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package service
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strings"
  6. . "app.yhyue.com/moapp/jybase/common"
  7. . "app.yhyue.com/moapp/jybase/date"
  8. . "app.yhyue.com/moapp/jybase/es"
  9. . "bp.jydev.jianyu360.cn/CRM/application/api/common"
  10. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  11. )
  12. var Network = &network{}
  13. type network struct {
  14. }
  15. //人脉库-添加/修改人脉
  16. func (n *network) AddOrUpdate(in *types.AddOrUpdateReq) *types.Reply {
  17. reply := &types.Reply{}
  18. itype := 0
  19. //firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 agency:招标代理机构
  20. switch in.Type {
  21. case "firstparty":
  22. itype = 1
  23. case "supplier:供应商":
  24. itype = 2
  25. case "adiffb":
  26. itype = 3
  27. case "middleman":
  28. itype = 4
  29. case "agency":
  30. itype = 5
  31. }
  32. nowFormat := NowFormat(Date_Full_Layout)
  33. var saveIntroduce = func(tx *sql.Tx, cid int64) int64 {
  34. values := []interface{}{}
  35. if in.Introduce_owner_id != "" {
  36. for k, v := range strings.Split(in.Introduce_owner_id, ",") {
  37. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_owner_name, ",")[k], 1, nowFormat)
  38. }
  39. }
  40. if in.Introduce_project_id != "" {
  41. for k, v := range strings.Split(in.Introduce_project_id, ",") {
  42. values = append(values, in.PositionId, in.EntId, in.EntDeptId, in.EntUserId, cid, v, strings.Split(in.Introduce_project_name, ",")[k], 2, nowFormat)
  43. }
  44. }
  45. var r3 int64
  46. if len(values) > 0 {
  47. _, 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)
  48. }
  49. return r3
  50. }
  51. if in.Id > 0 {
  52. if CrmMysql.ExecTx("更新人脉", func(tx *sql.Tx) bool {
  53. 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)
  54. r2 := CrmMysql.UpdateOrDeleteBySqlByTx(tx, `delete from crm.connection_introduce where connection_id=? and position_id=?`, in.Id, in.PositionId)
  55. return r1 > 0 && r2 >= 0 && saveIntroduce(tx, in.Id) >= 0
  56. }) {
  57. reply.Data = map[string]interface{}{
  58. "status": 1,
  59. }
  60. } else {
  61. reply.Data = map[string]interface{}{
  62. "status": 0,
  63. }
  64. }
  65. } else {
  66. id := 0
  67. if CrmMysql.ExecTx("新增人脉", func(tx *sql.Tx) bool {
  68. _, 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})
  69. return r1 > 0 && saveIntroduce(tx, r1) >= 0
  70. }) {
  71. reply.Data = map[string]interface{}{
  72. "status": 1,
  73. "id": id,
  74. }
  75. } else {
  76. reply.Data = map[string]interface{}{
  77. "status": 0,
  78. }
  79. }
  80. }
  81. return reply
  82. }
  83. //人脉库-业主名称联想
  84. func (n *network) Associate(in *types.AssociateReq) *types.Reply {
  85. //类型;firstparty:甲方 supplier:供应商 adiffb:同甲异业 middleman:中间人 middleman_owner:中间人-业主 middleman_project:中间人-项目 agency:招标代理机构
  86. res := []map[string]interface{}{}
  87. must := []string{fmt.Sprintf(`{"multi_match":{"query":"%s","type":"phrase","fields":["company_name"]}}`, in.Name)}
  88. switch in.Type {
  89. case "firstparty":
  90. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  91. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  92. case "supplier":
  93. must = append(must, fmt.Sprintf(`{"terms":{"company_label":["%s"]}}`, strings.Join(NetworkCom.GetEntTagSeat(2), `","`)))
  94. must = append(must, `{"term":{"company_unit_type":3}}`)
  95. case "middleman_owner":
  96. must = append(must, `{"terms":{"company_unit_type":[1,2]}}`)
  97. case "agency":
  98. must = append(must, `{"term":{"company_unit_type":4}}`)
  99. }
  100. datas := VarEs.Get("ent_info", "ent_info", fmt.Sprintf(`{"query":{"bool":{"must":[%s]}},"size":10,"_source":["id","company_name"]}`, strings.Join(must, ",")))
  101. if datas != nil {
  102. for _, v := range *datas {
  103. res = append(res, map[string]interface{}{
  104. "company_name": ObjToString(v["company_name"]),
  105. "company_id": ObjToString(v["id"]),
  106. })
  107. }
  108. }
  109. return &types.Reply{
  110. Data: datas,
  111. }
  112. }
  113. //人脉库-全部人脉项目
  114. func (n *network) Allproject(in *types.AllprojectReq) *types.Reply {
  115. return nil
  116. }
  117. //人脉库-列表
  118. func (n *network) List(in *types.NetWorkListReq) *types.Reply {
  119. return nil
  120. }