service2.go 5.2 KB
package service

import (
	"fmt"
	"time"

	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/adapter"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/task/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils/xtime"
)

// 异常中心查看

// 我负责的任务,异常列表
func (srv TaskService) ListTaskAnomaly1(param command.ListTaskAnomalyCommand) (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()
	}()
	taskAnomalyDao := dao.NewTaskAnomalyDao(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	limit := 500
	offset := 0
	cnt, anomalyList, err := taskAnomalyDao.List1(param.UserId, param.CompanyId, param.TaskName, param.Category, param.DayTime, limit, offset)
	if err != nil {
		return map[string]interface{}{}, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	listData := []adapter.TaskAnomalyItem{}
	nowTime := time.Now()
	for _, val := range anomalyList {
		item := adapter.TaskAnomalyItem{
			Id:       val.Id,
			TaskId:   val.TaskId,
			Category: val.Category,
			Leader:   val.LeaderName,
			CurrentStage: adapter.TaskStage{
				Id:                val.CurrentStage.Id,
				Name:              val.CurrentStage.Name,
				StatusDescription: val.CurrentStage.StatusDescription(),
				SortBy:            val.CurrentStage.SortBy,
				PlanCompletedAt:   time.Unix(val.CurrentStage.PlanCompletedAt, 0).Local().Format("2006-01-02"),
				RealCompletedAt:   "",
			},
			LastStage: adapter.TaskStage{
				Id:                val.LastStage.Id,
				Name:              val.LastStage.Name,
				StatusDescription: val.LastStage.StatusDescription(),
				SortBy:            val.LastStage.SortBy,
				PlanCompletedAt:   time.Unix(val.LastStage.PlanCompletedAt, 0).Local().Format("2006-01-02"),
				RealCompletedAt:   "",
			},
			AssessFlag:  val.AssessFlag,
			WarnFlag:    val.WarnFlag,
			AssistFlag:  val.AssistFlag,
			TaskLevel:   val.LevelName,
			TaskName:    val.TaskName,
			TaskAlias:   val.TaskAlias,
			TaskEndTime: int64(val.TaskEndTime),
			TaskSortBy:  val.TaskSortBy,
			RecordBegin: val.RecordBegin,
			MarkA:       "",
			MarkB:       "",
			MarkC:       "",
			MarkD:       "",
		}
		if len(val.Marks) > 0 {
			item.MarkA = val.Marks["a"]
			item.MarkB = val.Marks["b"]
			item.MarkC = val.Marks["c"]
			item.MarkD = val.Marks["d"]
		}
		if val.TaskStageCheck.RealCompletedAt > 0 {
			item.TaskStageCheck.RealCompletedAt = time.Unix(val.TaskStageCheck.RealCompletedAt, 0).Local().Format("2006-01-02")
		}
		if val.CurrentStage.RealCompletedAt > 0 {
			item.CurrentStage.RealCompletedAt = time.Unix(val.CurrentStage.RealCompletedAt, 0).Local().Format("2006-01-02")
		}
		if val.LastStage.RealCompletedAt > 0 {
			item.LastStage.RealCompletedAt = time.Unix(val.LastStage.RealCompletedAt, 0).Local().Format("2006-01-02")
		}
		switch val.Category {
		case 1:
			{
				//里程碑异常
				if val.TaskStageCheck.RealCompletedAt == 0 {
					//逾期未完成
					planCompletedAt := time.Unix(val.TaskStageCheck.PlanCompletedAt, 0)
					subDay := xtime.SubDayAbs(nowTime, planCompletedAt)
					item.AnomalyDesc = fmt.Sprintf("里程碑%s已逾期%d天", val.TaskStageCheck.Name, subDay)
				} else {
					//逾期完成
					planCompletedAt := time.Unix(val.TaskStageCheck.PlanCompletedAt, 0)
					realCompletedAt := time.Unix(val.TaskStageCheck.RealCompletedAt, 0)
					subDay := xtime.SubDayAbs(realCompletedAt, planCompletedAt)
					item.AnomalyDesc = fmt.Sprintf("里程碑%s逾期%d天完成", val.TaskStageCheck.Name, subDay)
				}
			}
		case 2:
			//反馈异常
			{
				recordBegin := time.Unix(val.RecordBegin, 0)
				subDay := xtime.SubDayAbs(nowTime, recordBegin)
				item.AnomalyDesc = fmt.Sprintf(
					"本任务自%s未正常反馈项目进度,已异常%d天",
					recordBegin.Local().Format("2006-01-02"),
					subDay,
				)
			}
		case 3:
			//辅导异常
			{
				recordBegin := time.Unix(val.RecordBegin, 0)
				subDay := xtime.SubDayAbs(nowTime, recordBegin)
				item.AnomalyDesc = fmt.Sprintf(
					"本任务计划于%s完成辅导,已逾期%d天",
					recordBegin.Local().Format("2006-01-02"),
					subDay,
				)
			}
		}

		listData = append(listData, item)
	}

	result := tool_funs.SimpleWrapGridMap(int64(cnt), listData)
	return result, nil
}

// 我的下级负责任务,异常列表
func (srv TaskService) ListTaskAnomaly2() {}

// 与我有关的任务,异常列表
func (srv TaskService) ListTaskAnomaly3() {}

// 对异常记录的操作标记
func (srv TaskService) MarkTaskAnomaly() {}