package score import ( "fmt" "strings" "zhiyuan/pkg/db" "github.com/gin-gonic/gin" ) type ScoreItem struct { ID int64 `json:"id" prop:"add:false"` ScoreId int64 `json:"scoreId" label:"类型" type:"int" prop:"add" search:"="` Name string `json:"name" label:"名称" type:"string" prop:"add edit" search:"like"` DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"` CreatedAt int64 `json:"created_at" prop:"add:false"` UpdatedAt int64 `json:"updated_at" prop:"add:false"` db.BaseModel } func (ScoreItem) TableName() string { return "zy_score_item" } func (model ScoreItem) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool { return true } func (ScoreItem) OnePrivilege(c *gin.Context, id int64) bool { return true } func (ScoreItem) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error { return nil } func (ScoreItem) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error { return nil } func (ScoreItem) DelPrivilege(c *gin.Context, id int64) error { return nil } func (ScoreItem) Page() bool { return false } func (ScoreItem) Count() bool { return true } func (model ScoreItem) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel { return []db.JoinModel{} } type ScoreItemMobile struct { Username string `json:"username" type:"string" prop:"select:admin.username"` Phone string `json:"phone" type:"string" prop:"select:admin.phone"` DeptId int64 `json:"dept_id" type:"int" prop:"select:admin.dept_id"` Name string `json:"name" type:"string" prop:"select:item.name"` ScoreItem } type ScoreOrders struct { ScoreId int64 `json:"scoreId" label:"类型" type:"int" prop:"add" search:"="` ItemId int64 `json:"itemId" label:"类型" type:"int" prop:"add" search:"="` AdminId int64 `json:"adminId" label:"类型" type:"int" prop:"add:false" search:"="` Score int64 `json:"score" label:"名称" type:"int" prop:"add edit"` ScoreOrder } func (model ScoreOrders) GroupBy() string { return fmt.Sprintf("`%s`.`adminId`,`%s`.`itemId`", model.TableName(), model.TableName()) } func (model ScoreOrders) OrderBy() string { return fmt.Sprintf("`%s`.`updated_at` desc", model.TableName()) } func (model ScoreItem) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} { scoreCache := make(map[int64]Score) for i, v := range list { scoreId, _ := db.ToInt64(v["scoreId"]) id, _ := db.ToInt64(v["id"]) var score Score if s, ok := scoreCache[scoreId]; ok { score = s } else { db.GetModel(map[string]interface{}{ "id": scoreId, "deleted_at": 0, }, &score) } juryIds := make([]int64, 0) for _, n := range strings.Split(score.JuryIds, ",") { m, _ := db.ToInt64(n) juryIds = append(juryIds, m) } orders := make([]ScoreOrders, 0) db.GetModel(map[string]interface{}{ "scoreId": scoreId, "itemId": id, "deleted_at": 0, }, &orders) N := float64(0) Sum := float64(0) juryN := float64(0) jurySum := float64(0) for _, order := range orders { inJury := false for _, n := range juryIds { if n == order.AdminId { inJury = true break } } if inJury { juryN++ jurySum += float64(order.Score) } else { N++ Sum += float64(order.Score) } } scoreProportion := 100 - score.JuryProportion scores := float64(0) if N != 0 { scores = Sum * float64(scoreProportion) / N / 100 } jury := float64(0) if juryN != 0 { jury = jurySum * float64(score.JuryProportion) / juryN / 100 } list[i]["score"] = scores + jury } return list }