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

db_enterprise.go 2.2 KiB

1 년 전
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type EnterpriseDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (enterpriseDb *EnterpriseDb) Set() { // set方法
  12. enterpriseDb.Db = Db
  13. }
  14. func (enterpriseDb *EnterpriseDb) GetEnterprise(id int) (m *model.Enterprise, err error) {
  15. m = new(model.Enterprise)
  16. has, err := enterpriseDb.Db.Where("id =?", id).Get(m)
  17. if err != nil {
  18. return nil, logx.Error(err)
  19. }
  20. if has == false {
  21. return nil, nil
  22. }
  23. return m, nil
  24. }
  25. func (enterpriseDb *EnterpriseDb) EnterpriseDeleteBySession(session *xorm.Session, id interface{}) (int64, error) {
  26. if reflect.TypeOf(id).Kind() == reflect.Slice {
  27. return session.In("id", id).Delete(model.Enterprise{})
  28. } else {
  29. return session.Where("id = ?", id).Delete(model.Enterprise{})
  30. }
  31. }
  32. func (enterpriseDb *EnterpriseDb) FindEnterprise(limit, start int) (*[]model.Enterprise, error) {
  33. var m []model.Enterprise
  34. if limit == 0 || start == 0 {
  35. if err := enterpriseDb.Db.Desc("id").Find(&m); err != nil {
  36. return nil, logx.Error(err)
  37. }
  38. } else {
  39. if err := enterpriseDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
  40. return nil, logx.Error(err)
  41. }
  42. }
  43. return &m, nil
  44. }
  45. func (enterpriseDb *EnterpriseDb) GetEnterpriseByName(name string) (m *model.Enterprise, err error) {
  46. m = new(model.Enterprise)
  47. has, err := enterpriseDb.Db.Where("name =?", name).Get(m)
  48. if err != nil {
  49. return nil, logx.Error(err)
  50. }
  51. if has == false {
  52. return nil, nil
  53. }
  54. return m, nil
  55. }
  56. func (enterpriseDb *EnterpriseDb) EnterpriseInsert(m *model.Enterprise) (int, error) {
  57. _, err := enterpriseDb.Db.InsertOne(m)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return m.Id, nil
  62. }
  63. func (enterpriseDb *EnterpriseDb) EnterpriseUpdate(m *model.Enterprise, columns ...string) (int64, error) {
  64. affected, err := enterpriseDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  65. if err != nil {
  66. return 0, err
  67. }
  68. return affected, nil
  69. }
  70. func (adminDb *AdminDb) FindEnterpriseGradeClass(enterpriseId int) (list []*AdminRolePermission, total int64, err error) {
  71. total, err = adminDb.Db.Where("enterprise.id =?", enterpriseId).
  72. Join("LEFT", "grade", "enterprise.id = grade.enterprise_id").
  73. Join("LEFT", "class", "class.grade_id = grade.id").
  74. FindAndCount(&list)
  75. return
  76. }