审查视图

pkg/application/task/service/service.go 35.8 KB
tangxvhui authored
1 2 3
package service

import (
tangxvhui authored
4 5
	"encoding/json"
	"fmt"
tangxvhui authored
6
	"strconv"
tangxvhui authored
7 8 9
	"time"

	"github.com/linmadan/egglib-go/core/application"
tangxvhui authored
10
	"github.com/linmadan/egglib-go/utils/tool_funs"
tangxvhui authored
11
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
tangxvhui authored
12
	roleService "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/role"
tangxvhui authored
13
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/adapter"
tangxvhui authored
14 15
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
tangxvhui authored
16
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
tangxvhui authored
17 18 19 20
)

type TaskService struct{}
tangxvhui authored
21 22 23 24 25
func NewTaskService() *TaskService {
	newService := &TaskService{}
	return newService
}
tangxvhui authored
26 27 28 29 30 31
func dayEndTime(t time.Time) time.Time {
	y, m, d := t.Local().Date()
	t2 := time.Date(y, m, d, 23, 59, 59, 0, time.Local)
	return t2
}
32 33 34 35 36 37
func dayZeroTime(t time.Time) time.Time {
	y, m, d := t.Local().Date()
	t2 := time.Date(y, m, d, 0, 0, 0, 0, time.Local)
	return t2
}
tangxvhui authored
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
func (srv TaskService) CreateTaskTest(param *command.CreateTaskCommand) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	err = srv.CreateTask(transactionContext, param)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}
tangxvhui authored
59 60 61 62 63 64 65 66 67
func (srv TaskService) CreateTask(transactionContext application.TransactionContext, param *command.CreateTaskCommand) error {
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	cnt, _, err := taskRepo.Find(map[string]interface{}{
		"name":     param.Name,
68
		"leaderId": strconv.Itoa(param.LeaderId),
tangxvhui authored
69
		"limit":    1,
tangxvhui authored
70 71 72 73 74
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务失败:"+err.Error())
	}
	if cnt > 0 {
tangxvhui authored
75
		//TODO
tangxvhui authored
76 77 78
		//任务已存在
		return nil
	}
tangxvhui authored
79 80 81 82 83 84 85 86 87 88 89 90 91 92
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})

	_, leaderList, err := userRepo.Find(map[string]interface{}{
		"id": param.LeaderId,
	})
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询人员失败:"+err.Error())
	}
	if len(leaderList) == 0 {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "没有查询到人员")
	}
	leaderData := leaderList[0]
tangxvhui authored
93 94
	nowTime := time.Now()
	newTask := domain.Task{
tangxvhui authored
95 96 97 98 99 100 101 102 103 104 105 106
		Id:        0,
		CreatedAt: time.Time{},
		UpdatedAt: time.Time{},
		DeletedAt: nil,
		CompanyId: int(leaderData.CompanyId),
		Name:      param.Name,
		Alias:     param.Name,
		Leader: domain.TaskLeader{
			Id:      leaderData.Id,
			Account: leaderData.Account,
			Name:    leaderData.Name,
		},
tangxvhui authored
107
		Status:      domain.TaskWait,
tangxvhui authored
108
		Level:       0,
tangxvhui authored
109
		LevelName:   "",
tangxvhui authored
110 111
		RelatedUser: []int{},
		RunAt:       nowTime.Unix(),
tangxvhui authored
112
		StopAt:      0,
tangxvhui authored
113
	}
tangxvhui authored
114
	newTask.ApplyLevelName("战略任务")
tangxvhui authored
115 116 117 118
	err = taskRepo.Save(&newTask)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
	}
tangxvhui authored
119
	nowEndTime := dayEndTime(nowTime)
tangxvhui authored
120 121 122 123 124 125
	newTaskStage := []*domain.TaskStage{
		{
			Id:              0,
			TaskId:          newTask.Id,
			Name:            "里程碑1",
			SortBy:          1,
tangxvhui authored
126 127
			PlanCompletedAt: nowEndTime.Add(30 * 24 * time.Hour).Unix(),
			RealCompletedAt: 0,
tangxvhui authored
128 129 130 131 132 133
		},
		{
			Id:              0,
			TaskId:          newTask.Id,
			Name:            "里程碑2",
			SortBy:          2,
tangxvhui authored
134 135
			PlanCompletedAt: nowEndTime.Add(60 * 24 * time.Hour).Unix(),
			RealCompletedAt: 0,
tangxvhui authored
136 137 138 139 140 141
		},
		{
			Id:              0,
			TaskId:          newTask.Id,
			Name:            "里程碑3",
			SortBy:          3,
tangxvhui authored
142 143
			PlanCompletedAt: nowEndTime.Add(90 * 24 * time.Hour).Unix(),
			RealCompletedAt: 0,
tangxvhui authored
144 145
		},
	}
tangxvhui authored
146
tangxvhui authored
147 148 149 150 151 152 153
	err = taskStageRepo.Save(newTaskStage)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "创建任务失败:"+err.Error())
	}
	return nil
}
tangxvhui authored
154
// 更新任务
tangxvhui authored
155 156 157 158 159 160 161 162 163 164 165
func (srv TaskService) UpdateTask(param *command.UpdateTaskCommand) (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()
	}()
tangxvhui authored
166 167 168 169 170 171 172 173 174 175
	if len(param.StageList) < 3 {
		return map[string]interface{}{
			"id": param.Id,
		}, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑数量至少3个")
	}
	if len(param.StageList) > 5 {
		return map[string]interface{}{
			"id": param.Id,
		}, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑数量太多")
	}
tangxvhui authored
176 177 178
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
tangxvhui authored
179 180 181
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
tangxvhui authored
182 183 184 185 186 187 188 189 190 191 192 193
	taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskData, err := taskRepo.FindOne(map[string]interface{}{
		"id": param.Id,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	_, stageList, err := taskStageRepo.Find(map[string]interface{}{
		"taskId": param.Id,
	})
tangxvhui authored
194 195 196
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	//任务的相关人员
	relatedUserIds := []int{}
	for _, val := range param.RelatedUserId {
		id, _ := strconv.Atoi(val)
		relatedUserIds = append(relatedUserIds, id)
	}
	var relatedUser []*domain.User
	if len(relatedUserIds) > 0 {
		_, relatedUser, err = userRepo.Find(map[string]interface{}{
			"ids": relatedUserIds,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取任务的相关人员"+err.Error())
		}
	}
	//更新相关人员
	taskData.RelatedUser = []int{}
	for _, val := range relatedUser {
		taskData.RelatedUser = append(taskData.RelatedUser, int(val.Id))
	}
	//更新任务名称
tangxvhui authored
218 219 220 221 222 223 224 225 226 227
	taskData.Alias = param.Alias
	taskData.ApplyLevelName(param.LevelName)
	nowTime := time.Now()
	stageMap := map[int]*domain.TaskStage{}
	for _, val := range stageList {
		val.DeletedAt = &nowTime
		stageMap[val.Id] = val
	}
	lastPlanCompletedAt := time.Time{}
	newStageList := []*domain.TaskStage{}
tangxvhui authored
228 229
	//标记完成时间变更的里程碑
	markStage := []domain.TaskStage{}
tangxvhui authored
230 231 232 233 234 235 236
	for i, val2 := range param.StageList {
		t, err := time.ParseInLocation("2006-01-02", val2.PlanCompletedAt, time.Local)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "请填写里程碑完成时间")
		}
		if i > 0 {
			if lastPlanCompletedAt.After(t) {
tangxvhui authored
237
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑["+val2.Name+"]完成时间填写错误")
tangxvhui authored
238 239 240 241 242 243 244 245 246
			}
		}
		lastPlanCompletedAt = t
		if val2.Id == 0 {
			newStage := domain.TaskStage{
				Id:              0,
				TaskId:          taskData.Id,
				Name:            val2.Name,
				SortBy:          i + 1,
tangxvhui authored
247
				PlanCompletedAt: 0,
tangxvhui authored
248
				RealCompletedAt: 0,
tangxvhui authored
249
			}
tangxvhui authored
250
			newStage.SetPlanCompleted(t)
tangxvhui authored
251 252 253 254 255 256 257 258 259 260
			newStageList = append(newStageList, &newStage)
			continue
		}
		oldStage, ok := stageMap[val2.Id]
		if !ok {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "里程碑数据被其他人变更")
		}
		oldStage.DeletedAt = nil
		oldStage.SortBy = i + 1
		oldStage.Name = val2.Name
tangxvhui authored
261
		if ok := oldStage.SetPlanCompleted(t); ok {
tangxvhui authored
262 263
			markStage = append(markStage, *oldStage)
		}
tangxvhui authored
264
	}
tangxvhui authored
265
tangxvhui authored
266
	stageList = append(stageList, newStageList...)
tangxvhui authored
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	//查询是否已经有每日评估
	taskRecordRepo := factory.CreateTaskRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	cnt, _, err := taskRecordRepo.Find(map[string]interface{}{
		"taskId": taskData.Id,
		"limit":  1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "相关联的任务反馈信息"+err.Error())
	}
	if cnt > 0 {
		for _, v := range stageList {
			if v.DeletedAt != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "已有日评数据,不能删除里程碑")
			}
		}
	}
tangxvhui authored
285
tangxvhui authored
286 287 288 289 290 291 292 293
	err = taskStageRepo.Save(stageList)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = taskRepo.Save(taskData)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
294 295 296 297 298 299 300
	messageRepo := factory.CreateMessagePersonalRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//更具markStage 记录的里程碑数据生成通知
	for _, val := range markStage {
		for _, val2 := range taskData.RelatedUser {
			t1 := time.Unix(val.PlanCompletedAt, 0).Format("2006-01-02")
tangxvhui authored
301
			s := fmt.Sprintf("【%s 任务里程碑 %s 完成时间变更为 %s,请知晓】", taskData.Name, val.Name, t1)
tangxvhui authored
302
			payload := map[string]string{
tangxvhui authored
303 304 305
				"id":            strconv.Itoa(val.Id),
				"taskName":      taskData.Name,
				"taskStageName": val.Name,
tangxvhui authored
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
			}
			payloadStr, _ := json.Marshal(payload)
			newMessage := domain.MessagePersonal{
				Id:           0,
				Types:        domain.MessageTypesTaskModify,
				TargetUserId: val2,
				ReadFlag:     domain.MessageUnread,
				Title:        s,
				Content:      s,
				Payload:      string(payloadStr),
			}
			err = messageRepo.Save(&newMessage)
			if err != nil {
				return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
			}
		}
	}
tangxvhui authored
323 324 325
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
326 327 328
	return map[string]interface{}{
		"id": param.Id,
	}, nil
tangxvhui authored
329 330
}
tangxvhui authored
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
// 获取任务详情
func (srv TaskService) GetTaskInfo(param *command.GetTaskCommand) (*adapter.TaskInfoAdapter, 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()
	}()
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	userRepo := factory.CreateUserRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskData, err := taskRepo.FindOne(map[string]interface{}{
		"id": param.TaskId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	_, stageList, err := taskStageRepo.Find(map[string]interface{}{
		"taskId": param.TaskId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	relatedUserId := taskData.RelatedUser
tangxvhui authored
366 367 368 369 370 371 372 373
	var relatedUserList []*domain.User
	if len(relatedUserId) > 0 {
		_, relatedUserList, err = userRepo.Find(map[string]interface{}{
			"ids": relatedUserId,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取相关人员"+err.Error())
		}
tangxvhui authored
374
	}
tangxvhui authored
375 376 377 378 379 380 381 382 383 384 385 386

	//查询是否已经有每日评估
	taskRecordRepo := factory.CreateTaskRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	cnt, _, err := taskRecordRepo.Find(map[string]interface{}{
		"taskId": param.TaskId,
		"limit":  1,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "相关联的任务反馈信息"+err.Error())
	}
tangxvhui authored
387 388 389 390 391
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	result := adapter.TaskInfoAdapter{
tangxvhui authored
392 393 394 395
		Id:       taskData.Id,
		Name:     taskData.Name,
		Alias:    taskData.Alias,
		LeaderId: taskData.Leader.Id,
tangxvhui authored
396 397 398 399 400
		Leader: adapter.UserData{
			Id:      taskData.Leader.Id,
			Account: taskData.Leader.Account,
			Name:    taskData.Leader.Name,
		},
401 402 403
		Status:            int(taskData.Status),
		StatusDescription: taskData.StatusDescription(),
		Level:             taskData.Level,
tangxvhui authored
404
		LevelName:         taskData.LevelName,
405 406 407
		RelatedUserId:     []string{},
		RelatedUser:       []adapter.UserData{},
		StageList:         []adapter.TaskStage{},
tangxvhui authored
408 409 410 411 412 413 414 415 416 417
	}
	for _, val := range relatedUserList {
		idStr := strconv.Itoa(int(val.Id))
		result.RelatedUserId = append(result.RelatedUserId, idStr)
		result.RelatedUser = append(result.RelatedUser, adapter.UserData{
			Id:      val.Id,
			Account: val.Account,
			Name:    val.Name,
		})
	}
tangxvhui authored
418
tangxvhui authored
419
	for _, val := range stageList {
tangxvhui authored
420 421 422 423 424 425 426 427
		t1 := ""
		t2 := ""
		if val.PlanCompletedAt > 0 {
			t1 = time.Unix(val.PlanCompletedAt, 0).Local().Format("2006-01-02")
		}
		if val.RealCompletedAt > 0 {
			t2 = time.Unix(val.RealCompletedAt, 0).Local().Format("2006-01-02")
		}
tangxvhui authored
428
		stage := adapter.TaskStage{
429 430 431 432 433 434
			Id:                val.Id,
			Name:              val.Name,
			PlanCompletedAt:   t1,
			RealCompletedAt:   t2,
			SortBy:            val.SortBy,
			StatusDescription: val.StatusDescription(),
tangxvhui authored
435 436 437 438 439
		}
		if cnt > 0 {
			stage.IsRef = true
		}
		result.StageList = append(result.StageList, stage)
tangxvhui authored
440 441
	}
	return &result, nil
tangxvhui authored
442 443
}
tangxvhui authored
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
// RunTask 启动任务
func (srv TaskService) RunTask(param *command.RunTaskCommand) (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()
	}()
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskData, err := taskRepo.FindOne(map[string]interface{}{
		"id": param.TaskId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if taskData.Status == domain.TaskRunning {
		return map[string]interface{}{"id": param.TaskId}, nil
	}
	taskData.Status = domain.TaskRunning
	taskData.RunAt = time.Now().Unix()
	err = taskRepo.Save(taskData)
	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 map[string]interface{}{"id": param.TaskId}, nil
}

// RunTask 停止任务
func (srv TaskService) StopTask(param *command.StopTaskCommand) (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()
	}()
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskData, err := taskRepo.FindOne(map[string]interface{}{
		"id": param.TaskId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if taskData.Status == domain.TaskStop {
		return map[string]interface{}{"id": param.TaskId}, nil
	}

	taskData.Status = domain.TaskStop
	taskData.StopAt = time.Now().Unix()
	err = taskRepo.Save(taskData)
	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 map[string]interface{}{"id": param.TaskId}, nil
}

// ListTask 任务列表
func (srv TaskService) ListTask(param *command.ListTaskCommand) (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()
	}()
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	//
	condition := map[string]interface{}{
		"offset":    0,
		"limit":     20,
		"companyId": param.CompanyId,
	}
	if len(param.SearchWord) > 0 {
		condition["aliasOrLeader"] = "%" + param.SearchWord + "%"
	}
	if param.PageSize > 0 {
		condition["limit"] = param.PageSize
		if param.PageNumber > 0 {
			condition["offset"] = (param.PageNumber - 1) * param.PageSize
		}
	}

	taskCnt, taskList, err := taskRepo.Find(condition)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务数据"+err.Error())
	}
	taskIdList := []int{}
	for _, val := range taskList {
		taskIdList = append(taskIdList, val.Id)
	}
tangxvhui authored
559 560 561 562 563 564 565 566
	var stageList []*domain.TaskStage
	if len(taskIdList) > 0 {
		_, stageList, err = taskStageRepo.Find(map[string]interface{}{
			"taskIdList": taskIdList,
		})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "查询任务阶段数据"+err.Error())
		}
tangxvhui authored
567
	}
tangxvhui authored
568
tangxvhui authored
569 570 571 572 573
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	taskResult := []*adapter.TaskItem{}
tangxvhui authored
574 575 576
	taskMapResult := map[int]*adapter.TaskItem{}
	for _, val := range taskList {
		tk := adapter.TaskItem{
tangxvhui authored
577 578 579 580
			Id:        val.Id,
			Name:      val.Name,
			Alias:     val.Alias,
			Leader:    val.Leader.Name,
tangxvhui authored
581 582
			Status:    int(val.Status),
			Level:     val.Level,
tangxvhui authored
583
			LevelName: val.LevelName,
tangxvhui authored
584
			Anomaly:   val.Anomaly,
585
			WarnFlag:  int(val.WarnFlag),
tangxvhui authored
586 587 588 589 590
			// StageA:    adapter.TaskStage{},
			// StageB:    adapter.TaskStage{},
			// StageC:    adapter.TaskStage{},
			// StageD:    adapter.TaskStage{},
			// StageE:    adapter.TaskStage{},
tangxvhui authored
591 592 593 594 595 596 597 598 599
		}
		taskResult = append(taskResult, &tk)
		taskMapResult[val.Id] = &tk
	}
	for _, val := range stageList {
		taskData, ok := taskMapResult[val.TaskId]
		if !ok {
			continue
		}
tangxvhui authored
600
		stage := adapter.TaskStage{
601 602 603 604
			Id:                val.Id,
			Name:              val.Name,
			StatusDescription: val.StatusDescription(),
			SortBy:            val.SortBy,
605 606 607 608 609
			PlanCompletedAt:   time.Unix(int64(val.PlanCompletedAt), 0).Local().Format("2006-01-02"),
			RealCompletedAt:   "",
		}
		if val.RealCompletedAt > 0 {
			stage.RealCompletedAt = time.Unix(int64(val.RealCompletedAt), 0).Local().Format("2006-01-02")
tangxvhui authored
610
		}
tangxvhui authored
611 612
		switch val.SortBy {
		case 1:
tangxvhui authored
613
			taskData.StageA = stage
tangxvhui authored
614
		case 2:
tangxvhui authored
615
			taskData.StageB = stage
tangxvhui authored
616
		case 3:
tangxvhui authored
617
			taskData.StageC = stage
tangxvhui authored
618
		case 4:
tangxvhui authored
619
			taskData.StageD = stage
tangxvhui authored
620
		case 5:
tangxvhui authored
621
			taskData.StageE = stage
tangxvhui authored
622 623
		}
	}
tangxvhui authored
624 625
	result := tool_funs.SimpleWrapGridMap(int64(taskCnt), taskResult)
	return result, nil
tangxvhui authored
626
}
tangxvhui authored
627
tangxvhui authored
628
// CancelAttention 用户取消关注某个任务
郑周 authored
629
func (srv TaskService) CancelAttention(param *command.CancelAttentionCommand) error {
tangxvhui authored
630 631 632 633 634 635 636 637 638 639
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
tangxvhui authored
640 641 642 643 644 645 646 647 648 649 650 651 652
	taskIgnoreRepo := factory.CreateTaskIgnoreRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskIgnore := domain.TaskIgnore{
		Id:        0,
		TaskId:    param.TaskId,
		UserId:    param.UserId,
		CreatedAt: time.Time{},
	}
	err = taskIgnoreRepo.Save(&taskIgnore)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
653 654 655 656 657
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil
}
tangxvhui authored
658 659

// 员工绩效-任务管理-列表
郑周 authored
660
func (srv TaskService) ListTask2(param *command.SearchTaskCommand) (map[string]interface{}, error) {
tangxvhui authored
661 662 663 664 665 666 667 668 669 670 671 672 673 674
	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()
	}()
	hrbpFlag, err := roleService.GetHrBp(transactionContext, int(param.CompanyId), int(param.UserId))
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息"+err.Error())
	}
tangxvhui authored
675 676 677
	if len(param.SearchWord) > 0 {
		param.SearchWord = "%" + param.SearchWord + "%"
	}
tangxvhui authored
678
	if hrbpFlag == domain.RoleTypeSystem {
郑周 authored
679
		listData, err := srv.listTask2ForHrbp(param)
tangxvhui authored
680 681
		return listData, err
	}
tangxvhui authored
682 683 684 685 686 687 688 689 690
	var limit = 20
	var offset = 0
	if param.PageSize > 0 {
		limit = param.PageSize
		if param.PageNumber > 0 {
			offset = (param.PageNumber - 1) * param.PageSize
		}
	}
	condition := dao.ListTaskCondition{
tangxvhui authored
691 692 693 694
		Limit:      limit,
		Offset:     offset,
		UserId:     param.UserId,
		CompanyId:  param.CompanyId,
tangxvhui authored
695
		OnlyMy:     param.OnlyMy,
696
		SearchWord: param.SearchWord,
tangxvhui authored
697
	}
tangxvhui authored
698
tangxvhui authored
699
	taskDao := dao.NewTaskDao(map[string]interface{}{
tangxvhui authored
700 701
		"transactionContext": transactionContext,
	})
tangxvhui authored
702
	//任务列表数据
tangxvhui authored
703
	tasklistData, err := taskDao.ListTaskStageNotHrbp(condition)
tangxvhui authored
704 705 706
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取任务列表"+err.Error())
	}
tangxvhui authored
707
	//查到的任务数据总数
tangxvhui authored
708
	taskCount, err := taskDao.CountTaskStageNotHrbp(condition)
tangxvhui authored
709 710 711
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统计任务总数"+err.Error())
	}
tangxvhui authored
712
	//
tangxvhui authored
713 714 715 716 717 718 719 720
	taskStageAnomalyCnt, err := taskDao.CountTaskStageAnomalyNotHrbp(condition)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统计任务里程碑总数"+err.Error())
	}
	taskAnomalyCnt, err := taskDao.CountTaskAnomalyNotHrbp(condition)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统计任务里程碑总数"+err.Error())
	}
tangxvhui authored
721 722 723
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
tangxvhui authored
724
tangxvhui authored
725 726 727 728 729 730 731 732 733 734 735 736 737 738
	taskResult := []*adapter.TaskItem{}
	taskMapResult := map[int]*adapter.TaskItem{}
	for _, val := range tasklistData {
		if _, ok := taskMapResult[val.TaskId]; ok {
			continue
		}
		tk := adapter.TaskItem{
			Id:        val.TaskId,
			Name:      val.TaskName,
			Alias:     val.TaskAliasName,
			Leader:    val.LeaderName,
			Status:    val.TaskStatus,
			Level:     val.Level,
			LevelName: val.LevelName,
739
			UpdatedAt: val.UpdatedAt.Local().Format("2006-01-02 15:04:05"),
tangxvhui authored
740
			Anomaly:   val.Anomaly,
741
			WarnFlag:  val.WarnFlag,
tangxvhui authored
742 743 744 745 746 747 748 749 750 751
		}
		taskMapResult[val.TaskId] = &tk
		taskResult = append(taskResult, &tk)
	}
	for _, val := range tasklistData {
		taskData, ok := taskMapResult[val.TaskId]
		if !ok {
			continue
		}
		stage := adapter.TaskStage{
tangxvhui authored
752 753 754 755 756 757 758 759
			Id:              val.StageId,
			Name:            val.StageName,
			SortBy:          val.StageSortBy,
			PlanCompletedAt: time.Unix(int64(val.PlanCompletedAt), 0).Local().Format("2006-01-02"),
			RealCompletedAt: "",
		}
		if val.RealCompletedAt > 0 {
			stage.RealCompletedAt = time.Unix(int64(val.RealCompletedAt), 0).Local().Format("2006-01-02")
tangxvhui authored
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
		}
		switch val.StageSortBy {
		case 1:
			taskData.StageA = stage
		case 2:
			taskData.StageB = stage
		case 3:
			taskData.StageC = stage
		case 4:
			taskData.StageD = stage
		case 5:
			taskData.StageE = stage
		}
	}
	result := tool_funs.SimpleWrapGridMap(int64(taskCount), taskResult)
tangxvhui authored
775 776
	result["taskStageAnomaly"] = taskStageAnomalyCnt
	result["taskAnomalyCnt"] = taskAnomalyCnt
tangxvhui authored
777
	return result, nil
tangxvhui authored
778
}
tangxvhui authored
779
780
// 以hrbp角色权限获取任务列表
tangxvhui authored
781
// 统计里程碑异常数,反馈异常数
郑周 authored
782
func (srv TaskService) listTask2ForHrbp(param *command.SearchTaskCommand) (map[string]interface{}, error) {
tangxvhui authored
783 784 785 786 787 788 789 790 791
	var limit = 20
	var offset = 0
	if param.PageSize > 0 {
		limit = param.PageSize
		if param.PageNumber > 0 {
			offset = (param.PageNumber - 1) * param.PageSize
		}
	}
	condition := dao.ListTaskCondition{
tangxvhui authored
792 793 794
		Limit:      limit,
		Offset:     offset,
		UserId:     param.UserId,
tangxvhui authored
795
		CompanyId:  param.CompanyId,
tangxvhui authored
796
		OnlyMy:     param.OnlyMy,
797
		SearchWord: param.SearchWord,
tangxvhui authored
798 799 800 801 802 803 804 805 806 807 808
	}
	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
809
	taskDao := dao.NewTaskDao(map[string]interface{}{
tangxvhui authored
810 811
		"transactionContext": transactionContext,
	})
tangxvhui authored
812
	tasklistData, err := taskDao.ListTaskStageByHrbp(condition)
tangxvhui authored
813 814 815
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取任务列表"+err.Error())
	}
tangxvhui authored
816
	taskCount, err := taskDao.CountTaskStageByHrbp(condition)
tangxvhui authored
817 818 819
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统计任务总数"+err.Error())
	}
tangxvhui authored
820 821 822 823 824 825 826 827
	taskStageAnomalyCnt, err := taskDao.CountTaskStageAnomalyByHrbp(condition)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统计任务里程碑总数"+err.Error())
	}
	taskAnomalyCnt, err := taskDao.CountTaskAnomalyByHrbp(condition)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "统计任务里程碑总数"+err.Error())
	}
tangxvhui authored
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	taskResult := []*adapter.TaskItem{}
	taskMapResult := map[int]*adapter.TaskItem{}
	for _, val := range tasklistData {
		if _, ok := taskMapResult[val.TaskId]; ok {
			continue
		}
		tk := adapter.TaskItem{
			Id:        val.TaskId,
			Name:      val.TaskName,
			Alias:     val.TaskAliasName,
			Leader:    val.LeaderName,
			Status:    val.TaskStatus,
			Level:     val.Level,
			LevelName: val.LevelName,
845
			UpdatedAt: val.UpdatedAt.Local().Format("2006-01-02 15:04:05"),
tangxvhui authored
846
			Anomaly:   val.Anomaly,
847
			WarnFlag:  val.WarnFlag,
tangxvhui authored
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
			// StageA:    adapter.TaskStage{},
			// StageB:    adapter.TaskStage{},
			// StageC:    adapter.TaskStage{},
			// StageD:    adapter.TaskStage{},
			// StageE:    adapter.TaskStage{},
		}
		taskMapResult[val.TaskId] = &tk
		taskResult = append(taskResult, &tk)
	}
	for _, val := range tasklistData {
		taskData, ok := taskMapResult[val.TaskId]
		if !ok {
			continue
		}
		stage := adapter.TaskStage{
tangxvhui authored
863 864 865 866 867 868 869 870
			Id:              val.StageId,
			Name:            val.StageName,
			SortBy:          val.StageSortBy,
			PlanCompletedAt: time.Unix(int64(val.PlanCompletedAt), 0).Local().Format("2006-01-02"),
			RealCompletedAt: "",
		}
		if val.RealCompletedAt > 0 {
			stage.RealCompletedAt = time.Unix(int64(val.RealCompletedAt), 0).Local().Format("2006-01-02")
tangxvhui authored
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
		}
		switch val.StageSortBy {
		case 1:
			taskData.StageA = stage
		case 2:
			taskData.StageB = stage
		case 3:
			taskData.StageC = stage
		case 4:
			taskData.StageD = stage
		case 5:
			taskData.StageE = stage
		}
	}
	result := tool_funs.SimpleWrapGridMap(int64(taskCount), taskResult)
tangxvhui authored
886 887
	result["taskStageAnomaly"] = taskStageAnomalyCnt
	result["taskAnomalyCnt"] = taskAnomalyCnt
tangxvhui authored
888 889 890
	return result, nil
}
891
// ReplyTaskStage 根据每日的任务反馈 ,更新任务里程碑
郑周 authored
892
func (srv TaskService) ReplyTaskStage(transactionContext application.TransactionContext, param []*domain.TaskRecord) error {
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskStageRepo := factory.CreateTaskStageRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	var taskList []*domain.Task
	var taskStages []*domain.TaskStage
	for _, val := range param {
		taskData, err := taskRepo.FindOne(map[string]interface{}{"id": val.TaskId})
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, "找不到任务数据"+err.Error())
		}
		_, stageList, err := taskStageRepo.Find(map[string]interface{}{"ids": []int{val.TaskStageCheck.Id}})
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, "找不到任务里程碑数据"+err.Error())
		}
tangxvhui authored
910 911 912
		if val.AnomalyState == domain.AnomalyState1 {
			taskData.Anomaly = 0
		}
913 914 915
		if len(stageList) > 0 {
			stageList[0].RealCompletedAt = val.TaskStageCheck.RealCompletedAt
			taskStages = append(taskStages, stageList[0])
tangxvhui authored
916 917 918 919 920
			if stageList[0].PlanCompletedAt < stageList[0].RealCompletedAt {
				taskData.WarnFlag = domain.TaskWarn
			} else {
				taskData.WarnFlag = domain.TaskWarnNull
			}
921
		}
tangxvhui authored
922 923

		taskList = append(taskList, taskData)
924 925 926 927 928 929 930 931 932 933 934 935
	}
	err := taskStageRepo.Save(taskStages)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存任务里程碑数据"+err.Error())
	}
	for _, val := range taskList {
		err = taskRepo.Save(val)
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, "保存任务数据"+err.Error())
		}
	}
	return nil
tangxvhui authored
936
}
tangxvhui authored
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

// 获取任务的反馈
func (srv TaskService) ListTaskRecord(param *command.ListTaskRecordCommand) (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()
	}()
	taskRecordRepo := factory.CreateTaskRecordRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	cnt, taskRecordList, err := taskRecordRepo.Find(map[string]interface{}{
		"companyId": param.CompanyId,
		"taskId":    param.TaskId,
	})
	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())
	}
	resultList := []adapter.TaskRecordAdapter{}
	for _, val := range taskRecordList {
		item := adapter.TaskRecordAdapter{
			Id:            val.Id,
			StaffAssessId: val.StaffAssessId,
			TaskId:        val.TaskId,
			TaskCategory:  val.TaskCategory,
			TaskName:      val.TaskName,
			TaskAlias:     val.TaskAlias,
			TaskLeader:    val.TaskLeader.Name,
			AssistLevel:   val.AssistLevel,
			AssistContent: val.AssistContent,
tangxvhui authored
975
			AnomalyState:  val.AnomalyState,
tangxvhui authored
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
			TaskStageCheck: adapter.TaskStage{
				Id:                val.Id,
				Name:              val.TaskStageCheck.Name,
				StatusDescription: val.TaskStageCheck.StatusDescription(),
				SortBy:            val.TaskStageCheck.SortBy,
				PlanCompletedAt:   "",
				RealCompletedAt:   "",
			},
			CreatedAt: val.CreatedAt.Local().Format("2006-01-02"),
		}
		t1 := time.Unix(val.TaskStageCheck.PlanCompletedAt, 0).Local().Format("2006-01-02")
		item.TaskStageCheck.PlanCompletedAt = t1
		if val.TaskStageCheck.RealCompletedAt > 0 {
			t2 := time.Unix(val.TaskStageCheck.RealCompletedAt, 0).Local().Format("2006-01-02")
			item.TaskStageCheck.RealCompletedAt = t2
		}
		resultList = append(resultList, item)
	}
	result := tool_funs.SimpleWrapGridMap(int64(cnt), resultList)
	return result, nil
}
tangxvhui authored
997
tangxvhui authored
998
// 删除任务
tangxvhui authored
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
func (srv TaskService) DeleteTaskInfo(param *command.GetTaskCommand) (*adapter.TaskInfoAdapter, 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()
	}()
	taskRepo := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	taskData, err := taskRepo.FindOne(map[string]interface{}{
		"id": param.TaskId,
	})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "xx"+err.Error())
	}
	if taskData.CompanyId != param.CompanyId {
		return nil, nil
	}
	nowTime := time.Now()
	taskData.DeletedAt = &nowTime
	err = taskRepo.Save(taskData)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "xx"+err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	return nil, nil
}
tangxvhui authored
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054

// 员工绩效-任务管理-列表筛选项
func (srv TaskService) ConditionForListTask2(param *command.GetSelcetCommand) (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()
	}()
	taskDao := dao.NewTaskDao(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	hrbpFlag, err := roleService.GetHrBp(transactionContext, int(param.CompanyId), int(param.UserId))
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取用户的角色信息"+err.Error())
	}
	var tasklistData []dao.TaskData1
	if hrbpFlag == domain.RoleTypeSystem {
1055
		tasklistData, err = taskDao.ListTask2ForHrbp(param.UserId, param.CompanyId)
tangxvhui authored
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取任务列表"+err.Error())
		}
	} else {
		tasklistData, err = taskDao.ListTask2(param.UserId)
		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())
	}
	selectList := []adapter.ConditionSelect{}
	uniqueMap := map[string]struct{}{}
	// 任务名称,负责人,优先级
	switch param.Cmd {
	case "任务名称":
		for _, val := range tasklistData {
tangxvhui authored
1074 1075 1076 1077
			if _, ok := uniqueMap[val.Name]; ok {
				continue
			}
			uniqueMap[val.Name] = struct{}{}
tangxvhui authored
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
			selectList = append(selectList, adapter.ConditionSelect{Name: val.Name})
		}
	case "负责人":
		for _, val := range tasklistData {
			if _, ok := uniqueMap[val.LeaderName]; ok {
				continue
			}
			uniqueMap[val.LeaderName] = struct{}{}
			selectList = append(selectList, adapter.ConditionSelect{Name: val.LeaderName})
		}
	case "优先级":
		for _, val := range tasklistData {
			if _, ok := uniqueMap[val.LevelName]; ok {
				continue
			}
			uniqueMap[val.LevelName] = struct{}{}
			if len(val.LevelName) > 0 {
				selectList = append(selectList, adapter.ConditionSelect{Name: val.LevelName})
			}
		}
	}
	result := map[string]interface{}{
		"list": selectList,
	}
	return result, nil
}