package vehicle

import (
	"wails-app/internal/dao"
	"wails-app/internal/global"
	"wails-app/internal/model/vehicle/request"
)

type ShortlistService struct{}

func (shortlistService *ShortlistService) QueryShortlistList(info request.ShortlistRequest) (list interface{}, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	shortlistList, t, err := dao.QueryShortlistList(limit, offset, info.ListType, info.PlateNumber, info.RFIDTag)
	if err != nil {
		return nil, 0, err
	}
	return shortlistList, t, nil
}

func (shortlistService *ShortlistService) QueryAllShortlists() (shortlists []dao.Shortlist, err error) {
	return dao.QueryAllShortlists()
}

func (shortlistService *ShortlistService) QueryShortlistByRFIDTag(rfidTag string) (shortlist dao.Shortlist, err error) {
	return dao.QueryShortlistByRFIDTag(rfidTag)
}

func (s *ShortlistService) CheckVehicleShortlist(plateNumber, rfidTag string) (isBlack bool, isWhite bool) {
	var vehicle dao.Vehicle
	query := global.GVA_DB
	if rfidTag != "" {
		query = query.Where("rfid_tag = ?", rfidTag)
	} else {
		query = query.Where("plate_number = ?", plateNumber)
	}
	if err := query.First(&vehicle).Error; err != nil {
		return false, false
	}

	var shortlist dao.Shortlist
	if err := global.GVA_DB.Where("vehicle_id = ?", vehicle.ID).First(&shortlist).Error; err != nil {
		return false, false
	}

	isBlack = shortlist.ListType == "黑名单"
	isWhite = shortlist.ListType == "白名单"
	return
}

func (shortlistService *ShortlistService) CreateShortlist(shortlist dao.Shortlist) error {
	return dao.CreateShortlist(shortlist)
}

func (shortlistService *ShortlistService) UpdateShortlist(shortlist dao.Shortlist) error {
	return dao.UpdateShortlist(shortlist)
}

func (shortlistService *ShortlistService) DeleteShortlist(id int) error {
	return dao.DeleteShortlist(id)
}
