hlyj.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "time"
  6. "app.yhyue.com/moapp/jybase/common"
  7. "app.yhyue.com/moapp/jybase/date"
  8. "app.yhyue.com/moapp/jybase/redis"
  9. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  10. "bp.jydev.jianyu360.cn/BaseService/biService/rpc/biservice"
  11. "github.com/gogf/gf/v2/frame/g"
  12. "github.com/gogf/gf/v2/util/gconv"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type HlyjStruct struct {
  16. Account string
  17. TokenUrl string
  18. Appid string
  19. Secret string
  20. CallUrl string
  21. Integratedid string
  22. CallFlag int
  23. }
  24. func (this *HlyjStruct) GetRedisKey() string {
  25. return "hlyj_access_token"
  26. }
  27. // 获取token
  28. func (this *HlyjStruct) GetAccessToken() string {
  29. //取缓存
  30. if redisToken := redis.GetStr("newother", this.GetRedisKey()); redisToken != "" {
  31. return redisToken
  32. }
  33. str := Get(this.TokenUrl, g.Map{"account": this.Account, "appid": this.Appid, "secret": this.Secret})
  34. token, _ := gconv.Map(str)["accessToken"].(string)
  35. // 计算时间差
  36. invalidTime, _ := gconv.Map(str)["invalidTime"].(string)
  37. t, _ := time.ParseInLocation(date.Date_Full_Layout, invalidTime, time.Local)
  38. duration := time.Since(t)
  39. timeout := -gconv.Int(duration.Seconds()) - 60 //
  40. //缓存
  41. redis.Put("newother", this.GetRedisKey(), token, timeout)
  42. return token
  43. }
  44. func Get(url string, param map[string]interface{}) (str string) {
  45. ctx := context.Background()
  46. if r, err := g.Client().Get(ctx, url, param); err != nil {
  47. g.Log().Error(ctx, err)
  48. } else {
  49. defer r.Close()
  50. str = r.ReadAllString()
  51. logx.Info(url, "-", str)
  52. }
  53. return
  54. }
  55. // 打电话
  56. func (this *HlyjStruct) Call(phone string, positionId int64) string {
  57. //获取token
  58. // token := this.GetAccessToken()
  59. // if token == "" {
  60. // logx.Info("未获取到token")
  61. // return false
  62. // }
  63. //获取坐席id
  64. integratedid, _ := getSeatNumber(positionId)
  65. if integratedid == "" {
  66. logx.Info("未获取到坐席id")
  67. return ""
  68. }
  69. id := JyBiTidb.Insert("call_history", map[string]interface{}{
  70. "phone": phone,
  71. "positionId": positionId,
  72. "seatNumber": integratedid,
  73. "createTime": time.Now().Format(date.Date_Full_Layout),
  74. })
  75. data := map[string]interface{}{"callId": id}
  76. bytes, _ := json.Marshal(&data)
  77. return string(bytes)
  78. //打电话接口
  79. // ret := Get(this.CallUrl, g.Map{"flag": this.CallFlag, "account": this.Account, "integratedid": integratedid, "accessToken": token, "phonenum": phone})
  80. // return ret == "200"
  81. }
  82. func AutoFollow(this *biservice.CallReq) *biservice.ClueImportResp {
  83. status := 1
  84. data := JyBiTidb.FindOne("dwd_f_userbase_contacts", map[string]interface{}{"phone": this.Phone}, "", "")
  85. if data != nil {
  86. baseInfoId := common.ObjToString((*data)["baseinfo_id"])
  87. clueData := JyBiTidb.FindOne("dwd_f_crm_clue_info", map[string]interface{}{"uid": baseInfoId}, "", "")
  88. if clueData != nil {
  89. clueId := common.Int64All((*clueData)["id"])
  90. callData := CallTidb.FindOne("voice_record", map[string]interface{}{"CalledNo": this.Phone}, "", "createTime desc")
  91. if callData != nil {
  92. callMap := map[string]interface{}{
  93. "": "1",
  94. "dealing": "2",
  95. "voicemail": "3",
  96. "notDeal": "4",
  97. "blackList": "5",
  98. "queueLeak": "6",
  99. "leak": "7",
  100. "robotServer": "8",
  101. "autoHangup": "9",
  102. }
  103. state := common.ObjToString((*callData)["State"])
  104. hanguper := common.ObjToString((*callData)["Hanguper"])
  105. updateData := map[string]interface{}{}
  106. if state != "dealing" && hanguper != "agent" {
  107. JyBiTidb.Insert("dwd_f_crm_trail_content", map[string]interface{}{
  108. "clue_id": clueId,
  109. "position_id": this.PositionId,
  110. "content": "客户未接通",
  111. "operator_id": -1,
  112. "createtime": time.Now().Format(date.Date_Full_Layout),
  113. })
  114. updateData["taskstatus"] = 1
  115. //
  116. updateData["trail_time"] = time.Now().Format(date.Date_Full_Layout)
  117. updateData["content"] = "客户未接通"
  118. //
  119. }
  120. updateData["STATE"] = callMap[state]
  121. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  122. } else {
  123. time.Sleep(10 * time.Second)
  124. callData := CallTidb.FindOne("voice_record", map[string]interface{}{"phone": this.Phone}, "", "createTime desc")
  125. if callData != nil {
  126. callMap := map[string]interface{}{
  127. "": "1",
  128. "dealing": "2",
  129. "voicemail": "3",
  130. "notDeal": "4",
  131. "blackList": "5",
  132. "queueLeak": "6",
  133. "leak": "7",
  134. "robotServer": "8",
  135. "autoHangup": "9",
  136. }
  137. state := common.ObjToString((*callData)["State"])
  138. hanguper := common.ObjToString((*callData)["Hanguper"])
  139. updateData := map[string]interface{}{}
  140. if state != "dealing" && hanguper != "agent" {
  141. JyBiTidb.Insert("dwd_f_crm_trail_content", map[string]interface{}{
  142. "clue_id": clueId,
  143. "position_id": this.PositionId,
  144. "content": "客户未接通",
  145. "operator_id": -1,
  146. "createtime": time.Now().Format(date.Date_Full_Layout),
  147. })
  148. updateData["taskstatus"] = 1
  149. //
  150. updateData["trail_time"] = time.Now().Format(date.Date_Full_Layout)
  151. updateData["content"] = "客户未接通"
  152. //
  153. }
  154. updateData["STATE"] = callMap[state]
  155. JyBiTidb.Update("dwd_f_crm_clue_info", map[string]interface{}{"id": clueId}, updateData)
  156. }
  157. }
  158. }
  159. } else {
  160. status = -1
  161. }
  162. return &biservice.ClueImportResp{
  163. ErrorCode: 0,
  164. Data: &biservice.ClueImport{
  165. Status: int64(status),
  166. },
  167. }
  168. }