privilege.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/admin/main/vip/model"
  6. "github.com/jinzhu/gorm"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _vipPrivileges = "vip_privileges"
  11. _vipPrivilegesResources = "vip_privileges_resources"
  12. updateOrderSQL = "UPDATE vip_privileges a, vip_privileges b SET a.order_num = b.order_num, b.order_num = a.order_num WHERE a.id = ? AND b.id = ?;"
  13. )
  14. // PrivilegeList query .
  15. func (d *Dao) PrivilegeList(c context.Context, langType int8) (res []*model.Privilege, err error) {
  16. db := d.vip.Table(_vipPrivileges).Where("deleted=0 AND lang_type=?", langType).Order("order_num ASC")
  17. if err := db.Find(&res).Error; err != nil {
  18. return nil, err
  19. }
  20. return
  21. }
  22. // PrivilegeResourcesList query privilege resources .
  23. func (d *Dao) PrivilegeResourcesList(c context.Context) (res []*model.PrivilegeResources, err error) {
  24. db := d.vip.Table(_vipPrivilegesResources)
  25. if err := db.Find(&res).Error; err != nil {
  26. return nil, err
  27. }
  28. return
  29. }
  30. // UpdateStatePrivilege update state privilege.
  31. func (d *Dao) UpdateStatePrivilege(c context.Context, p *model.Privilege) (a int64, err error) {
  32. stage := map[string]interface{}{
  33. "state": p.State,
  34. }
  35. db := d.vip.Table(_vipPrivileges).Where("id = ?", p.ID).Updates(stage)
  36. if err = db.Error; err != nil {
  37. return
  38. }
  39. a = db.RowsAffected
  40. return
  41. }
  42. // DeletePrivilege dekete privilege.
  43. func (d *Dao) DeletePrivilege(c context.Context, id int64) (a int64, err error) {
  44. stage := map[string]interface{}{
  45. "deleted": 1,
  46. }
  47. db := d.vip.Table(_vipPrivileges).Where("id = ?", id).Updates(stage)
  48. if err = db.Error; err != nil {
  49. return
  50. }
  51. a = db.RowsAffected
  52. return
  53. }
  54. // AddPrivilege add privilege.
  55. func (d *Dao) AddPrivilege(tx *gorm.DB, ps *model.Privilege) (id int64, err error) {
  56. db := tx.Table(_vipPrivileges).Save(ps)
  57. if err = db.Error; err != nil {
  58. return
  59. }
  60. id = ps.ID
  61. return
  62. }
  63. // MaxOrder max priivilege order.
  64. func (d *Dao) MaxOrder(c context.Context) (order int64, err error) {
  65. p := new(model.Privilege)
  66. db := d.vip.Table(_vipPrivileges).Order("order_num DESC").First(&p)
  67. if err = db.Error; err != nil {
  68. return
  69. }
  70. return p.Order, err
  71. }
  72. // AddPrivilegeResources add privilege resources.
  73. func (d *Dao) AddPrivilegeResources(tx *gorm.DB, p *model.PrivilegeResources) (a int64, err error) {
  74. db := tx.Table(_vipPrivilegesResources).Save(p)
  75. if err = db.Error; err != nil {
  76. return
  77. }
  78. a = db.RowsAffected
  79. return
  80. }
  81. // UpdatePrivilege update privilege .
  82. func (d *Dao) UpdatePrivilege(tx *gorm.DB, ps *model.Privilege) (a int64, err error) {
  83. val := map[string]interface{}{
  84. "privileges_name": ps.Name,
  85. "title": ps.Title,
  86. "explains": ps.Explain,
  87. "privileges_type": ps.Type,
  88. "operator": ps.Operator,
  89. }
  90. if ps.IconURL != "" {
  91. val["icon_url"] = ps.IconURL
  92. }
  93. if ps.IconGrayURL != "" {
  94. val["icon_gray_url"] = ps.IconGrayURL
  95. }
  96. db := tx.Table(_vipPrivileges).Where("id = ?", ps.ID).Updates(val)
  97. if err = db.Error; err != nil {
  98. return
  99. }
  100. a = db.RowsAffected
  101. return
  102. }
  103. // UpdatePrivilegeResources update privilege resources .
  104. func (d *Dao) UpdatePrivilegeResources(tx *gorm.DB, ps *model.PrivilegeResources) (aff int64, err error) {
  105. stage := map[string]interface{}{
  106. "link": ps.Link,
  107. }
  108. if ps.ImageURL != "" {
  109. stage["image_url"] = ps.ImageURL
  110. }
  111. db := tx.Table(_vipPrivilegesResources).Where("pid = ? AND resources_type = ?", ps.PID, ps.Type).Updates(stage)
  112. if err = db.Error; err != nil {
  113. return
  114. }
  115. aff = db.RowsAffected
  116. return
  117. }
  118. // UpdateOrder update privilege order.
  119. func (d *Dao) UpdateOrder(c context.Context, aid, bid int64) (a int64, err error) {
  120. var res xsql.Result
  121. if res, err = d.db.Exec(c, updateOrderSQL, aid, bid); err != nil {
  122. err = errors.WithStack(err)
  123. return
  124. }
  125. if a, err = res.RowsAffected(); err != nil {
  126. err = errors.WithStack(err)
  127. return
  128. }
  129. return
  130. }