123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package service
- import (
- "fmt"
- "strings"
- cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
- "github.com/gogf/gf/v2/util/gconv"
- )
- // CanAdd 是否允许多人针对一个创建
- func CanAdd(entId int64, key string, employInfoId, employCustomId, positionId int64) (status int64, error_msg string) {
- if employInfoId <= 0 && employCustomId <= 0 {
- return 1, ""
- }
- accountData := cm.BaseMysql.SelectBySql(`select * from base_service.base_account where ent_id =? and person_id =0 and type =1 limit 1`, entId)
- if accountData == nil || len(*accountData) <= 0 {
- return 1, ""
- }
- accountId := (*accountData)[0]["id"]
- //
- tenantData := cm.BaseMysql.SelectBySql(`select * from crm.config_tenant where account_id =?`, accountId)
- //不允许
- if (len(*tenantData) == 0) || (len(*tenantData) > 0 && gconv.Int((*tenantData)[0][key]) == 0) {
- employInfoIdArr := []int64{}
- if employInfoId > 0 {
- 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)
- if employ_info != nil && len(*employ_info) > 0 {
- for _, v := range *employ_info {
- employInfoIdArr = append(employInfoIdArr, gconv.Int64(v["id"]))
- }
- } else {
- employInfoIdArr = []int64{employInfoId}
- }
- } else {
- employInfoIdArr = []int64{employInfoId}
- }
- args, ws := GetInForCommaArr(employInfoIdArr)
- args = append(args, entId)
- //判断是否已经创建
- if strings.Contains(key, "clue") {
- if hasZero(employInfoIdArr) {
- return 1, ""
- }
- 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...)
- if d != nil && len(*d) > 0 {
- for _, v := range *d {
- position_id := gconv.Int64(v["position_id"])
- if positionId == position_id {
- return 1, ""
- }
- return -1, fmt.Sprintf("%v已经基于该资讯创建了线索", v["create_person"])
- }
- }
- } else if strings.Contains(key, "chance") {
- if hasZero(employInfoIdArr) {
- return 1, ""
- }
- d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.sale_chance where employ_info_id in ( `+ws+`) and ent_id =? `, args...)
- if d != nil && len(*d) > 0 {
- for _, v := range *d {
- position_id := gconv.Int64(v["position_id"])
- if positionId == position_id {
- return 1, ""
- }
- return -1, fmt.Sprintf("%v已经基于该资讯创建了机会", v["create_person"])
- }
- }
- } else if strings.Contains(key, "custom") {
- if employInfoId > 0 {
- d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.custom where employ_info_id in( `+ws+`) and ent_id =? `, args...)
- if d != nil && len(*d) > 0 {
- for _, v := range *d {
- position_id := gconv.Int64(v["position_id"])
- if positionId == position_id {
- return 1, ""
- }
- return -1, fmt.Sprintf("%v已经基于该资讯创建了客户", v["create_person"])
- }
- }
- } else if employCustomId > 0 {
- employCustomIdArr := []int64{}
- employ_info := cm.BaseMysql.SelectBySql(`select id from crm.employ_custom where company_id =(SELECT company_id FROM crm.employ_custom WHERE id =?)`, employCustomId)
- if employ_info != nil && len(*employ_info) > 0 {
- for _, v := range *employ_info {
- employCustomIdArr = append(employCustomIdArr, gconv.Int64(v["id"]))
- }
- } else {
- employCustomIdArr = []int64{employCustomId}
- }
- args, ws := GetInForCommaArr(employCustomIdArr)
- args = append(args, entId)
- d := cm.BaseMysql.SelectBySql(`select create_person,position_id from crm.custom where employ_custom_id in( `+ws+`) and ent_id =? `, args...)
- if d != nil && len(*d) > 0 {
- for _, v := range *d {
- position_id := gconv.Int64(v["position_id"])
- if positionId == position_id {
- return 1, ""
- }
- return -1, fmt.Sprintf("%v已经基于该候选客户创建了客户", v["create_person"])
- }
- }
- }
- }
- }
- return 1, ""
- }
- // 获取sql语句的in操作相关参数
- func GetInForCommaArr(ids []int64) ([]interface{}, string) {
- args := []interface{}{}
- ws := []string{}
- for _, v := range ids {
- args = append(args, v)
- ws = append(ws, "?")
- }
- return args, strings.Join(ws, ",")
- }
- func hasZero(arr []int64) bool {
- for i := 0; i < len(arr); i++ {
- if arr[i] == int64(0) {
- return true
- }
- }
- return false
- }
|