punch_clock_logic.go 2.7 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. var nowDay = utils.GetTimeYmd()
  31. err := l.svcCtx.SqlConn.QueryRowPartial(&aar, fmt.Sprintf("select * from i2bill_acquirer_attendance_record where `user_id` = ? and date=? limit 1"), l.UserId, nowDay)
  32. if err == sqlc.ErrNotFound {
  33. aar.DelFlag = sql.NullInt64{Int64: 0, Valid: true}
  34. aar.UserId = sql.NullInt64{Int64: l.UserId, Valid: true}
  35. aar.CreateTime = sql.NullTime{Time: time.Now(), Valid: true}
  36. aar.Date = sql.NullTime{Time: nowDay, Valid: true}
  37. }
  38. // 上班卡
  39. if req.Type == 1 && !utils.ValidTime(aar.StartTime.Time) {
  40. aar.StartTime = sql.NullTime{Time: time.Now(), Valid: true}
  41. aar.StartLat = sql.NullString{String: req.Lat, Valid: true}
  42. aar.StartAddress = sql.NullString{String: req.Address, Valid: true}
  43. } else if req.Type == 2 && !utils.ValidTime(aar.EndTime.Time) {
  44. // 下班卡
  45. aar.EndTime = sql.NullTime{Time: time.Now(), Valid: true}
  46. aar.EndLat = sql.NullString{String: req.Lat, Valid: true}
  47. aar.EndAddress = sql.NullString{String: req.Address, Valid: true}
  48. }
  49. if aar.Id == 0 {
  50. _, err = l.svcCtx.SqlConn.Exec(`
  51. INSERT INTO i2bill_acquirer_attendance_record
  52. (user_id,start_lat,start_address,end_lat,end_address,start_time,end_time,create_time,del_flag,date)
  53. VALUES
  54. (?,?,?,?,?,?,?,?,?,?)`,
  55. aar.UserId, aar.StartLat, aar.StartAddress, aar.EndLat, aar.EndAddress, aar.StartTime, aar.EndTime, aar.CreateTime, aar.DelFlag, aar.Date)
  56. if err != nil {
  57. logx.Error(err)
  58. return nil, err
  59. }
  60. }
  61. _, 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 = ?`,
  62. aar.UserId, aar.StartLat, aar.StartAddress, aar.EndLat, aar.EndAddress, aar.StartTime, aar.EndTime, aar.CreateTime, aar.DelFlag, aar.Id)
  63. if err != nil {
  64. logx.Error(err)
  65. return nil, err
  66. }
  67. return utils.ReturnHTTPSuccess("ok"), nil
  68. }