智慧食堂
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

79 satır
1.8 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 CompanyDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. }
  11. func (companyDb *CompanyDb) Set() { // set方法
  12. companyDb.Db = Db
  13. }
  14. func (companyDb *CompanyDb) GetCompany(id int) (m *model.Company, err error) {
  15. m = new(model.Company)
  16. has, err := companyDb.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 (companyDb *CompanyDb) FindCompany(limit, start int) (*[]model.Company, error) {
  26. var m []model.Company
  27. if limit == 0 || start == 0 {
  28. if err := companyDb.Db.Desc("id").Find(&m); err != nil {
  29. return nil, logx.Error(err)
  30. }
  31. } else {
  32. if err := companyDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
  33. return nil, logx.Error(err)
  34. }
  35. }
  36. return &m, nil
  37. }
  38. func (companyDb *CompanyDb) GetCompanyByName(name string) (m *model.Company, err error) {
  39. m = new(model.Company)
  40. has, err := companyDb.Db.Where("name =?", name).Get(m)
  41. if err != nil {
  42. return nil, logx.Error(err)
  43. }
  44. if has == false {
  45. return nil, nil
  46. }
  47. return m, nil
  48. }
  49. func (companyDb *CompanyDb) CompanyInsert(m *model.Company) (int, error) {
  50. _, err := companyDb.Db.InsertOne(m)
  51. if err != nil {
  52. return 0, err
  53. }
  54. return m.Id, nil
  55. }
  56. func (companyDb *CompanyDb) CompanyUpdate(m *model.Company, columns ...string) (int64, error) {
  57. affected, err := companyDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return affected, nil
  62. }
  63. func (companyDb *CompanyDb) CompanyDelete(id interface{}) (int64, error) {
  64. if reflect.TypeOf(id).Kind() == reflect.Slice {
  65. return Db.In("id", id).Delete(model.Company{})
  66. } else {
  67. return Db.Where("id = ?", id).Delete(model.Company{})
  68. }
  69. }