package devices

import (
	"errors"
	"gorm.io/gorm"
	"server/dao/devices"
	"server/model/common/request"
	"server/utils/logger"
)

type RadarService struct{}

func (radarService *RadarService) GetRadarInfoList(info request.PageInfo) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	keyword := info.Keyword
	shunt := info.Shunt

	gatewayList, total, err := devices.QueryRadarInfoList(limit, offset, keyword, shunt)

	return gatewayList, total, err
}

func (radarService *RadarService) UpdateRadar(radar devices.Radar) error {
	err := radar.UpdateRadar()
	return err
}

func (radarService *RadarService) AddRadar(radar devices.Radar) error {
	err := radar.IsExistedByCode()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		logger.Logger.Errorf("Create IsExistedByCode \n")
		return errors.New("设备编码已存在！")
	}
	err = radar.AddRadar()
	return err
}

func (radarService *RadarService) DelRadar(id int) error {
	r := devices.Radar{}
	err := r.DelRadar(id)
	return err
}
