challenge.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package model
  2. import (
  3. "net/url"
  4. xtime "go-common/library/time"
  5. )
  6. // Chall is the row view for every challenge
  7. type Chall struct {
  8. Cid int64 `json:"cid" gorm:"column:id"`
  9. Gid int64 `json:"gid" gorm:"column:gid"`
  10. Oid int64 `json:"oid" gorm:"column:oid"`
  11. OidStr string `json:"oid_str" gorm:"-"`
  12. Business int8 `json:"business" gorm:"column:business"`
  13. Mid int64 `json:"mid" gorm:"column:mid"`
  14. MName string `json:"m_name" gorm:"-"`
  15. Tid int64 `json:"tid" gorm:"column:tid"`
  16. State int8 `json:"state"`
  17. BusinessState int8 `json:"business_state"`
  18. DispatchState uint32 `json:"-" gorm:"column:dispatch_state"`
  19. DispatchTime xtime.Time `json:"dispatch_time" gorm:"column:dispatch_time"`
  20. Description string `json:"description" gorm:"column:description"`
  21. Metadata string `json:"metadata" gorm:"column:metadata"`
  22. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  23. MTime xtime.Time `json:"mtime" gorm:"column:mtime"`
  24. BusinessObject *Business `json:"business_object,omitempty" gorm:"-"`
  25. AssigneeAdminID int64 `json:"assignee_adminid" gorm:"column:assignee_adminid"`
  26. AdminID int64 `json:"adminid" gorm:"column:adminid"`
  27. AssigneeAdminName string `json:"assignee_admin_name" gorm:"-"`
  28. AdminName string `json:"admin_name" gorm:"-"`
  29. TotalStates string `json:"total_states" gorm:"-"`
  30. // tag related fields
  31. Tag string `json:"tag" gorm:"-"`
  32. Round int8 `json:"round" gorm:"-"`
  33. // log related
  34. LastLog string `json:"last_log" gorm:"-"`
  35. // event related
  36. LastEvent *Event `json:"last_event" gorm:"-"`
  37. // Attachments
  38. Attachments []string `json:"attachments" gorm:"-"`
  39. // linked group object
  40. Group *Group `json:"group,omitempty" gorm:"-"`
  41. Meta interface{} `json:"meta" gorm:"-"`
  42. AuditLog interface{} `json:"audit_log" gorm:"-"`
  43. Producer *Account `json:"producer" gorm:"-"` //举报人
  44. // business table
  45. Title string `json:"title,omitempty" gorm:"-"`
  46. TypeID int64 `json:"type_id,omitempty" gorm:"-"`
  47. }
  48. // TinyChall is the tiny row view for every challenge
  49. type TinyChall struct {
  50. Cid int64 `json:"cid" gorm:"column:id"`
  51. Gid int64 `json:"gid" gorm:"column:gid"`
  52. Mid int64 `json:"mid" gorm:"column:mid"`
  53. CTime xtime.Time `json:"ctime" gorm:"column:ctime"`
  54. State int8 `json:"state" gorm:"-"`
  55. Title string `json:"title" gorm:"-"`
  56. }
  57. // ChallTagSlice is the slice to ChallTag
  58. type ChallTagSlice []*ChallTag
  59. // ChallTag is the model to retrive user submitted tags in group view
  60. type ChallTag struct {
  61. ID int64 `json:"id"`
  62. Tag string `json:"tag"`
  63. Round int8 `json:"round"`
  64. Count int64 `json:"count"`
  65. Percent int32 `json:"percent"`
  66. }
  67. // TableName is used to identify chall table name in gorm
  68. func (Chall) TableName() string {
  69. return "workflow_chall"
  70. }
  71. func (c ChallTagSlice) Len() int {
  72. return len(c)
  73. }
  74. func (c ChallTagSlice) Swap(i, j int) {
  75. c[i], c[j] = c[j], c[i]
  76. }
  77. func (c ChallTagSlice) Less(i, j int) bool {
  78. return c[i].Percent < c[j].Percent
  79. }
  80. // FixAttachments will fix attachments url as user friendly
  81. // ignore https case
  82. // FIXME: this should be removed after attachment url is be normed
  83. func (c *Chall) FixAttachments() {
  84. if c.Attachments == nil {
  85. return
  86. }
  87. fixed := make([]string, 0, len(c.Attachments))
  88. for _, a := range c.Attachments {
  89. u, err := url.Parse(a)
  90. if err != nil {
  91. continue
  92. }
  93. u.Scheme = "http"
  94. fixed = append(fixed, u.String())
  95. }
  96. c.Attachments = fixed
  97. }
  98. // SetState update state of a role
  99. // ex. oldState=0x3a4b5c6d, state=15, role=1 then result is 0x3a4b5cfd
  100. func (c *Chall) SetState(state uint32, role uint8) {
  101. oldState := c.DispatchState
  102. mod := uint32(^(0xf << (4 * role)))
  103. oldState = oldState & mod // all bit keep unchanged and bits you want update change to 0
  104. c.DispatchState = oldState + state<<(4*role)
  105. }
  106. // getState return state of a role from dispatchState field
  107. // ex. dispatchState=0x3a4b5c6d, role=1 then result is 0x6
  108. func (c *Chall) getState(role uint8) (result int8) {
  109. dispatchState := c.DispatchState
  110. mod := uint32(0xf << (4 * role))
  111. dispatchState &= mod
  112. result = int8(dispatchState >> (4 * role))
  113. return
  114. }
  115. // FromState set State and BusinessState field from DispatchState field
  116. func (c *Chall) FromState() {
  117. c.State = c.getState(uint8(0))
  118. c.BusinessState = c.getState(uint8(1))
  119. }
  120. // FormatState transform state in queue
  121. func (c *Chall) FormatState() {
  122. if c.State == QueueState {
  123. c.State = QueueStateBefore
  124. }
  125. if c.BusinessState == QueueBusinessState {
  126. c.BusinessState = QueueBusinessStateBefore
  127. }
  128. }