GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB

3 years ago
  1. package schedule
  2. import (
  3. common "LAPP_GAAS_GFrame_BACKEND/models/base"
  4. "log"
  5. "reflect"
  6. "strings"
  7. "time"
  8. )
  9. // 基于时间单位获取秒数
  10. func GetTimeUomSeconds(uomId string)(seconds float64){
  11. seconds = 0
  12. switch strings.ToUpper(uomId) {
  13. case common.TIME_UOM_WEEK:
  14. seconds = 7 * 24 * 3600
  15. case common.TIME_UOM_DAY:
  16. seconds = 24 * 3600
  17. case common.TIME_UOM_HOUR:
  18. seconds = 3600
  19. case common.TIME_UOM_MINUTE:
  20. seconds = 60
  21. case common.TIME_UOM_SECOND:
  22. seconds = 1
  23. }
  24. return
  25. }
  26. // 设置表的更新时间
  27. func SetTableLastModify(sp interface{},s interface{}, setMode string, lastUser string){
  28. var (
  29. hasLastmodify, hasLastuser, hasCreatetime bool
  30. )
  31. avp := reflect.ValueOf(sp)
  32. av := reflect.ValueOf(s)
  33. at := reflect.TypeOf(s)
  34. // 简单判断下是否为结构体
  35. if at.Kind() != reflect.Struct {
  36. log.Printf("s must be a struct")
  37. return
  38. }
  39. // 判断是否有LastModify
  40. hasLastmodify = false
  41. hasLastuser = false
  42. hasCreatetime = false
  43. avp = reflect.ValueOf(avp.Interface())
  44. for i := 0; i < av.NumField(); i++ {
  45. if at.Field(i).Name == "Lastmodify" {
  46. hasLastmodify = true
  47. }
  48. if at.Field(i).Name == "Lastuser" {
  49. hasLastuser = true
  50. }
  51. if at.Field(i).Name == "Createtime" {
  52. hasCreatetime = true
  53. }
  54. }
  55. if hasLastmodify && hasLastuser && hasCreatetime {
  56. switch setMode {
  57. case common.MODIFY_MODE_CREATE:
  58. f1 := avp.Elem().FieldByName("Createtime")
  59. f1.Set(reflect.ValueOf(time.Now()))
  60. f2 := avp.Elem().FieldByName("Lastuser")
  61. if lastUser != ""{
  62. f2.Set(reflect.ValueOf(lastUser))
  63. }else{
  64. f2.Set(reflect.ValueOf("sys_service"))
  65. }
  66. case common.MODIFY_MODE_UPDATE:
  67. f1 := avp.Elem().FieldByName("Lastmodify")
  68. f1.Set(reflect.ValueOf(time.Now()))
  69. f2 := avp.Elem().FieldByName("Lastuser")
  70. if lastUser != ""{
  71. f2.Set(reflect.ValueOf(lastUser))
  72. }else{
  73. f2.Set(reflect.ValueOf("sys_service"))
  74. }
  75. case common.MODIFY_MODE_DELETE:
  76. }
  77. }
  78. }