service.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package service
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "wails-app/internal/dao"
  7. "wails-app/internal/global"
  8. "wails-app/internal/modules/shift/repository"
  9. )
  10. type ShiftService struct {
  11. repo *repository.ShiftRepository
  12. }
  13. func NewShiftService() *ShiftService {
  14. return &ShiftService{repo: &repository.ShiftRepository{}}
  15. }
  16. func (s *ShiftService) Start(operatorID uint, startingCash float64) (*dao.ShiftRecord, error) {
  17. active, _ := s.repo.GetActive(operatorID)
  18. if active != nil {
  19. return nil, errors.New("已有当班记录,请先交班")
  20. }
  21. rec := &dao.ShiftRecord{
  22. OperatorID: operatorID, StartTime: time.Now(),
  23. StartingCash: startingCash, Status: "active",
  24. }
  25. if err := s.repo.Create(rec); err != nil {
  26. return nil, err
  27. }
  28. return rec, nil
  29. }
  30. func (s *ShiftService) End(operatorID uint, actualCash float64, remark string) (*dao.ShiftRecord, error) {
  31. active, err := s.repo.GetActive(operatorID)
  32. if err != nil {
  33. return nil, errors.New("无当班记录")
  34. }
  35. var collected float64
  36. global.GVA_DB.Raw("SELECT COALESCE(SUM(amount),0) FROM payment_record WHERE payment_method='cash' AND paid_at >= ? AND paid_at < ?",
  37. active.StartTime, time.Now()).Scan(&collected)
  38. now := time.Now()
  39. expected := active.StartingCash + collected
  40. diff := actualCash - expected
  41. active.EndTime = &now
  42. active.CollectedCash = collected
  43. active.ExpectedTotal = expected
  44. active.ActualTotal = actualCash
  45. active.Difference = diff
  46. active.Status = "closed"
  47. active.Remark = remark
  48. if err := s.repo.Update(active); err != nil {
  49. return nil, err
  50. }
  51. fmt.Printf("交班: 接班%.2f+当班%.2f=应交%.2f 实交%.2f 差异%.2f\n",
  52. active.StartingCash, collected, expected, actualCash, diff)
  53. return active, nil
  54. }
  55. func (s *ShiftService) GetCurrent(operatorID uint) (*dao.ShiftRecord, error) {
  56. return s.repo.GetActive(operatorID)
  57. }
  58. func (s *ShiftService) List(q repository.ShiftListQuery, operatorID uint) ([]dao.ShiftRecord, int64, error) {
  59. return s.repo.List(q, operatorID)
  60. }