作者 tangxvhui

综合自评消息提示

@@ -208,3 +208,11 @@ func CreateLogSmsRepository(options map[string]interface{}) domain.LogSmsReposit @@ -208,3 +208,11 @@ func CreateLogSmsRepository(options map[string]interface{}) domain.LogSmsReposit
208 } 208 }
209 return repository.NewLogSmsRepository(transactionContext) 209 return repository.NewLogSmsRepository(transactionContext)
210 } 210 }
  211 +
  212 +func CreateMessagePersonalRepository(options map[string]interface{}) domain.MessagePersonalRepository {
  213 + var transactionContext *pg.TransactionContext
  214 + if value, ok := options["transactionContext"]; ok {
  215 + transactionContext = value.(*pg.TransactionContext)
  216 + }
  217 + return repository.NewMessagePersonalRepository(transactionContext)
  218 +}
1 package notify 1 package notify
2 2
3 import ( 3 import (
  4 + "encoding/json"
  5 + "fmt"
  6 + "strconv"
4 "time" 7 "time"
5 8
6 "github.com/linmadan/egglib-go/core/application" 9 "github.com/linmadan/egglib-go/core/application"
@@ -20,7 +23,7 @@ func NewMessagePersonalService() *MessagePersonalService { @@ -20,7 +23,7 @@ func NewMessagePersonalService() *MessagePersonalService {
20 } 23 }
21 24
22 // 获取今天的周期综合自评消息提示 25 // 获取今天的周期综合自评消息提示
23 -func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *command.GetUserMessageCommand) (interface{}, error) { 26 +func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
24 transactionContext, err := factory.CreateTransactionContext(nil) 27 transactionContext, err := factory.CreateTransactionContext(nil)
25 if err != nil { 28 if err != nil {
26 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) 29 return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
@@ -41,11 +44,61 @@ func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *comm @@ -41,11 +44,61 @@ func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *comm
41 "types": domain.EvaluationSelf, 44 "types": domain.EvaluationSelf,
42 "beginTime": nowTime, 45 "beginTime": nowTime,
43 "endTime": nowTime, 46 "endTime": nowTime,
44 - "limit": 20,  
45 }) 47 })
46 - _ = evaluationList 48 + if err != nil {
  49 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "周期综合自评"+err.Error())
  50 + }
  51 + resp := map[string]interface{}{
  52 + "needNotify": false,
  53 + "content": "",
  54 + }
  55 + if len(evaluationList) == 0 {
  56 + return resp, nil
  57 + }
  58 +
  59 + messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
  60 + "transactionContext": transactionContext,
  61 + })
  62 + newMessageList := []domain.MessagePersonal{}
  63 + for _, val := range evaluationList {
  64 + payload := map[string]string{
  65 + "id": strconv.Itoa(val.Id),
  66 + }
  67 + payloadStr, _ := json.Marshal(payload)
  68 + cnt, _, err := messageRepo.Find(map[string]interface{}{
  69 + "types": domain.MessageTypesOther,
  70 + "targetUserId": param.UserId,
  71 + "playload": string(payloadStr),
  72 + "limit": 1,
  73 + })
  74 + if err != nil {
  75 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查自评消息记录"+err.Error())
  76 + }
  77 + if cnt == 0 {
  78 + newMessageList = append(newMessageList, domain.MessagePersonal{
  79 + Id: 0,
  80 + Types: domain.MessageTypesOther,
  81 + TargetUserId: val.TargetUser.UserId,
  82 + ReadFlag: domain.MessageIsRead,
  83 + Title: fmt.Sprintf("%s综合自评已开启,请前往进行评估", val.CycleName),
  84 + Content: fmt.Sprintf("%s综合自评已开启,请前往进行评估", val.CycleName),
  85 + CreatedAt: time.Time{},
  86 + UpdatedAt: time.Time{},
  87 + Payload: string(payloadStr),
  88 + })
  89 + resp["needNotify"] = true
  90 + resp["content"] = fmt.Sprintf("%s综合自评已开启,请前往进行评估", val.CycleName)
  91 + break
  92 + }
  93 + }
  94 + for i := range newMessageList {
  95 + err = messageRepo.Save(&newMessageList[i])
  96 + if err != nil {
  97 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存自评消息记录"+err.Error())
  98 + }
  99 + }
47 if err := transactionContext.CommitTransaction(); err != nil { 100 if err := transactionContext.CommitTransaction(); err != nil {
48 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) 101 return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
49 } 102 }
50 - return nil, nil 103 + return resp, nil
51 } 104 }
@@ -5,7 +5,7 @@ import "time" @@ -5,7 +5,7 @@ import "time"
5 // MessagePersonal 个人的消息提示 5 // MessagePersonal 个人的消息提示
6 type MessagePersonal struct { 6 type MessagePersonal struct {
7 Id int `json:"id"` // 7 Id int `json:"id"` //
8 - Types string `json:"types"` //消息类型 8 + Types MessageTypes `json:"types"` //消息类型
9 TargetUserId int `json:"targetUserId"` //消息指向的用户 9 TargetUserId int `json:"targetUserId"` //消息指向的用户
10 ReadFlag MessageReadFlag `json:"readFlag"` //1:已读、2:未读 10 ReadFlag MessageReadFlag `json:"readFlag"` //1:已读、2:未读
11 Title string `json:"title"` //消息的标题 11 Title string `json:"title"` //消息的标题
@@ -27,3 +27,8 @@ const ( @@ -27,3 +27,8 @@ const (
27 MessageIsRead MessageReadFlag = "read" 27 MessageIsRead MessageReadFlag = "read"
28 MessageUnread MessageReadFlag = "unread" 28 MessageUnread MessageReadFlag = "unread"
29 ) 29 )
  30 +
  31 +type MessagePersonalRepository interface {
  32 + Save(param *MessagePersonal) error
  33 + Find(queryOptions map[string]interface{}) (int, []*MessagePersonal, error)
  34 +}
@@ -9,8 +9,8 @@ import ( @@ -9,8 +9,8 @@ import (
9 func TestGenerateToken(t *testing.T) { 9 func TestGenerateToken(t *testing.T) {
10 ut := UserAuth{ 10 ut := UserAuth{
11 CompanyId: 8, 11 CompanyId: 8,
12 - UserId: 3435675157551872,  
13 - Phone: "18060823798", 12 + UserId: 3422181461696000,
  13 + Phone: "13066667702",
14 PlatformId: 29, 14 PlatformId: 29,
15 AdminType: 1, 15 AdminType: 1,
16 } 16 }
@@ -13,5 +13,5 @@ type MessagePersonal struct { @@ -13,5 +13,5 @@ type MessagePersonal struct {
13 Content string `pg:"content"` //消息的内容 13 Content string `pg:"content"` //消息的内容
14 CreatedAt time.Time `pg:"created_at"` 14 CreatedAt time.Time `pg:"created_at"`
15 UpdatedAt time.Time `pg:"updated_at"` 15 UpdatedAt time.Time `pg:"updated_at"`
16 - Payload string `pg:"payload"` //消息的额外承载的数据 16 + Payload string `pg:"payload,type:jsonb"` //消息的额外承载的数据
17 } 17 }
  1 +package repository
  2 +
  3 +import (
  4 + "time"
  5 +
  6 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  7 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
  9 +)
  10 +
  11 +type MessagePersonalRepository struct {
  12 + transactionContext *pgTransaction.TransactionContext
  13 +}
  14 +
  15 +var _ domain.MessagePersonalRepository = (*MessagePersonalRepository)(nil)
  16 +
  17 +func NewMessagePersonalRepository(tx *pgTransaction.TransactionContext) *MessagePersonalRepository {
  18 + return &MessagePersonalRepository{
  19 + transactionContext: tx,
  20 + }
  21 +}
  22 +
  23 +func (repo *MessagePersonalRepository) TransformToDomain(param *models.MessagePersonal) *domain.MessagePersonal {
  24 + return &domain.MessagePersonal{
  25 + Id: param.Id,
  26 + Types: domain.MessageTypes(param.Types),
  27 + TargetUserId: param.TargetUserId,
  28 + ReadFlag: domain.MessageReadFlag(param.ReadFlag),
  29 + Title: param.Title,
  30 + Content: param.Content,
  31 + Payload: param.Payload,
  32 + UpdatedAt: param.UpdatedAt,
  33 + CreatedAt: param.CreatedAt,
  34 + }
  35 +}
  36 +
  37 +func (repo *MessagePersonalRepository) Save(param *domain.MessagePersonal) error {
  38 + message := &models.MessagePersonal{
  39 + Id: param.Id,
  40 + Types: string(param.Types),
  41 + TargetUserId: param.TargetUserId,
  42 + ReadFlag: string(param.ReadFlag),
  43 + Title: param.Title,
  44 + Content: param.Content,
  45 + Payload: param.Payload,
  46 + UpdatedAt: time.Now(),
  47 + CreatedAt: param.CreatedAt,
  48 + }
  49 + tx := repo.transactionContext.PgTx
  50 + if message.Id == 0 {
  51 + message.CreatedAt = time.Now()
  52 + _, err := tx.Model(message).Insert()
  53 +
  54 + return err
  55 + }
  56 + _, err := tx.Model(message).WherePK().Update()
  57 + return err
  58 +}
  59 +
  60 +func (repo *MessagePersonalRepository) Find(param map[string]interface{}) (int, []*domain.MessagePersonal, error) {
  61 + tx := repo.transactionContext.PgTx
  62 + var m []*models.MessagePersonal
  63 + query := tx.Model(&m).Limit(20)
  64 + if v, ok := param["targetUserId"]; ok {
  65 + query.Where("target_user_id=?", v)
  66 + }
  67 + if v, ok := param["types"]; ok {
  68 + query.Where("types=?", v)
  69 + }
  70 + if v, ok := param["playload"]; ok {
  71 + query.Where("playload @>?", v)
  72 + }
  73 + query.Order("id desc")
  74 + count, err := query.SelectAndCount()
  75 + if err != nil {
  76 + return 0, nil, err
  77 + }
  78 + var datas []*domain.MessagePersonal
  79 + for _, v := range m {
  80 + d := repo.TransformToDomain(v)
  81 + datas = append(datas, d)
  82 + }
  83 + return count, datas, nil
  84 +
  85 +}