public.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package dao
  2. import (
  3. "demo/internal/model"
  4. "github.com/pkg/errors"
  5. "github.com/siddontang/go/log"
  6. )
  7. func (d *Dao) GetBarList(index, pagesize int64, barName, pastureid string) ([]*model.Bar, int64, error) {
  8. dataList := make([]*model.Bar, 0)
  9. rows := d.db.NewSession()
  10. defer rows.Close()
  11. if pastureid != "" {
  12. rows.Where("FARM_UUID = ?", pastureid)
  13. }
  14. if barName != "" {
  15. rows.Where("HOUSE_NAME = ?", barName)
  16. }
  17. if index != 0 && pagesize != 0 {
  18. first := (index - 1) * pagesize
  19. rows.Limit(int(pagesize), int(first))
  20. }
  21. count, err := rows.FindAndCount(&dataList)
  22. if err != nil {
  23. err = errors.Wrap(err, "GetBarList-db-error")
  24. log.Error(err)
  25. return dataList, count, err
  26. }
  27. return dataList, count, nil
  28. }
  29. func (d *Dao) GetPastureList(index, pagesize int64) ([]*model.Pasture, int64, error) {
  30. dataList := make([]*model.Pasture, 0)
  31. rows := d.db.NewSession()
  32. defer rows.Close()
  33. if index != 0 && pagesize != 0 {
  34. first := (index - 1) * pagesize
  35. rows.Limit(int(pagesize), int(first))
  36. }
  37. count, err := rows.FindAndCount(&dataList)
  38. if err != nil {
  39. err = errors.Wrap(err, "GetPastureList-db-error")
  40. log.Error(err)
  41. return dataList, count, err
  42. }
  43. return dataList, count, nil
  44. }
  45. func (d *Dao) GetHousephyList(index, pagesize int64, pastureId string) ([]*model.Housephy, int64, error) {
  46. dataList := make([]*model.Housephy, 0)
  47. rows := d.db.NewSession()
  48. defer rows.Close()
  49. if index != 0 && pagesize != 0 {
  50. first := (index - 1) * pagesize
  51. rows.Limit(int(pagesize), int(first))
  52. }
  53. rows.Where("FARM_UUID = ?", pastureId)
  54. rows.Where("DEL = 0")
  55. count, err := rows.OrderBy("PHYSICALCODE").FindAndCount(&dataList)
  56. if err != nil {
  57. err = errors.Wrap(err, "GetHousephyList-db-error")
  58. log.Error(err)
  59. return dataList, count, err
  60. }
  61. return dataList, count, nil
  62. }
  63. func (d *Dao) UpdateHousephyImage(housephy *model.Housephy) error {
  64. rows := d.db.NewSession()
  65. defer rows.Close()
  66. _, err := rows.Cols("image").Where("UUID = ? ", housephy.Id).Update(housephy)
  67. if err != nil {
  68. err = errors.Wrap(err, "UpdateHousephyImage-db-error")
  69. log.Error(err)
  70. return err
  71. }
  72. return nil
  73. }
  74. func (d *Dao) GetHousephyImage(id string) (*model.Housephy, error) {
  75. rows := d.db.NewSession()
  76. defer rows.Close()
  77. housephy := new(model.Housephy)
  78. _, err := rows.Where("UUID = ? ", id).Get(housephy)
  79. if err != nil {
  80. err = errors.Wrap(err, "GetHousephyImage-db-error")
  81. log.Error(err)
  82. return nil, err
  83. }
  84. return housephy, nil
  85. }