package com.boman.web.core.service.attendance.set;

import com.boman.common.core.utils.DateUtils;
import com.boman.common.core.utils.LatLgtUtil;
import com.boman.domain.dto.AjaxResult;
import com.boman.web.core.domain.AttendanceSet;
import com.boman.web.core.mapper.AttendanceSetMapper;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 考勤设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-04
 */
@Service
public class AttendanceSetServiceImpl implements IAttendanceSetService
{
    @Autowired
    private AttendanceSetMapper attendanceSetMapper;

    /**
     * 查询考勤设置
     * 
     * @param id 考勤设置ID
     * @return 考勤设置
     */
    @Override
    public AttendanceSet selectAttendanceSetById(Long id)
    {
        return attendanceSetMapper.selectAttendanceSetById(id);
    }

    /**
     * 查询考勤设置列表
     * 
     * @param attendanceSet 考勤设置
     * @return 考勤设置
     */
    @Override
    public List<AttendanceSet> selectAttendanceSetList(AttendanceSet attendanceSet)
    {
        return attendanceSetMapper.selectAttendanceSetList(attendanceSet);
    }

    /**
     * 新增考勤设置
     * 
     * @param attendanceSet 考勤设置
     * @return 结果
     */
    @Override
    public int insertAttendanceSet(AttendanceSet attendanceSet)
    {
        attendanceSet.setCreateTime(DateUtils.getNowDate());
        return attendanceSetMapper.insertAttendanceSet(attendanceSet);
    }

    /**
     * 修改考勤设置
     * 
     * @param attendanceSet 考勤设置
     * @return 结果
     */
    @Override
    public int updateAttendanceSet(AttendanceSet attendanceSet)
    {
        attendanceSet.setUpdateTime(DateUtils.getNowDate());
        return attendanceSetMapper.updateAttendanceSet(attendanceSet);
    }

    /**
     * 批量删除考勤设置
     * 
     * @param ids 需要删除的考勤设置ID
     * @return 结果
     */
    @Override
    public int deleteAttendanceSetByIds(Long[] ids)
    {
        return attendanceSetMapper.deleteAttendanceSetByIds(ids);
    }

    /**
     * 删除考勤设置信息
     * 
     * @param id 考勤设置ID
     * @return 结果
     */
    @Override
    public int deleteAttendanceSetById(Long id)
    {
        return attendanceSetMapper.deleteAttendanceSetById(id);
    }

    /**
     * 根据传入的经纬度计算是否在设置的范围之内
     * @param globalCoordinates
     * @return
     */
    @Override
    public AjaxResult getDistanceMeter(GlobalCoordinates globalCoordinates) {
        AttendanceSet attendanceSet = new AttendanceSet();
        List<AttendanceSet> attendanceSets = selectAttendanceSetList(attendanceSet);
        if (attendanceSets.size() == 0){
            return AjaxResult.error("当前没有设置考勤范围");
        }
        AttendanceSet attendanceSet1 = attendanceSets.get(0);
        //经度
        String lgt = attendanceSet1.getLgt();
        //纬度
        String lat = attendanceSet1.getLat();

        GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(lat),Double.parseDouble(lgt));
        double distanceMeter = LatLgtUtil.getDistanceMeter(globalCoordinates, target, Ellipsoid.Sphere);
        double range = Double.parseDouble(attendanceSet1.getLimitRange());
        if (range > distanceMeter){
            //在范围内
            return AjaxResult.success();
        }

        return AjaxResult.error("不在范围内");
    }
}
