init
This commit is contained in:
176
api/v1/enter.go
Normal file
176
api/v1/enter.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package v1
|
||||
|
||||
type Controller struct {
|
||||
TestApi
|
||||
PeakValley // 谷峰规则
|
||||
//UserApi // 用户管理
|
||||
//DictApi // 字典管理
|
||||
//ProductApi // 产品管理
|
||||
//OTAApi // ota管理
|
||||
//UpLoadApi // 上传
|
||||
//ProtocolPluginApi // 协议插件
|
||||
//DeviceApi // 设备
|
||||
//DeviceModelApi // 设备物模型
|
||||
//UiElementsApi // UI元素控制
|
||||
//BoardApi // 首页
|
||||
//TelemetryDataApi // 遥测数据
|
||||
//AttributeDataApi // 属性数据
|
||||
//EventDataApi // 事件数据
|
||||
//CommandSetLogApi // 命令下发记录
|
||||
//OperationLogsApi // 系统日志
|
||||
//LogoApi // 站标
|
||||
//DataPolicyApi // 数据清理
|
||||
//DeviceConfigApi // 设备配置
|
||||
//DataScriptApi // 数据处理脚本
|
||||
//RoleApi // 用户管理
|
||||
//CasbinApi // 权限管理
|
||||
//NotificationGroupApi // 通知组
|
||||
//NotificationHistoryApi // 通知历史
|
||||
//NotificationServicesConfigApi // 通知服务配置
|
||||
//AlarmApi // 告警
|
||||
//SceneAutomationsApi //场景联动
|
||||
//SceneApi //场景
|
||||
//SystemApi //系统相关
|
||||
//SysFunctionApi //功能设置
|
||||
//VisPluginApi //可视化插件
|
||||
//ServicePluginApi //插件管理
|
||||
//ServiceAccessApi //服务接入管理
|
||||
}
|
||||
|
||||
var Controllers = new(Controller)
|
||||
|
||||
//var Validate *validator.Validate
|
||||
|
||||
func init() {
|
||||
//Validate = validator.New()
|
||||
}
|
||||
|
||||
// ValidateStruct validates the request structure
|
||||
//func ValidateStruct(i interface{}) error {
|
||||
// err := Validate.Struct(i)
|
||||
// if err != nil {
|
||||
// if _, ok := err.(*validator.InvalidValidationError); ok {
|
||||
// return err
|
||||
// }
|
||||
//
|
||||
// var errors []string
|
||||
// for _, err := range err.(validator.ValidationErrors) {
|
||||
// var error string
|
||||
// switch err.Tag() {
|
||||
// case "required":
|
||||
// error = fmt.Sprintf("Field '%s' is required", err.Field())
|
||||
// case "email":
|
||||
// error = fmt.Sprintf("Field '%s' must be a valid email address", err.Field())
|
||||
// case "gte":
|
||||
// error = fmt.Sprintf("The value of field '%s' must be at least %s", err.Field(), err.Param())
|
||||
// case "lte":
|
||||
// error = fmt.Sprintf("The value of field '%s' must be at most %s", err.Field(), err.Param())
|
||||
// default:
|
||||
// error = fmt.Sprintf("Field '%s' failed validation (%s)", err.Field(), validationErrorToText(err))
|
||||
// }
|
||||
// errors = append(errors, error)
|
||||
// }
|
||||
//
|
||||
// return fmt.Errorf("%s", errors[0])
|
||||
// }
|
||||
// return nil
|
||||
//}
|
||||
|
||||
// validationErrorToText converts validation errors to more descriptive text
|
||||
//func validationErrorToText(e validator.FieldError) string {
|
||||
// switch e.Tag() {
|
||||
// case "min":
|
||||
// return fmt.Sprintf("At least %s characters", e.Param())
|
||||
// case "max":
|
||||
// return fmt.Sprintf("At most %s characters", e.Param())
|
||||
// case "len":
|
||||
// return fmt.Sprintf("Must be %s characters", e.Param())
|
||||
// // Add more cases as needed
|
||||
// default:
|
||||
// return "Does not meet validation rules"
|
||||
// }
|
||||
//}
|
||||
|
||||
// 定义统一的HTTP响应结构
|
||||
//type ApiResponse struct {
|
||||
// Code int `json:"code"`
|
||||
// Message string `json:"message"`
|
||||
// Data interface{} `json:"data,omitempty"`
|
||||
//}
|
||||
|
||||
// ErrorHandler 统一错误处理
|
||||
//func ErrorHandler(c *gin.Context, code int, err error) {
|
||||
// if strings.Contains(err.Error(), "SQLSTATE 23503") {
|
||||
// // 处理外键约束违反
|
||||
// err = fmt.Errorf("操作无法完成:请先删除与此项相关联的数据后再进行尝试")
|
||||
// }
|
||||
// if strings.Contains(err.Error(), "SQLSTATE 23505") {
|
||||
// // 处理唯一键约束违反
|
||||
// err = fmt.Errorf("操作无法完成:已存在相同的数据")
|
||||
// }
|
||||
// // fmt.Printf("%T\n", err)
|
||||
// // // 检查这个错误是否是 *pgconn.PgError
|
||||
// // var pgErr *pgconn.PgError
|
||||
// // if errors.As(err, &pgErr) {
|
||||
// // logrus.Error("-----------------")
|
||||
// // // 现在 pgErr 是 err 中的 *pgconn.PgError 部分(如果存在)
|
||||
// // if pgErr.SQLState() == "23503" {
|
||||
// // // 这就是一个外键约束违反错误
|
||||
// // err = fmt.Errorf("外键约束违反: %w", err)
|
||||
// // }
|
||||
// // }
|
||||
// logrus.Error(err)
|
||||
// c.JSON(http.StatusOK, ApiResponse{
|
||||
// Code: code,
|
||||
// Message: err.Error(),
|
||||
// })
|
||||
//}
|
||||
//
|
||||
//// SuccessHandler 统一成功响应
|
||||
//func SuccessHandler(c *gin.Context, message string, data interface{}) {
|
||||
// c.JSON(http.StatusOK, ApiResponse{
|
||||
// Code: http.StatusOK,
|
||||
// Message: message,
|
||||
// Data: data,
|
||||
// })
|
||||
//}
|
||||
//
|
||||
//// SuccessOK 统一成功响应
|
||||
//func SuccessOK(c *gin.Context) {
|
||||
// c.JSON(http.StatusOK, ApiResponse{
|
||||
// Code: http.StatusOK,
|
||||
// Message: "Success",
|
||||
// })
|
||||
//}
|
||||
//
|
||||
//func BindAndValidate(c *gin.Context, obj interface{}) bool {
|
||||
// // 判断请求方法
|
||||
// if c.Request.Method == http.MethodGet {
|
||||
// if err := c.ShouldBindQuery(obj); err != nil {
|
||||
// ErrorHandler(c, http.StatusBadRequest, err)
|
||||
// return false
|
||||
// }
|
||||
// } else if c.Request.Method == http.MethodPost || c.Request.Method == http.MethodPut || c.Request.Method == http.MethodDelete {
|
||||
// if err := c.ShouldBindJSON(obj); err != nil {
|
||||
// ErrorHandler(c, http.StatusBadRequest, err)
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// if err := ValidateStruct(obj); err != nil {
|
||||
// // 如果是验证错误,返回422 Unprocessable Entity
|
||||
// ErrorHandler(c, http.StatusUnprocessableEntity, err)
|
||||
// return false
|
||||
// }
|
||||
//
|
||||
// return true
|
||||
//}
|
||||
//
|
||||
//var Wsupgrader = websocket.Upgrader{
|
||||
// ReadBufferSize: 1024,
|
||||
// WriteBufferSize: 1024,
|
||||
// CheckOrigin: func(r *http.Request) bool {
|
||||
// // 不做跨域检查
|
||||
// return true
|
||||
// },
|
||||
//}
|
||||
62
api/v1/peak_valley.go
Normal file
62
api/v1/peak_valley.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"energy-management-system/form"
|
||||
"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{}
|
||||
|
||||
// GetPeakValleyTypes 获取峰谷类型
|
||||
func (r *PeakValley) GetPeakValleyTypes(c *gin.Context) {
|
||||
types := service.GroupServices.PeakValley.GetPeakValleyType()
|
||||
response.SuccessData(gin.H{"types": types}, c)
|
||||
}
|
||||
|
||||
// PeakValleyRuleList 谷峰规则列表
|
||||
func (r *PeakValley) PeakValleyRuleList(c *gin.Context) {
|
||||
var req form.PeakValleyRuleListReq
|
||||
request.BindParam(c, &req)
|
||||
respData := service.GroupServices.PeakValley.PeakValleyRuleList(&req)
|
||||
response.SuccessData(respData, c)
|
||||
}
|
||||
|
||||
// CreatePeakValleyRule 创建谷峰规则
|
||||
func (r *PeakValley) CreatePeakValleyRule(c *gin.Context) {
|
||||
var req form.CreatePeakValleyRuleReq
|
||||
request.BindJson(c, &req)
|
||||
service.GroupServices.PeakValley.CreatePeakValleyRule(&req)
|
||||
response.Success(c)
|
||||
}
|
||||
|
||||
// UpdatePeakValleyRule 修改谷峰规则
|
||||
func (r *PeakValley) UpdatePeakValleyRule(c *gin.Context) {
|
||||
var req form.UpdatePeakValleyRuleReq
|
||||
request.BindJson(c, &req)
|
||||
service.GroupServices.PeakValley.UpdatePeakValleyRule(&req)
|
||||
//response.Success(c)
|
||||
}
|
||||
|
||||
// PeakValleyRuleDetail 谷峰规则详情
|
||||
func (r *PeakValley) PeakValleyRuleDetail(c *gin.Context) {
|
||||
var req form.PeakValleyRuleDetailReq
|
||||
id, err := strconv.Atoi(c.Param("id"))
|
||||
exception.PanicMsgBool(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.PanicMsgBool(err != nil, "参数有误")
|
||||
req.RuleId = id
|
||||
respData := service.GroupServices.PeakValley.PeakValleyRuleEditDetail(&req)
|
||||
response.SuccessData(respData, c)
|
||||
}
|
||||
33
api/v1/test.go
Normal file
33
api/v1/test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"energy-management-system/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type TestApi struct {
|
||||
}
|
||||
|
||||
func (r *TestApi) Test(c *gin.Context) {
|
||||
response.Success(c)
|
||||
}
|
||||
|
||||
func (r *TestApi) TestData(c *gin.Context) {
|
||||
response.SuccessData(gin.H{"userName": "iuu"}, c)
|
||||
}
|
||||
|
||||
//func (a *AlarmApi) CreateAlarmConfig(c *gin.Context) {
|
||||
// var req model.CreateAlarmConfigReq
|
||||
// if !BindAndValidate(c, &req) {
|
||||
// return
|
||||
// }
|
||||
// var userClaims = c.MustGet("claims").(*utils.UserClaims)
|
||||
// req.TenantID = userClaims.TenantID
|
||||
// data, err := service.GroupApp.Alarm.CreateAlarmConfig(&req)
|
||||
// if err != nil {
|
||||
// ErrorHandler(c, http.StatusInternalServerError, err)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// SuccessHandler(c, "Create successfully", data)
|
||||
//}
|
||||
Reference in New Issue
Block a user