util.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  6. "github.com/gogf/gf/v2/util/gconv"
  7. )
  8. //CanAdd 是否允许多人针对一个创建
  9. func CanAdd(entId int64, key string, employInfoId, employCustomId, positionId int64) (status int64, error_msg string) {
  10. accountData := cm.BaseMysql.SelectBySql(`select * from base_service.base_account where ent_id =? and person_id =0 and type =1 limit 1`, entId)
  11. if accountData == nil || len(*accountData) <= 0 {
  12. return 1, ""
  13. }
  14. accountId := (*accountData)[0]["id"]
  15. //
  16. tenantData := cm.BaseMysql.SelectBySql(`select * from crm.config_tenant where account_id =?`, accountId)
  17. //不允许
  18. if (len(*tenantData) == 0) || (len(*tenantData) > 0 && gconv.Int((*tenantData)[0][key]) == 0) {
  19. employInfoIdArr := []int64{}
  20. if employInfoId > 0 {
  21. employ_info := cm.BaseMysql.SelectBySql(`select id from crm.employ_info where source_id =(SELECT source_id FROM crm.employ_info WHERE id =?) and status=1`, employInfoId)
  22. if employ_info != nil && len(*employ_info) > 0 {
  23. for _, v := range *employ_info {
  24. employInfoIdArr = append(employInfoIdArr, gconv.Int64(v["id"]))
  25. }
  26. } else {
  27. employInfoIdArr = []int64{employInfoId}
  28. }
  29. } else {
  30. employInfoIdArr = []int64{employInfoId}
  31. }
  32. args, ws := GetInForCommaArr(employInfoIdArr)
  33. args = append(args, entId)
  34. //判断是否已经创建
  35. if strings.Contains(key, "clue") {
  36. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.sale_clue where employ_info_id in( `+ws+`) and ent_id =? and is_close=0`, args...)
  37. if d != nil && len(*d) > 0 {
  38. for _, v := range *d {
  39. position_id := gconv.Int64(v["position_id"])
  40. if positionId == position_id {
  41. return 1, ""
  42. }
  43. return -1, fmt.Sprintf("%v已经基于该咨询创建了线索", v["create_person"])
  44. }
  45. }
  46. } else if strings.Contains(key, "chance") {
  47. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.sale_chance where employ_info_id in ( `+ws+`) and ent_id =? `, args...)
  48. if d != nil && len(*d) > 0 {
  49. for _, v := range *d {
  50. position_id := gconv.Int64(v["position_id"])
  51. if positionId == position_id {
  52. return 1, ""
  53. }
  54. return -1, fmt.Sprintf("%v已经基于该咨询创建了机会", v["create_person"])
  55. }
  56. }
  57. } else if strings.Contains(key, "custom") {
  58. if employInfoId > 0 {
  59. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.custom where employ_info_id in( `+ws+`) and ent_id =? `, args...)
  60. if d != nil && len(*d) > 0 {
  61. for _, v := range *d {
  62. position_id := gconv.Int64(v["position_id"])
  63. if positionId == position_id {
  64. return 1, ""
  65. }
  66. return -1, fmt.Sprintf("%v已经基于该咨询创建了客户", v["create_person"])
  67. }
  68. }
  69. } else if employCustomId > 0 {
  70. employCustomIdArr := []int64{}
  71. employ_info := cm.BaseMysql.SelectBySql(`select id from crm.employ_custom where company_id =(SELECT company_id FROM crm.employ_custom WHERE id =?)`, employCustomId)
  72. if employ_info != nil && len(*employ_info) > 0 {
  73. for _, v := range *employ_info {
  74. employCustomIdArr = append(employCustomIdArr, gconv.Int64(v["id"]))
  75. }
  76. } else {
  77. employCustomIdArr = []int64{employCustomId}
  78. }
  79. args, ws := GetInForCommaArr(employCustomIdArr)
  80. args = append(args, entId)
  81. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.custom where employ_custom_id in( `+ws+`) and ent_id =? `, args...)
  82. if d != nil && len(*d) > 0 {
  83. for _, v := range *d {
  84. position_id := gconv.Int64(v["position_id"])
  85. if positionId == position_id {
  86. return 1, ""
  87. }
  88. return -1, fmt.Sprintf("%v已经基于该候选客户创建了客户", v["create_person"])
  89. }
  90. }
  91. }
  92. }
  93. }
  94. return 1, ""
  95. }
  96. //获取sql语句的in操作相关参数
  97. func GetInForCommaArr(ids []int64) ([]interface{}, string) {
  98. args := []interface{}{}
  99. ws := []string{}
  100. for _, v := range ids {
  101. args = append(args, v)
  102. ws = append(ws, "?")
  103. }
  104. return args, strings.Join(ws, ",")
  105. }