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.common.utils.StringUtils;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 实现考勤打卡
 *
 * @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 kaoqinConfig
     * @return
     */
    @GetMapping(value = "/daKa")
    public AjaxResult daKa(KaoqinConfig kaoqinConfig) {
        //先去 根据当前人员的部门id查询是否有规则,没有再去差祖籍列表
        KaoqinConfig kaoqinConfigDept = kaoqinConfigService.selectKaoqinConfigByDeptId(kaoqinConfig.getDeptId());
        if (kaoqinConfigDept == null){
            //根据部门祖籍列表,查询该部门考勤范围 ,经纬度
            List<KaoqinConfig> kaoqinConfigs = kaoqinConfigService.selectKaoqinConfigList(kaoqinConfig);
            if (kaoqinConfigs != null && kaoqinConfigs.size() > 1) {
                //如果有多条考勤规则,先选离自己最近的规则
                String ancestors = kaoqinConfig.getAncestors();
                String[] split = ancestors.split(",");
                Map<Long, List<KaoqinConfig>> collect = kaoqinConfigs.stream().collect(Collectors.groupingBy(KaoqinConfig::getDeptId));
                for (int i = split.length - 1; i >= 0; i--) {
                    String s = split[i];
                    List<KaoqinConfig> kaoqinConfigsDept = collect.get(Long.parseLong(s));
                    if (kaoqinConfigsDept != null) {
                        return AjaxResult.success(kaoqinConfigsDept);
                    }
                }
            }
        }
        return AjaxResult.success(kaoqinConfigDept);
    }


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

    /**
     * 查询考勤异常数据信息
     * 包括所有异常的数量
     * 本月考勤记录的异常分类数量
     */
    @GetMapping("/abnormal")
    public AjaxResult abnormal(KaoqinRecord kaoqinRecord) {
        //传入userId/年 和 月份 设置查询异常的考勤
        kaoqinRecord.setKaStatus("2");
        List<KaoqinRecord> kaoQinRecords = kaoqinRecordService.selectKaoqinRecordList(kaoqinRecord);
        //记录该月有多少次异常总数
        Long count = 0L;
        SysDictData dictData = new SysDictData();
        dictData.setDictType("kaoqin_abnormal");
        List<SysDictData> sysDictData = dictDataService.selectDictDataList(dictData);
        Map<String, Long> map = new HashMap();
        for (SysDictData sysDictDatum : sysDictData) {
            //获取异常的值
            String dictValue = sysDictDatum.getDictValue();
            if (!"1".equals(dictValue)) {
                Long aLong = 0L;
                map.put(dictValue, aLong);
            }
        }
        for (KaoqinRecord kaoQinRecord : kaoQinRecords) {
            String kaTypeAmIn = kaoQinRecord.getKaTypeAmIn();
            String kaTypeAmOut = kaoQinRecord.getKaTypeAmOut();
            String kaTypePmIn = kaoQinRecord.getKaTypePmIn();
            String kaTypePmOut = kaoQinRecord.getKaTypePmOut();
            if (!"1".equals(kaTypeAmIn) && StringUtils.isNotBlank(kaTypeAmIn)) {
                Long aLong = map.get(kaTypeAmIn);
                map.put(kaTypeAmIn, aLong + 1);
                count = count + 1;
            }  if (!"1".equals(kaTypeAmOut) && StringUtils.isNotBlank(kaTypeAmOut)) {
                Long aLong = map.get(kaTypeAmOut);
                map.put(kaTypeAmOut, aLong + 1);
                count = count + 1;
            }  if (!"1".equals(kaTypePmIn) && StringUtils.isNotBlank(kaTypePmIn)) {
                Long aLong = map.get(kaTypePmIn);
                map.put(kaTypePmIn, aLong + 1);
                count = count + 1;
            }  if (!"1".equals(kaTypePmOut) && StringUtils.isNotBlank(kaTypePmOut)) {
                Long aLong = map.get(kaTypePmOut);
                map.put(kaTypePmOut, aLong + 1);
                count = count + 1;
            }
        }
        map.put("count", count);
        return AjaxResult.success(map);
    }


    /**
     * 考勤日历
     */
    @GetMapping("/calendar")
    public AjaxResult calendar(KaoqinRecord kaoqinRecord) {
        //根据打卡人id和月份去找对应的打卡记录
        List<KaoqinRecord> kaoqinRecords = kaoqinRecordService.selectKaoqinRecordList(kaoqinRecord);
        List list = new ArrayList();
        if (kaoqinRecords != null && kaoqinRecords.size() > 0) {
            Map<String, List<KaoqinRecord>> collect = kaoqinRecords.stream().collect(Collectors.groupingBy(KaoqinRecord::getKaTime));
            for (String date : collect.keySet()) {
                Map<String, Object> map = new HashMap();
                List<KaoqinRecord> kaoqinRecords1 = collect.get(date);
                //定义标签显示状态
                String abnormal = "";
                Boolean flag = true;
                for (KaoqinRecord record : kaoqinRecords1) {
                    String kaTypeAmIn = record.getKaTypeAmIn();
                    String kaTypeAmOut = record.getKaTypeAmOut();
                    String kaTypePmIn = record.getKaTypePmIn();
                    String kaTypePmOut = record.getKaTypePmOut();
                    if (!"1".equals(kaTypeAmIn) && StringUtils.isNotBlank(kaTypeAmIn)) {
                        abnormal = kaTypeAmIn + ",";
                        flag = false;
                    }
                    if (!"1".equals(kaTypeAmOut) && StringUtils.isNotBlank(kaTypeAmOut)) {
                        abnormal = kaTypeAmOut + ",";
                        flag = false;
                    }
                    if (!"1".equals(kaTypePmIn) && StringUtils.isNotBlank(kaTypePmIn)) {
                        abnormal = kaTypePmIn + ",";
                        flag = false;
                    }
                    if (!"1".equals(kaTypePmOut) && StringUtils.isNotBlank(kaTypePmOut)) {
                        abnormal = kaTypePmOut + ",";
                        flag = false;
                    }
                }
                if (flag) {
                    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
     * @return
     */
    @PostMapping("/clockRecord")
    public AjaxResult clockRecord(@RequestBody KaoqinRecord kaoqinRecord) {
        //定义返回值
        List list = new ArrayList();
        //根据部门id查询考勤数据
        List<KaoqinRecord> kaoqinRecords = kaoqinRecordService.selectKaoqinRecordList(kaoqinRecord);
        if (kaoqinRecords != null) {
            //根据userId进行分组
            Map<Long, List<KaoqinRecord>> kaoQinRecords = kaoqinRecords.stream().collect(Collectors.groupingBy(KaoqinRecord::getUserId));
            SysDictData dictData = new SysDictData();
            dictData.setDictType("kaoqin_abnormal");
            //找到所有的异常
            List<SysDictData> sysDictData = dictDataService.selectDictDataList(dictData);
            Map<String, String> map = new HashMap<>();
            if (sysDictData != null) {
                for (SysDictData sysDictDatum : sysDictData) {
                    //获取异常名称
                    String dictLabel = sysDictDatum.getDictLabel();
                    //获取异常的值
                    String dictValue = sysDictDatum.getDictValue();
                    if (!"1".equals(dictValue)) {
                        map.put(dictValue, dictLabel);
                    }
                }
            }

            for (List<KaoqinRecord> value : kaoQinRecords.values()) {
                if (value != null) {
                    //定义返回的人
                    Map<String, Object> userMap = new HashMap();
                    userMap.put("userName", value.get(0).getUserName());
                    userMap.put("deptName", value.get(0).getDeptName());
                    userMap.put("list", value);
                    for (String dictValue : map.keySet()) {
                        String dictLabel = map.get(dictValue);
                        userMap.put(dictLabel, 0);
                    }
                    for (KaoqinRecord kaoQinRecord : value) {
                        String kaTypeAmIn = kaoQinRecord.getKaTypeAmIn();
                        String kaTypeAmOut = kaoQinRecord.getKaTypeAmOut();
                        String kaTypePmIn = kaoQinRecord.getKaTypePmIn();
                        String kaTypePmOut = kaoQinRecord.getKaTypePmOut();
                        if (!"1".equals(kaTypeAmIn) && StringUtils.isNotBlank(kaTypeAmIn)) {
                            //获取异常的名称
                            String dictLabel = map.get(kaTypeAmIn);
                            Integer num = (Integer) userMap.get(dictLabel);
                            userMap.put(dictLabel, num + 1);
                        }  if (!"1".equals(kaTypeAmOut) && StringUtils.isNotBlank(kaTypeAmOut)) {
                            String dictLabel = map.get(kaTypeAmOut);
                            //获取异常的名称
                            Integer num = (Integer) userMap.get(dictLabel);
                            userMap.put(dictLabel, num + 1);
                        }  if (!"1".equals(kaTypePmIn) && StringUtils.isNotBlank(kaTypePmIn)) {
                            String dictLabel = map.get(kaTypePmIn);
                            //获取异常的名称
                            Integer num = (Integer) userMap.get(dictLabel);
                            userMap.put(dictLabel, num + 1);
                        }  if (!"1".equals(kaTypePmOut) && StringUtils.isNotBlank(kaTypePmOut)) {
                            String dictLabel = map.get(kaTypePmOut);
                            //获取异常的名称
                            Integer num = (Integer) userMap.get(dictLabel);
                            userMap.put(dictLabel, num + 1);
                        }
                    }
                    list.add(userMap);
                }
            }
        }
        return AjaxResult.success(list);
    }
}
