package com.ruoyi.web.controller.kaoqin;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.KaoqinConfig;
import com.ruoyi.system.domain.KaoqinRecord;
import com.ruoyi.system.domain.PlatPunch;
import com.ruoyi.system.service.IKaoqinConfigService;
import com.ruoyi.system.service.IKaoqinRecordService;
import com.ruoyi.system.service.ISysDictDataService;
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GlobalCoordinates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ruoyi.common.PunchUtils.getDistanceMeter;

/**
 * 实现考勤打卡
 *
 * @Author: tjf
 * @Date: 2023/2/1 14:46
 * @Describe:
 */
@RestController
@RequestMapping("/kaoQin")
public class KaoQinController extends BaseController {

    @Autowired
    private IKaoqinConfigService kaoqinConfigService;

    @Autowired
    private IKaoqinRecordService kaoqinRecordService;


    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 定位打卡提供计算参数
     *
     * @param platPunch
     * @return
     */
    @GetMapping(value = "/daKa")
    public AjaxResult daKa(PlatPunch platPunch) {
        //根据部门id,查询该部门考勤范围 ,经纬度
        KaoqinConfig kaoqinConfig = new KaoqinConfig();
        kaoqinConfig.setDeptId(platPunch.getDeptId());
        List<KaoqinConfig> kaoqinConfigs = kaoqinConfigService.selectKaoqinConfigList(kaoqinConfig);
        return AjaxResult.success(kaoqinConfigs);
    }


    /**
     * 查询考勤记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(KaoqinRecord kaoqinRecord) {
        List<KaoqinRecord> list = kaoqinRecordService.selectKaoqinRecordList(kaoqinRecord);
        return getDataTable(list);
    }

    /**
     * 查询考勤异常数据信息
     * 包括所有异常的数量
     * 本月考勤记录的异常分类数量
     */
    @GetMapping("/abnormal")
    public AjaxResult abnormal(KaoqinRecord kaoqinRecord) {
        //设置查询异常的考勤
        kaoqinRecord.setKaStatus("2");
        List<KaoqinRecord> kaoqinRecords = kaoqinRecordService.selectKaoqinRecordList(kaoqinRecord);
        //对异常进行分组统计数量
        Map<String, Long> collect = kaoqinRecords.stream().collect(Collectors.groupingBy(KaoqinRecord::getKaType, Collectors.counting()));
        SysDictData dictData = new SysDictData();
        dictData.setDictType("kaoqin_abnormal");
        List<SysDictData> sysDictData = dictDataService.selectDictDataList(dictData);
        Map map = new HashMap();
        for (SysDictData sysDictDatum : sysDictData) {
            //获取异常名称
             String dictLabel = sysDictDatum.getDictLabel();
             //获取异常的值
            String dictValue = sysDictDatum.getDictValue();
            if (!"1".equals(dictValue)){
                Long aLong = 0L;
                aLong =  collect.get(dictValue);
                map.put(dictLabel,aLong);
            }
        }
        map.put("count",kaoqinRecords.size());
        return AjaxResult.success(map);
    }


    /**
     *考勤日历
     */
    @GetMapping("/calendar")
    public AjaxResult calendar(KaoqinRecord kaoqinRecord) {
        //根据打卡人id和月份去找对应的打卡记录
        List<KaoqinRecord> kaoqinRecords = kaoqinRecordService.selectKaoqinRecordList(kaoqinRecord);
        Map<String, List<KaoqinRecord>> collect = kaoqinRecords.stream().collect(Collectors.groupingBy(KaoqinRecord::getKaTime));
        List list = new ArrayList();
        Map<String,Object> map = new HashMap();
        for (String date : collect.keySet()) {
            List<KaoqinRecord> kaoqinRecords1 = collect.get(date);
            //定义标签显示状态
            String abnormal = "";
            int num = 0;
            for (KaoqinRecord record : kaoqinRecords1) {
                String kaType = record.getKaType();
                if (!"1".equals(kaType)){
                    abnormal = kaType + ",";
                    num = num+1;
                }
            }
            if (num==0){
                abnormal = "1";
            }else {
                abnormal = abnormal.substring(0,abnormal.length() -1);
            }
            //日期
            map.put("data",date);
            //标签显示状态
            map.put("info",abnormal);
            //打卡记录列表
            map.put("list",kaoqinRecords1);
            list.add(map);
        }
        return AjaxResult.success(list);
    }


    /**
     * 计算是否在范围内
     *
     * @param platPunch
     * @return
     */
    public Boolean getPunch(PlatPunch platPunch) {
        //1、设置目的地经度
        String longitudeS = "117.21";
        //设置目的纬度
        String latitudeS = "31.84";
        // 2、由前端传过来的用户所在位置 经纬度
        String longitudeT = platPunch.getLongitude();
        String latitudeT = platPunch.getLatitude();
        // 3、对比
        GlobalCoordinates source = new GlobalCoordinates(Double.parseDouble(latitudeS), Double.parseDouble(longitudeS));
        GlobalCoordinates target = new GlobalCoordinates(Double.parseDouble(latitudeT), Double.parseDouble(longitudeT));
        //这是两种坐标系计算方法，这是第一种坐标系计算方法（我们用的这种）
        double geoCurve = getDistanceMeter(source, target, Ellipsoid.Sphere);
        //这是两种坐标系计算方法，这是第二种坐标系计算方法
        double geoCurve2 = getDistanceMeter(source, target, Ellipsoid.WGS84);
        System.out.println(geoCurve + "----------" + geoCurve2);
        //如果用户和目的地位置想吃2千米，在不能打卡；
        if (geoCurve > 2000) {
            System.out.println("不能打卡！！！");
            //反之，可以打卡
        } else {
            System.out.println("能打卡");
        }
        return true;
    }
}
