123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- package dao
- import (
- "encoding/json"
- . "gitdxz.51daxuetong.cn/go_dev/jjl-tools/tinodeService/lib"
- "strconv"
- "strings"
- "time"
- )
- type MessageDao struct {
- }
- type Messages struct {
- Createdat time.Time `json:"createdat"`
- Updatedat string `json:"updatedat"`
- Topic string `json:"topic"`
- From int64 `json:"from"`
- Realname string `json:"realname"`
- Avatar string `json:"avatar"`
- Content string `json:"content"`
- Role int `json:"role"`
- Head string `json:"head"`
- ChatId string `json:"chat_id"`
- }
- type ChatUser struct {
- Realname string `json:"realname"`
- Avatar string `json:"avatar"`
- Uid int `json:"uid"`
- }
- type Members struct {
- Id int
- Uid int
- ChatUid int
- Nickname string
- Avatar string
- CreateTime string
- }
- type InspectionDao struct {
- }
- func (insdao *InspectionDao) getUserFromDB(role int, uid int64) (ChatUser, error) {
- var member ChatUser
- var tableName string
- if role == 1 {
- tableName = "go_user"
- } else {
- tableName = "members"
- }
- err := GetTiNodeDbInstance().Debug().Table(tableName).Where("chat_uid=?",uid).Find(&member).Error
- if err !=nil {
- return member, err
- }
- return member, nil
- }
- func (insdao *InspectionDao) GetUserFromRedis(role int, uid int64) (ChatUser, error) {
- var customer ChatUser
- var member Members
- ccd := InspectionDao{}
-
- if role == 1 {
- key := "customer:info:" + strconv.FormatInt(uid, 10)
- redisCli := GetRedisInstance().Connect(1)
- info, err := redisCli.Get(key).Result()
- if err != nil {
- customer, errs := ccd.getUserFromDB(1, uid)
- if customer.Realname == "" || errs != nil {
- customer.Realname = "无此客服"
- }
- return customer, nil
- }
- err = json.Unmarshal([]byte(info), &customer)
- if err != nil {
- customer.Realname = "无此客服"
- }
- return customer, nil
- } else {
- leadsDao := LeadsDao{}
-
- var mcacheKey = "member:info:*:" + strconv.FormatInt(uid, 10)
- uidStr := strconv.FormatInt(uid, 10)
- redis := GetRedisInstance().Connect(2)
- cacheKey := redis.Keys(mcacheKey).Val()
- if len(cacheKey) == 0 {
-
- customer, errs := ccd.getUserFromDB(2, uid)
- if customer.Realname == "" || errs != nil {
- customer.Realname = UidSEncodeNoErr(uidStr)
- }
-
- leadsInfo, _ := leadsDao.GetLeadsInfo(map[string]interface{}{"uid": uid})
- if leadsInfo.Name != "" {
- customer.Realname = leadsInfo.Name
- }
- return customer, nil
- }
- info, _ := redis.Get(cacheKey[0]).Result()
- json.Unmarshal([]byte(info), &member)
- customer.Realname = member.Nickname
- customer.Avatar = member.Avatar
- if member.Nickname == "" {
- customer.Realname = UidSEncodeNoErr(uidStr)
- }
-
- leadsInfo, _ := leadsDao.GetLeadsInfo(map[string]interface{}{"uid": uid})
- if leadsInfo.Name != "" {
- customer.Realname = leadsInfo.Name
- }
- return customer, nil
- }
- return customer, nil
- }
- func (mesDao MessageDao) GetMessagesList(topic, sid, messageTime string, limit int) (mesList []Messages, err error) {
- res := GetTiNodeDbInstance().Debug().Table("messages").Joins("left join dellog on dellog.topic = messages.topic AND messages.seqid = dellog.low").Where("messages.deletedat is null AND messages.delid = 0 AND dellog.deletedfor IS NULL")
- if topic != "" {
- res = res.Where("messages.topic = ?", topic)
- }
- if sid != "" {
- res = res.Where("messages.chat_id = ?", sid)
- }
- if messageTime != "" {
- res = res.Where("messages.createdat < ?", messageTime)
- }
- res = res.Order("messages.id DESC").Limit(limit).Find(&mesList)
- if err != nil {
- return nil, err
- }
- ccd := InspectionDao{}
- var customer ChatUser
- for k, v := range mesList {
- customer.Realname = ""
- customer, _ := ccd.GetUserFromRedis(v.Role, v.From)
- mesList[k].Realname = customer.Realname
- mesList[k].Avatar = customer.Avatar
-
- mesList[k].Content = strings.Trim(v.Content, "\"")
- }
- return
- }
- type ChatRecordDao struct {
- }
- type SemJsChatRecord struct {
- CHAT_ID int `gorm:"column:CHAT_ID"`
- SID string `gorm:"column:SID"`
- CREATE_TIME string `gorm:"column:CREATE_TIME"`
- VISITOR_ID string `gorm:"column:VISITOR_ID"`
- USER_ID string `gorm:"column:USER_ID"`
- VISITOR_STATIC_ID string `gorm:"column:VISITOR_STATIC_ID"`
- INVITE_MODE int64 `gorm:"column:INVITE_MODE"`
- REFER_PAGE string `gorm:"column:REFER_PAGE" json:"refer_page"`
- COMPANY_ID int `gorm:"column:COMPANY_ID"`
- CHAT_URL string `gorm:"column:CHAT_URL"`
- FIRST_URL string `gorm:"column:FIRST_URL"`
- BIZ int `gorm:"column:BIZ"`
- CHANNLE_ID int `gorm:"column:CHANNLE_ID"`
- CATEGORY_ID int `gorm:"column:CATEGORY_ID"`
- END int `gorm:"column:END"`
- GrUserId string `json:"gr_user_id"`
- Topic string `json:"topic"`
- CustomerMsgNum int `json:"customer_msg_num"`
- UserMsgNum int `json:"user_msg_num"`
- UserFirstMsgTime string `json:"user_first_msg_time"`
- CustomerFirstReplyTime string `json:"customer_first_reply_time"`
- CallEndTime string `json:"call_end_time"`
- CallEndType string `json:"call_end_type"`
- Ip string `json:"ip"`
- Seqid int `json:"seqid"`
- Delid int `json:"delid"`
- Tags string `json:"tags"`
- LeadsId int `json:"leads_id"`
- LeadsName string `json:"leads_name"`
- Telephone string `json:"telephone"`
- Mobile string `json:"mobile"`
- Industry string `json:"industry"`
- Remark string `json:"remark"`
- CreateUserId string `json:"create_user_id"`
- OwnerUserId string `json:"owner_user_id"`
- Uid string `json:"uid"`
- MobileEncrypted string `json:"mobile_encrypted""`
- MobileMask string `json:"mobile_mask"`
- Country int `json:"country"`
- City int `json:"city"`
- WeChat string `json:"we_chat"`
- Qq string `json:"qq"`
- CUid string `json:"cUid"`
- GroupId int `json:"group_id"`
- GroupName string `json:"group_name"`
- Realname string `json:"realname"`
- ChannelName string `json:"channel_name"`
- CategoryName string `json:"category_name"`
- VisitorName string `json:"visitor_name"`
- }
- func (chatdao *ChatRecordDao) GetChatRecordByChatId(chatId string) (info SemJsChatRecord, err error) {
- err = GetTiNodeDbInstance().Debug().Table("sem_js_chat_record").Where("SID = ?", chatId).First(&info).Error
- if err != nil {
- return
- }
- return
- }
- func (chatdao *ChatRecordDao) GetGroupByChatRecordLists(topic, sid string) (list []SemJsChatRecord, err error) {
- sql := GetTiNodeDbInstance().Debug().Table("sem_js_chat_record")
- if topic != "" {
- sql = sql.Where("topic = ?", topic)
- }
- if sid != "" {
- sql = sql.Where("SID = ?", sid)
- }
- err = sql.Find(&list).Error
- return
- }
|