finsh.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // finish
  2. package finishtime
  3. import (
  4. qu "qfw/util"
  5. "strings"
  6. "time"
  7. )
  8. var workfig map[string]map[string]string
  9. var morning_on, morning_off, afternoon_on, afternoon_off string
  10. func init() {
  11. qu.ReadConfig("./worktime.json", &workfig)
  12. morning_on = workfig["morning"]["on"]
  13. morning_off = workfig["morning"]["off"]
  14. afternoon_on = workfig["afternoon"]["on"]
  15. afternoon_off = workfig["afternoon"]["off"]
  16. }
  17. //获取完成时间
  18. func CompleteTime(urgent string) int64 {
  19. duration := workfig["urgency"][urgent]
  20. do := int64(0)
  21. if strings.Contains(duration, "h") { //单位为小时,需要计算
  22. do = qu.Int64All(strings.Replace(duration, "h", "", -1)) * int64(3600)
  23. } else {
  24. return 0
  25. }
  26. endtime := time.Now()
  27. for i := 0; i < 100; i++ { //轮询调度直到,剩余工时为零
  28. if do > 0 {
  29. do, endtime = getWorkTime(do, endtime)
  30. } else {
  31. break
  32. }
  33. }
  34. return endtime.Unix()
  35. }
  36. /*计算剩余工时,和完成时间
  37. do:工时
  38. t:时间
  39. spare:剩余工时
  40. endtime:完成时间
  41. */
  42. func getWorkTime(do int64, t time.Time) (spare int64, endtime time.Time) {
  43. mon, _ := time.ParseInLocation(qu.Date_Full_Layout, t.Format(qu.Date_Short_Layout)+" "+morning_on+":00", time.Local)
  44. moff, _ := time.ParseInLocation(qu.Date_Full_Layout, t.Format(qu.Date_Short_Layout)+" "+morning_off+":00", time.Local)
  45. aon, _ := time.ParseInLocation(qu.Date_Full_Layout, t.Format(qu.Date_Short_Layout)+" "+afternoon_on+":00", time.Local)
  46. aoff, _ := time.ParseInLocation(qu.Date_Full_Layout, t.Format(qu.Date_Short_Layout)+" "+afternoon_off+":00", time.Local)
  47. if t.Unix() <= mon.Unix() {
  48. //早上上班前
  49. work := moff.Unix() - mon.Unix()
  50. if work >= do {
  51. spare = 0
  52. endtime = mon.Add(time.Duration(do) * time.Second)
  53. } else {
  54. spare = do - work
  55. endtime = moff
  56. }
  57. } else if t.Unix() >= mon.Unix() && t.Unix() < moff.Unix() {
  58. //中午下班前
  59. work := moff.Unix() - t.Unix()
  60. if work >= do {
  61. spare = 0
  62. endtime = t.Add(time.Duration(do) * time.Second)
  63. } else {
  64. spare = do - work
  65. endtime = moff
  66. }
  67. } else if t.Unix() >= moff.Unix() && t.Unix() < aon.Unix() {
  68. //下午上班前
  69. work := aoff.Unix() - aon.Unix()
  70. if work >= do {
  71. spare = 0
  72. endtime = aon.Add(time.Duration(do) * time.Second)
  73. } else {
  74. spare = do - work
  75. endtime = aoff
  76. }
  77. } else if t.Unix() >= aon.Unix() && t.Unix() <= aoff.Unix() {
  78. //下午下班前
  79. work := aoff.Unix() - t.Unix()
  80. if work >= do {
  81. spare = 0
  82. endtime = t.Add(time.Duration(do) * time.Second)
  83. } else {
  84. spare = do - work
  85. endtime = mon.AddDate(0, 0, 1)
  86. }
  87. } else {
  88. //下午下班
  89. endtime = mon.AddDate(0, 0, 1)
  90. }
  91. //判断星期天
  92. if endtime.Weekday().String() == "Sunday" {
  93. endtime = endtime.AddDate(0, 0, 1)
  94. } else if endtime.Weekday().String() == "Saturday" {
  95. endtime = endtime.AddDate(0, 0, 2)
  96. }
  97. return spare, endtime
  98. }