score_order.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package score
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "zhiyuan/pkg/db"
  7. "zhiyuan/services/dept"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type ScoreOrder struct {
  11. ID int64 `json:"id" prop:"add:false"`
  12. ScoreId int64 `json:"scoreId" label:"类型" type:"int" prop:"add" search:"="`
  13. ItemId int64 `json:"itemId" label:"类型" type:"int" prop:"add" search:"="`
  14. AdminId int64 `json:"adminId" label:"类型" type:"int" prop:"add:false" search:"="`
  15. Score int64 `json:"score" label:"名称" type:"int" prop:"add edit"`
  16. DeletedAt int64 `json:"deleted_at" prop:"add:false select:false"`
  17. CreatedAt int64 `json:"created_at" prop:"add:false"`
  18. UpdatedAt int64 `json:"updated_at" prop:"add:false"`
  19. db.BaseModel
  20. }
  21. func (ScoreOrder) TableName() string {
  22. return "zy_score_order"
  23. }
  24. func (model ScoreOrder) ListPrivilege(c *gin.Context, data map[string]interface{}, s *db.Select) bool {
  25. return true
  26. }
  27. func (ScoreOrder) OnePrivilege(c *gin.Context, id int64) bool {
  28. return true
  29. }
  30. func (ScoreOrder) AddPrivilege(c *gin.Context, data map[string]interface{}, post map[string]interface{}) error {
  31. itemId, _ := db.ToInt64(data["itemId"])
  32. var item ScoreItem
  33. db.GetModel(map[string]interface{}{
  34. "id": itemId,
  35. "deleted_at": 0,
  36. }, &item)
  37. if item.ID == 0 {
  38. return errors.New("没有权限")
  39. }
  40. var score Score
  41. db.GetModel(map[string]interface{}{
  42. "id": item.ScoreId,
  43. "deleted_at": 0,
  44. }, &score)
  45. if score.ID == 0 {
  46. return errors.New("没有权限")
  47. }
  48. now := time.Now().Unix()
  49. if now < score.StartTime || now >= score.EndTime {
  50. return errors.New("没有权限")
  51. }
  52. var order ScoreOrder
  53. db.GetModel(map[string]interface{}{
  54. "itemId": itemId,
  55. "adminId": c.GetInt("adminID"),
  56. "deleted_at": 0,
  57. }, &order)
  58. if order.ID != 0 {
  59. return errors.New("没有权限")
  60. }
  61. data["adminId"] = c.GetInt("adminID")
  62. return nil
  63. }
  64. func (ScoreOrder) EditPrivilege(c *gin.Context, id int64, data map[string]interface{}, post map[string]interface{}) error {
  65. var order ScoreOrder
  66. db.GetModel(map[string]interface{}{
  67. "id": id,
  68. "deleted_at": 0,
  69. }, &order)
  70. if order.ID == 0 {
  71. return errors.New("没有权限")
  72. }
  73. if int(order.AdminId) != c.GetInt("adminID") {
  74. return errors.New("没有权限")
  75. }
  76. var item ScoreItem
  77. db.GetModel(map[string]interface{}{
  78. "id": order.ItemId,
  79. "deleted_at": 0,
  80. }, &item)
  81. if item.ID == 0 {
  82. return errors.New("没有权限")
  83. }
  84. var score Score
  85. db.GetModel(map[string]interface{}{
  86. "id": item.ScoreId,
  87. "deleted_at": 0,
  88. }, &score)
  89. if score.ID == 0 {
  90. return errors.New("没有权限")
  91. }
  92. now := time.Now().Unix()
  93. if now < score.StartTime || now >= score.EndTime {
  94. return errors.New("没有权限")
  95. }
  96. return nil
  97. }
  98. func (ScoreOrder) DelPrivilege(c *gin.Context, id int64) error {
  99. return nil
  100. }
  101. func (ScoreOrder) Page() bool {
  102. return false
  103. }
  104. func (ScoreOrder) Count() bool {
  105. return true
  106. }
  107. func (model ScoreOrder) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  108. return []db.JoinModel{}
  109. }
  110. type JoinAdmin struct {
  111. db.BaseModel
  112. }
  113. func (JoinAdmin) TableName() string {
  114. return "zy_admin"
  115. }
  116. type ScoreOrderMobile struct {
  117. Username string `json:"username" type:"string" prop:"select:admin.username"`
  118. Phone string `json:"phone" type:"string" prop:"select:admin.phone"`
  119. DeptId int64 `json:"dept_id" type:"int" prop:"select:admin.dept_id"`
  120. Name string `json:"name" type:"string" prop:"select:item.name"`
  121. ScoreOrder
  122. }
  123. func (model ScoreOrderMobile) ListAfter(c *gin.Context, data map[string]interface{}, list []map[string]interface{}) []map[string]interface{} {
  124. for i, v := range list {
  125. dept_id, _ := db.ToInt64(v["dept_id"])
  126. list[i]["dept_name"] = dept.GetDeptFullName(int(dept_id), "-")
  127. }
  128. return list
  129. }
  130. func (model ScoreOrderMobile) GroupBy() string {
  131. return fmt.Sprintf("`%s`.`adminId`,`%s`.`itemId`", model.TableName(), model.TableName())
  132. }
  133. func (model ScoreOrderMobile) OrderBy() string {
  134. return fmt.Sprintf("`%s`.`updated_at` desc", model.TableName())
  135. }
  136. func (model ScoreOrderMobile) LeftJoin(data map[string]interface{}, s *db.Select) []db.JoinModel {
  137. return append(model.ScoreOrder.LeftJoin(data, s),
  138. db.JoinModel{
  139. Model: JoinAdmin{},
  140. As: "admin",
  141. On: []string{"`admin`.`id` = " + model.TableName() + ".`adminId`"},
  142. },
  143. db.JoinModel{
  144. Model: ScoreItem{},
  145. As: "item",
  146. On: []string{"`item`.`id` = " + model.TableName() + ".`itemId`"},
  147. })
  148. }