package controllers

import (
	"fmt"

	"github.com/astaxie/beego"

	"lc/common/models"
)

type AlarmStrategyController struct {
	BaseController
}

type ReqAlarmStrategy struct {
	ID       int64   `json:"id"`       //告警策略编号
	Name     string  `json:"name"`     //策略名称
	UpLimit  float32 `json:"limit"`    //上限
	LowLimit float32 `json:"lowlimit"` //下限
	Duration uint8   `json:"duration"` //持续时间
	State    int     `json:"state"`    //1启用，0禁用
}

type AlarmAssociate struct {
	Code  string `json:"code"`  //设备编码
	Mid   int    `json:"mid"`   //物模型ID
	Sid   int    `json:"sid"`   //物模型数据id
	Cid   int    `json:"cid"`   //告警策略id
	State int    `json:"state"` //1启用，0禁用
}

// Create @Title 创建告警策略
// @Description 创建告警策略
// @Param   body  controllers.ReqAlarmStrategy true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/create [post]
func (o *AlarmStrategyController) Create() {
	var obj ReqAlarmStrategy
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
		beego.Debug(string(o.Ctx.Input.RequestBody))
		o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
		return
	}
	oo := models.AlarmStrategy{
		ID:       obj.ID,
		Name:     obj.Name,
		UpLimit:  obj.UpLimit,
		LowLimit: obj.LowLimit,
		Duration: obj.Duration,
		State:    obj.State,
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// Update @Title 更新告警策略
// @Description 更新告警策略
// @Param   body body controllers.ReqAlarmStrategy true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/update [post]
func (o *AlarmStrategyController) Update() {
	var obj ReqAlarmStrategy
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &obj); err != nil {
		beego.Debug(string(o.Ctx.Input.RequestBody))
		o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
		return
	}
	oo := models.AlarmStrategy{
		ID:       obj.ID,
		Name:     obj.Name,
		UpLimit:  obj.UpLimit,
		LowLimit: obj.LowLimit,
		Duration: obj.Duration,
		State:    obj.State,
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据更新失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// Delete @Title 删除告警策略
// @Description 删除告警策略
// @Param   code    query   string     true "设备ID"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/delete [post]
func (o *AlarmStrategyController) Delete() {
	ID, err := o.GetInt64("id", -1)
	if err != nil {
		o.Response(Failure, fmt.Sprintf("解析id错误:%s", err.Error()), nil)
		return
	}
	c := models.AlarmStrategy{ID: ID}
	if err := c.Delete(); err != nil {
		beego.Error(fmt.Sprintf("删除失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("数据删除失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", c.ID)
}

// Associate @Title 设置告警策略
// @Description 设置告警策略
// @Param   body body []controllers.AlarmAssociate true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/associate [post]
func (o *AlarmStrategyController) Associate() {
	var arr []AlarmAssociate
	if err := json.Unmarshal(o.Ctx.Input.RequestBody, &arr); err != nil {
		beego.Debug(string(o.Ctx.Input.RequestBody))
		o.Response(Failure, fmt.Sprintf("数据包解析错误:%s", err.Error()), nil)
		return
	}
	for _, v := range arr {
		oo := models.AlarmAssociate{
			ID:    v.Code,
			Mid:   v.Mid,
			Sid:   v.Sid,
			Cid:   v.Cid,
			State: v.State,
		}
		if err := oo.SaveFromWeb(); err != nil {
			o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
			return
		}
	}
	o.Response(Success, "成功", nil)
}
