evaluation_rule.go 3.9 KB
package domain

import (
	"time"
)

const (
	EvaluationTypeRating int = 0 // 评估方式-评级
	EvaluationTypeScore  int = 1 // 评估方式-评分
)

const (
	EvaluationSysTypeCommon int = 0 // 系统类型-系统添加
	EvaluationSysTypeSystem int = 1 // 系统类型-系统预制(不可删除、编辑)
)

type Rating struct {
	Levels []*RatingLevel `json:"levels" comment:"配置等级"`
}

type RatingLevel struct {
	Code           string  `json:"code" comment:"等级代号"`
	Name           string  `json:"name" comment:"等级名称"`
	Color          int     `json:"color" comment:"等级颜色"`
	QuantizedValue float64 `json:"quantizedValue" comment:"等级量化值"`
}

type Score struct {
	Min           float64       `json:"min" comment:"评分下限"`
	Max           float64       `json:"max" comment:"评分上限"`
	IntervalState int           `json:"intervalState" comment:"按分数子区间匹配等级(0关闭、1开启)"`
	DecimalPlaces int           `json:"decimalPlaces" comment:"保留小数点(0不保留、1保留一位、以此类推)"`
	Levels        []*ScoreLevel `json:"levels" comment:"配置等级区间"`
}

type ScoreLevel struct {
	Code  string  `json:"code" comment:"等级代号"`
	Name  string  `json:"name" comment:"等级名称"`
	Start float64 `json:"start" comment:"分数区间开始值"`
	End   float64 `json:"end" comment:"分数区间结束值"`
}

type EvaluationRule struct {
	Id        int64      `json:"id,string" comment:"ID"`
	Name      string     `json:"name" comment:"名称"`
	Remark    string     `json:"remark" comment:"备注"`
	CompanyId int64      `json:"companyId,string" comment:"公司ID"`
	CreatorId int64      `json:"creatorId,string" comment:"创建人ID"`
	Type      int        `json:"type" comment:"评估方式(0评级、1评分)"`
	SysType   int        `json:"sysType" comment:"系统类型(0普通、1系统固定)" pg:",use_zero"`
	Rating    Rating     `json:"rating" comment:"评级"`
	Score     Score      `json:"score" comment:"评分"`
	CreatedAt time.Time  `json:"createdAt" comment:"创建时间"`
	UpdatedAt time.Time  `json:"updatedAt" comment:"更新时间"`
	DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}

// GenerateSysRule 当前公司下的生成默认规则
func GenerateSysRule(companyId int64) *EvaluationRule {
	levels := make([]*RatingLevel, 0)
	levels = append(levels, &RatingLevel{
		Code:           "S",
		Name:           "SS",
		Color:          1,
		QuantizedValue: 90,
	})
	levels = append(levels, &RatingLevel{
		Code:           "A",
		Name:           "AA",
		Color:          2,
		QuantizedValue: 80,
	})
	levels = append(levels, &RatingLevel{
		Code:           "B",
		Name:           "BB",
		Color:          3,
		QuantizedValue: 70,
	})

	levels = append(levels, &RatingLevel{
		Code:           "C",
		Name:           "CC",
		Color:          4,
		QuantizedValue: 60,
	})

	levels = append(levels, &RatingLevel{
		Code:           "D",
		Name:           "DD",
		Color:          5,
		QuantizedValue: 50,
	})

	newRule := &EvaluationRule{
		Id:        0,
		Name:      "评级",
		Remark:    "",
		CompanyId: companyId,
		CreatorId: 0,
		Type:      EvaluationTypeRating,
		SysType:   EvaluationSysTypeSystem,
		Rating: Rating{
			Levels: levels,
		},
		Score: Score{},
	}
	return newRule
}

type EvaluationRuleRepository interface {
	Insert(rule *EvaluationRule) (*EvaluationRule, error)
	Remove(rule *EvaluationRule) (*EvaluationRule, error)
	FindOne(queryOptions map[string]interface{}) (*EvaluationRule, error)
	Find(queryOptions map[string]interface{}) (int64, []*EvaluationRule, error)
	Count(queryOptions map[string]interface{}) (int64, error)
}

func (rule *EvaluationRule) GetLevelCodes() []string {
	codes := []string{}
	if rule.Type == EvaluationTypeRating {
		for _, v := range rule.Rating.Levels {
			codes = append(codes, v.Code)
		}
	}
	if rule.Type == EvaluationTypeScore {
		for _, v := range rule.Score.Levels {
			codes = append(codes, v.Code)
		}
	}
	return codes
}