management.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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) AddManagement(arg model.Management, mnagementBh []*model.ManagementBh) error {
  8. row := d.db.NewSession()
  9. defer row.Close()
  10. row.Begin()
  11. _, err := row.Insert(&arg)
  12. if err != nil {
  13. err = errors.Wrap(err, "AddManagement-db-error")
  14. log.Error(err)
  15. row.Rollback()
  16. return err
  17. }
  18. for _, bh := range mnagementBh {
  19. bh.ManagementID = arg.Id
  20. _, err := row.Insert(bh)
  21. if err != nil {
  22. err = errors.Wrap(err, "AddManagement-db-error")
  23. log.Error(err)
  24. row.Rollback()
  25. return err
  26. }
  27. }
  28. err = row.Commit()
  29. if err != nil {
  30. err = errors.Wrap(err, "AddManagement-db-error")
  31. log.Error(err)
  32. row.Rollback()
  33. return err
  34. }
  35. return nil
  36. }
  37. func (d *Dao) GetManagement(index, pagesize int64, pastureid string, genre int64, startDate, endDate string) ([]*model.ManagementList, int64, error) {
  38. //rows := d.db.NewSession()
  39. rows := d.db.NewSession().Table(new(model.Management).TableName()).Alias("m")
  40. rows.Join("inner", []string{"t_nz_farm", "f"}, "m.FARM_UUID=f.UUID")
  41. rows.Select("m.*,f.FARM_NAME")
  42. defer rows.Close()
  43. //importdate
  44. if pastureid != "" {
  45. rows.Where("m.FARM_UUID = ?", pastureid)
  46. }
  47. if genre >= 0 {
  48. //0 售卖,1 返厂 2 置换
  49. rows.Where("m.genre = ?", genre)
  50. }
  51. if startDate != "" && endDate != "" {
  52. rows.Where("m.importdate between ? and ? ", startDate, endDate)
  53. }
  54. if index != 0 && pagesize != 0 {
  55. first := (index - 1) * pagesize
  56. rows.Limit(int(pagesize), int(first))
  57. }
  58. dataList := make([]*model.ManagementList, 0)
  59. count, err := rows.Desc("importdate").FindAndCount(&dataList)
  60. if err != nil {
  61. err = errors.Wrap(err, "GetManagement-db-error")
  62. log.Error(err)
  63. return dataList, count, err
  64. }
  65. return dataList, count, nil
  66. }
  67. func (d *Dao) GetManagementBh(index, pagesize, managementId int64) ([]*model.ManagementBh, int64, error) {
  68. rows := d.db.NewSession()
  69. defer rows.Close()
  70. rows.Where("managementid = ?", managementId)
  71. if index != 0 && pagesize != 0 {
  72. first := (index - 1) * pagesize
  73. rows.Limit(int(pagesize), int(first))
  74. }
  75. dataList := make([]*model.ManagementBh, 0)
  76. count, err := rows.OrderBy("REGIST_NUM_2").FindAndCount(&dataList)
  77. if err != nil {
  78. err = errors.Wrap(err, "GetManagementBh-db-error")
  79. log.Error(err)
  80. return dataList, count, err
  81. }
  82. return dataList, count, nil
  83. }