message_personal.go 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 160 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 190 191 192 193 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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
package service

import (
	"encoding/json"
	"fmt"
	"strconv"
	"strings"
	"time"

	"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/adapter"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/notify/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
)

// 个人信息提示

type MessagePersonalService struct {
}

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

// 获取今天的周期综合自评消息提示
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())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()

	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,
	})
	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,
			"payload":      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 resp, nil
}

// 获取关于任务反馈碑异常的消息
// 每日一次
func (srv *MessagePersonalService) TodayMessageTaskRecordAnomaly(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
	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()
	}()

	messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	nowDay := time.Now().Format("2006-01-02")
	cnt, _, err := messageRepo.Find(map[string]interface{}{
		"types":        domain.MessageTypesTaskRecord,
		"targetUserId": param.UserId,
		"createdAtDay": nowDay,
		"limit":        1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查任务里程碑异常的消息"+err.Error())
	}
	if cnt > 0 {
		resp := map[string]interface{}{
			"needNotify": false,
			"list":       []adapter.MessageListAdapter{},
		}
		return resp, nil
	}
	userDao := dao.NewUserDao(map[string]interface{}{"transactionContext": transactionContext})

	taskDao := dao.NewTaskDao(map[string]interface{}{"transactionContext": transactionContext})
	// 我作为任务负责人
	leaderIds := []string{strconv.Itoa(param.UserId)}
	taskStageList, err := taskDao.TaskAnomalyByLeader(leaderIds, 1)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查任务里程碑异常的消息"+err.Error())
	}
	//获取员工全部子集
	childUser, err := userDao.AllChildUser(param.UserId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取下级员工"+err.Error())
	}
	childUserId := []string{}
	for _, val := range childUser {
		if val.Level == 2 {
			childUserId = append(childUserId, strconv.Itoa(val.Id))
		}
	}
	//我作为上级, 获取我的下一级的异常任务
	var taskStageList2 []dao.TaskData2
	if len(childUserId) > 0 {
		taskStageList2, err = taskDao.TaskAnomalyByLeader(childUserId, 3)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查任务里程碑异常的消息"+err.Error())
		}
	}
	childUserId2 := []string{}
	for _, val := range childUser {
		if val.Level >= 3 {
			childUserId2 = append(childUserId2, strconv.Itoa(val.Id))
		}
	}
	// 我作为上级,获取我的下下级的异常任务
	var taskStageList3 []dao.TaskData2
	if len(childUserId2) > 0 {
		taskStageList3, err = taskDao.TaskAnomalyByLeader(childUserId2, 5)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查任务里程碑异常的消息"+err.Error())
		}
	}

	msgList := []adapter.MessageListAdapter{}
	for _, val := range taskStageList {
		s := fmt.Sprintf("【您负责的项目【%s】已超过%s日未反馈进度,请前往该战略任务进行反馈。】", val.Name, val.Anomaly)
		msgList = append(msgList, adapter.MessageListAdapter{
			Content: s,
		})
		payload := map[string]string{
			"id":         strconv.Itoa(val.Id),
			"taskName":   val.Name,
			"taskAlias":  val.Alias,
			"leaderName": val.LeaderName,
		}
		payloadStr, _ := json.Marshal(payload)
		newMessage := domain.MessagePersonal{
			Id:           0,
			Types:        domain.MessageTypesTaskRecord,
			TargetUserId: param.UserId,
			ReadFlag:     domain.MessageIsRead,
			Title:        s,
			Content:      s,
			CreatedAt:    time.Time{},
			UpdatedAt:    time.Time{},
			Payload:      string(payloadStr),
		}
		err = messageRepo.Save(&newMessage)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
	for _, val := range taskStageList2 {
		s := fmt.Sprintf("【您下级%s负责的项目【%s】已超过%s日未反馈进度,请前往辅导。】", val.LeaderName, val.Name, val.Anomaly)
		msgList = append(msgList, adapter.MessageListAdapter{
			Content: s,
		})
		payload := map[string]string{
			"id":         strconv.Itoa(val.Id),
			"taskName":   val.Name,
			"taskAlias":  val.Alias,
			"leaderName": val.LeaderName,
		}
		payloadStr, _ := json.Marshal(payload)
		newMessage := domain.MessagePersonal{
			Id:           0,
			Types:        domain.MessageTypesTaskRecord,
			TargetUserId: param.UserId,
			ReadFlag:     domain.MessageIsRead,
			Title:        s,
			Content:      s,
			CreatedAt:    time.Time{},
			UpdatedAt:    time.Time{},
			Payload:      string(payloadStr),
		}
		err = messageRepo.Save(&newMessage)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
	for _, val := range taskStageList3 {
		s := fmt.Sprintf("【您下级%s关注的项目【%s】已超过%s日未反馈进度,请前往辅导。】", val.LeaderName, val.Name, val.Anomaly)
		msgList = append(msgList, adapter.MessageListAdapter{
			Content: s,
		})
		payload := map[string]string{
			"id":         strconv.Itoa(val.Id),
			"taskName":   val.Name,
			"taskAlias":  val.Alias,
			"leaderName": val.LeaderName,
		}
		payloadStr, _ := json.Marshal(payload)
		newMessage := domain.MessagePersonal{
			Id:           0,
			Types:        domain.MessageTypesTaskRecord,
			TargetUserId: param.UserId,
			ReadFlag:     domain.MessageIsRead,
			Title:        s,
			Content:      s,
			CreatedAt:    time.Time{},
			UpdatedAt:    time.Time{},
			Payload:      string(payloadStr),
		}
		err = messageRepo.Save(&newMessage)
		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())
	}

	resp := map[string]interface{}{
		"needNotify": true,
		"list":       msgList,
	}
	if len(msgList) == 0 {
		resp["needNotify"] = false
	}
	return resp, nil
}

// TodayMessage 任务里程碑变更通知
func (srv *MessagePersonalService) TodayMessageTaskStageModify(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
	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()
	}()

	messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	_, messageList, err := messageRepo.Find(map[string]interface{}{
		"types":        domain.MessageTypesTaskModify,
		"targetUserId": param.UserId,
		"readFlag":     domain.MessageUnread,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查个人消息的消息"+err.Error())
	}
	for _, val := range messageList {
		val.ReadFlag = domain.MessageIsRead
		err = messageRepo.Save(val)
		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())
	}
	msgList := []adapter.MessageListAdapter{}
	for _, val := range messageList {
		msgList = append(msgList, adapter.MessageListAdapter{
			Content: val.Content,
		})
	}
	resp := map[string]interface{}{
		"needNotify": true,
		"list":       msgList,
	}
	if len(msgList) == 0 {
		resp["needNotify"] = false
	}
	return resp, nil
}

// 任务里程碑异常的消息弹窗通知,不再使用
func (srv *MessagePersonalService) TodayMessageTaskStageAnomalyV2(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
	resp := map[string]interface{}{
		"needNotify": false,
		"list":       []adapter.MessageListAdapter{},
	}
	return resp, nil
}

// 任务反馈碑异常的消息弹窗内容,不再使用
func (srv *MessagePersonalService) TodayMessageTaskRecordAnomalyV2(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
	resp := map[string]interface{}{
		"needNotify": false,
		"list":       []adapter.MessageListAdapter{},
	}
	return resp, nil
}

// 任务里程碑变更通知,不再使用
func (srv *MessagePersonalService) TodayMessageTaskStageModifyV2(param *command.GetUserMessageCommand) (map[string]interface{}, error) {
	resp := map[string]interface{}{
		"needNotify": false,
		"list":       []adapter.MessageListAdapter{},
	}
	return resp, nil
}

// 里程碑异常的消息弹窗通知
func (srv *MessagePersonalService) TodayMessageTaskAnomaly(param *command.GetUserMessageCommand) (map[string]interface{}, error) {

	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()
	}()

	messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	nowDay := time.Now().Format("2006-01-02")
	cnt, _, err := messageRepo.Find(map[string]interface{}{
		"types":        domain.MessageTypesTaskRecord,
		"targetUserId": param.UserId,
		"createdAtDay": nowDay,
		"limit":        1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查任务里程碑异常的消息"+err.Error())
	}
	if cnt > 0 {
		resp := map[string]interface{}{
			"needNotify": false,
			"list":       []adapter.MessageListAdapter{},
		}
		return resp, nil
	}
	userDao := dao.NewUserDao(map[string]interface{}{"transactionContext": transactionContext})
	// 获取员工全部子集
	childUser, err := userDao.AllChildUser(param.UserId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取下级员工"+err.Error())
	}
	taskDao := dao.NewTaskDao(map[string]interface{}{"transactionContext": transactionContext})
	//获取异常任务数据
	childUserId := []string{}
	for _, val := range childUser {
		if val.Level == 2 || val.Level == 1 {
			childUserId = append(childUserId, strconv.Itoa(val.Id))
		}
	}
	taskList, err := taskDao.TaskIsAnomalyForNotice(param.CompanyId, childUserId, param.UserId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "检查任务里异常的消息"+err.Error())
	}
	myUserId := strconv.Itoa(param.UserId)

	var num1, num2, num3 int //我负责的任务,我的下级任务,我相关的任务
	var todayMessage []domain.MessagePersonal
	var msg string
	for _, val := range taskList {
		payload := map[string]string{
			"id":         strconv.Itoa(val.TaskId),
			"taskName":   val.TaskName,
			"taskAlias":  val.TaskAlias,
			"leaderName": val.LeaderName,
		}
		payloadStr, _ := json.Marshal(payload)
		newMessage := domain.MessagePersonal{
			Id:           0,
			Types:        domain.MessageTypesTaskRecord,
			TargetUserId: param.UserId,
			ReadFlag:     domain.MessageIsRead,
			Title:        "",
			Content:      "",
			CreatedAt:    time.Time{},
			UpdatedAt:    time.Time{},
			Payload:      string(payloadStr),
		}
		if val.LeaderId == myUserId {
			//是我负责的项目

			t := 0
			if val.WarnFlag > 0 {
				t += 1
				msg = fmt.Sprintf("您负责的任务【%s】里程碑发生异常,请前往处理。", val.TaskAlias)
			}
			if val.Anomaly > 1 {
				t += 1
				msg = fmt.Sprintf("您负责的任务【%s】已超过%d天没反馈,有异常风险,请前往处理。", val.TaskAlias, val.Anomaly)
			}
			if t > 1 {
				msg = fmt.Sprintf("您负责的任务【%s】里程碑异常、反馈异常,请前往处理。", val.TaskAlias)
			}
			newMessage.Title = msg
			newMessage.Content = msg
			if t > 0 {
				num1 += 1
				todayMessage = append(todayMessage, newMessage)
			}
			continue
		}
		var inChild bool
		for _, val2 := range childUserId {
			//我的下级负责的任务
			if val2 != val.LeaderId {
				continue
			}
			inChild = true
			t := 0
			t2 := []string{}
			if val.WarnFlag > 0 {
				t += 1
				msg = fmt.Sprintf("您下级负责的任务【%s】里程碑发生异常,请前往处理。", val.TaskAlias)
				t2 = append(t2, "里程碑异常")
			}
			if val.Anomaly > 1 {
				t += 1
				msg = fmt.Sprintf("您下级负责的任务【%s】已超过%d天没反馈,有异常风险,请前往处理。", val.TaskAlias, val.Anomaly)
				t2 = append(t2, "反馈异常")
			}
			if val.AssistFlag > 0 {
				t += 1
				msg = fmt.Sprintf("您需辅导的任务【%s】发生异常,请前往处理", val.TaskAlias)
				t2 = append(t2, "辅导异常")
			}
			if t > 1 {
				msg = fmt.Sprintf("您下级负责的任务【%s】%s,请前往处理。", val.TaskAlias, strings.Join(t2, "、"))
			}
			newMessage.Title = msg
			newMessage.Content = msg
			if t > 0 {
				num2 += 1
				todayMessage = append(todayMessage, newMessage)
			}
			break
		}
		if inChild {
			continue
		}

		// 与我相关的任务
		t := 0
		if val.WarnFlag > 0 {
			t += 1
			msg = fmt.Sprintf("与您相关的任务【%s】里程碑发生异常,请知晓。", val.TaskAlias)
		}
		if val.Anomaly > 1 {
			t += 1
			msg = fmt.Sprintf("与您相关的任务【%s】已超过%d天没反馈,有异常风险,请知晓。", val.TaskAlias, val.Anomaly)
		}
		if t > 1 {
			msg = fmt.Sprintf("与您相关的任务【%s】里程碑异常、反馈异常,请知晓。", val.TaskAlias)
		}
		newMessage.Title = msg
		newMessage.Content = msg
		if t > 0 {
			num3 += 1
			todayMessage = append(todayMessage, newMessage)
		}
	}

	if len(todayMessage) > 0 {
		for i := range todayMessage {
			err = messageRepo.Save(&todayMessage[i])
			if err != nil {
				resp := map[string]interface{}{
					"needNotify": false,
					"list":       []adapter.MessageListAdapter{{Content: err.Error()}},
				}
				return resp, nil
			}
		}
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	resp := map[string]interface{}{
		"needNotify": false,
		"pageSkip":   "1",
	}
	if (num1 + num2 + num3) == 1 {
		resp["needNotify"] = true
		resp["list"] = []adapter.MessageListAdapter{{Content: msg}}
		if num1 > 0 {
			//跳我负责的任务
			resp["pageSkip"] = "1"
		}
		if num2 > 0 {
			//跳我的下级任务
			resp["pageSkip"] = "2"
		}
		if num3 > 0 {
			//跳我的相关的任务
			resp["pageSkip"] = "3"
		}

	} else if (num1 + num2 + num3) > 1 {
		resp["needNotify"] = true
		str := []string{}
		if num1 > 0 {
			str = append(str, fmt.Sprintf("您负责的任务%d个任务", num1))
		}
		if num2 > 0 {
			str = append(str, fmt.Sprintf("下级负责的%d个任务", num2))
		}
		if num3 > 0 {
			str = append(str, fmt.Sprintf("与您有关的%d个任务发生异常", num3))
		}
		resp["list"] = []adapter.MessageListAdapter{{Content: strings.Join(str, "、") + "发生异常,请前往处理。"}}
	}
	return resp, nil
}