business.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package gorm
  2. import (
  3. "context"
  4. "go-common/app/admin/main/aegis/model/business"
  5. "github.com/jinzhu/gorm"
  6. )
  7. // AddBusiness .
  8. func (d *Dao) AddBusiness(c context.Context, e *business.Business) (id int64, err error) {
  9. err = d.orm.Table("business").Create(&e).Error
  10. id = e.ID
  11. return
  12. }
  13. // UpdateBusiness .
  14. func (d *Dao) UpdateBusiness(c context.Context, e *business.Business) (err error) {
  15. return d.orm.Table("business").Where("id = ?", e.ID).Update(map[string]interface{}{
  16. "name": e.Name,
  17. "desc": e.Desc,
  18. "developer": e.Developer,
  19. }).Error
  20. }
  21. // EnableBusiness .
  22. func (d *Dao) EnableBusiness(c context.Context, id int64) (err error) {
  23. return d.orm.Table("business").Where("id = ?", id).Update(map[string]interface{}{
  24. "state": business.StateEnable,
  25. }).Error
  26. }
  27. // DisableBusiness .
  28. func (d *Dao) DisableBusiness(c context.Context, id int64) (err error) {
  29. return d.orm.Table("business").Where("id = ?", id).Update(map[string]interface{}{
  30. "state": business.StateDisable,
  31. }).Error
  32. }
  33. // Business .
  34. func (d *Dao) Business(c context.Context, id int64) (res *business.Business, err error) {
  35. res = &business.Business{}
  36. if err = d.orm.Where("id = ?", id).First(&res).Error; err == gorm.ErrRecordNotFound {
  37. res = nil
  38. err = nil
  39. }
  40. return
  41. }
  42. // BusinessList .
  43. func (d *Dao) BusinessList(c context.Context, tp int8, ids []int64, onlyEnable bool) (res []*business.Business, err error) {
  44. res = []*business.Business{}
  45. db := d.orm
  46. if len(ids) > 0 {
  47. db = db.Where("id in (?)", ids)
  48. }
  49. if onlyEnable {
  50. db = db.Where("state=?", business.StateEnable)
  51. }
  52. if tp > 0 {
  53. db = db.Where("type = ?", tp)
  54. }
  55. err = db.Find(&res).Error
  56. return
  57. }