punch_clock_logic.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package user
  2. import (
  3. "context"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  9. "git.i2edu.net/i2/i2-bill-api/internal/types"
  10. "git.i2edu.net/i2/i2-bill-api/internal/utils"
  11. "git.i2edu.net/i2/i2-bill-api/model"
  12. "git.i2edu.net/i2/go-zero/core/logx"
  13. "git.i2edu.net/i2/go-zero/core/stores/sqlc"
  14. )
  15. type PunchClockLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. UserId int64
  20. SessionKey string
  21. }
  22. func NewPunchClockLogic(ctx context.Context, svcCtx *svc.ServiceContext) PunchClockLogic {
  23. return PunchClockLogic{
  24. Logger: logx.WithContext(ctx),
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. }
  28. }
  29. func (l *PunchClockLogic) PunchClock(req types.PunchClockRequest) (*types.Response, error) {
  30. partTimeUser, err := model.GetPartTimeXormByUserId(l.UserId, l.svcCtx.DB)
  31. if err != nil {
  32. logx.Error(err)
  33. return nil, err
  34. }
  35. if partTimeUser.UserId == 0 {
  36. return nil, errors.New("请先申请成为兼职")
  37. }
  38. var aar model.I2billAcquirerAttendanceRecord
  39. var nowDay = utils.GetTimeYmd()
  40. err = l.svcCtx.SqlConn.QueryRowPartial(&aar, fmt.Sprintf("select * from i2bill_acquirer_attendance_record where `user_id` = ? and date=? limit 1"), l.UserId, nowDay)
  41. if err == sqlc.ErrNotFound {
  42. aar.DelFlag = sql.NullInt64{Int64: 0, Valid: true}
  43. aar.UserId = sql.NullInt64{Int64: l.UserId, Valid: true}
  44. aar.CreateTime = sql.NullTime{Time: time.Now(), Valid: true}
  45. aar.Date = sql.NullTime{Time: nowDay, Valid: true}
  46. }
  47. // 上班卡
  48. if req.Type == 1 && !utils.ValidTime(aar.StartTime.Time) {
  49. aar.StartTime = sql.NullTime{Time: time.Now(), Valid: true}
  50. aar.StartLat = sql.NullString{String: req.Lat, Valid: true}
  51. aar.StartAddress = sql.NullString{String: req.Address, Valid: true}
  52. } else if req.Type == 2 && !utils.ValidTime(aar.EndTime.Time) {
  53. // 下班卡
  54. aar.EndTime = sql.NullTime{Time: time.Now(), Valid: true}
  55. aar.EndLat = sql.NullString{String: req.Lat, Valid: true}
  56. aar.EndAddress = sql.NullString{String: req.Address, Valid: true}
  57. }
  58. if aar.Id == 0 {
  59. _, err = l.svcCtx.SqlConn.Exec(`
  60. INSERT INTO i2bill_acquirer_attendance_record
  61. (user_id,start_lat,start_address,end_lat,end_address,start_time,end_time,create_time,del_flag,date)
  62. VALUES
  63. (?,?,?,?,?,?,?,?,?,?)`,
  64. aar.UserId, aar.StartLat, aar.StartAddress, aar.EndLat, aar.EndAddress, aar.StartTime, aar.EndTime, aar.CreateTime, aar.DelFlag, aar.Date)
  65. if err != nil {
  66. logx.Error(err)
  67. return nil, err
  68. }
  69. }
  70. _, err = l.svcCtx.SqlConn.Exec(`update i2bill_acquirer_attendance_record set user_id = ?, start_lat = ?, start_address = ?, end_lat = ?, end_address = ?, start_time = ?, end_time = ?, create_time = ?, del_flag = ? where id = ?`,
  71. aar.UserId, aar.StartLat, aar.StartAddress, aar.EndLat, aar.EndAddress, aar.StartTime, aar.EndTime, aar.CreateTime, aar.DelFlag, aar.Id)
  72. if err != nil {
  73. logx.Error(err)
  74. return nil, err
  75. }
  76. return utils.ReturnHTTPSuccess("ok"), nil
  77. }