dxc hace 2 años
padre
commit
499d3ddc50
Se han modificado 3 ficheros con 29 adiciones y 1 borrados
  1. 11 0
      internal/logic/callback/callbackmsglogic.go
  2. 7 0
      model/cbcustomermodel.go
  3. 11 1
      model/cbmsgmodel.go

+ 11 - 0
internal/logic/callback/callbackmsglogic.go

@@ -329,6 +329,17 @@ func msgTypeEventHandle(svcCtx *svc.ServiceContext, msg *model.CbMsg) {
 
 // 同步状态
 func InitCustomerState(svcCtx *svc.ServiceContext) {
+	//更新最后消息时间为空的用户
+	list, _ := svcCtx.CbCustomerModel.GetCustomerByZeroLastMsgTime()
+	for _, customer := range list {
+		if msg, _ := svcCtx.CbMsgModel.GetCustomerLastMsg(customer.OpenKfid, customer.ExternalUserid); msg.Id != 0 {
+			err := svcCtx.CbCustomerModel.UpdateCustomerLastMsgTime(customer.OpenKfid, customer.ExternalUserid, msg.SendTime)
+			if err != nil {
+				logx.Error()
+			}
+		}
+	}
+	//更新用户状态
 	page := 1
 	size := 1000
 	for {

+ 7 - 0
model/cbcustomermodel.go

@@ -15,6 +15,7 @@ type (
 		cbCustomerModel
 		GetCustomerByExternalUserid(openKfid, externalUserid string) (d *CbCustomer, err error)
 		GetCustomerByPage(page, size int) (list []CbCustomer, err error)
+		GetCustomerByZeroLastMsgTime() (list []CbCustomer, err error)
 		UpdateCustomerState(openKfid, externalUserid string, state int) (err error)
 		UpdateCustomerLastMsgTime(openKfid, externalUserid string, t int64) (err error)
 	}
@@ -56,3 +57,9 @@ func (m *customCbCustomerModel) GetCustomerByPage(page, size int) (list []CbCust
 	err = m.conn.QueryRows(&list, query, time.Now().AddDate(0, 0, -2).Unix(), (page-1)*size, size)
 	return
 }
+
+func (m *customCbCustomerModel) GetCustomerByZeroLastMsgTime() (list []CbCustomer, err error) {
+	query := fmt.Sprintf("select * from %s where last_msg_time = 0 OR last_msg_time IS NULL", m.table)
+	err = m.conn.QueryRows(&list, query)
+	return
+}

+ 11 - 1
model/cbmsgmodel.go

@@ -1,6 +1,9 @@
 package model
 
-import "github.com/zeromicro/go-zero/core/stores/sqlx"
+import (
+	"fmt"
+	"github.com/zeromicro/go-zero/core/stores/sqlx"
+)
 
 var _ CbMsgModel = (*customCbMsgModel)(nil)
 
@@ -9,6 +12,7 @@ type (
 	// and implement the added methods in customCbMsgModel.
 	CbMsgModel interface {
 		cbMsgModel
+		GetCustomerLastMsg(openKfid, externalUserid string) (CbMsg, error)
 	}
 
 	customCbMsgModel struct {
@@ -22,3 +26,9 @@ func NewCbMsgModel(conn sqlx.SqlConn) CbMsgModel {
 		defaultCbMsgModel: newCbMsgModel(conn),
 	}
 }
+
+func (c *customCbMsgModel) GetCustomerLastMsg(openKfid, externalUserid string) (msg CbMsg, err error) {
+	query := fmt.Sprintf("select * from %s where id IN (select MAX(id) from %s where `open_kfid` = ? AND `external_userid` = ?)", c.table, c.table)
+	err = c.conn.QueryRow(&msg, query, openKfid, externalUserid)
+	return
+}