审查视图

pkg/application/notify/app_message.go 8.9 KB
tangxvhui authored
1 2
package notify
3 4 5 6 7
import (
	"encoding/json"
	"fmt"
	"strconv"
	"time"
tangxvhui authored
8
9 10
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
tangxvhui authored
11
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/constant"
12 13
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
tangxvhui authored
14
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/serviceGateway"
15 16 17
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
)
tangxvhui authored
18
func messageTaskStageAnomaly() ([]*domain.MessagePersonal, error) {
19 20 21 22 23 24 25 26 27 28 29 30
	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()
	}()
	taskDao := dao.NewTaskDao(map[string]interface{}{"transactionContext": transactionContext})
	userDao := dao.NewUserDao(map[string]interface{}{"transactionContext": transactionContext})
tangxvhui authored
31
	taskData, err := taskDao.TaskStageAnomalyAll()
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	if err != nil {
		return nil, fmt.Errorf("获取任务信息%s", err)
	}
	if len(taskData) == 0 {
		return nil, nil
	}
	var allMessage []*domain.MessagePersonal
	for _, val := range taskData {
		//获取我全上级
		userList, err := userDao.AllParentUser(val.LeaderId)
		if err != nil {
			return nil, fmt.Errorf("获取上级人员信息%s", err)
		}
		if len(userList) == 0 {
			continue
		}
49
		var leaderParentId int
tangxvhui authored
50
		//通知负责人
51 52 53
		for _, val2 := range userList {
			content := ""
			if val2.Level == 1 {
tangxvhui authored
54
				content = fmt.Sprintf("【您负责的项目【%s】里程碑未按时完成,请重点关注,积极寻找上级辅导。】", val.TaskName)
55
			} else if val2.Level == 2 {
56
				leaderParentId = val2.Id
57
				content = fmt.Sprintf("【您下级%s负责的项目【%s】里程碑未按时完成,请前往辅导。】", val.LeaderName, val.TaskName)
tangxvhui authored
58
			} else if val2.Level >= 3 {
59 60 61 62 63 64
				content = fmt.Sprintf("【您下级%s关注的项目【%s】里程碑未按时完成,请前往辅导。】", val.LeaderName, val.TaskName)
			}
			if content == "" {
				continue
			}
			payload := map[string]string{
tangxvhui authored
65 66 67
				"taskId":    strconv.Itoa(val.TaskId),
				"taskAlias": val.TaskAlias,
				"taskName":  val.TaskName,
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
			}
			payloadStr, _ := json.Marshal(payload)
			newMessage := domain.MessagePersonal{
				Id:           0,
				Types:        domain.MessageTypesTaskStageApp,
				TargetUserId: val2.Id,
				ReadFlag:     domain.MessageIsRead,
				Title:        content,
				Content:      content,
				CreatedAt:    time.Time{},
				UpdatedAt:    time.Time{},
				Payload:      string(payloadStr),
			}
			allMessage = append(allMessage, &newMessage)
		}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		//通知相关人员
		//【您关注的【%s】里程碑未按时完成,请知晓。】
		for _, val2 := range val.RelatedUser {
			if val2 == val.LeaderId {
				continue
			}
			if val2 == leaderParentId {
				continue
			}
			payload := map[string]string{
				"taskId":    strconv.Itoa(val.TaskId),
				"taskAlias": val.TaskAlias,
				"taskName":  val.TaskName,
			}
			payloadStr, _ := json.Marshal(payload)
			content := fmt.Sprintf("【您关注的【%s】里程碑未按时完成,请知晓。】", val.TaskAlias)
			newMessage := domain.MessagePersonal{
				Id:           0,
				Types:        domain.MessageTypesTaskStageApp,
				TargetUserId: val2,
				ReadFlag:     domain.MessageIsRead,
				Title:        content,
				Content:      content,
				CreatedAt:    time.Time{},
				UpdatedAt:    time.Time{},
				Payload:      string(payloadStr),
			}
			allMessage = append(allMessage, &newMessage)
		}
112 113 114 115 116
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, err
	}
	return allMessage, nil
tangxvhui authored
117 118
}
tangxvhui authored
119 120
// messageTaskRecordAnomaly 反馈异常
func messageTaskRecordAnomaly() ([]*domain.MessagePersonal, error) {
tangxvhui authored
121 122 123 124 125 126 127 128 129 130
	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()
	}()
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	taskDao := dao.NewTaskDao(map[string]interface{}{"transactionContext": transactionContext})
	userDao := dao.NewUserDao(map[string]interface{}{"transactionContext": transactionContext})
	taskData, err := taskDao.TaskRecordAnomalyAll()
	if err != nil {
		return nil, fmt.Errorf("获取任务信息%s", err)
	}
	if len(taskData) == 0 {
		return nil, nil
	}
	var allMessage []*domain.MessagePersonal
	for _, val := range taskData {
		//获取我全上级
		userList, err := userDao.AllParentUser(val.LeaderId)
		if err != nil {
			return nil, fmt.Errorf("获取上级人员信息%s", err)
		}
		if len(userList) == 0 {
			continue
		}
		//通知负责人
		for _, val2 := range userList {
			payload := map[string]string{
				"taskId":    strconv.Itoa(val.TaskId),
				"taskAlias": val.TaskAlias,
				"taskName":  val.TaskName,
			}
			payloadStr, _ := json.Marshal(payload)
			newMessage := domain.MessagePersonal{
				Id:           0,
tangxvhui authored
160
				Types:        domain.MessageTypesTaskRecordApp,
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
				TargetUserId: val2.Id,
				ReadFlag:     domain.MessageIsRead,
				Title:        "",
				Content:      "",
				CreatedAt:    time.Time{},
				UpdatedAt:    time.Time{},
				Payload:      string(payloadStr),
			}
			content := ""
			if val2.Level == 1 {
				content = fmt.Sprintf("【您负责的项目【%s】已超过%d日未反馈进度,请前往该战略任务进行反馈。】", val.TaskName, val.Anomaly)
				newMessage.Content = content
				newMessage.Title = content
				allMessage = append(allMessage, &newMessage)
			}
			if val2.Level == 2 && val.Anomaly > 3 {
				content = fmt.Sprintf("【您下级%s负责的项目【%s】已超过%d日未反馈进度,请前往辅导。】", val.LeaderName, val.TaskName, val.Anomaly)
				newMessage.Content = content
				newMessage.Title = content
				allMessage = append(allMessage, &newMessage)
			}
			if val2.Level == 3 && val.Anomaly > 5 {
				content = fmt.Sprintf("【您下级%s关注的项目【%s】已超过%d日未反馈进度,请前往辅导。】", val.LeaderName, val.TaskName, val.Anomaly)
				newMessage.Content = content
				newMessage.Title = content
				allMessage = append(allMessage, &newMessage)
			}
		}
	}
tangxvhui authored
190 191 192
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, err
	}
193
	return allMessage, nil
tangxvhui authored
194 195
}
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
func saveAllMessagePersonal(msgList []*domain.MessagePersonal) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return err
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	for _, val := range msgList {
		err = messageRepo.Save(val)
		if err != nil {
			return fmt.Errorf("保存MessagePersonal%s", err)
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return err
	}
	return nil
}
tangxvhui authored
221
tangxvhui authored
222
// 发送手机端通知
223 224
func appMessageSend() error {
	var appMessage []*domain.MessagePersonal
tangxvhui authored
225 226 227
	messageList, err := messageTaskStageAnomaly()
	if err != nil {
		return fmt.Errorf("生成里程碑异常的消息通知失败%s", err)
228
	}
tangxvhui authored
229
	appMessage = append(appMessage, messageList...)
230 231 232 233 234
	messageList2, err := messageTaskRecordAnomaly()
	if err != nil {
		return fmt.Errorf("生成任务反馈异常的消息通知失败%s", err)
	}
	appMessage = append(appMessage, messageList2...)
tangxvhui authored
235 236
	openApi := serviceGateway.NewHttplibMmmOpenApiServiceGateway()
	for _, val := range appMessage {
tangxvhui authored
237
		_, err := openApi.PushInfo(0, constant.APP_MESSAGE_KEY, []int64{int64(val.TargetUserId)}, val.Title, val.Content)
tangxvhui authored
238 239 240 241
		if err != nil {
			log.Logger.Error("发送远端openApi关于里程碑异常的消息通知:" + err.Error())
		}
	}
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	err = saveAllMessagePersonal(appMessage)
	if err != nil {
		return fmt.Errorf("保存里程碑异常的消息通知失败%s", err)
	}
	return nil
}

// 关于里程碑异常的消息通知;早上9点 开始检查,并确认发送的消息
func AppMessageRun() {
	nowTime := time.Now()
	y, m, d := nowTime.Date()
	t1 := time.Date(y, m, d, 9, 0, 0, 0, time.Local) //今天的9点
	interval := t1.Sub(nowTime)
	if interval < 0 {
		interval = (24 * time.Hour) + interval
	}
tangxvhui authored
258 259
	// interval := 10 * time.Second
260 261 262
	timer := time.NewTimer(interval)
	for {
		<-timer.C
tangxvhui authored
263
		beginTime := time.Now()
264 265
		err := appMessageSend()
		if err != nil {
tangxvhui authored
266
			log.Logger.Error("发送关于任务异常的消息通知:" + err.Error())
267 268
		}
		timer.Reset(24 * time.Hour)
tangxvhui authored
269
		log.Logger.Info(fmt.Sprintf("发送关于任务异常的消息通知用时:%v", time.Since(beginTime).Seconds()))
270
	}
tangxvhui authored
271
}