package controllers import ( "fmt" "github.com/goccy/go-json" "rtzh_elec_temperature/rtelec_app_public_lib/models" "rtzh_elec_temperature/rtelec_app_public_lib/service" "strconv" "strings" ) type AlarmDeviceSettingController struct { BaseController } var alarmDeviceService = new(service.AlarmDeviceService) // 获取设备策略配置列表 // @Summary 获取设备策略配置列表 // @Description 获取设备策略配置列表。返回获取设备策略配置列表和策略明细信息 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param deviceid query int64 true "设备ID必传." // @Param pageIndex query int true "分页当前页." // @Param pageSize query int true "分页大小." // @Success 200 {object} ApiOK|ApiError 服务访问成功 // @Failure 401 status 认证未通过,一般是未指定token或token已失效 // @Failure 500 status 服务器|后台发生错误 // @router /list [get] func (c *AlarmDeviceSettingController) GetTacticsList() { deviceid, err := c.GetInt64("deviceid") if err != nil { c.Data["json"] = c.ApiError("设备id不能为空或者0!" + err.Error()) c.ServeJSON() } pageIndex, _ := c.GetInt("pageIndex", 1) pageSize, _ := c.GetInt("pageSize", 10) if err != nil { c.Data["json"] = c.ApiError("设备id不能为空或者0!" + err.Error()) c.ServeJSON() return } // 需要实现标准点和独立配置点的组装 // attrname,mpname,mpid, lst, err := alarmDeviceService.GetDeviceTactics(deviceid, pageIndex, pageSize) if err != nil { c.Data["json"] = c.ApiError("获取策略列表不正常!" + err.Error()) c.ServeJSON() } else { c.Data["json"] = c.ApiOK(lst) c.ServeJSON() } return } // 复制一个设备的策略到另外的设备 // @Summary 复制一个设备的策略到另外的设备 // @Description 复制一个设备的策略到另外的设备 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param olddeviceid query int64 true "旧设备ID必传." // @Param newdeviceid query string true "新设备ID必传.按逗号分割的字符串" // @Success 200 {object} ApiOK|ApiError 服务访问成功 // @Failure 401 status 认证未通过,一般是未指定token或token已失效 // @Failure 500 status 服务器|后台发生错误 // @router /copytactics [POST] func (c *AlarmDeviceSettingController) CopyDeviceTactics() { deviceid, err := c.GetInt64("olddeviceid") if err != nil { c.Data["json"] = c.ApiError("旧设备Id参数(deviceid)不允许为空或0!" + err.Error()) c.ServeJSON() return } newdeviceids := c.GetString("newdeviceid") newdeviceidList := strings.Split(newdeviceids, ",") if len(newdeviceidList) == 0 { c.Data["json"] = c.ApiError("新设备id至少选一个!") c.ServeJSON() return } for _, row := range newdeviceidList { i, err := strconv.ParseInt(row, 10, 64) if err != nil { c.Data["json"] = c.ApiError("新设备id不能为空或者0!" + err.Error()) c.ServeJSON() return } _, err = alarmDeviceService.CopyDeviceTactics(deviceid, i) if err != nil { c.Data["json"] = c.ApiError("设备策略信息复制出现错误:" + err.Error()) c.ServeJSON() return } } c.Data["json"] = c.ApiOK("ok") c.ServeJSON() return } // 添加独立策略点 // @Summary 添加独立策略点 // @Description 添加独立策略点。批量提交json格式的字符串[{},{},{}...],大部分字段值可由list获取,只有threshold需要手动填写 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param alonelist query string true "json字符串格式" // @Param alonelist.deviceid query int64 true "设备ID必传." // @Param alonelist.mpid query int64 true "测点ID必传." // @Param alonelist.attrname query int64 true "测点属性名必传." // @Param alonelist.mpname query string true "测点名称." // @Param alonelist.alarm_type query int true "告警类型(1:绝对值,2:升温)." // @Param alonelist.alarm_id query int64 true "告警策略id." // @Param alonelist.upcycle query int true "当alarm_type=2时需要传此值." // @Param alonelist.threshold query float32 true "阈值." // @router /saveAloneTactics [POST] func (c *AlarmDeviceSettingController) SaveAloneTactics() { // 把前端获取的json字符串转换为map reqAloneList := []map[string]interface{}{} jsonStr := c.GetString("alonelist") err := json.Unmarshal([]byte(jsonStr), &reqAloneList) if err != nil { if err != nil { c.Data["json"] = c.ApiError("json转换出错:" + err.Error()) c.ServeJSON() return } } for _, row := range reqAloneList { var obj models.T_base_alone_alarm // 组装新增对象 deviceid, err := strconv.ParseInt(fmt.Sprint(row["deviceid"]), 10, 64) if err != nil { if err != nil { c.Data["json"] = c.ApiError("设备id不能为空或者0!" + err.Error()) c.ServeJSON() return } } obj.Deviceid = deviceid obj.Attrname = fmt.Sprint(row["attrname"]) obj.Mpname = fmt.Sprint(row["mpname"]) mpid, err := strconv.ParseInt(fmt.Sprint(row["mpid"]), 10, 64) if err != nil { if err != nil { c.Data["json"] = c.ApiError("测点id不能为空或者0!" + err.Error()) c.ServeJSON() return } } obj.Mpid = mpid alarmType, err := strconv.ParseInt(fmt.Sprint(row["alarm_type"]), 10, 64) if err != nil { if err != nil { c.Data["json"] = c.ApiError("告警类型不能为空或者0!" + err.Error()) c.ServeJSON() return } } obj.Alarm_type = int(alarmType) alarmId, err := strconv.ParseInt(fmt.Sprint(row["alarm_id"]), 10, 64) if err != nil { if err != nil { c.Data["json"] = c.ApiError("告警id不能为空或者0!" + err.Error()) c.ServeJSON() return } } obj.Alarm_id = alarmId if alarmType == 2 { upcycle, err := strconv.ParseInt(fmt.Sprint(row["upcycle"]), 10, 64) if err != nil { if err != nil { c.Data["json"] = c.ApiError("上升周期不能为空或者0!" + err.Error()) c.ServeJSON() return } } obj.Upcycle = int(upcycle) } threshold, err := strconv.ParseFloat(fmt.Sprint(row["threshold"]), 64) if err != nil { if err != nil { c.Data["json"] = c.ApiError("阈值不能为空或者0!" + err.Error()) c.ServeJSON() return } } obj.Threshold = float32(threshold) // 清除现有的所有独立配置 err = alarmDeviceService.DelByDeviceMp(deviceid, mpid, alarmId) if err != nil { if err != nil { c.Data["json"] = c.ApiError(fmt.Sprintf("批量删除设备下的独立配置出错:%s", err.Error())) c.ServeJSON() return } } // 再添加新的配置 err = alarmDeviceService.InsertAloneDeviceTactics(obj) if err != nil { if err != nil { c.Data["json"] = c.ApiError(fmt.Sprintf("批量添加设备下的独立配置出错:%s", err.Error())) c.ServeJSON() return } } } c.Data["json"] = c.ApiOK("ok") c.ServeJSON() return } // 删除一个设备的独立策略点 // @Summary 删除一个设备的独立策略点 // @Description 删除一个设备的独立策略点,会把独立配置的所有的测点的阈值都清除 // @Tags api // @Accept x-www-form-urlencoded // @Produce json // @Param deviceid query int64 true "设备ID必传." // @router /delAloneTactics [POST] func (c *AlarmDeviceSettingController) DelAloneTactics() { deviceid, err := c.GetInt64("deviceid") if err != nil { if err != nil { c.Data["json"] = c.ApiError("设备id不能为空或者0!" + err.Error()) c.ServeJSON() return } } err = alarmDeviceService.DelAloneDeviceTactics(deviceid) if err != nil { if err != nil { c.Data["json"] = c.ApiError(fmt.Sprintf("批量删除设备下的独立配置出错:%s", err.Error())) c.ServeJSON() return } } c.Data["json"] = c.ApiOK("ok") c.ServeJSON() return }