package devices

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

type CameraService struct{}

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

	userList, total, err := devices.QueryCameraInfoList(limit, offset, keyword, shunt)

	return userList, total, err
}

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

func (cameraService *CameraService) DelCamera(id int) error {
	c := devices.Camera{}
	err := c.DelCamera(id)
	return err
}

func (cameraService *CameraService) UpdateCamera(camera devices.Camera) error {
	err := camera.UpdateCamera()
	return err
}
