punchclocklogic.go 2.6 KB

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