面包店
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.

db_enterprise.go 2.4 KiB

8 maanden geleden
8 maanden geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 GetEnterpriseMore(eg *xorm.Engine, enterpriseIds []int) map[int]model.Enterprise {
  71. enterpriseMap := make(map[int]model.Enterprise)
  72. if len(enterpriseIds) > 0 {
  73. enterpriseData := make([]model.Enterprise, 0)
  74. eg.In("id", enterpriseIds).Find(&enterpriseData)
  75. for _, v := range enterpriseData {
  76. enterpriseMap[v.Id] = v
  77. }
  78. }
  79. return enterpriseMap
  80. }
  81. func GetEnterpriseList(eg *xorm.Engine) *[]model.Enterprise {
  82. var data []model.Enterprise
  83. err := eg.Where("1=1").Find(&data)
  84. if err != nil {
  85. return nil
  86. }
  87. return &data
  88. }