repo.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package repository
  2. import (
  3. "wails-app/internal/dao"
  4. "wails-app/internal/global"
  5. )
  6. type ShiftRepository struct{}
  7. func (r *ShiftRepository) Create(record *dao.ShiftRecord) error {
  8. return global.GVA_DB.Create(record).Error
  9. }
  10. func (r *ShiftRepository) Update(record *dao.ShiftRecord) error {
  11. return global.GVA_DB.Save(record).Error
  12. }
  13. func (r *ShiftRepository) GetActive(operatorID uint) (*dao.ShiftRecord, error) {
  14. var s dao.ShiftRecord
  15. err := global.GVA_DB.Where("operator_id = ? AND status = ?", operatorID, "active").First(&s).Error
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &s, nil
  20. }
  21. type ShiftListQuery struct {
  22. Page int `form:"page"`
  23. PageSize int `form:"page_size"`
  24. ShowAll bool `form:"show_all"`
  25. }
  26. type ShiftListItem struct {
  27. dao.ShiftRecord
  28. OperatorName string `json:"operator_name"`
  29. }
  30. func (r *ShiftRepository) List(q ShiftListQuery, operatorID uint) ([]ShiftListItem, int64, error) {
  31. db := global.GVA_DB.Table("shift_record sr").
  32. Select("sr.*, su.nick_name as operator_name").
  33. Joins("LEFT JOIN sys_users su ON su.id = sr.operator_id")
  34. if !q.ShowAll {
  35. db = db.Where("sr.operator_id = ?", operatorID)
  36. }
  37. var total int64
  38. db.Count(&total)
  39. if q.Page <= 0 {
  40. q.Page = 1
  41. }
  42. if q.PageSize <= 0 {
  43. q.PageSize = 20
  44. }
  45. var list []ShiftListItem
  46. err := db.Order("sr.id DESC").Offset((q.Page - 1) * q.PageSize).Limit(q.PageSize).Scan(&list).Error
  47. return list, total, err
  48. }