作者 tangxvhui

综合自评消息提示

... ... @@ -208,3 +208,11 @@ func CreateLogSmsRepository(options map[string]interface{}) domain.LogSmsReposit
}
return repository.NewLogSmsRepository(transactionContext)
}
func CreateMessagePersonalRepository(options map[string]interface{}) domain.MessagePersonalRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewMessagePersonalRepository(transactionContext)
}
... ...
package notify
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/linmadan/egglib-go/core/application"
... ... @@ -20,7 +23,7 @@ func NewMessagePersonalService() *MessagePersonalService {
}
// 获取今天的周期综合自评消息提示
func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *command.GetUserMessageCommand) (interface{}, error) {
func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ... @@ -41,11 +44,61 @@ func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *comm
"types": domain.EvaluationSelf,
"beginTime": nowTime,
"endTime": nowTime,
"limit": 20,
})
_ = evaluationList
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "周期综合自评"+err.Error())
}
resp := map[string]interface{}{
"needNotify": false,
"content": "",
}
if len(evaluationList) == 0 {
return resp, nil
}
messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
newMessageList := []domain.MessagePersonal{}
for _, val := range evaluationList {
payload := map[string]string{
"id": strconv.Itoa(val.Id),
}
payloadStr, _ := json.Marshal(payload)
cnt, _, err := messageRepo.Find(map[string]interface{}{
"types": domain.MessageTypesOther,
"targetUserId": param.UserId,
"playload": string(payloadStr),
"limit": 1,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查自评消息记录"+err.Error())
}
if cnt == 0 {
newMessageList = append(newMessageList, domain.MessagePersonal{
Id: 0,
Types: domain.MessageTypesOther,
TargetUserId: val.TargetUser.UserId,
ReadFlag: domain.MessageIsRead,
Title: fmt.Sprintf("%s综合自评已开启,请前往进行评估", val.CycleName),
Content: fmt.Sprintf("%s综合自评已开启,请前往进行评估", val.CycleName),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
Payload: string(payloadStr),
})
resp["needNotify"] = true
resp["content"] = fmt.Sprintf("%s综合自评已开启,请前往进行评估", val.CycleName)
break
}
}
for i := range newMessageList {
err = messageRepo.Save(&newMessageList[i])
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存自评消息记录"+err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return nil, nil
return resp, nil
}
... ...
... ... @@ -5,7 +5,7 @@ import "time"
// MessagePersonal 个人的消息提示
type MessagePersonal struct {
Id int `json:"id"` //
Types string `json:"types"` //消息类型
Types MessageTypes `json:"types"` //消息类型
TargetUserId int `json:"targetUserId"` //消息指向的用户
ReadFlag MessageReadFlag `json:"readFlag"` //1:已读、2:未读
Title string `json:"title"` //消息的标题
... ... @@ -27,3 +27,8 @@ const (
MessageIsRead MessageReadFlag = "read"
MessageUnread MessageReadFlag = "unread"
)
type MessagePersonalRepository interface {
Save(param *MessagePersonal) error
Find(queryOptions map[string]interface{}) (int, []*MessagePersonal, error)
}
... ...
... ... @@ -9,8 +9,8 @@ import (
func TestGenerateToken(t *testing.T) {
ut := UserAuth{
CompanyId: 8,
UserId: 3435675157551872,
Phone: "18060823798",
UserId: 3422181461696000,
Phone: "13066667702",
PlatformId: 29,
AdminType: 1,
}
... ...
... ... @@ -13,5 +13,5 @@ type MessagePersonal struct {
Content string `pg:"content"` //消息的内容
CreatedAt time.Time `pg:"created_at"`
UpdatedAt time.Time `pg:"updated_at"`
Payload string `pg:"payload"` //消息的额外承载的数据
Payload string `pg:"payload,type:jsonb"` //消息的额外承载的数据
}
... ...
package repository
import (
"time"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)
type MessagePersonalRepository struct {
transactionContext *pgTransaction.TransactionContext
}
var _ domain.MessagePersonalRepository = (*MessagePersonalRepository)(nil)
func NewMessagePersonalRepository(tx *pgTransaction.TransactionContext) *MessagePersonalRepository {
return &MessagePersonalRepository{
transactionContext: tx,
}
}
func (repo *MessagePersonalRepository) TransformToDomain(param *models.MessagePersonal) *domain.MessagePersonal {
return &domain.MessagePersonal{
Id: param.Id,
Types: domain.MessageTypes(param.Types),
TargetUserId: param.TargetUserId,
ReadFlag: domain.MessageReadFlag(param.ReadFlag),
Title: param.Title,
Content: param.Content,
Payload: param.Payload,
UpdatedAt: param.UpdatedAt,
CreatedAt: param.CreatedAt,
}
}
func (repo *MessagePersonalRepository) Save(param *domain.MessagePersonal) error {
message := &models.MessagePersonal{
Id: param.Id,
Types: string(param.Types),
TargetUserId: param.TargetUserId,
ReadFlag: string(param.ReadFlag),
Title: param.Title,
Content: param.Content,
Payload: param.Payload,
UpdatedAt: time.Now(),
CreatedAt: param.CreatedAt,
}
tx := repo.transactionContext.PgTx
if message.Id == 0 {
message.CreatedAt = time.Now()
_, err := tx.Model(message).Insert()
return err
}
_, err := tx.Model(message).WherePK().Update()
return err
}
func (repo *MessagePersonalRepository) Find(param map[string]interface{}) (int, []*domain.MessagePersonal, error) {
tx := repo.transactionContext.PgTx
var m []*models.MessagePersonal
query := tx.Model(&m).Limit(20)
if v, ok := param["targetUserId"]; ok {
query.Where("target_user_id=?", v)
}
if v, ok := param["types"]; ok {
query.Where("types=?", v)
}
if v, ok := param["playload"]; ok {
query.Where("playload @>?", v)
}
query.Order("id desc")
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var datas []*domain.MessagePersonal
for _, v := range m {
d := repo.TransformToDomain(v)
datas = append(datas, d)
}
return count, datas, nil
}
... ...