package devices

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

type ScreensService struct{}

func (screensService *ScreensService) GetScreensInfoList(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.QueryScreensInfoList(limit, offset, keyword, shunt)

	return gatewayList, total, err
}

func (screensService *ScreensService) QueryScreensParms() (list interface{}, err error) {
	resolution := devices.Resolution{}
	getResolution, err := resolution.GetResolution()

	screensSize := devices.ScreensSize{}
	getScreensSize, err := screensSize.GetScreensSize()

	way := devices.GateWay{}
	gatewayList, err := way.PublicGateway()

	params := model.RspParams{Resolution: getResolution, ScreensSize: getScreensSize, PubGateway: gatewayList}

	return params, err
}

func (screensService *ScreensService) DelScreens(id int) error {
	s := devices.Screens{}
	err := s.DelScreens(id)
	return err
}

func (screensService *ScreensService) AddScreens(screens devices.Screens) error {
	err := screens.IsExistedByCode()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		logger.Logger.Errorf("Create IsExistedByCode \n")
		return errors.New("设备编码已存在！")
	}
	err = screens.AddScreens()
	return err
}

func (screensService *ScreensService) UpdateScreens(screens devices.Screens) error {
	err := screens.UpdateScreens()
	return err
}
