package devices

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

type IntersectService struct{}

func (intersectService *IntersectService) PublicIntersect() (interface{}, error) {
	intersect := devices.Intersect{}
	list, err := intersect.PublicIntersect()
	return list, err
}

func (intersectService *IntersectService) AddIntersect(intersect devices.Intersect) error {
	err := intersect.IsExistedByName()
	if !errors.Is(err, gorm.ErrRecordNotFound) {
		logger.Logger.Errorf("Create IsExistedByName \n")
		return errors.New("路口名称已存在！")
	}

	err = intersect.AddIntersect()
	return err
}

func (intersectService *IntersectService) UpdateIntersect(intersect devices.Intersect) error {
	err := intersect.UpdateIntersect()
	return err
}

func (intersectService *IntersectService) DelIntersect(id int) error {
	intersect := devices.Intersect{}
	err := intersect.DelIntersect(id)
	return err
}

func (intersectService *IntersectService) GetIntersectInfoList(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.QueryIntersectInfoList(limit, offset, keyword, shunt)

	return userList, total, err
}
