package com.sundot.airport.web.controller.attendance;

import cn.hutool.core.collection.CollectionUtil;
import com.sundot.airport.attendance.domain.AttendanceArea;
import com.sundot.airport.attendance.service.AttendanceAreaService;
import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/attendance/area")
public class AttendanceAreaController extends BaseController {

    @Autowired
    private AttendanceAreaService attendanceService;

    /**
     * 查询是否在考勤范围
     *
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    @PostMapping("/check-in")
    public AjaxResult checkUserInValidArea(@RequestParam BigDecimal lng,
                                           @RequestParam BigDecimal lat) {
        List<AttendanceArea> validAreas = attendanceService.checkUserInValidArea(lng, lat);
        if (CollectionUtil.isEmpty(validAreas)) {
            return AjaxResult.error("不在任何考勤范围内");
        }
        return success(validAreas);
    }

    /**
     * 考勤打卡列表
     *
     * @return
     */
    @PostMapping("/list")
    public AjaxResult areaList() {
        List<AttendanceArea> validAreas = attendanceService.areaList();
        if (CollectionUtil.isEmpty(validAreas)) {
            return AjaxResult.error("没有考勤范围");
        }
        return success(validAreas);
    }
}
