智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 rivejä
2.5 KiB

  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) FindEnterpriseByKind(kind int) (*[]model.Enterprise, error) {
  46. var m []model.Enterprise
  47. if err := enterpriseDb.Db.Where("kind =?", kind).Desc("id").Find(&m); err != nil {
  48. return nil, logx.Error(err)
  49. }
  50. return &m, nil
  51. }
  52. func (enterpriseDb *EnterpriseDb) GetEnterpriseByName(name string) (m *model.Enterprise, err error) {
  53. m = new(model.Enterprise)
  54. has, err := enterpriseDb.Db.Where("name =?", name).Get(m)
  55. if err != nil {
  56. return nil, logx.Error(err)
  57. }
  58. if has == false {
  59. return nil, nil
  60. }
  61. return m, nil
  62. }
  63. func (enterpriseDb *EnterpriseDb) EnterpriseInsert(m *model.Enterprise) (int, error) {
  64. _, err := enterpriseDb.Db.InsertOne(m)
  65. if err != nil {
  66. return 0, err
  67. }
  68. return m.Id, nil
  69. }
  70. func (enterpriseDb *EnterpriseDb) EnterpriseUpdate(m *model.Enterprise, columns ...string) (int64, error) {
  71. affected, err := enterpriseDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  72. if err != nil {
  73. return 0, err
  74. }
  75. return affected, nil
  76. }
  77. func (adminDb *AdminDb) FindEnterpriseGradeClass(enterpriseId int) (list []*AdminRolePermission, total int64, err error) {
  78. total, err = adminDb.Db.Where("enterprise.id =?", enterpriseId).
  79. Join("LEFT", "grade", "enterprise.id = grade.enterprise_id").
  80. Join("LEFT", "class", "class.grade_id = grade.id").
  81. FindAndCount(&list)
  82. return
  83. }