util.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. if employInfoId <= 0 && employCustomId <= 0 {
  11. return 1, ""
  12. }
  13. accountData := cm.BaseMysql.SelectBySql(`select * from base_service.base_account where ent_id =? and person_id =0 and type =1 limit 1`, entId)
  14. if accountData == nil || len(*accountData) <= 0 {
  15. return 1, ""
  16. }
  17. accountId := (*accountData)[0]["id"]
  18. //
  19. tenantData := cm.BaseMysql.SelectBySql(`select * from crm.config_tenant where account_id =?`, accountId)
  20. //不允许
  21. if (len(*tenantData) == 0) || (len(*tenantData) > 0 && gconv.Int((*tenantData)[0][key]) == 0) {
  22. employInfoIdArr := []int64{}
  23. if employInfoId > 0 {
  24. 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)
  25. if employ_info != nil && len(*employ_info) > 0 {
  26. for _, v := range *employ_info {
  27. employInfoIdArr = append(employInfoIdArr, gconv.Int64(v["id"]))
  28. }
  29. } else {
  30. employInfoIdArr = []int64{employInfoId}
  31. }
  32. } else {
  33. employInfoIdArr = []int64{employInfoId}
  34. }
  35. args, ws := GetInForCommaArr(employInfoIdArr)
  36. args = append(args, entId)
  37. //判断是否已经创建
  38. if strings.Contains(key, "clue") {
  39. if hasZero(employInfoIdArr) {
  40. return 1, ""
  41. }
  42. 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...)
  43. if d != nil && len(*d) > 0 {
  44. for _, v := range *d {
  45. position_id := gconv.Int64(v["position_id"])
  46. if positionId == position_id {
  47. return 1, ""
  48. }
  49. return -1, fmt.Sprintf("%v已经基于该资讯创建了线索", v["create_person"])
  50. }
  51. }
  52. } else if strings.Contains(key, "chance") {
  53. if hasZero(employInfoIdArr) {
  54. return 1, ""
  55. }
  56. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.sale_chance where employ_info_id in ( `+ws+`) and ent_id =? `, args...)
  57. if d != nil && len(*d) > 0 {
  58. for _, v := range *d {
  59. position_id := gconv.Int64(v["position_id"])
  60. if positionId == position_id {
  61. return 1, ""
  62. }
  63. return -1, fmt.Sprintf("%v已经基于该资讯创建了机会", v["create_person"])
  64. }
  65. }
  66. } else if strings.Contains(key, "custom") {
  67. if employInfoId > 0 {
  68. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.custom where employ_info_id in( `+ws+`) and ent_id =? `, args...)
  69. if d != nil && len(*d) > 0 {
  70. for _, v := range *d {
  71. position_id := gconv.Int64(v["position_id"])
  72. if positionId == position_id {
  73. return 1, ""
  74. }
  75. return -1, fmt.Sprintf("%v已经基于该资讯创建了客户", v["create_person"])
  76. }
  77. }
  78. } else if employCustomId > 0 {
  79. employCustomIdArr := []int64{}
  80. employ_info := cm.BaseMysql.SelectBySql(`select id from crm.employ_custom where company_id =(SELECT company_id FROM crm.employ_custom WHERE id =?)`, employCustomId)
  81. if employ_info != nil && len(*employ_info) > 0 {
  82. for _, v := range *employ_info {
  83. employCustomIdArr = append(employCustomIdArr, gconv.Int64(v["id"]))
  84. }
  85. } else {
  86. employCustomIdArr = []int64{employCustomId}
  87. }
  88. args, ws := GetInForCommaArr(employCustomIdArr)
  89. args = append(args, entId)
  90. d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.custom where employ_custom_id in( `+ws+`) and ent_id =? `, args...)
  91. if d != nil && len(*d) > 0 {
  92. for _, v := range *d {
  93. position_id := gconv.Int64(v["position_id"])
  94. if positionId == position_id {
  95. return 1, ""
  96. }
  97. return -1, fmt.Sprintf("%v已经基于该候选客户创建了客户", v["create_person"])
  98. }
  99. }
  100. }
  101. }
  102. }
  103. return 1, ""
  104. }
  105. // 获取sql语句的in操作相关参数
  106. func GetInForCommaArr(ids []int64) ([]interface{}, string) {
  107. args := []interface{}{}
  108. ws := []string{}
  109. for _, v := range ids {
  110. args = append(args, v)
  111. ws = append(ws, "?")
  112. }
  113. return args, strings.Join(ws, ",")
  114. }
  115. func hasZero(arr []int64) bool {
  116. for i := 0; i < len(arr); i++ {
  117. if arr[i] == int64(0) {
  118. return true
  119. }
  120. }
  121. return false
  122. }