package com.sundot.airport.attendance.service.impl;

import com.sundot.airport.attendance.domain.AttendanceArea;
import com.sundot.airport.attendance.mapper.AttendanceAreaMapper;
import com.sundot.airport.attendance.service.AttendanceAreaService;
import com.sundot.airport.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class AttendanceAreaServiceImpl implements AttendanceAreaService {
    @Autowired
    private AttendanceAreaMapper attendanceAreaMapper;

    /**
     * 查询是否在考勤范围
     *
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    @Override
    public List<AttendanceArea> checkUserInValidArea(BigDecimal lng, BigDecimal lat) {
        // 验证经纬度有效性
        if (lng == null || lat == null) {
            throw new ServiceException("经纬度不能为空");
        }

        // 检查经纬度范围（经度：-180到180，纬度：-90到90）
        if (lng.compareTo(new BigDecimal("-180")) < 0 ||
                lng.compareTo(new BigDecimal("180")) > 0) {
            throw new ServiceException("经度必须在-180到180之间");
        }

        if (lat.compareTo(new BigDecimal("-90")) < 0 ||
                lat.compareTo(new BigDecimal("90")) > 0) {
            throw new ServiceException("纬度必须在-90到90之间");
        }
        return attendanceAreaMapper.findValidAreasEfficiently(lng, lat);
    }

    /**
     * 查询所有的考勤范围点
     *
     * @return
     */
    @Override
    public List<AttendanceArea> areaList() {
        return attendanceAreaMapper.areaList();
    }
}
