score_item.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package score
  2. import (
  3. "fmt"
  4. "strings"
  5. "zhiyuan/pkg/db"
  6. "github.com/gin-gonic/gin"
  7. )
  8. type ScoreItem struct {
  9. ID int64 `json:"id" prop:"add:false"`
  10. ScoreId int64 `json:"scoreId" label:"类型" type:"int" prop:"add" search:"="`
  11. Name string `json:"name" label:"名称" type:"string" prop:"add edit" search:"like"`
  12. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  13. CreatedAt int64 `json:"created_at" prop:"add:false"`
  14. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  15. db.BaseModel
  16. }
  17. func (ScoreItem) TableName() string {
  18. return "zy_score_item"
  19. }
  20. func (model ScoreItem) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  21. return true
  22. }
  23. func (ScoreItem) OnePrivilege(c *gin.Context, id int64) bool {
  24. return true
  25. }
  26. func (ScoreItem) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  27. return nil
  28. }
  29. func (ScoreItem) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  30. return nil
  31. }
  32. func (ScoreItem) DelPrivilege(c *gin.Context, id int64) error {
  33. return nil
  34. }
  35. func (ScoreItem) Page() bool {
  36. return false
  37. }
  38. func (ScoreItem) Count() bool {
  39. return true
  40. }
  41. func (model ScoreItem) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  42. return []db.JoinModel{}
  43. }
  44. type ScoreItemMobile struct {
  45. Username string `json:"username" type:"string" prop:"select:admin.username"`
  46. Phone string `json:"phone" type:"string" prop:"select:admin.phone"`
  47. DeptId int64 `json:"dept_id" type:"int" prop:"select:admin.dept_id"`
  48. Name string `json:"name" type:"string" prop:"select:item.name"`
  49. ScoreItem
  50. }
  51. type ScoreOrders struct {
  52. ScoreId int64 `json:"scoreId" label:"类型" type:"int" prop:"add" search:"="`
  53. ItemId int64 `json:"itemId" label:"类型" type:"int" prop:"add" search:"="`
  54. AdminId int64 `json:"adminId" label:"类型" type:"int" prop:"add:false" search:"="`
  55. Score int64 `json:"score" label:"名称" type:"int" prop:"add edit"`
  56. ScoreOrder
  57. }
  58. func (model ScoreOrders) GroupBy() string {
  59. return fmt.Sprintf("`%s`.`adminId`,`%s`.`itemId`", model.TableName(), model.TableName())
  60. }
  61. func (model ScoreOrders) OrderBy() string {
  62. return fmt.Sprintf("`%s`.`updated_at` desc", model.TableName())
  63. }
  64. func (model ScoreItem) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} {
  65. scoreCache := make(map[int64]Score)
  66. for i, v := range list {
  67. scoreId, _ := db.ToInt64(v["scoreId"])
  68. id, _ := db.ToInt64(v["id"])
  69. var score Score
  70. if s, ok := scoreCache[scoreId]; ok {
  71. score = s
  72. } else {
  73. db.GetModel(map[string]interface{}{
  74. "id": scoreId,
  75. "deleted_at": 0,
  76. }, &score)
  77. }
  78. juryIds := make([]int64, 0)
  79. for _, n := range strings.Split(score.JuryIds, ",") {
  80. m, _ := db.ToInt64(n)
  81. juryIds = append(juryIds, m)
  82. }
  83. orders := make([]ScoreOrders, 0)
  84. db.GetModel(map[string]interface{}{
  85. "scoreId": scoreId,
  86. "itemId": id,
  87. "deleted_at": 0,
  88. }, &orders)
  89. N := float64(0)
  90. Sum := float64(0)
  91. juryN := float64(0)
  92. jurySum := float64(0)
  93. for _, order := range orders {
  94. inJury := false
  95. for _, n := range juryIds {
  96. if n == order.AdminId {
  97. inJury = true
  98. break
  99. }
  100. }
  101. if inJury {
  102. juryN++
  103. jurySum += float64(order.Score)
  104. } else {
  105. N++
  106. Sum += float64(order.Score)
  107. }
  108. }
  109. scoreProportion := 100 - score.JuryProportion
  110. scores := float64(0)
  111. if N != 0 {
  112. scores = Sum * float64(scoreProportion) / N / 100
  113. }
  114. jury := float64(0)
  115. if juryN != 0 {
  116. jury = jurySum * float64(score.JuryProportion) / juryN / 100
  117. }
  118. list[i]["score"] = scores + jury
  119. }
  120. return list
  121. }