package service import ( "context" "encoding/json" "time" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/date" "app.yhyue.com/moapp/jybase/redis" . "bp.jydev.jianyu360.cn/BaseService/biService/entity" "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" "github.com/zeromicro/go-zero/core/logx" ) type HlyjStruct struct { Account string TokenUrl string Appid string Secret string CallUrl string Integratedid string CallFlag int } func (this *HlyjStruct) GetRedisKey() string { return "hlyj_access_token" } // 获取token func (this *HlyjStruct) GetAccessToken() string { //取缓存 if redisToken := redis.GetStr("newother", this.GetRedisKey()); redisToken != "" { return redisToken } str := Get(this.TokenUrl, g.Map{"account": this.Account, "appid": this.Appid, "secret": this.Secret}) token, _ := gconv.Map(str)["accessToken"].(string) // 计算时间差 invalidTime, _ := gconv.Map(str)["invalidTime"].(string) t, _ := time.ParseInLocation(date.Date_Full_Layout, invalidTime, time.Local) duration := time.Since(t) timeout := -gconv.Int(duration.Seconds()) - 60 // //缓存 redis.Put("newother", this.GetRedisKey(), token, timeout) return token } func Get(url string, param map[string]interface{}) (str string) { ctx := context.Background() if r, err := g.Client().Get(ctx, url, param); err != nil { g.Log().Error(ctx, err) } else { defer r.Close() str = r.ReadAllString() logx.Info(url, "-", str) } return } // 打电话 func (this *HlyjStruct) Call(phone string, positionId int64) string { //获取token // token := this.GetAccessToken() // if token == "" { // logx.Info("未获取到token") // return false // } //获取坐席id integratedid, _ := getSeatNumber(positionId) if integratedid == "" { logx.Info("未获取到坐席id") return "" } id := JyBiTidb.Insert("call_history", map[string]interface{}{ "phone": phone, "positionId": positionId, "seatNumber": integratedid, "createTime": time.Now().Format(date.Date_Full_Layout), }) data := map[string]interface{}{"callId": id} bytes, _ := json.Marshal(&data) return string(bytes) //打电话接口 // ret := Get(this.CallUrl, g.Map{"flag": this.CallFlag, "account": this.Account, "integratedid": integratedid, "accessToken": token, "phonenum": phone}) // return ret == "200" } func AutoFollow(this *biservice.CallReq) *biservice.ClueImportResp { status := 1 data := JyBiTidb.FindOne("dwd_f_userbase_contacts", map[string]interface{}{"phone": this.Phone}, "", "") if data != nil { baseInfoId := common.ObjToString((*data)["baseinfo_id"]) clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": baseInfoId}, "", "") if clueData != nil { clueId := common.Int64All((*clueData)["id"]) callData := CallTidb.FindOne("voice_record", map[string]interface{}{"CalledNo": this.Phone}, "", "createTime desc") if callData != nil { callMap := map[string]interface{}{ "": "1", "dealing": "2", "voicemail": "3", "notDeal": "4", "blackList": "5", "queueLeak": "6", "leak": "7", "robotServer": "8", "autoHangup": "9", } state := common.ObjToString((*callData)["State"]) hanguper := common.ObjToString((*callData)["Hanguper"]) updateData := map[string]interface{}{} if state != "dealing" && hanguper != "agent" { JyBiTidb.Insert("dwd_f_crm_trail_content", map[string]interface{}{ "clue_id": clueId, "position_id": this.PositionId, "content": "客户未接通", "operator_id": -1, "createtime": time.Now().Format(date.Date_Full_Layout), }) updateData["taskstatus"] = 1 // updateData["trail_time"] = time.Now().Format(date.Date_Full_Layout) updateData["content"] = "客户未接通" // } updateData["STATE"] = callMap[state] JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData) } else { time.Sleep(10 * time.Second) callData := CallTidb.FindOne("voice_record", map[string]interface{}{"phone": this.Phone}, "", "createTime desc") if callData != nil { callMap := map[string]interface{}{ "": "1", "dealing": "2", "voicemail": "3", "notDeal": "4", "blackList": "5", "queueLeak": "6", "leak": "7", "robotServer": "8", "autoHangup": "9", } state := common.ObjToString((*callData)["State"]) hanguper := common.ObjToString((*callData)["Hanguper"]) updateData := map[string]interface{}{} if state != "dealing" && hanguper != "agent" { JyBiTidb.Insert("dwd_f_crm_trail_content", map[string]interface{}{ "clue_id": clueId, "position_id": this.PositionId, "content": "客户未接通", "operator_id": -1, "createtime": time.Now().Format(date.Date_Full_Layout), }) updateData["taskstatus"] = 1 // updateData["trail_time"] = time.Now().Format(date.Date_Full_Layout) updateData["content"] = "客户未接通" // } updateData["STATE"] = callMap[state] JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData) } } } } else { status = -1 } return &biservice.ClueImportResp{ ErrorCode: 0, Data: &biservice.ClueImport{ Status: int64(status), }, } }