package controller

import (
	"fmt"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	"be.ems/src/framework/utils/parse"
	"be.ems/src/modules/ne/model"
	neService "be.ems/src/modules/ne/service"

	"github.com/gin-gonic/gin"
)

// 实例化控制层 NeHostCmdController 结构体
var NewNeHostCmd = &NeHostCmdController{
	neHostCmdService: neService.NewNeHostCmd,
}

// 网元主机命令请求
//
// PATH /hostCmd
type NeHostCmdController struct {
	neHostCmdService *neService.NeHostCmd // 网元主机命令服务
}

// 网元主机命令列表
//
// GET /list
func (s NeHostCmdController) List(c *gin.Context) {
	query := reqctx.QueryMap(c)
	rows, total := s.neHostCmdService.FindByPage(query)
	c.JSON(200, resp.OkData(map[string]any{"total": total, "rows": rows}))
}

// 网元主机命令信息
//
// GET /:id
func (s NeHostCmdController) Info(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	id := parse.Number(c.Param("id"))
	if id <= 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
		return
	}

	neHost := s.neHostCmdService.FindById(id)
	if neHost.ID != id {
		// 没有可访问主机命令数据！
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
		return
	}

	c.JSON(200, resp.OkData(neHost))
}

// 网元主机命令新增
//
// POST /
func (s NeHostCmdController) Add(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHostCmd
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查属性值唯一
	uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, 0)
	if !uniqueHostCmd {
		// 主机命令操作【%s】失败，同组内名称已存在
		msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	body.CreateBy = reqctx.LoginUserToUserName(c)
	insertId := s.neHostCmdService.Insert(body)
	if insertId > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 网元主机命令修改
//
// PUT /
func (s NeHostCmdController) Edit(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.NeHostCmd
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查属性值唯一
	uniqueHostCmd := s.neHostCmdService.CheckUniqueGroupTitle(body.GroupID, body.Title, body.CmdType, body.ID)
	if !uniqueHostCmd {
		// 主机命令操作【%s】失败，同组内名称已存在
		msg := i18n.TTemplate(language, "neHostCmd.errKeyExists", map[string]any{"name": body.Title})
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	// 检查是否存在
	neHost := s.neHostCmdService.FindById(body.ID)
	if neHost.ID != body.ID {
		// 没有可访问主机命令数据！
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neHostCmd.noData")))
		return
	}

	body.UpdateBy = reqctx.LoginUserToUserName(c)
	rows := s.neHostCmdService.Update(body)
	if rows > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 网元主机命令删除
//
// DELETE /:id
func (s NeHostCmdController) Remove(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	id := c.Param("id")
	if id == "" {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
		return
	}

	// 处理字符转id数组后去重
	uniqueIDs := parse.RemoveDuplicatesToArray(id, ",")
	// 转换成int64数组类型
	ids := make([]int64, 0)
	for _, v := range uniqueIDs {
		ids = append(ids, parse.Number(v))
	}

	rows, err := s.neHostCmdService.DeleteByIds(ids)
	if err != nil {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
	c.JSON(200, resp.OkMsg(msg))
}
