package service

import (
	"fmt"

	"nms_nbi/src/modules/network_element/model"
	"nms_nbi/src/modules/network_element/repository"
)

// 实例化服务层 NeHostCmdImpl 结构体
var NewNeHostCmdImpl = &NeHostCmdImpl{
	neHostCmdRepository: repository.NewNeHostCmdImpl,
}

// NeHostCmdImpl 网元主机命令 服务层处理
type NeHostCmdImpl struct {
	// 网元主机命令表
	neHostCmdRepository repository.INeHostCmd
}

// SelectNeHostPage 分页查询列表数据
func (r *NeHostCmdImpl) SelectPage(query map[string]any) map[string]any {
	return r.neHostCmdRepository.SelectPage(query)
}

// SelectConfigList 查询列表
func (r *NeHostCmdImpl) SelectList(neHostCmd model.NeHostCmd) []model.NeHostCmd {
	return r.neHostCmdRepository.SelectList(neHostCmd)
}

// SelectByIds 通过ID查询
func (r *NeHostCmdImpl) SelectById(cmdId string) model.NeHostCmd {
	if cmdId == "" {
		return model.NeHostCmd{}
	}
	neHosts := r.neHostCmdRepository.SelectByIds([]string{cmdId})
	if len(neHosts) > 0 {
		return neHosts[0]
	}
	return model.NeHostCmd{}
}

// Insert 新增信息
func (r *NeHostCmdImpl) Insert(neHostCmd model.NeHostCmd) string {
	return r.neHostCmdRepository.Insert(neHostCmd)
}

// Update 修改信息
func (r *NeHostCmdImpl) Update(neHostCmd model.NeHostCmd) int64 {
	return r.neHostCmdRepository.Update(neHostCmd)
}

// DeleteByIds 批量删除信息
func (r *NeHostCmdImpl) DeleteByIds(cmdIds []string) (int64, error) {
	// 检查是否存在
	ids := r.neHostCmdRepository.SelectByIds(cmdIds)
	if len(ids) <= 0 {
		return 0, fmt.Errorf("neHostCmd.noData")
	}

	if len(ids) == len(cmdIds) {
		rows := r.neHostCmdRepository.DeleteByIds(cmdIds)
		return rows, nil
	}
	// 删除信息失败！
	return 0, fmt.Errorf("delete fail")
}

// CheckUniqueGroupTitle 校验同类型组内是否唯一
func (r *NeHostCmdImpl) CheckUniqueGroupTitle(groupId, title, cmdType, cmdId string) bool {
	uniqueId := r.neHostCmdRepository.CheckUniqueGroupTitle(model.NeHostCmd{
		CmdType: cmdType,
		GroupID: groupId,
		Title:   title,
	})
	if uniqueId == cmdId {
		return true
	}
	return uniqueId == ""
}
