staff_attendance.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "access-control-monitor/consts"
  6. "access-control-monitor/errors"
  7. "fmt"
  8. "time"
  9. "github.com/jaryhe/gopkgs/logger"
  10. "github.com/jinzhu/gorm"
  11. "go.uber.org/zap"
  12. )
  13. type StaffAttendance struct {
  14. ID int64 `gorm:"primary_key"`
  15. ProjectId int64 `json:"project_id"`
  16. WorkNo string `json:"work_no"`
  17. Attendance string `json:"start_time"`
  18. InOut int `json:"in_out"`
  19. DayTime string `json:"day_time"`
  20. CreatedAt string `json:"created_at"`
  21. UpdatedAt string `json:"updated_at"`
  22. TabType int `gorm:"-"` // 用于判断表0 t_staff_attendance, 1-12 t_staff_attendance_monthx, 13 t_staff_attendance_day
  23. }
  24. const (
  25. AttendanceTab = iota
  26. AttendanceTabMonth1
  27. AttendanceTabMonth2
  28. AttendanceTabMonth3
  29. AttendanceTabMonth4
  30. AttendanceTabMonth5
  31. AttendanceTabMonth6
  32. AttendanceTabMonth7
  33. AttendanceTabMonth8
  34. AttendanceTabMonth9
  35. AttendanceTabMonth10
  36. AttendanceTabMonth11
  37. AttendanceTabMonth12
  38. AttendanceTabDay
  39. )
  40. func (p StaffAttendance) TableName() string {
  41. switch p.TabType {
  42. case AttendanceTab:
  43. return "t_staff_attendance"
  44. case AttendanceTabDay:
  45. return "t_staff_attendance_day"
  46. default:
  47. if p.TabType >=AttendanceTabMonth1 && p.TabType <= AttendanceTabMonth12 {
  48. return fmt.Sprintf("t_staff_attendance_month%d", p.TabType)
  49. }
  50. }
  51. return "t_staff_attendance"
  52. }
  53. // Insert 插入一条记录
  54. func (p *StaffAttendance) Insert(db *gorm.DB) error {
  55. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  56. p.CreatedAt = timeNow
  57. //p.UpdatedAt = timeNow
  58. err := db.Table(p.TableName()).Create(p).Error
  59. if err != nil {
  60. fields, _ := json.MarshalToString(*p)
  61. logger.Error("mysql",
  62. zap.String("sql", "insert into "+p.TableName()),
  63. zap.String("fields", fields),
  64. zap.String("error", err.Error()))
  65. }
  66. return err
  67. }
  68. func (p *StaffAttendance) Delete(db *gorm.DB, filter map[string]interface{}) error {
  69. cond, val, err := whereBuild(filter)
  70. if err != nil {
  71. return err
  72. }
  73. err = db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  74. if err != nil {
  75. fields, _ := json.MarshalToString(filter)
  76. logger.Error("mysql",
  77. zap.String("sql", "delete from "+p.TableName()),
  78. zap.String("fields", fields),
  79. zap.String("error", err.Error()))
  80. }
  81. return err
  82. }
  83. func (p *StaffAttendance) Save(db *gorm.DB) error {
  84. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  85. p.CreatedAt = timeNow
  86. err := db.Table(p.TableName()).Save(p).Error
  87. if err != nil {
  88. fields, _ := json.MarshalToString(*p)
  89. logger.Error("mysql",
  90. zap.String("sql", "save " +p.TableName()),
  91. zap.String("fields", fields),
  92. zap.String("error", err.Error()))
  93. }
  94. return err
  95. }
  96. func (p *StaffAttendance) Update(db *gorm.DB) error {
  97. timeNow := time.Now().Format(consts.TimeSecondLayOut)
  98. p.UpdatedAt = timeNow
  99. err := db.Table(p.TableName()).Model(p).Updates(p).Error
  100. if err != nil {
  101. fields, _ := json.MarshalToString(*p)
  102. logger.Error("mysql",
  103. zap.String("sql", "update "+ p.TableName()),
  104. zap.String("fields", fields),
  105. zap.String("error", err.Error()))
  106. }
  107. return err
  108. }
  109. // 通过结构体变量更新字段值, gorm库会忽略零值字段。就是字段值等于0, nil, "", false这些值会被忽略掉,不会更新。如果想更新零值,可以使用map类型替代结构体。
  110. func (p *StaffAttendance) UpdateSome(db *gorm.DB, filed map[string]interface{}) error {
  111. if filed == nil {
  112. return errors.ParamsError
  113. }
  114. //timeNow := time.Now().Format(consts.TimeSecondLayOut)
  115. //filed["updated_at"] = timeNow
  116. err := db.Table(p.TableName()).Updates(filed).Error
  117. if err != nil {
  118. fields, _ := json.MarshalToString(filed)
  119. logger.Error("mysql",
  120. zap.String("sql", "update "+p.TableName()),
  121. zap.String("fields", fields),
  122. zap.String("error", err.Error()))
  123. }
  124. return err
  125. }
  126. func (p *StaffAttendance) Query(db *gorm.DB, filter map[string]interface{}) error {
  127. err := db.Table(p.TableName()).Where(filter).Find(p).Error
  128. if err != nil {
  129. fields, _ := json.MarshalToString(filter)
  130. logger.Error("mysql",
  131. zap.String("sql", "select from "+p.TableName()),
  132. zap.String("fields", fields),
  133. zap.String("error", err.Error()))
  134. }
  135. return err
  136. }
  137. func (p *StaffAttendance) QueryAll(db *gorm.DB, filter map[string]interface{}, out interface{}) error {
  138. err := db.Table(p.TableName()).Where(filter).Find(out).Error
  139. if err != nil {
  140. fields, _ := json.MarshalToString(filter)
  141. logger.Error("mysql",
  142. zap.String("sql", "select from "+p.TableName()),
  143. zap.String("fields", fields),
  144. zap.String("error", err.Error()))
  145. }
  146. return err
  147. }