package jnpf.attendance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.attendance.mapper.AttendanceUserFaceMapper;
import jnpf.attendance.mapper.AttendanceUserSettingMapper;
import jnpf.attendance.service.AttendanceBaseSettingService;
import jnpf.attendance.service.AttendanceGroupService;
import jnpf.attendance.service.AttendanceUserService;
import jnpf.attendance.service.AttendanceUserSettingService;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.AttendanceGroupUser;
import jnpf.entity.attendance.AttendanceAppUserSetting;
import jnpf.entity.attendance.AttendanceUserFace;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.enums.attendance.UserSettingTypeEnum;
import jnpf.model.attendance.dto.AppUserSettingQueryDto;
import jnpf.model.attendance.dto.AttendanceAppUserSettingDto;
import jnpf.model.attendance.vo.AttendanceBaseSettingVo;
import jnpf.model.attendance.vo.AttendanceGroupVo;
import jnpf.model.attendance.vo.RemindClockInVo;
import jnpf.model.attendance.vo.UserSettingVo;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author huanglinpan
 * @Date 2024/8/8 10:55
 * @Version 1.0 （版本号）
 */
@Slf4j
@Service
public class AttendanceUserSettingServiceImpl extends SuperServiceImpl<AttendanceUserSettingMapper, AttendanceAppUserSetting> implements AttendanceUserSettingService {

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private AttendanceUserSettingMapper attendanceUserSettingMapper;

    @Resource
    private AttendanceGroupService attendanceGroupService;

    @Resource
    private AttendanceUserService attendanceUserService;

    @Resource
    private AttendanceBaseSettingService attendanceBaseSettingService;

    @Resource
    private AttendanceUserFaceMapper attendanceUserFaceMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(AttendanceAppUserSettingDto attendanceAppUserSettingDto) {
        String userId = userProvider.get().getUserId();
        attendanceAppUserSettingDto.setAssociationId(1 == attendanceAppUserSettingDto.getType() ? userId : attendanceAppUserSettingDto.getAssociationId());
        // 先根据Id 判断是新增还是修改
        if (null == attendanceAppUserSettingDto.getId()  || attendanceAppUserSettingDto.getId().isEmpty()) {
            // 校验是否重复生成
            String id = attendanceUserSettingMapper.getDetail(attendanceAppUserSettingDto);
            if (null == id) {
                // 新增时，直接将参数保存到ftb_attendance_app_user_setting表
                attendanceAppUserSettingDto.setId(FtbUtil.getId());
                attendanceUserSettingMapper.add(attendanceAppUserSettingDto);
                if (1 == attendanceAppUserSettingDto.getType()) {
                    if (UserSettingEnum.PRE_WORK_REMINDER.equals(attendanceAppUserSettingDto.getCode()) || UserSettingEnum.END_WORK_REMINDER.equals(attendanceAppUserSettingDto.getCode())) {
                        refreshRemindRedisCache(attendanceAppUserSettingDto.getCode(), userId, attendanceAppUserSettingDto.getValue(), userProvider.get().getTenantId());
                    }
                }
            } else {
                attendanceAppUserSettingDto.setId(id);
                //修改
                attendanceUserSettingMapper.update(attendanceAppUserSettingDto);
                if (1 == attendanceAppUserSettingDto.getType()) {
                    if (UserSettingEnum.PRE_WORK_REMINDER.equals(attendanceAppUserSettingDto.getCode()) || UserSettingEnum.END_WORK_REMINDER.equals(attendanceAppUserSettingDto.getCode())) {
                        refreshRemindRedisCache(attendanceAppUserSettingDto.getCode(), userId, attendanceAppUserSettingDto.getValue(), userProvider.get().getTenantId());
                    }
                }
            }
        } else {
            // 修改时，根据Id修改 用户配置
            attendanceUserSettingMapper.update(attendanceAppUserSettingDto);
            if (1 == attendanceAppUserSettingDto.getType()) {
                if (UserSettingEnum.PRE_WORK_REMINDER.equals(attendanceAppUserSettingDto.getCode()) || UserSettingEnum.END_WORK_REMINDER.equals(attendanceAppUserSettingDto.getCode())) {
                    refreshRemindRedisCache(attendanceAppUserSettingDto.getCode(), userId, attendanceAppUserSettingDto.getValue(), userProvider.get().getTenantId());
                }
            }

        }

    }

    @Override
    public List<UserSettingVo> getList(AppUserSettingQueryDto appUserSettingQueryDto) {
        String userId = userProvider.get().getUserId();
        // 根据type ，判断走考勤组逻辑还是个人设置逻辑
        if (1 == appUserSettingQueryDto.getType()) {
            // 个人设置 根据PId查询个人设置列表
            appUserSettingQueryDto.setPId(null == appUserSettingQueryDto.getPId() ? "0000" : appUserSettingQueryDto.getPId());
            appUserSettingQueryDto.setAssociationId(userId);
            List<UserSettingVo> list = getUserSettingList(appUserSettingQueryDto);
            if (null == list) {
                return null;
            }
            // 是否包含查询考勤消息接收
            UserSettingVo filter = list.stream().filter(t -> t.getCode().equals(UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE.getCode())).findFirst().orElse(null);
            // 如果考勤消息接受打开，查询子集
            if (null != filter && 1 == filter.getStatus()) {
                appUserSettingQueryDto.setPId(filter.getSettingId());
                filter.setChild(getUserSettingList(appUserSettingQueryDto));
            }
            // 查询是否包含人脸录入信息
            UserSettingVo face = list.stream().filter(t -> t.getCode().equals(UserSettingEnum.ATTENDANCE_SETTING_FACE.getCode())).findFirst().orElse(null);
            if (null != face) {
                // 查看user_face表有无该用户的人脸信息
                LambdaQueryWrapper<AttendanceUserFace> faceQuery = new LambdaQueryWrapper<AttendanceUserFace>()
                        .eq(AttendanceUserFace::getUserId, userId)
                        .eq(AttendanceUserFace::getDeleteMark, ConstantUtil.NUM_FALSE);
                AttendanceUserFace attendanceUserFace = attendanceUserFaceMapper.selectOne(faceQuery);
                face.setStatus(null != attendanceUserFace ? 1 : 0);
            }
            return list;
        } else if (2 == appUserSettingQueryDto.getType()){
            // 考勤组逻辑，查询用户能看到的考勤组列表并将对应考勤组的设置返回
            return getGroupSetting(appUserSettingQueryDto.getGroupName());

        }else if (3 == appUserSettingQueryDto.getType()){
            // web全局设置
            // 查询设置列表 web 全局设置 查询全部
            List<UserSettingVo> userSettingVos = attendanceUserSettingMapper.getSettingList(null,appUserSettingQueryDto.getType());
            if (null == userSettingVos) {
                return new ArrayList<>();
            }
            // 查看现有配置
            List<UserSettingVo> userSetting = attendanceUserSettingMapper.getUserSetting(appUserSettingQueryDto);
            // 设置默认数据
            List<UserSettingVo> one = userSettingVos.stream().filter(v -> "000000".equals(v.getPId())).sorted(Comparator.comparing(UserSettingVo::getSort)).collect(Collectors.toList());
            // 赋值默认值
            one.forEach(v->{
                // 获取子集
                List<UserSettingVo> collect = userSettingVos.stream().filter(va -> v.getSettingId().equals(va.getPId())).sorted(Comparator.comparing(UserSettingVo::getSort)).collect(Collectors.toList());
                Map<String, UserSettingVo> map;
                if (userSetting != null && !userSetting.isEmpty()) {
                    map = userSetting.stream().collect(Collectors.toMap(UserSettingVo::getCode, Function.identity()));
                } else {
                    map = null;
                }
                collect.forEach(value -> {
                    if (null == map || null == map.get(value.getCode())) {
                        // 设置默认值 默认全部打开
                        value.setStatus(1);
                        setDefault(value.getCode(), value);
                    } else {
                        UserSettingVo userSettingVo = map.get(value.getCode());
                        value.setStatus(userSettingVo.getStatus());
                        value.setValue(userSettingVo.getValue());
                    }
                });
                v.setChild(collect);
            });
            return one;
        } else {
            return null;
        }
    }

    @Nullable
    private List<UserSettingVo> getGroupSetting(String groupName) {
        List<AttendanceGroupVo> attendanceGroupVos = attendanceGroupService.viewApprovalGroupList(groupName);
        if (null == attendanceGroupVos || attendanceGroupVos.isEmpty()) {
            return null;
        }
        // 查询考勤组的设置
        List<String> groupIds = attendanceGroupVos.stream().map(AttendanceGroupVo::getId).collect(Collectors.toList());
        return setGroupSetting(groupIds, attendanceGroupVos);
    }

    @NotNull
    private List<UserSettingVo> setGroupSetting(List<String> groupIds, List<AttendanceGroupVo> attendanceGroupVos) {
        List<UserSettingVo> list = new ArrayList<>();
        List<UserSettingVo> groupSetting = attendanceUserSettingMapper.getGroupSetting(groupIds, 2);
        Map<String, UserSettingVo> map;
        if (groupSetting != null && !groupSetting.isEmpty()) {
            map = groupSetting.stream().collect(Collectors.toMap(UserSettingVo::getAssociationId, Function.identity()));
        } else {
            map = null;
        }
        attendanceGroupVos.forEach(v -> {
            UserSettingVo userSettingVo = new UserSettingVo(v.getGroupName(), 2, v.getId());
            if (null != map && !map.isEmpty() && null != map.get(v.getId())) {
                UserSettingVo vo = map.get(v.getId());
                userSettingVo.setId(vo.getId());
                userSettingVo.setStatus(vo.getStatus());
            } else {
                userSettingVo.setStatus(1);
            }
            list.add(userSettingVo);
        });
        return list;
    }


    private List<UserSettingVo> getUserSettingList(AppUserSettingQueryDto appUserSettingQueryDto) {
        List<UserSettingVo> list = new ArrayList<>();
        // 查询设置列表
        List<UserSettingVo> userSettingVos = attendanceUserSettingMapper.getSettingList(appUserSettingQueryDto.getPId(),appUserSettingQueryDto.getType());
        if (null == userSettingVos) {
            return list;
        }
        // 查询用户设置信息
        List<UserSettingVo> userSetting = attendanceUserSettingMapper.getUserSetting(appUserSettingQueryDto);
        Map<String, UserSettingVo> map = null;
        if (userSetting != null && !userSetting.isEmpty()) {
            map = userSetting.stream().collect(Collectors.toMap(UserSettingVo::getCode, Function.identity()));
            // 如果查的是最高级且是个人设置
            if ("0000".equals(appUserSettingQueryDto.getPId()) && 1 == appUserSettingQueryDto.getType()) {
                // 查询用户实时所属考勤组的 内勤打卡是否需要拍照设置
                List<AttendanceGroupUser> attendanceGroupUsers = attendanceUserService.getAttendanceGroupUsersOfSecondment(new Date(), new Date(), List.of(userProvider.get().getUserId()), null);
                if (CollUtil.isNotEmpty(attendanceGroupUsers)){
                    AttendanceBaseSettingVo one = attendanceBaseSettingService.getOne(attendanceGroupUsers.get(0).getGroupId());
                    if (null != one && (1 == one.getAttendancePhoto() || 1 == one.getFace())){
                        // 如果该设置打开 ，上下班极速打卡默认为关闭。
                        // 因为这里查找的是用户配置后的数据,可能会没有
                        if (null != map.get(UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode())) {
                            map.get(UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode()).setStatus(0);
                        }
                        if (null != map.get(UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode())) {
                            map.get(UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode()).setStatus(0);
                        }
                    }
                }
            }
        } else {
            // 没有设置过个人设置
            // 如果查的是最高级且是个人设置,这时上下班极速打卡需要再次判断，是否打开需要拍照与人脸设置
            if ("0000".equals(appUserSettingQueryDto.getPId()) && 1 == appUserSettingQueryDto.getType()) {
                map = new HashMap<>();
                UserSettingVo userSettingVo = new UserSettingVo();
                userSettingVo.setCode(UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode());
                UserSettingVo userSettingVo1 = new UserSettingVo();
                userSettingVo1.setCode(UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode());
                // 查询用户实时所属考勤组的 内勤打卡是否需要拍照设置,=与人脸设置是否打开
                List<AttendanceGroupUser> attendanceGroupUsers = attendanceUserService.getAttendanceGroupUsersOfSecondment(new Date(), new Date(), List.of(userProvider.get().getUserId()), null);
                if (CollUtil.isNotEmpty(attendanceGroupUsers)){
                    AttendanceBaseSettingVo one = attendanceBaseSettingService.getOne(attendanceGroupUsers.get(0).getGroupId());
                    if (Objects.isNull(one)) {
                        userSettingVo.setStatus(1);
                        userSettingVo1.setStatus(1);
                    } else {
                        userSettingVo.setStatus(( 1 == one.getFace() || 1 == one.getAttendancePhoto()) ? 0 : 1);
                        userSettingVo1.setStatus(( 1 == one.getFace() || 1 == one.getAttendancePhoto()) ? 0 : 1);
                    }
                    map.put(UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode(), userSettingVo);
                    map.put(UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode(), userSettingVo1);
                }
            }
        }
        // 因没有初始化数据，所以可能查不到数据，查不到默认开启
        Map<String, UserSettingVo> finalMap = map;
        userSettingVos.forEach(v -> {
            UserSettingVo userSettingVo = new UserSettingVo(v.getSettingId(), v.getSettingName(), v.getCode(), appUserSettingQueryDto.getType(), appUserSettingQueryDto.getAssociationId(),v.getSort());
            if (null != finalMap && !finalMap.isEmpty()) {
                UserSettingVo vo = finalMap.get(v.getCode());
                if (null != vo) {
                    userSettingVo.setId(vo.getId());
                    userSettingVo.setStatus(vo.getStatus());
                    userSettingVo.setValue(vo.getValue());
                } else {
                    userSettingVo.setStatus(1);
                    setDefault(v.getCode(), userSettingVo);
                }
            } else {
                userSettingVo.setStatus(1);
                setDefault(v.getCode(), userSettingVo);
            }
            list.add(userSettingVo);
        });
        // 根据sort字段正序排序
        return list.stream().sorted(Comparator.comparing(UserSettingVo::getSort)).collect(Collectors.toList());
    }

    /**
     * 默认设置value
     *
     * @param code          code
     * @param userSettingVo 用户配置
     */
    private static void setDefault(String code, UserSettingVo userSettingVo) {
        if (UserSettingEnum.ATTENDANCE_SETTING_PRE_WORK_REMINDER.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 10);
        }
        if (UserSettingEnum.ATTENDANCE_SETTING_END_WORK_REMINDER.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 0);
        }
        if (UserSettingEnum.ATTENDANCE_SETTING_ATTENDANCE_GROUP_DAILY_REPORT_SEND.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 1);
        }
        if (UserSettingEnum.ATTENDANCE_SETTING_ATTENDANCE_GROUP_MONTHLY_REPORT_SEND.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 1);
        }
        if (UserSettingEnum.ATTENDANCE_SETTING_ATTENDANCE_GROUP_NO_CHECKIN_REMIND_ADMIN.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 3);
        }
        if (UserSettingEnum.ATTENDANCE_SETTING_ATTENDANCE_TEAM_MONTHLY_REPORT_SEND.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 1);
        }
        if (UserSettingEnum.ATTENDANCE_SETTING_UN_SCHEDULED_REMIND_ADMIN.getCode().equals(code)) {
            userSettingVo.setValue(Objects.nonNull(userSettingVo.getValue()) ? userSettingVo.getValue() : 3);
        }
    }

    @Override
    public List<UserSettingVo> getSettingList(String associationId, Integer type, String code) {
        return getSettingList(CollUtil.newArrayList(associationId), type, CollUtil.newArrayList(code));
    }

    @Override
    public List<UserSettingVo> getSettingList(List<String> associationId, Integer type, String code) {
        return getSettingList(associationId, type, CollUtil.newArrayList(code));
    }

    @Override
    public List<UserSettingVo> getSettingList(List<String> associationId, Integer type, List<String> code) {
        List<UserSettingVo> list = new ArrayList<>();
        // 根据类型走不同逻辑
        if (1 == type) {
            // 个人设置
            List<UserSettingVo> groupSetting = attendanceUserSettingMapper.getUserSettingByCode(associationId, type, code);
            Map<String, List<UserSettingVo>> map;
            if (groupSetting != null && !groupSetting.isEmpty()) {
                map = groupSetting.stream().collect(Collectors.groupingBy(UserSettingVo::getAssociationId));
            } else {
                map = null;
            }
            associationId.forEach(v -> {
                List<UserSettingVo> userSettingVosByAssociationId = null;
                if (map != null) {
                    userSettingVosByAssociationId = map.get(v);
                }
                if (null != userSettingVosByAssociationId && !userSettingVosByAssociationId.isEmpty()) {
                    Map<String, UserSettingVo> codeMap = userSettingVosByAssociationId.stream().collect(Collectors.toMap(UserSettingVo::getCode, Function.identity()));
                    code.forEach(v1 -> {
                        if (null != codeMap.get(v1)) {
                            UserSettingVo userSettingVo = new UserSettingVo(type, v, codeMap.get(v1).getStatus(), v1, codeMap.get(v1).getValue());
                            setDefault(v1, userSettingVo);
                            list.add(userSettingVo);
                        } else {
                            UserSettingVo userSettingVo = new UserSettingVo(type, v, 1, v1);
                            setDefault(v1, userSettingVo);
                            list.add(userSettingVo);
                        }
                    });
                } else {
                    code.forEach(v1 -> {
                        UserSettingVo userSettingVo = new UserSettingVo(type, v, 1, v1);
                        setDefault(v1, userSettingVo);
                        list.add(userSettingVo);
                    });
                }
            });
            return list;
        } else {
            // 考勤组设置
            List<UserSettingVo> groupSetting = attendanceUserSettingMapper.getGroupSetting(associationId, type);
            Map<String, UserSettingVo> map;
            if (groupSetting != null && !groupSetting.isEmpty()) {
                map = groupSetting.stream().collect(Collectors.toMap(UserSettingVo::getAssociationId, Function.identity()));
            } else {
                map = null;
            }
            associationId.forEach(v -> {
                UserSettingVo userSettingVo = new UserSettingVo(type, v);
                if (null != map && !map.isEmpty() && null != map.get(v)) {
                    userSettingVo.setStatus(map.get(v).getStatus());
                } else {
                    userSettingVo.setStatus(1);
                }
                list.add(userSettingVo);
            });
            return list;
        }
    }

    @Override
    public UserSettingVo checkWebSetting(String code) {
        // 校验指定的web全局设置是否打开
        List<UserSettingVo> groupSetting = attendanceUserSettingMapper.getUserSettingByCode(null, UserSettingTypeEnum.WEB_MESSAGE_SETTING.getCode(), CollUtil.newArrayList(code));
        if (null != groupSetting && !groupSetting.isEmpty()) {
            return groupSetting.get(0);
        }
        // 设置默认值
        UserSettingVo userSettingVo = new UserSettingVo();
        userSettingVo.setCode(code);
        userSettingVo.setStatus(1);
        setDefault(code, userSettingVo);
        return userSettingVo;
    }

    @Override
    public void refreshRemindRedisCache(String code, String userId, Integer value, String tenantId) {

        DateDetail dateDetail = new DateDetail();
        String todayStr = DateDetail.getDate2Str(dateDetail.getCurrentDate(), DateDetail.DF);
        String hashKey = tenantId + "_" + todayStr;
        String json = redisUtil.getHashValues(ConstantUtil.FTB_REMIND_KEY, hashKey);
        Map<String, List<RemindClockInVo>> remindMap = new HashMap<>();
        Map map = JSONUtil.toBean(json, Map.class);
        for (Object key : map.keySet()) {
            JSONArray jsonArray = JSONUtil.parseArray(map.get(key));
            remindMap.put(key.toString(), JSONUtil.toList(jsonArray, RemindClockInVo.class));
        }
        // 重新计算对应用户未处理的记录
        remindMap.forEach((k, v) -> {
            if (k.equals(userId)) {
                for (RemindClockInVo clockIn : v) {
                    try {
                        if (clockIn.getDeal().equals(ConstantUtil.NUM_TRUE)) {
                            continue;
                        }
                        // 计算工作时间与提醒时间的差
                        Date date = DateDetail.getStr2DateTime(clockIn.getWorkTime());
                        Date remindTime;
                        if (code.equals(UserSettingEnum.PRE_WORK_REMINDER)) {
                            // 工作时间 - value
                            remindTime = dateDetail.addMinute(date, -value);
                        } else {
                            remindTime = dateDetail.addMinute(date, value);
                        }
                        clockIn.setRemindTime(DateDetail.getDate2Str(remindTime, DateDetail.DF2));
                    } catch (Exception e) {
                        log.info(e.getMessage());
                    }
                }
            }
        });
        redisUtil.insertHash(ConstantUtil.FTB_REMIND_KEY, hashKey, JSONUtil.toJsonStr(remindMap));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateList(List<AttendanceAppUserSettingDto> attendanceAppUserSettingDto) {
        // 循环保存，因一部分设置需要做消息通知
        attendanceAppUserSettingDto.forEach(this::saveOrUpdate);
    }

    @Override
    public List<UserSettingVo> getListNew(AppUserSettingQueryDto appUserSettingQueryDto) {
        String userId = userProvider.get().getUserId();
        // 根据type ，判断走考勤组逻辑还是个人设置逻辑
        if (1 == appUserSettingQueryDto.getType()) {
            // 个人设置 根据PId查询个人设置列表
            appUserSettingQueryDto.setPId(null == appUserSettingQueryDto.getPId() ? "0000" : appUserSettingQueryDto.getPId());
            appUserSettingQueryDto.setAssociationId(userId);
            List<UserSettingVo> list = getUserSettingList(appUserSettingQueryDto);
            if (null == list) {
                return null;
            }
            // 是否包含查询考勤消息接收
            UserSettingVo filter = list.stream().filter(t -> t.getCode().equals(UserSettingEnum.ATTENDANCE_SETTING_MESSAGE_RECEIVE.getCode())).findFirst().orElse(null);
            // 如果考勤消息接受打开，查询子集
            if (null != filter && 1 == filter.getStatus()) {
                appUserSettingQueryDto.setPId(filter.getSettingId());
                filter.setChild(getUserSettingList(appUserSettingQueryDto));
            }
            // 查询是否包含人脸录入信息
            UserSettingVo face = list.stream().filter(t -> t.getCode().equals(UserSettingEnum.ATTENDANCE_SETTING_FACE.getCode())).findFirst().orElse(null);
            if (null != face) {
                // 查看user_face表有无该用户的人脸信息
                LambdaQueryWrapper<AttendanceUserFace> faceQuery = new LambdaQueryWrapper<AttendanceUserFace>()
                        .eq(AttendanceUserFace::getUserId, userId)
                        .eq(AttendanceUserFace::getDeleteMark, ConstantUtil.NUM_FALSE);
                AttendanceUserFace attendanceUserFace = attendanceUserFaceMapper.selectOne(faceQuery);
                face.setStatus(null != attendanceUserFace ? 1 : 0);
            }
            return list;
        } else if (2 == appUserSettingQueryDto.getType()){
            // 考勤组逻辑，查询用户能看到的考勤组列表并将对应考勤组的设置返回   考勤V1.9优化
            return getGroupSettingNew(appUserSettingQueryDto.getGroupName());

        }else if (3 == appUserSettingQueryDto.getType()){
            // web全局设置
            // 查询设置列表 web 全局设置 查询全部
            List<UserSettingVo> userSettingVos = attendanceUserSettingMapper.getSettingList(null,appUserSettingQueryDto.getType());
            if (null == userSettingVos) {
                return new ArrayList<>();
            }
            // 查看现有配置
            List<UserSettingVo> userSetting = attendanceUserSettingMapper.getUserSetting(appUserSettingQueryDto);
            // 设置默认数据
            List<UserSettingVo> one = userSettingVos.stream().filter(v -> "000000".equals(v.getPId())).sorted(Comparator.comparing(UserSettingVo::getSort)).collect(Collectors.toList());
            // 赋值默认值
            one.forEach(v->{
                // 获取子集
                List<UserSettingVo> collect = userSettingVos.stream().filter(va -> v.getSettingId().equals(va.getPId())).sorted(Comparator.comparing(UserSettingVo::getSort)).collect(Collectors.toList());
                Map<String, UserSettingVo> map;
                if (userSetting != null && !userSetting.isEmpty()) {
                    map = userSetting.stream().collect(Collectors.toMap(UserSettingVo::getCode, Function.identity()));
                } else {
                    map = null;
                }
                collect.forEach(value -> {
                    if (null == map || null == map.get(value.getCode())) {
                        // 设置默认值 默认全部打开
                        value.setStatus(1);
                        setDefault(value.getCode(), value);
                    } else {
                        UserSettingVo userSettingVo = map.get(value.getCode());
                        value.setStatus(userSettingVo.getStatus());
                        value.setValue(userSettingVo.getValue());
                    }
                });
                v.setChild(collect);
            });
            return one;
        } else {
            return null;
        }
    }

    @Nullable
    private List<UserSettingVo> getGroupSettingNew(String groupName) {
        List<AttendanceGroupVo> attendanceGroupVos = attendanceGroupService.viewApprovalGroupListNew(groupName);
        if (null == attendanceGroupVos || attendanceGroupVos.isEmpty()) {
            return null;
        }
        // 查询考勤组的设置
        List<String> groupIds = attendanceGroupVos.stream().map(AttendanceGroupVo::getId).collect(Collectors.toList());
        return setGroupSetting(groupIds, attendanceGroupVos);
    }
}
