task_anomaly.go 6.5 KB
package domain

import (
	"fmt"
	"strconv"
	"time"

	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils/xtime"
)

const (
	AnomalyCategoryType1 int = 1 //里程碑异常
	AnomalyCategoryType2 int = 2 //反馈异常
	AnomalyCategoryType3 int = 3 //辅导异常
)

// 任务的异常记录
type TaskAnomaly struct {
	Id             int
	CreatedAt      time.Time           `json:"createdAt"`
	UpdatedAt      time.Time           `json:"updatedAt"`
	CompanyId      int                 `json:"companyId,string"`
	TaskId         int                 `json:"taskId,string"`       // 任务id
	TaskRecordId   int                 `json:"taskRecordId,string"` // 任务反馈的进度
	Category       int                 `json:"category"`            // 异常记录的分类 1里程碑异常;2反馈异常;3 辅导异常
	CurrentStage   TaskStage           `json:"currentStage"`        // 计划进度的里程碑
	LastStage      TaskStage           `json:"lastStage"`           // 实际进度完成的里程碑
	TaskStageCheck TaskStage           `json:"taskStageCheck"`      // 当天任务反馈操作里程碑记录
	AssessFlag     int                 `json:"assessFlag"`          // 异常反馈的次数
	WarnFlag       int                 `json:"warnFlag"`            // 里程碑异常次数
	AssistFlag     int                 `json:"assistFlag"`          // 辅导异常次数
	IsLast         int                 `json:"isLast"`              // 是否是最新的一条记录 1 是最新的记录 2 不是最新的记录
	RecordBegin    int64               `json:"recordBegin"`         // 异常记录的开始时间
	NoticeWho      []map[string]string `json:"noticeWho"`           // 异常记录消息通知的人
	Marks          map[string]string   `json:"marks"`               // 已经操作的标记
	Remark         string              `json:"remark"`
}

// 存储
type TaskAnomalyRepository interface {
	Save(param *TaskAnomaly) error
	FindOne(queryOptions map[string]interface{}) (*TaskAnomaly, error)
	Find(queryOptions map[string]interface{}) (int, []*TaskAnomaly, error)
}

// 已点击“去更新”
func (t *TaskAnomaly) MarkA() {
	t.Marks["a"] = "已更新"
}

// 已点击 "去反馈"
func (t *TaskAnomaly) MarkB() {
	t.Marks["b"] = "已反馈"
}

// 已点击 "找上级"
func (t *TaskAnomaly) MarkC() {
	t.Marks["c"] = "已求助"
}

// 已点击 "去辅导"
func (t *TaskAnomaly) MarkD() {
	t.Marks["d"] = "已辅导"
}

func (t *TaskAnomaly) SetNoticeWho(u *User) {
	t.NoticeWho = append(t.NoticeWho, map[string]string{
		"id": strconv.Itoa(int(u.Id)), "name": u.Name,
	})
}

// 异常状况描述
func (t *TaskAnomaly) AnomalyDesc() (str string) {
	nowTime := t.CreatedAt
	if nowTime.IsZero() {
		nowTime = time.Now()
	}
	switch t.Category {
	case 1:
		//里程碑异常
		{
			//逾期未完成
			planCompletedAt := time.Unix(t.CurrentStage.PlanCompletedAt, 0)
			subDay := xtime.SubDayAbs(nowTime, planCompletedAt)
			str = fmt.Sprintf("里程碑%s已逾期%d天", t.TaskStageCheck.Name, subDay)
		}
	case 2:
		//反馈异常
		{
			recordBegin := time.Unix(t.RecordBegin, 0)
			subDay := xtime.SubDayAbs(nowTime, recordBegin)
			str = fmt.Sprintf(
				"本任务自%s未正常反馈项目进度,已异常%d天",
				recordBegin.Local().Format("2006-01-02"),
				subDay,
			)

		}
	case 3:
		// 辅导异常
		{
			recordBegin := time.Unix(t.RecordBegin, 0)
			subDay := xtime.SubDayAbs(nowTime, recordBegin)
			str = fmt.Sprintf(
				"本任务计划于%s完成辅导,已逾期%d天",
				recordBegin.Local().Format("2006-01-02"),
				subDay,
			)

		}
	}
	t.Remark = str
	return str
}

// 检查是否是反馈异常
func (t *TaskAnomaly) IsAssessFlag(taskData *Task, taskRecordData *TaskRecord) bool {
	if taskData.Anomaly <= 0 {
		return false
	}
	*t = TaskAnomaly{
		Id:             0,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
		CompanyId:      taskData.CompanyId,
		TaskId:         taskData.Id,
		TaskRecordId:   taskRecordData.Id,
		Category:       AnomalyCategoryType2,
		CurrentStage:   taskData.CurrentStage,
		LastStage:      taskData.LastStage,
		TaskStageCheck: taskRecordData.TaskStageCheck,
		AssessFlag:     taskData.Anomaly,
		WarnFlag:       0,
		AssistFlag:     0,
		IsLast:         1,
		RecordBegin:    xtime.DayZeroTime(taskRecordData.CreatedAt).Unix(),
		NoticeWho:      []map[string]string{},
		Marks: map[string]string{
			"a": "去更新", "b": "去反馈", "c": "找上级", "d": "去辅导",
		},
		Remark: "",
	}
	return true
}

// 检查是否是里程碑异常
func (t *TaskAnomaly) IsWarnFlag(taskData *Task, taskRecordData *TaskRecord) bool {
	if taskData.CurrentStage.Id == 0 {
		// 没有新的里程碑
		return false
	}
	if taskData.CurrentStage.PlanCompletedAt > time.Now().Unix() {
		//新的里程碑未过期, 不计入异常情况
		return false
	}
	*t = TaskAnomaly{
		Id:             0,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
		CompanyId:      taskData.CompanyId,
		TaskId:         taskData.Id,
		TaskRecordId:   taskRecordData.Id,
		Category:       AnomalyCategoryType1,
		CurrentStage:   taskData.CurrentStage,
		LastStage:      taskData.LastStage,
		TaskStageCheck: taskRecordData.TaskStageCheck,
		AssessFlag:     0,
		WarnFlag:       1,
		AssistFlag:     0,
		IsLast:         1,
		RecordBegin:    taskData.CurrentStage.PlanCompletedAt,
		NoticeWho:      []map[string]string{},
		Marks: map[string]string{
			"a": "去更新", "b": "去反馈", "c": "找上级", "d": "去辅导",
		},
		Remark: "",
	}
	return true
}

// 检查是否是辅导异常,
func (t *TaskAnomaly) IsAssistFlag(taskData *Task, taskRecordData *TaskRecord) bool {
	if taskData.AssistFlag <= taskData.AssistFlagMax {
		return false
	}
	*t = TaskAnomaly{
		Id:             0,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
		CompanyId:      taskData.CompanyId,
		TaskId:         taskData.Id,
		TaskRecordId:   taskRecordData.Id,
		Category:       AnomalyCategoryType3,
		CurrentStage:   taskData.CurrentStage,
		LastStage:      taskData.LastStage,
		TaskStageCheck: taskRecordData.TaskStageCheck,
		AssessFlag:     taskData.Anomaly,
		WarnFlag:       0,
		AssistFlag:     1,
		IsLast:         1,
		RecordBegin:    xtime.DayZeroTime(taskRecordData.CreatedAt).Unix(),
		NoticeWho:      []map[string]string{},
		Marks: map[string]string{
			"a": "去更新", "b": "去反馈", "c": "找上级", "d": "去辅导",
		},
		Remark: "",
	}
	nowTime := xtime.DayZeroTime(time.Now()).Unix()
	if taskData.CurrentStage.Id > 0 && taskData.CurrentStage.PlanCompletedAt < nowTime {
		t.WarnFlag = 1
	}
	return true
}