gitee.com/h79/goutils@v1.22.10/dao/wrapper/time.go (about) 1 package wrapper 2 3 import ( 4 "strings" 5 "time" 6 ) 7 8 // Time 时间范围 9 type Time struct { 10 Column string `form:"col" json:"col"` 11 Operator string `form:"operator" json:"operator"` 12 Min int64 `form:"begin" json:"begin"` 13 Max int64 `form:"end" json:"end"` 14 } 15 16 func (t *Time) Adjust() { 17 if t.Min > t.Max { 18 t.Min, t.Max = t.Max, t.Min 19 } 20 } 21 22 func (t *Time) Legal(legal int64) { 23 t.Adjust() 24 if t.Max-t.Min <= legal { 25 return 26 } 27 t.Min = t.Max - legal 28 } 29 30 func (t *Time) Month(month time.Duration, unit int) int64 { 31 return t.At(time.Hour*24*30*month, unit) 32 } 33 34 func (t *Time) Day(day time.Duration, unit int) int64 { 35 return t.At(time.Hour*24*day, unit) 36 } 37 38 func (t *Time) At(at time.Duration, unit int) int64 { 39 end := at.Milliseconds() 40 if unit == 1 { 41 end = int64(at.Seconds()) 42 } else if unit == 2 { 43 end = int64(at.Minutes()) 44 } else if unit == 3 { 45 end = int64(at.Hours()) 46 } 47 return end 48 } 49 50 func (t *Time) HasValid() bool { 51 // 时间有效,操作符长度不能太长(wrapper.Gt,wrapper.Between...),太长就是无效,在进行ToUpper时,造成时间消耗 52 return len(t.Operator) > 0 && len(t.Operator) < 8 && len(t.Column) > 0 53 } 54 55 func (t *Time) Cond(cond *Condition) { 56 if !t.HasValid() { 57 return 58 } 59 operator := strings.ToUpper(t.Operator) 60 if operator == ">" { 61 cond.Gt(t.Column, t.Min) 62 } else if operator == ">=" { 63 cond.Gte(t.Column, t.Min) 64 } else if operator == "<" { 65 cond.Lt(t.Column, t.Max) 66 } else if operator == "<=" { 67 cond.Lte(t.Column, t.Max) 68 } else if operator == "BETWEEN" { 69 cond.Between(t.Column, t.Min, t.Max) 70 } else if operator == "EQ" { 71 cond.Eq(t.Column, t.Min) 72 } 73 } 74 75 func (t *Time) And(cond *Condition) { 76 if !t.HasValid() { 77 return 78 } 79 op := strings.ToUpper(t.Operator) 80 if op == ">" { 81 cond.AndGt(t.Column, t.Min) 82 } else if op == ">=" { 83 cond.AndGte(t.Column, t.Min) 84 } else if op == "<" { 85 cond.AndLt(t.Column, t.Max) 86 } else if op == "<=" { 87 cond.AndLte(t.Column, t.Max) 88 } else if op == "BETWEEN" { 89 cond.AndBetween(t.Column, t.Min, t.Max) 90 } else if op == "=" { 91 cond.AndEq(t.Column, t.Min) 92 } 93 } 94 95 func (t *Time) Or(cond *Condition) { 96 if !t.HasValid() { 97 return 98 } 99 op := strings.ToUpper(t.Operator) 100 if op == ">" { 101 cond.OrGt(t.Column, t.Min) 102 } else if op == ">=" { 103 cond.OrGte(t.Column, t.Min) 104 } else if op == "<" { 105 cond.OrLt(t.Column, t.Max) 106 } else if op == "<=" { 107 cond.OrLte(t.Column, t.Max) 108 } else if op == "BETWEEN" { 109 cond.OrBetween(t.Column, t.Min, t.Max) 110 } else if op == "=" { 111 cond.OrEq(t.Column, t.Min) 112 } 113 } 114 115 type TimeV2 struct { 116 TimCol string `form:"time_col" json:"time_col"` 117 TimOp string `form:"time_op" json:"time_op"` 118 Start int64 `form:"start" json:"start"` 119 End int64 `form:"end" json:"end"` 120 } 121 122 func (t *TimeV2) HasValid() bool { 123 // 时间有效,操作符长度不能太长,太长就是无效,在进行ToUpper时,造成时间消耗 124 return len(t.TimOp) > 0 && len(t.TimOp) < 5 && len(t.TimCol) > 0 125 } 126 127 func (t *TimeV2) BuildCond(cond *Condition) { 128 if !t.HasValid() { 129 return 130 } 131 op := strings.ToUpper(t.TimOp) 132 if op == "GT" { 133 cond.Gt(t.TimCol, t.Start) 134 } else if op == "GTE" { 135 cond.Gte(t.TimCol, t.Start) 136 } else if op == "LT" { 137 cond.Lt(t.TimCol, t.End) 138 } else if op == "LTE" { 139 cond.Lte(t.TimCol, t.End) 140 } else if op == "BET" { 141 cond.Between(t.TimCol, t.Start, t.End) 142 } else if op == "EQ" { 143 cond.Eq(t.TimCol, t.Start) 144 } 145 } 146 147 func (t *TimeV2) And(cond *Condition) { 148 if !t.HasValid() { 149 return 150 } 151 op := strings.ToUpper(t.TimOp) 152 if op == "GT" { 153 cond.AndGt(t.TimCol, t.Start) 154 } else if op == "GTE" { 155 cond.AndGte(t.TimCol, t.Start) 156 } else if op == "LT" { 157 cond.AndLt(t.TimCol, t.End) 158 } else if op == "LTE" { 159 cond.AndLte(t.TimCol, t.End) 160 } else if op == "BET" { 161 cond.AndBetween(t.TimCol, t.Start, t.End) 162 } else if op == "EQ" { 163 cond.AndEq(t.TimCol, t.Start) 164 } 165 } 166 167 func (t *TimeV2) Or(cond *Condition) { 168 if !t.HasValid() { 169 return 170 } 171 op := strings.ToUpper(t.TimOp) 172 if op == "GT" { 173 cond.OrGt(t.TimCol, t.Start) 174 } else if op == "GTE" { 175 cond.OrGte(t.TimCol, t.Start) 176 } else if op == "LT" { 177 cond.OrLt(t.TimCol, t.End) 178 } else if op == "LTE" { 179 cond.OrLte(t.TimCol, t.End) 180 } else if op == "BET" { 181 cond.OrBetween(t.TimCol, t.Start, t.End) 182 } else if op == "EQ" { 183 cond.OrEq(t.TimCol, t.Start) 184 } 185 } 186 187 func (t *TimeV2) Adjust() { 188 if t.Start > t.End { 189 t.Start, t.End = t.End, t.Start 190 } 191 } 192 193 func (t *TimeV2) Legal(legal int64) { 194 t.Adjust() 195 if t.End-t.Start <= legal { 196 return 197 } 198 t.Start = t.End - legal 199 } 200 201 func (t *TimeV2) Month(month time.Duration, unit int) int64 { 202 return t.At(time.Hour*24*30*month, unit) 203 } 204 205 func (t *TimeV2) Day(day time.Duration, unit int) int64 { 206 return t.At(time.Hour*24*day, unit) 207 } 208 209 func (t *TimeV2) At(at time.Duration, unit int) int64 { 210 end := at.Milliseconds() 211 if unit == 1 { 212 end = int64(at.Seconds()) 213 } else if unit == 2 { 214 end = int64(at.Minutes()) 215 } else if unit == 3 { 216 end = int64(at.Hours()) 217 } 218 return end 219 }