模型库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

136 lines
4.1 KiB

  1. package implement
  2. import (
  3. "code.fnuoos.com/zhimeng/model.git/src/dao"
  4. "code.fnuoos.com/zhimeng/model.git/src/model"
  5. zhios_order_relate_logx "code.fnuoos.com/zhimeng/model.git/utils/logx"
  6. "reflect"
  7. "strings"
  8. "xorm.io/xorm"
  9. )
  10. func NewMediumDb(engine *xorm.Engine) dao.MediumDao {
  11. return &MediumDb{Db: engine}
  12. }
  13. type MediumDb struct {
  14. Db *xorm.Engine
  15. }
  16. func (m MediumDb) UpdateMedium(medium *model.Medium, columns ...string) (int64, error) {
  17. affected, err := m.Db.Where("id =?", medium.Id).Cols(columns...).Update(m)
  18. if err != nil {
  19. return 0, err
  20. }
  21. return affected, nil
  22. }
  23. func (m MediumDb) GetMediumByUsername(username string) (medium *model.Medium, err error) {
  24. medium = new(model.Medium)
  25. has, err := m.Db.Where("username =?", username).Get(medium)
  26. if err != nil {
  27. return nil, zhios_order_relate_logx.Error(err)
  28. }
  29. if has == false {
  30. return nil, nil
  31. }
  32. return medium, nil
  33. }
  34. func (m MediumDb) MediumInsert(medium *model.Medium) (int64, error) {
  35. insertAffected, err := m.Db.InsertOne(medium)
  36. if err != nil {
  37. return 0, err
  38. }
  39. return insertAffected, nil
  40. }
  41. func (m MediumDb) MediumDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  42. if reflect.TypeOf(id).Kind() == reflect.Slice {
  43. return session.In("id", id).Delete(model.Admin{})
  44. } else {
  45. return session.Where("id = ?", id).Delete(model.Admin{})
  46. }
  47. }
  48. func (m MediumDb) GetMediumRolePermission(id int) (list []*dao.MediumWithRolePermission, total int64, err error) {
  49. total, err = m.Db.Where("medium.id =?", id).
  50. Join("LEFT", "medium_with_role", "medium.id = medium_with_role.medium_id").
  51. Join("LEFT", "medium_role", "admin_with_role.role_id = medium_role.id").
  52. Join("LEFT", "medium_role_permission_group", "medium_role.id = medium_role_permission_group.role_id").
  53. Join("LEFT", "medium_permission_group", "medium_role_permission_group.group_id = medium_permission_group.id").
  54. Join("LEFT", "medium_permission_group_permission", "medium_permission_group.id = medium_permission_group_permission.group_id").
  55. Join("LEFT", "medium_permission", "medium_permission_group_permission.permission_id = medium_permission.id").
  56. FindAndCount(&list)
  57. return
  58. }
  59. func (m MediumDb) FindAdmin(username string, state, page, limit int) (list []model.Medium, total int64, err error) {
  60. sess := m.Db.Desc("id").Limit(limit, (page-1)*limit)
  61. if username != "" {
  62. sess.And("username like ?", "%"+username+"%")
  63. }
  64. if state != 0 {
  65. sess.And("state = ?", state)
  66. }
  67. total, err = sess.FindAndCount(&list)
  68. if err != nil {
  69. return nil, 0, err
  70. }
  71. return
  72. }
  73. func (m MediumDb) GetMedium(id int) (mm *model.Medium, err error) {
  74. mm = new(model.Medium)
  75. has, err := m.Db.Where("id =?", id).Get(mm)
  76. if err != nil {
  77. return nil, zhios_order_relate_logx.Error(err)
  78. }
  79. if has == false {
  80. return nil, nil
  81. }
  82. return mm, nil
  83. }
  84. func (m MediumDb) FindMediumRolePermissionGroup(id int) (list []*dao.MediumWithRolePermissionGroup, total int64, err error) {
  85. total, err = m.Db.Where("medium.id =?", id).
  86. Join("LEFT", "medium_with_role", "medium.id = medium_with_role.medium_id").
  87. Join("LEFT", "medium_role", "medium_with_role.role_id = medium_role.id").
  88. FindAndCount(&list)
  89. return
  90. }
  91. func (m MediumDb) FindSuperAdmin(username, memo string, page, limit int) (list []model.Medium, total int64, err error) {
  92. sess := m.Db.Where("is_super_administrator=1").Desc("id").Limit(limit, (page-1)*limit)
  93. if username != "" {
  94. sess.And("username like ?", "%"+username+"%")
  95. }
  96. if memo != "" {
  97. sess.And("memo like ?", "%"+memo+"%")
  98. }
  99. total, err = sess.FindAndCount(&list)
  100. if err != nil {
  101. return nil, 0, err
  102. }
  103. return
  104. }
  105. func (m MediumDb) FindSuperAdminByMediumId(mediumId string, page, limit int) (list []model.Medium, total int64, err error) {
  106. sess := m.Db.Where("is_super_administrator=1").Desc("id").Limit(limit, (page-1)*limit)
  107. if mediumId != "" {
  108. sess.In("medium_id", strings.Split(mediumId, ","))
  109. }
  110. total, err = sess.FindAndCount(&list)
  111. if err != nil {
  112. return nil, 0, err
  113. }
  114. return
  115. }
  116. func (m MediumDb) GetSuperAdmin(mediumId int) (list *model.Medium) {
  117. list = new(model.Medium)
  118. sess := m.Db.Where("is_super_administrator=1 and medium_id=?", mediumId)
  119. get, err := sess.Get(list)
  120. if err != nil || get == false {
  121. return nil
  122. }
  123. return list
  124. }