面包店
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

db_company.go 1.9 KiB

8 mesi fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. }
  70. type EnterpriseWithCompany struct {
  71. model.Enterprise `xorm:"extends"`
  72. model.Company `xorm:"extends"`
  73. }
  74. func (EnterpriseWithCompany) TableName() string {
  75. return "enterprise"
  76. }