package controllers

import (
	"fmt"

	"github.com/astaxie/beego"
	validation "github.com/go-ozzo/ozzo-validation/v4"
	"github.com/go-ozzo/ozzo-validation/v4/is"

	"lc/common/models"
)

type ItsController struct {
	BaseController
}

type ReqIts struct {
	Tenant       string `json:"tenant"`       //租户ID
	Code         string `json:"code"`         //设备编号，禁止修改
	GID          string `json:"gid"`          //网关ID
	Name         string `json:"name"`         //设备名称
	Brand        string `json:"brand"`        //品牌
	Model        string `json:"model"`        //型号
	IP           string `json:"ip"`           //IP地址
	Port         int    `json:"port"`         //端口
	SuggestSpeed int    `json:"suggestspeed"` //建议速度，提示限速
	State        int    `json:"state"`        //1启用，0禁用
}

func (a ReqIts) Validate() error {
	return validation.ValidateStruct(&a,
		validation.Field(&a.Code, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.GID, validation.Required, validation.Length(4, 32)),
		validation.Field(&a.Tenant, validation.Required, validation.Length(4, 8)),
		validation.Field(&a.IP, validation.Required, is.IP),
		validation.Field(&a.Port, validation.Required, validation.Min(80), validation.Max(65535)),
		validation.Field(&a.SuggestSpeed, validation.Required, validation.Min(15), validation.Max(120)),
		validation.Field(&a.State, validation.In(0, 1)),
	)
}

// CreateIts @Title 创建抓拍单元
// @Description 创建抓拍单元
// @Param   body controllers.ReqIts true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/create [post]
func (o *ItsController) CreateIts() {
	var obj ReqIts
	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
	}
	if err := obj.Validate(); err != nil {
		beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
		return
	}
	oo := models.ItsDevice{
		ID:           obj.Code,         //设备ID，DID
		Name:         obj.Name,         //集控器名称
		GID:          obj.GID,          //网关ID
		Brand:        obj.Brand,        //品牌
		Model:        obj.Model,        //型号
		DevType:      5,                //5抓拍单元
		IP:           obj.IP,           //抓拍单元IP
		Port:         uint16(obj.Port), //抓拍单元端口
		Tenant:       obj.Tenant,       //租户ID
		SuggestSpeed: obj.SuggestSpeed, //建议速度阈值
		State:        obj.State,        //1启用，0禁用
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据插入失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// UpdateIts @Title 更新抓拍单元
// @Description 更新抓拍单元
// @Param   body controllers.ReqConcentrator true "数据"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/update [post]
func (o *ItsController) UpdateIts() {
	var obj ReqIts
	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
	}
	if err := obj.Validate(); err != nil {
		beego.Error(fmt.Sprintf("请求参数验证失败:%s", err.Error()))
		o.Response(Failure, fmt.Sprintf("请求参数验证失败:%s", err.Error()), nil)
		return
	}
	oo := models.ItsDevice{
		ID:           obj.Code,         //设备ID，DID
		Name:         obj.Name,         //集控器名称
		GID:          obj.GID,          //网关ID
		Brand:        obj.Brand,        //品牌
		Model:        obj.Model,        //型号
		DevType:      5,                //5，抓拍单元
		IP:           obj.IP,           //抓拍单元IP
		Port:         uint16(obj.Port), //抓拍单元端口
		Tenant:       obj.Tenant,       //租户ID
		SuggestSpeed: obj.SuggestSpeed, //建议速度阈值
		State:        obj.State,        //1启用，0禁用
	}
	if err := oo.SaveFromWeb(); err != nil {
		o.Response(Failure, fmt.Sprintf("数据更新失败:%s", err.Error()), nil)
		return
	}
	o.Response(Success, "成功", oo.ID)
}

// DeleteIts @Title 删除抓拍单元
// @Description 删除抓拍单元
// @Param   id    query   int     true "设备ID"
// @Success 0 {int} BaseResponse.Code "成功"
// @Failure 1 {int} BaseResponse.Code "失败"
// @router /v1/delete [post]
func (o *ItsController) DeleteIts() {
	id := o.GetString("code")
	if id == "" {
		beego.Error("缺查询参数id")
		o.Response(Failure, "缺查询参数id", nil)
		return
	}
	c := models.ItsDevice{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)
}
