package data_stat

import (
	"encoding/json"
	"eta/eta_api/controllers"
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/services/data_stat"
	"eta/eta_api/utils"
)

// EdbTerminalController 数据源终端管理
type EdbTerminalController struct {
	controllers.BaseAuthController
}

// Save
// @Title 保存数据源终端
// @Description 保存数据源终端接口
// @Param	request  body  true data_manage.AddEdbTerminalListReq  "type json string"
// @Success 200 string "操作成功"
// @router /terminal/save [post]
func (this *EdbTerminalController) Save() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req *data_manage.AddEdbTerminalListReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.Name == "" {
		br.Msg = "请输入终端名称"
		return
	}
	/*if req.ServerUrl == "" && req.Value == "" {
		br.Msg = "请输入终端地址或者token"
		return
	}*/
	if req.Num <= 0 {
		br.Msg = "请输入指标数据量"
		return
	}
	if req.Source == 0 {
		br.Msg = "请输入终端类型"
		return
	}

	// todo 校验终端数据

	errMsg, err := data_stat.SaveEdbTerminal(req)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "SaveEnglishVideoCover ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// SetStatus
// @Title 启用/禁用数据源终端
// @Description 启用/禁用数据源终端
// @Param	request  body  true data_manage.SetEdbTerminalStatusReq  "type json string"
// @Success 200 string "操作成功"
// @router /terminal/status/set [post]
func (this *EdbTerminalController) SetStatus() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req *data_manage.SetEdbTerminalStatusReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	if req.TerminalId <= 0 {
		br.Msg = "请选择要操作的终端"
		return
	}

	if req.Status != 1 && req.Status != 2 {
		br.Msg = "请选择正确的终端状态"
		return
	}

	errMsg, err := data_stat.SetEdbTerminalStatus(req.TerminalId, req.Status)
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = "SetEdbTerminalStatus ErrMsg:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	return
}

// List
// @Title 数据源终端列表
// @Description 数据源终端列表接口
// @Success 200 {object} data_manage.EdbTerminalLisResp
// @router /terminal/list [get]
func (this *EdbTerminalController) List() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	list, err := data_manage.GetEdbTerminalList()
	if err != nil {
		br.Msg = "获取终端列表失败"
		br.ErrMsg = "获取终端列表失败 ErrMsg:" + err.Error()
		return
	}
	resp := &data_manage.EdbTerminalListResp{
		List: list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// TerminalCodeList
// @Title 数据源终端列表
// @Description 数据源终端列表接口
// @Success 200 {object} data_manage.EdbTerminalCodeResp
// @router /terminal/code [get]
func (this *EdbTerminalController) TerminalCodeList() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	source, _ := this.GetInt("Source", utils.DATA_SOURCE_MYSTEEL_CHEMICAL)
	list, err := data_manage.GetEdbTerminalBySource(source)
	if err != nil {
		br.Msg = "获取终端列表失败"
		br.ErrMsg = "获取终端列表失败 ErrMsg:" + err.Error()
		return
	}
	codeList := make([]*data_manage.EdbTerminalCode, 0)
	if len(list) > 0 {
		for _, v := range list {
			tmp := new(data_manage.EdbTerminalCode)
			tmp.TerminalCode = v.TerminalCode
			tmp.Source = v.Source
			tmp.Name = v.Name
			codeList = append(codeList, tmp)
		}
	}
	resp := &data_manage.EdbTerminalCodeResp{
		List: codeList,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
