api.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package shift
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "wails-app/internal/model/common/response"
  5. "wails-app/internal/modules/shift/repository"
  6. "wails-app/internal/modules/shift/service"
  7. utils "wails-app/internal/pkg"
  8. )
  9. var shiftSvc = service.NewShiftService()
  10. type startReq struct {
  11. StartingCash float64 `json:"starting_cash"`
  12. }
  13. type endReq struct {
  14. ActualCash float64 `json:"actual_cash"`
  15. Remark string `json:"remark"`
  16. }
  17. func StartShift(c *gin.Context) {
  18. var req startReq
  19. if err := c.ShouldBindJSON(&req); err != nil {
  20. response.FailWithMessage(err.Error(), c)
  21. return
  22. }
  23. rec, err := shiftSvc.Start(utils.GetUserID(c), req.StartingCash)
  24. if err != nil {
  25. response.FailWithMessage(err.Error(), c)
  26. return
  27. }
  28. response.OkWithData(rec, c)
  29. }
  30. func EndShift(c *gin.Context) {
  31. var req endReq
  32. if err := c.ShouldBindJSON(&req); err != nil {
  33. response.FailWithMessage(err.Error(), c)
  34. return
  35. }
  36. rec, err := shiftSvc.End(utils.GetUserID(c), req.ActualCash, req.Remark)
  37. if err != nil {
  38. response.FailWithMessage(err.Error(), c)
  39. return
  40. }
  41. response.OkWithData(rec, c)
  42. }
  43. func GetCurrentShift(c *gin.Context) {
  44. rec, err := shiftSvc.GetCurrent(utils.GetUserID(c))
  45. if err != nil {
  46. response.FailWithMessage("无当班", c)
  47. return
  48. }
  49. response.OkWithData(rec, c)
  50. }
  51. func ListShifts(c *gin.Context) {
  52. var q repository.ShiftListQuery
  53. if err := c.ShouldBindQuery(&q); err != nil {
  54. response.FailWithMessage(err.Error(), c)
  55. return
  56. }
  57. list, total, err := shiftSvc.List(q, utils.GetUserID(c))
  58. if err != nil {
  59. response.FailWithMessage(err.Error(), c)
  60. return
  61. }
  62. response.OkWithDetailed(response.PageResult{List: list, Total: total, Page: q.Page, PageSize: q.PageSize}, "查询成功", c)
  63. }
  64. func SetupShiftRouter(router *gin.RouterGroup) {
  65. sr := router.Group("/shift")
  66. sr.POST("/start", StartShift)
  67. sr.POST("/end", EndShift)
  68. sr.GET("/current", GetCurrentShift)
  69. sr.GET("/list", ListShifts)
  70. }