diff --git a/api/v1/common.go b/api/v1/common.go new file mode 100644 index 0000000..2b705bc --- /dev/null +++ b/api/v1/common.go @@ -0,0 +1,14 @@ +package v1 + +import ( + "energy-management-system/response" + "energy-management-system/service" + "github.com/gin-gonic/gin" +) + +type Common struct{} + +func (r *Common) GetEnergyTypeMap(c *gin.Context) { + respData := service.GroupServices.Common.GetEnergyTypeMap() + response.SuccessData(respData, c) +} diff --git a/api/v1/device_point.go b/api/v1/device_point.go new file mode 100644 index 0000000..32e88e2 --- /dev/null +++ b/api/v1/device_point.go @@ -0,0 +1,22 @@ +package v1 + +import ( + "energy-management-system/form" + "energy-management-system/request" + "github.com/gin-gonic/gin" +) + +type DevicePoint struct{} + +// 获取测点名称select +// PointNameSelect 季度列表 +func (r *DevicePoint) PointNameSelect(c *gin.Context) { + var req form.PointNameSelectReq + request.BindParam(c, &req) + //respData := service.GroupServices.PeakValley.PeakValleyQuarterPage(&req) + //response.SuccessData(respData, c) + +} + +// 创建设备测点 +// 修改设备测点 diff --git a/api/v1/enter.go b/api/v1/enter.go index 249d12d..53d8a90 100644 --- a/api/v1/enter.go +++ b/api/v1/enter.go @@ -1,8 +1,10 @@ package v1 type Controller struct { + Common TestApi - PeakValley // 谷峰规则 + PeakValley // 谷峰规则 + DevicePoint // //UserApi // 用户管理 //DictApi // 字典管理 //ProductApi // 产品管理 diff --git a/api/v1/peak_valley.go b/api/v1/peak_valley.go index 4b603b3..94ca683 100644 --- a/api/v1/peak_valley.go +++ b/api/v1/peak_valley.go @@ -5,9 +5,7 @@ import ( "energy-management-system/request" "energy-management-system/response" "energy-management-system/service" - "energy-management-system/utils/exception" "github.com/gin-gonic/gin" - "strconv" ) type PeakValley struct{} @@ -40,9 +38,10 @@ func (r *PeakValley) UpdatePeakValleyQuarter(c *gin.Context) { // PeakValleyQuarterEditDetail 查看季度 func (r *PeakValley) PeakValleyQuarterEditDetail(c *gin.Context) { var req form.PeakValleyQuarterEditDetailReq - id, err := strconv.Atoi(c.Param("id")) - exception.PBM(err != nil, "参数有误") - req.Id = id + request.BindParam(c, &req) + //id, err := strconv.Atoi(c.Param("id")) + //exception.PBM(err != nil, "参数有误") + //req.Id = id respData := service.GroupServices.PeakValley.PeakValleyQuarterEditDetail(&req) response.SuccessData(respData, c) } @@ -80,18 +79,21 @@ func (r *PeakValley) UpdatePeakValleyRule(c *gin.Context) { // PeakValleyRuleDetail 谷峰规则详情 func (r *PeakValley) PeakValleyRuleDetail(c *gin.Context) { var req form.PeakValleyRuleDetailReq - id, err := strconv.Atoi(c.Param("id")) - exception.PBM(err != nil, "参数有误") - req.RuleId = id + request.BindParam(c, &req) + + //id, err := strconv.Atoi(c.Param("id")) + //exception.PBM(err != nil, "参数有误") + //req.RuleId = id respData := service.GroupServices.PeakValley.PeakValleyRuleDetail(&req) response.SuccessData(respData, c) } func (r *PeakValley) PeakValleyRuleEditDetail(c *gin.Context) { var req form.PeakValleyRuleEditDetailReq - id, err := strconv.Atoi(c.Param("id")) - exception.PBM(err != nil, "参数有误") - req.RuleId = id + request.BindParam(c, &req) + //id, err := strconv.Atoi(c.Param("id")) + //exception.PBM(err != nil, "参数有误") + //req.RuleId = id respData := service.GroupServices.PeakValley.PeakValleyRuleEditDetail(&req) response.SuccessData(respData, c) } diff --git a/config.yaml b/config.yaml index 6eef66d..360a5e7 100644 --- a/config.yaml +++ b/config.yaml @@ -1,4 +1,6 @@ is_debug: true + + service: http: host: 0.0.0.0 # 默认localhost @@ -15,4 +17,10 @@ db: log_level: 4 slow_threshold: 200 idle_conns: 10 - open_conns: 50 \ No newline at end of file + open_conns: 50 + +influxdb: + # host: "http://192.168.0.47:18086" + # token: "BfG_zW41Wddgbuig0GXt6TuDWpHjUgjTJGDFi9ZI6fOXeYwyWKhakTrwbRT8f4uqFQCWXbRGxs8f5GaChW5tqw==" + host: "http://192.168.0.9:8086" + token: "4Sz5qRK-VY0aGh7m7sYDkG5tWCwKjbDYAlkgFpvUcZTBn7XahGqczeye7BQCRjcWP8fqsefPeNkwfvUnqa69oA==" \ No newline at end of file diff --git a/config/config.go b/config/config.go index a666ded..453d714 100644 --- a/config/config.go +++ b/config/config.go @@ -14,7 +14,8 @@ const ( ) type Config struct { - IsDebug bool `mapstructure:"is_debug" json:"is_debug" yaml:"is_debug"` - Db DbConf `mapstructure:"db" json:"db" yaml:"db"` - Service ServiceConf `mapstructure:"service" json:"service" yaml:"service"` + IsDebug bool `mapstructure:"is_debug" json:"is_debug" yaml:"is_debug"` + Db DbConf `mapstructure:"db" json:"db" yaml:"db"` + Service ServiceConf `mapstructure:"service" json:"service" yaml:"service"` + InFluxDb InFluxDbConf `mapstructure:"influxdb" json:"influxdb" yaml:"influxdb"` } diff --git a/config/influxdb.go b/config/influxdb.go new file mode 100644 index 0000000..fbc51d7 --- /dev/null +++ b/config/influxdb.go @@ -0,0 +1,6 @@ +package config + +type InFluxDbConf struct { + Host string `mapstructure:"host" json:"host" yaml:"host"` + Token string `mapstructure:"token" json:"token" yaml:"token"` +} diff --git a/core/cron/crontab.go b/core/cron/crontab.go index af03145..d4e1dd7 100644 --- a/core/cron/crontab.go +++ b/core/cron/crontab.go @@ -1,33 +1,96 @@ package cron import ( - "energy-management-system/utils" + "energy-management-system/global" + "energy-management-system/utils/exception" "energy-management-system/utils/recovery" "fmt" "github.com/robfig/cron/v3" + "os" "reflect" "runtime" + "time" ) -var servers server +var funs customFunc +var jobs customJob -type server struct { - Cron []string - Func []func() +type customFunc struct { + Specs []string + Func []func() } -func (c *server) register(cron string, f func()) { - c.Cron = append(c.Cron, cron) +func (c *customFunc) register(spec string, f func()) { + c.Specs = append(c.Specs, spec) c.Func = append(c.Func, f) } -func CronRun() { - c := cron.New() - for k, v := range servers.Cron { - eid, err := c.AddFunc(v, wrap(servers.Func[k])) - fmt.Println("定时任务:", eid, "添加成功") - utils.Exit(err, "添加定时器错误:") +// RegisterCronFunc 参数:规则(s m h d m w),函数 +func RegisterCronFunc(spec string, f func()) { + funs.register(spec, f) +} + +func RegisterRuntimeCronFunc(spec string, f func()) { + _, err := global.Cron.AddFunc(spec, wrap(f)) + if err != nil { + exception.PEM(err, "添加定时func失败") + return } +} + +type customJob struct { + Specs []string + Jobs []cron.Job +} + +func (c *customJob) register(spec string, f cron.Job) { + c.Specs = append(c.Specs, spec) + c.Jobs = append(c.Jobs, f) +} + +// RegisterCronJob 参数:规则(s m h d m w),函数 +func RegisterCronJob(spec string, job cron.Job) { + jobs.register(spec, job) +} +func RegisterRuntimeCronJob(spec string, job cron.Job) { + _, err := global.Cron.AddJob(spec, job) + if err != nil { + exception.PEM(err, "添加定时job失败") + return + } +} + +func InitCron() { + // 设置时区 + loc, _ := time.LoadLocation("Asia/Shanghai") + + location := cron.WithLocation(loc) + // 开启秒 + withSeconds := cron.WithSeconds() + // 自定义规则 + withParser := cron.WithParser(cron.NewParser(cron.SecondOptional | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor)) + + // cron初始化 + c := cron.New(location, withSeconds, withParser) + + // 初始化预定Job + for idx, spec := range jobs.Specs { + _, err := c.AddJob(spec, jobs.Jobs[idx]) + //_, err := c.AddFunc(v, wrap(servers.Func[k])) + if err != nil { + fmt.Printf("[-]注册初始定时job失败: %s \n", err) + os.Exit(1) + } + } + // 初始化预定func + for idx, spec := range funs.Specs { + _, err := c.AddFunc(spec, wrap(funs.Func[idx])) + if err != nil { + fmt.Printf("[-]注册初始定时func失败: %s \n", err) + os.Exit(1) + } + } + global.Cron = c c.Start() } @@ -37,8 +100,3 @@ func wrap(f func()) func() { f() } } - -// Register 参数:规则(s m h d m w),函数 -func Register(rule string, f func()) { - servers.register(rule, f) -} diff --git a/core/gorm/gorm.go b/core/gorm/gorm.go index ca26a6a..8beea84 100644 --- a/core/gorm/gorm.go +++ b/core/gorm/gorm.go @@ -2,6 +2,7 @@ package gorm import ( "energy-management-system/global" + "energy-management-system/model" "energy-management-system/model/init-db-data" peak_valley "energy-management-system/model/peak-valley" "fmt" @@ -66,6 +67,9 @@ func AutoMigrate(db *gorm.DB) { new(peak_valley.PeakValleyQuarter), new(peak_valley.PeakValleyRule), + + new(model.DevicePoint), + new(model.PointName), //new(model.Role), //new(model.UserRole), //new(model.Api), diff --git a/core/influxdb/influxdb.go b/core/influxdb/influxdb.go new file mode 100644 index 0000000..bba686d --- /dev/null +++ b/core/influxdb/influxdb.go @@ -0,0 +1,22 @@ +package influxdb + +import ( + "energy-management-system/global" + influxdb2 "github.com/influxdata/influxdb-client-go/v2" +) + +func InitInFluxDb() { + + // Create a new client using an InfluxDB server base URL and an authentication token + client := influxdb2.NewClient(global.AppConf.InFluxDb.Host, global.AppConf.InFluxDb.Token) + // Use blocking write client for writes to desired bucket + + //获取非阻塞式写入对象 + //writeAPI = client.WriteAPI("my-org", "my-bucket") + //writeAPI := client.WriteAPIBlocking("my-org", "my-bucket") + + //获取阻塞式写入对象 + //writeBlockingAPI := client.WriteAPIBlocking("iuu", "iuu-bucket") + + global.InFluxDb = client +} diff --git a/core/viper/viper.go b/core/viper/viper.go index b0ed4d9..e64726d 100644 --- a/core/viper/viper.go +++ b/core/viper/viper.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "github.com/fsnotify/fsnotify" + "github.com/gookit/goutil/dump" "github.com/spf13/viper" "os" ) @@ -54,6 +55,6 @@ func InitViper(path ...string) { } fmt.Println("[+]加载配置文件完成") - //dump.P(global.AppConf) + dump.P(global.AppConf) } diff --git a/form/device_point.go b/form/device_point.go new file mode 100644 index 0000000..fd35a92 --- /dev/null +++ b/form/device_point.go @@ -0,0 +1,5 @@ +package form + +type PointNameSelectReq struct { + Keyword string `form:"keyword" json:"keyword"` +} diff --git a/form/peak_valley_rule.go b/form/peak_valley_rule.go index c7528bc..0befab7 100644 --- a/form/peak_valley_rule.go +++ b/form/peak_valley_rule.go @@ -11,7 +11,7 @@ type CreatePeakValleyQuarterReq struct { QuarterName string `json:"quarter_name"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` - RuleId uint `json:"rule_id"` + RuleId int `json:"rule_id"` } type UpdatePeakValleyQuarterReq struct { @@ -19,7 +19,7 @@ type UpdatePeakValleyQuarterReq struct { QuarterName string `json:"quarter_name"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` - RuleId uint `json:"rule_id"` + RuleId int `json:"rule_id"` } type PeakValleyRuleListReq struct { @@ -48,7 +48,7 @@ type UpdatePeakValleyRuleReq struct { type TimeBlockPriceReq struct { CustomName string `json:"custom_name"` - PeakValleyType uint `json:"peak_valley_type"` + PeakValleyType int `json:"peak_valley_type"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` Price int `json:"price"` diff --git a/global/var.go b/global/var.go index 86e1260..8fef108 100644 --- a/global/var.go +++ b/global/var.go @@ -2,6 +2,8 @@ package global import ( "energy-management-system/config" + "github.com/influxdata/influxdb-client-go/v2" + "github.com/robfig/cron/v3" "gorm.io/gorm" ) @@ -11,9 +13,9 @@ var ( // Db 数据库 Db *gorm.DB + InFluxDb influxdb2.Client + Log *Logger - // Trans 定义一个全局翻译器T - //Trans ut.Translator - //Validate *validator.Validate + Cron *cron.Cron ) diff --git a/go.mod b/go.mod index 89200d3..3a18577 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,10 @@ require ( github.com/go-playground/universal-translator v0.18.1 github.com/go-playground/validator/v10 v10.20.0 github.com/gookit/color v1.5.4 + github.com/gookit/goutil v0.6.16 + github.com/influxdata/influxdb-client-go/v2 v2.14.0 github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible + github.com/robfig/cron/v3 v3.0.0 github.com/sirupsen/logrus v1.9.3 github.com/spf13/viper v1.19.0 gorm.io/driver/postgres v1.5.9 @@ -17,6 +20,7 @@ require ( ) require ( + github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect github.com/bytedance/sonic v1.11.6 // indirect github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect @@ -24,7 +28,9 @@ require ( github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/goccy/go-json v0.10.2 // indirect + github.com/google/uuid v1.4.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect github.com/jackc/pgx/v5 v5.5.5 // indirect @@ -41,9 +47,9 @@ require ( github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/oapi-codegen/runtime v1.0.0 // indirect github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/robfig/cron/v3 v3.0.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect diff --git a/go.sum b/go.sum index 3bf1df9..db9c881 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= +github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= +github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= +github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= @@ -33,10 +37,18 @@ github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MG github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= +github.com/gookit/goutil v0.6.16 h1:9fRMCF4X9abdRD5+2HhBS/GwafjBlTUBjRtA5dgkvuw= +github.com/gookit/goutil v0.6.16/go.mod h1:op2q8AoPDFSiY2+qkHxcBWQMYxOLQ1GbLXqe7vrwscI= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/influxdata/influxdb-client-go/v2 v2.14.0 h1:AjbBfJuq+QoaXNcrova8smSjwJdUHnwvfjMF71M1iI4= +github.com/influxdata/influxdb-client-go/v2 v2.14.0/go.mod h1:Ahpm3QXKMJslpXl3IftVLVezreAUtBOTZssDrjZEFHI= +github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU= +github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -53,6 +65,7 @@ github.com/jonboulle/clockwork v0.4.0 h1:p4Cf1aMWXnXAUh8lVfewRBx1zaTSYKrKMF2g3ST github.com/jonboulle/clockwork v0.4.0/go.mod h1:xgRqUGwRcjKCO1vbZUEtSLrqKoPSsUpK7fnezOII0kc= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= @@ -80,6 +93,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/oapi-codegen/runtime v1.0.0 h1:P4rqFX5fMFWqRzY9M/3YF9+aPSPPB06IzP2P7oOxrWo= +github.com/oapi-codegen/runtime v1.0.0/go.mod h1:LmCUMQuPB4M/nLXilQXhHw+BLZdDb18B34OO356yJ/A= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -107,6 +122,7 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI= github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg= +github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/jobs/job.go b/jobs/job.go new file mode 100644 index 0000000..bde7464 --- /dev/null +++ b/jobs/job.go @@ -0,0 +1,5 @@ +package jobs + +type Job interface { + GetJobName() string +} diff --git a/jobs/test.go b/jobs/test.go new file mode 100644 index 0000000..0c7a93a --- /dev/null +++ b/jobs/test.go @@ -0,0 +1,24 @@ +package jobs + +import ( + "energy-management-system/utils/exception" + "energy-management-system/utils/recovery" + "fmt" + "reflect" + "runtime" +) + +type Test struct { + Name string +} + +func (t *Test) Run() { + defer recovery.CronRecovery(runtime.FuncForPC(reflect.ValueOf(t.Run).Pointer()).Name()) + exception.PM("定时任务错误") + //t.GetJobName() + fmt.Println(t.GetJobName() + "Test Run") +} + +func (t *Test) GetJobName() string { + return t.Name +} diff --git a/main.go b/main.go index 4e28280..c305eff 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "energy-management-system/core/cron" "energy-management-system/core/gorm" + "energy-management-system/core/influxdb" "energy-management-system/core/logger" "energy-management-system/core/viper" "energy-management-system/global" @@ -22,22 +23,28 @@ func init() { viper.InitViper() // 日志 logger.InitLogger() + // InFluxDb + influxdb.InitInFluxDb() // 数据库 gorm.InitGorm() - // gin验证器 - validator.InitTrans() // 可选初始化表结构 gorm.AutoMigrate(global.Db) // 可选初始化表数据 - gorm.AutoInitDbData() + //gorm.AutoInitDbData() + // gin验证器 + validator.InitTrans() } func main() { - cron.Register("0/1 * * * * ", func() { - fmt.Println(time.Now()) - }) - cron.CronRun() + cron.InitCron() + + //cron.RegisterCronJob("0/2 * * * * ?", &jobs.Test{Name: "测试任务"}) + //cron.RegisterCronFunc("0/2 * * * * ?", func() { + // exception.PM("定时任务错误") + // //panic("xxxxx") + // fmt.Println(111) + //}) r := router.InitRouter() host := global.AppConf.Service.Http.Host diff --git a/middleware/recovery.go b/middleware/recovery.go- similarity index 100% rename from middleware/recovery.go rename to middleware/recovery.go- diff --git a/model/device_point.go b/model/device_point.go new file mode 100644 index 0000000..650f7a3 --- /dev/null +++ b/model/device_point.go @@ -0,0 +1,23 @@ +package model + +import ( + "energy-management-system/global" + "gorm.io/gorm" + "time" +) + +// DevicePoint 设备测点配置 +type DevicePoint struct { + Id int `gorm:"column:id;primaryKey" json:"id"` + DriverId int `gorm:"column:driver_id;comment:设备ID" json:"driver_id"` + PointId int `gorm:"column:point_id;comment:测点ID" json:"point_id"` + EnergyType int `gorm:"column:energy_type;comment:能源类型" json:"energy_type"` + PointNameId int `gorm:"column:point_name_id;comment:测点名称ID" json:"point_name_id"` + Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"` + Updated time.Time `gorm:"column:updated;autoUpdateTime;comment:修改时间" json:"updated"` + DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"` +} + +func (r *DevicePoint) TableName() string { + return global.AppConf.Db.TablePrefix + "device_point_configs" +} diff --git a/model/init-db-data/init.go b/model/init-db-data/init.go index 2f1028e..dc1ff7f 100644 --- a/model/init-db-data/init.go +++ b/model/init-db-data/init.go @@ -21,7 +21,10 @@ func initDB(InitDBFunctions ...InitDBFunc) (err error) { } func InitDbData() { - err := initDB(initPeakValleyTimeBlockData) + err := initDB( + initPeakValleyTimeBlockData, + initPointNameData, + ) if err != nil { fmt.Println("[-]初始化基础数据失败:", err) os.Exit(0) diff --git a/model/init-db-data/point_name.go b/model/init-db-data/point_name.go new file mode 100644 index 0000000..856267c --- /dev/null +++ b/model/init-db-data/point_name.go @@ -0,0 +1,56 @@ +package init_db_data + +import ( + "energy-management-system/global" + "energy-management-system/model" + "github.com/gookit/color" + "gorm.io/gorm" +) + +type InitPointNameData struct{} + +var initPointNameData = new(InitPointNameData) + +// type api struct{} +//func buildData() (dataList []*peak_valley_model.PeakValleyTimeBlock) { +// // 一天有24小时,即1440分钟 +// totalMinutes := 24 * 60 +// // 时间块的持续时间:10分钟 +// blockDuration := 10 +// // 遍历一天中的所有时间块 +// for i := 0; i < totalMinutes; i += blockDuration { +// startTime := i +// endTime := i + blockDuration +// dataList = append(dataList, &peak_valley_model.PeakValleyTimeBlock{ +// StartTime: uint(startTime), +// EndTime: uint(endTime), +// }) +// } +// return dataList +//} + +var pointNames = []model.PointName{ + {Name: "测点-用电量"}, + {Name: "测点-用水量"}, +} + +// Init 初始化用户数据 +func (i *InitPointNameData) Init() error { + return global.Db.Transaction(func(tx *gorm.DB) error { + m := &model.PointName{} + var count int64 + err := tx.Model(&model.PointName{}).Count(&count).Error + if err != nil { + return err + } + if count > 0 { + color.Danger.Println("\n[PGSQL] --> " + m.TableName() + " 表的初始数据已存在!") + return nil + } + if err := tx.Create(&pointNames).Error; err != nil { // 遇到错误时回滚事务 + return err + } + color.Info.Println("\n[PGSQL] --> " + m.TableName() + " 表初始数据成功!") + return nil + }) +} diff --git a/model/peak-valley/peak_valley_quarter.go b/model/peak-valley/peak_valley_quarter.go index dff99f9..9ed1874 100644 --- a/model/peak-valley/peak_valley_quarter.go +++ b/model/peak-valley/peak_valley_quarter.go @@ -7,11 +7,11 @@ import ( ) type PeakValleyQuarter struct { - Id uint `gorm:"column:id;primaryKey" json:"id"` + Id int `gorm:"column:id;primaryKey" json:"id"` QuarterName string `gorm:"column:quarter_name;comment:季度名称" json:"quarter_name"` StartTime time.Time `gorm:"column:start_time;type:date;comment:开始时间" json:"start_time"` EndTime time.Time `gorm:"column:end_time;type:date;comment:结束时间" json:"end_time"` - Rid uint `gorm:"column:rid;comment:季度使用规则" json:"rid"` + Rid int `gorm:"column:rid;comment:季度使用规则" json:"rid"` Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"` Updated time.Time `gorm:"column:updated;autoUpdateTime;comment:修改时间" json:"updated"` DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"` diff --git a/model/peak-valley/peak_valley_rule.go b/model/peak-valley/peak_valley_rule.go index a41b4e3..775c3e5 100644 --- a/model/peak-valley/peak_valley_rule.go +++ b/model/peak-valley/peak_valley_rule.go @@ -16,7 +16,7 @@ import ( //(将时间段 转化为十分钟区块 查找到对应十分钟区块 创建该规则的电价) type PeakValleyRule struct { - RuleId uint `gorm:"column:rule_id;primaryKey" json:"rule_id"` + RuleId int `gorm:"column:rule_id;primaryKey" json:"rule_id"` RuleName string `gorm:"column:rule_name;comment:规则名称" json:"rule_name"` Description string `gorm:"column:description;comment:描述" json:"description"` Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"` diff --git a/model/peak-valley/peak_valley_time_block.go b/model/peak-valley/peak_valley_time_block.go index 4118e60..3b13ccd 100644 --- a/model/peak-valley/peak_valley_time_block.go +++ b/model/peak-valley/peak_valley_time_block.go @@ -11,7 +11,7 @@ const MinutesInADay = 24 * 60 // PeakValleyTimeBlock 峰谷时间区块 type PeakValleyTimeBlock struct { - BlockIndex uint `gorm:"column:block_index;primaryKey;comment:区块编号" json:"block_index"` + BlockIndex int `gorm:"column:block_index;primaryKey;comment:区块编号" json:"block_index"` StartTime uint `gorm:"column:start_time;comment:开始时间" json:"start_time"` EndTime uint `gorm:"column:end_time;comment:结束时间" json:"end_time"` Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"` diff --git a/model/peak-valley/peak_valley_time_block_price.go b/model/peak-valley/peak_valley_time_block_price.go index 00a0c1b..854a543 100644 --- a/model/peak-valley/peak_valley_time_block_price.go +++ b/model/peak-valley/peak_valley_time_block_price.go @@ -35,12 +35,12 @@ type PeakValleyType struct { // PeakValleyTimeBlockPrice 峰谷时间区块价格 type PeakValleyTimeBlockPrice struct { - Id uint `gorm:"column:id;primaryKey" json:"id"` - BlockId uint `gorm:"column:block_id;comment:时间区块编号" json:"block_id"` + Id int `gorm:"column:id;primaryKey" json:"id"` + BlockId int `gorm:"column:block_id;comment:时间区块编号" json:"block_id"` Price int `gorm:"column:price;comment:价格" json:"price"` CustomName string `gorm:"column:custom_name;comment:自定义名称" json:"custom_name"` - PeakValleyRuleId uint `gorm:"column:peak_valley_rule_id;comment:峰谷规则" json:"peak_valley_rule_id"` - PeakValleyType uint `gorm:"column:peak_valley_type;default:1;size:10;comment:峰谷类型[1:尖,2:峰,3:平,4:谷,5:深谷]" json:"peak_valley_type"` // 峰谷类型 + PeakValleyRuleId int `gorm:"column:peak_valley_rule_id;comment:峰谷规则" json:"peak_valley_rule_id"` + PeakValleyType int `gorm:"column:peak_valley_type;default:1;size:10;comment:峰谷类型[1:尖,2:峰,3:平,4:谷,5:深谷]" json:"peak_valley_type"` // 峰谷类型 Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"` Updated time.Time `gorm:"column:updated;autoUpdateTime;comment:修改时间" json:"updated"` DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"` diff --git a/model/point_name.go b/model/point_name.go new file mode 100644 index 0000000..ab61b4f --- /dev/null +++ b/model/point_name.go @@ -0,0 +1,20 @@ +package model + +import ( + "energy-management-system/global" + "gorm.io/gorm" + "time" +) + +// PointName 测点名称 +type PointName struct { + Id int `gorm:"column:id;primaryKey" json:"id"` + Name string `gorm:"column:name;comment:测点名称" json:"name"` + Created time.Time `gorm:"column:created;autoCreateTime;comment:创建时间" json:"created"` + Updated time.Time `gorm:"column:updated;autoUpdateTime;comment:修改时间" json:"updated"` + DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"-"` +} + +func (r *PointName) TableName() string { + return global.AppConf.Db.TablePrefix + "point_names" +} diff --git a/repository/peak-valley/peak_valley_time_block.go b/repository/peak-valley/peak_valley_time_block.go index aca9436..573ac26 100644 --- a/repository/peak-valley/peak_valley_time_block.go +++ b/repository/peak-valley/peak_valley_time_block.go @@ -6,7 +6,7 @@ import ( "gorm.io/gorm" ) -func (r *PeakValley) GetTimeBlockIdsByTimeBlock(tx *gorm.DB, startTime, endTime uint) (ids []uint, err error) { +func (r *PeakValley) GetTimeBlockIdsByTimeBlock(tx *gorm.DB, startTime, endTime uint) (ids []int, err error) { err = tx.Model(&peak_valley_model.PeakValleyTimeBlock{}). Where("start_time < ? AND end_time > ?", endTime, startTime). Pluck("block_index", &ids).Error diff --git a/router/router.go b/router/router.go index 198b7af..257ddb3 100644 --- a/router/router.go +++ b/router/router.go @@ -50,7 +50,9 @@ func InitRouter() *gin.Engine { //apiV1.Use(middleware.CasbinRBAC()) { + routes.GroupRoutes.Common.InitCommon(apiV1) routes.GroupRoutes.PeakValley.InitPeakValley(apiV1) + routes.GroupRoutes.DevicePoint.InitDevicePoint(apiV1) } } diff --git a/router/routes/common.go b/router/routes/common.go new file mode 100644 index 0000000..c60a892 --- /dev/null +++ b/router/routes/common.go @@ -0,0 +1,17 @@ +package routes + +import ( + v1 "energy-management-system/api/v1" + "github.com/gin-gonic/gin" +) + +type Common struct{} + +func (r *Common) InitCommon(Router *gin.RouterGroup) { + commonApi := Router.Group("common") + { + + commonApi.GET("energyTypes", v1.Controllers.Common.GetEnergyTypeMap) + + } +} diff --git a/router/routes/device_point.go b/router/routes/device_point.go new file mode 100644 index 0000000..3a66715 --- /dev/null +++ b/router/routes/device_point.go @@ -0,0 +1,17 @@ +package routes + +import ( + v1 "energy-management-system/api/v1" + "github.com/gin-gonic/gin" +) + +type DevicePoint struct{} + +func (r *DevicePoint) InitDevicePoint(Router *gin.RouterGroup) { + devicePointApi := Router.Group("devicePoint") + { + + devicePointApi.GET("pointNameSelect", v1.Controllers.DevicePoint.PointNameSelect) + + } +} diff --git a/router/routes/enter.go b/router/routes/enter.go index 4856981..da8d93b 100644 --- a/router/routes/enter.go +++ b/router/routes/enter.go @@ -1,7 +1,9 @@ package routes type groupRoutes struct { + Common PeakValley + DevicePoint } var GroupRoutes = new(groupRoutes) diff --git a/router/routes/peak_valley.go b/router/routes/peak_valley.go index c996c03..32c1ab9 100644 --- a/router/routes/peak_valley.go +++ b/router/routes/peak_valley.go @@ -15,13 +15,13 @@ func (r *PeakValley) InitPeakValley(Router *gin.RouterGroup) { peakValleyApi.GET("rulePage", v1.Controllers.PeakValley.GetPeakValleyRulePage) peakValleyApi.POST("createRule", v1.Controllers.PeakValley.CreatePeakValleyRule) peakValleyApi.PUT("updateRule", v1.Controllers.PeakValley.UpdatePeakValleyRule) - peakValleyApi.GET("ruleDetail/:id", v1.Controllers.PeakValley.PeakValleyRuleDetail) - peakValleyApi.GET("ruleEditDetail/:id", v1.Controllers.PeakValley.PeakValleyRuleEditDetail) + peakValleyApi.GET("ruleDetail", v1.Controllers.PeakValley.PeakValleyRuleDetail) + peakValleyApi.GET("ruleEditDetail", v1.Controllers.PeakValley.PeakValleyRuleEditDetail) peakValleyApi.GET("quarterPage", v1.Controllers.PeakValley.PeakValleyQuarterPage) peakValleyApi.POST("createQuarter", v1.Controllers.PeakValley.CreatePeakValleyQuarter) peakValleyApi.PUT("updateQuarter", v1.Controllers.PeakValley.UpdatePeakValleyQuarter) - peakValleyApi.GET("quarterEditDetail/:id", v1.Controllers.PeakValley.PeakValleyQuarterEditDetail) + peakValleyApi.GET("quarterEditDetail", v1.Controllers.PeakValley.PeakValleyQuarterEditDetail) //userapi.DELETE(":id", api.Controllers.UserApi.DeleteUser) diff --git a/service/common.go b/service/common.go new file mode 100644 index 0000000..4311239 --- /dev/null +++ b/service/common.go @@ -0,0 +1,17 @@ +package service + +const ( + WATER = iota + 1 // WATER 水 + ELECTRICITY // ELECTRICITY 电 +) + +var EnergyTypeStrMap = map[int]string{ + WATER: "水", + ELECTRICITY: "电", +} + +type Common struct{} + +func (r *Common) GetEnergyTypeMap() map[int]string { + return EnergyTypeStrMap +} diff --git a/service/enter.go b/service/enter.go index c404c99..2d28ada 100644 --- a/service/enter.go +++ b/service/enter.go @@ -1,6 +1,7 @@ package service type groupService struct { + Common PeakValley } diff --git a/service/peak_valley.go b/service/peak_valley.go index e9d83c0..79c774a 100644 --- a/service/peak_valley.go +++ b/service/peak_valley.go @@ -112,8 +112,8 @@ func (r *PeakValley) CreatePeakValleyRule(req *form.CreatePeakValleyRuleReq) { var total, start, end, blockTotal = 0, 0, 0, 0 // 每个时间段的时间区块ID - var timeBlockIds []uint - var timeBlockIdsMap = make(map[string][]uint) + var timeBlockIds []int + var timeBlockIdsMap = make(map[string][]int) var timeGroup [][]int for _, item := range req.RuleItem { @@ -207,8 +207,8 @@ func (r *PeakValley) UpdatePeakValleyRule(req *form.UpdatePeakValleyRuleReq) { var total, start, end, blockTotal = 0, 0, 0, 0 // 每个时间段的时间区块ID - var timeBlockIds []uint - var timeBlockIdsMap = make(map[string][]uint) + var timeBlockIds []int + var timeBlockIdsMap = make(map[string][]int) var timeGroup [][]int for _, item := range req.RuleItem {