123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- package common
- import (
- "fmt"
- "gorm.io/gorm"
- "math/rand"
- "strconv"
- "strings"
- "time"
- )
- const (
- OperationDefault = iota
- OperationLogin
- OperationLogout
- OperationCreate
- OperationUpdate
- OperationRemove
- OperationImport
- OperationLightStrategy
- OperationOrderCreate
- OperationOrderChange
- OperationOrderHandle
- OperationProgramPublish
- OperationProgramResult
- OperationStrategyRelation
- OperationControl
- OperationAlarmHandle
- OperationSuccess = 0
- OperationFail = 1
- )
- const (
- ModuleTypeDefault = iota
- ModuleTypeDevice
- ModuleTypeInfoBar
- ModuleTypeOrder
- ModuleTypeNotification
- ModuleTypeLightStrategy
- ModuleTypeLighting
- ModuleTypeSystem
- ModuleTypeAlarm
- ModuleTypeOperation
- ModuleTypeRecord
- ModuleTypeWisdomLighting
- )
- const (
- DeviceTypeDefault = 0
- DeviceTypeCamera = 100
- DeviceTypeLightControl = 101
- DeviceTypeInfoBoard = 102
- DeviceTypeSwitchBox = 103
- DeviceTypeGateway = 104
- DeviceTypeOptoSensor = 105
- DeviceTypeZigbee = 106
- DeviceTypeAlarmTerminal = 107
- DeviceTypeAlarmServer = 108
- DeviceTypeBridgeSensor = 109
- DeviceTypeBridge = 110
- DeviceTypeIPBroadcast = 111
- DeviceTypeManholeCover = 112
- DeviceTypeGarbage = 113
- DeviceTypeLampPoleGroup = 114
- DeviceTypeLampPole = 115
- DeviceTypeCaptureUnit = 116
- DeviceTypePoint = 117
- DeviceTypeGarbageGroup = 118
- DeviceTypeLightStrategy = 119
- DeviceTypeOnDemandGroup = 120
- DeviceTypeOnDemandSensor = 121
- DeviceTypeTransformer = 122
- )
- var mLocation *time.Location
- func init() {
- loc, err := time.LoadLocation("Asia/Shanghai")
- if err != nil {
- mLocation = time.FixedZone("CST", 8*3600)
- } else {
- mLocation = loc
- }
- }
- func GetDeviceObject(id int, name string) string {
- return strconv.Itoa(id) + "(" + name + ")"
- }
- func StringToInt(id string) int {
- if id != "" {
- id, err := strconv.Atoi(id)
- if err == nil {
- return id
- }
- }
- return -1
- }
- func RandomString(n int) string {
- var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
- rand.Seed(time.Now().Unix())
- b := make([]rune, n)
- for i := range b {
- b[i] = letters[rand.Intn(len(letters))]
- }
- return string(b)
- }
- func RandomString2(n int) string {
- var letters = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
- rand.Seed(time.Now().Unix())
- b := make([]rune, n)
- for i := range b {
- b[i] = letters[rand.Intn(len(letters))]
- }
- return string(b)
- }
- func StringToInt64Array(str string) []int64 {
- tmp := strings.Split(str, ",")
- var result []int64
- for _, t := range tmp {
- i, _ := strconv.ParseInt(t, 10, 64)
- result = append(result, i)
- }
- return result
- }
- func StringToIntArray(str string) []int {
- tmp := strings.Split(str, ",")
- var result []int
- for _, t := range tmp {
- i, _ := strconv.Atoi(t)
- result = append(result, i)
- }
- return result
- }
- func MlParseTime(strTime string) (time.Time, error) {
- if strings.Contains(strTime, ".") {
- return time.ParseInLocation("2006-01-02 15:04:05.000", strTime, mLocation)
- }
- return time.ParseInLocation("2006-01-02 15:04:05", strTime, mLocation)
- }
- func IsAdmin(roleId int64) bool {
- return roleId == 1
- }
- type TableModelAuto struct {
- Model interface{}
- Comment string
- }
- func MultimediaEfficacyTime(stime Time, etime Time, time2 string) bool {
- stimeStr := stime.String()
- etimeStr := etime.String()
- if stimeStr == "" || etimeStr == "" || time2 == "" {
- return true
- }
- currDate := time.Now().Format("2006-01-02")
- kstime := currDate + time2
- nowTime, _ := time.Parse("2006-01-02 15:04:05", kstime)
- time3 := time.Now()
- if etimeStr == stimeStr && currDate == etimeStr && time3.Before(nowTime) {
- return false
- }
- return true
- }
- func Paginate(page int, size int) func(db *gorm.DB) *gorm.DB {
- return func(db *gorm.DB) *gorm.DB {
- page := page
- if page == 0 {
- page = 1
- }
- pageSize := size
- switch {
- case pageSize > 100:
- pageSize = 100
- case pageSize <= 0:
- pageSize = 10
- }
- offset := (page - 1) * pageSize
- return db.Offset(offset).Limit(pageSize)
- }
- }
- var timeTemplates = []string{
- "2006-01-02 15:04:05",
- "2006/01/02 15:04:05",
- "2006-01-02",
- "2006/01/02",
- "15:04:05",
- }
- func TimeStringToGoTime(tm string) time.Time {
- for i := range timeTemplates {
- t, err := time.ParseInLocation(timeTemplates[i], tm, time.Local)
- if nil == err && !t.IsZero() {
- return t
- }
- }
- return time.Time{}
- }
- func GetTimeDays(start_time, stop_time string) []string {
- tm1, _ := time.Parse("2006-01-02", start_time)
- tm2, _ := time.Parse("2006-01-02", stop_time)
- sInt := tm1.Unix()
- eInt := tm2.Unix()
- var args []string
- for {
- sInt += 86400
- st := time.Unix(sInt, 0).Format("2006-01-02")
- args = append(args, st)
- if sInt > eInt {
- break
- }
- }
- var days []string
- for i := 0; i < len(args); i++ {
- parse, _ := time.Parse("2006-01-02", start_time)
- date := parse.AddDate(0, 0, i).Format("2006-01-02")
- days = append(days, date)
- }
- return Reverse(days)
- }
- func GetTimeMonths(start_time, stop_time string) []string {
- tm1, _ := time.Parse("2006-01", start_time)
- tm2, _ := time.Parse("2006-01", stop_time)
- sInt := tm1.Unix()
- eInt := tm2.Unix()
- var args []string
- for {
- sInt += 86400
- st := time.Unix(sInt, 0).Format("20060102")
- args = append(args, st)
- if sInt > eInt {
- break
- }
- }
- var months []string
- i := 0
- for {
- parse, _ := time.Parse("2006-01-02", start_time)
- endStr, _ := time.Parse("2006-01-02", stop_time)
- month := parse.AddDate(0, i, 0).Format("2006-01")
- month = month + "-01"
- months = append(months, month)
- if month == endStr.Format("2006-01")+"-01" || i > 24 {
- break
- }
- i++
- }
- return Reverse(months)
- }
- func Decimal(value float64) float64 {
- value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
- return value
- }
- func Reverse(s []string) []string {
- for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
- s[i], s[j] = s[j], s[i]
- }
- return s
- }
|