审查视图

pkg/application/notify/service/message_personal.go 3.4 KB
tangxvhui authored
1
package service
tangxvhui authored
2 3

import (
tangxvhui authored
4 5 6
	"encoding/json"
	"fmt"
	"strconv"
tangxvhui authored
7 8
	"time"
tangxvhui authored
9 10 11
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/notify/command"
tangxvhui authored
12
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
tangxvhui authored
13 14 15 16 17 18 19 20 21 22 23 24 25
)

// 个人信息提示

type MessagePersonalService struct {
}

func NewMessagePersonalService() *MessagePersonalService {
	newService := &MessagePersonalService{}
	return newService
}

// 获取今天的周期综合自评消息提示
tangxvhui authored
26
func (srv *MessagePersonalService) TodayMessageSummaryEvaluationSelf(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
tangxvhui authored
27 28 29 30 31 32 33 34 35 36 37
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
tangxvhui authored
38 39 40 41 42 43 44 45 46 47
	nowTime := time.Now()
	evaluationRepo := factory.CreateSummaryEvaluationRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, evaluationList, err := evaluationRepo.Find(map[string]interface{}{
		"targetUserId": param.UserId,
		"types":        domain.EvaluationSelf,
		"beginTime":    nowTime,
		"endTime":      nowTime,
	})
tangxvhui authored
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	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,
tangxvhui authored
71
			"payload":      string(payloadStr),
tangxvhui authored
72 73 74 75 76 77 78 79 80 81 82
			"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,
tangxvhui authored
83 84
				Title:        fmt.Sprintf("%s综合自评已开启,请前往进行评估.", val.CycleName),
				Content:      fmt.Sprintf("%s综合自评已开启,请前往进行评估.", val.CycleName),
tangxvhui authored
85 86 87 88 89
				CreatedAt:    time.Time{},
				UpdatedAt:    time.Time{},
				Payload:      string(payloadStr),
			})
			resp["needNotify"] = true
tangxvhui authored
90
			resp["content"] = fmt.Sprintf("%s综合自评已开启,请前往进行评估.", val.CycleName)
tangxvhui authored
91 92 93 94 95 96 97 98 99
			break
		}
	}
	for i := range newMessageList {
		err = messageRepo.Save(&newMessageList[i])
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存自评消息记录"+err.Error())
		}
	}
tangxvhui authored
100 101 102
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
103
	return resp, nil
tangxvhui authored
104
}