package jnpf.attendance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import jnpf.attendance.mapper.AttendanceBaseSettingMapper;
import jnpf.attendance.mapper.AttendanceFieldPersonnelMapper;
import jnpf.attendance.service.*;
import jnpf.attendance.service.handle.notice.AttendanceNoticeHandler;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.AttendanceGroup;
import jnpf.entity.AttendanceGroupUser;
import jnpf.entity.attendance.AttendanceBaseSetting;
import jnpf.entity.attendance.AttendanceFieldPersonnel;
import jnpf.enums.attendance.AttendanceNoticeEnum;
import jnpf.enums.attendance.UserSettingEnum;
import jnpf.exception.HandleException;
import jnpf.exception.QueryException;
import jnpf.model.attendance.dto.AttendanceBaseSettingDto;
import jnpf.model.attendance.dto.AttendanceGroupStatusDto;
import jnpf.model.attendance.model.RuleChangeNoticeModel;
import jnpf.model.attendance.vo.AttendanceBaseSettingVo;
import jnpf.model.attendance.vo.AttendanceGroupUserVo;
import jnpf.model.attendance.vo.GroupInfoVo;
import jnpf.model.attendance.vo.UserSettingVo;
import jnpf.model.attendance.vo.attendance.QuickCheckInVo;
import jnpf.permission.V2UserApi;
import jnpf.permission.vo.v2.user.UserBoundVO;
import jnpf.util.*;
import jnpf.util.data.DataSourceContextHolder;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
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;
import java.util.stream.Stream;

import static jnpf.constants.RedisConstant.ATTENDANCE_BASE_SETTING_CACHE_KEY;

/**
 * <p>
 * 考勤基础设置表 服务实现类
 * </p>
 *
 * @author ahua
 * @since 2023-11-29
 */
@Service
public class AttendanceBaseSettingServiceImpl extends SuperServiceImpl<AttendanceBaseSettingMapper, AttendanceBaseSetting> implements AttendanceBaseSettingService {
    @Resource
    private V2UserApi v2UserApi;
    @Resource
    private UserProvider userProvider;
    @Resource
    private AttendanceUserService attendanceUserService;
    @Autowired
    private AttendanceGroupService attendanceGroupService;
    @Autowired
    private AttendanceNoticeHandler attendanceNoticeHandler;
    @Resource
    private AttendanceClockInService attendanceClockInService;
    @Resource
    private AttendanceSuperAdminService attendanceSuperAdminService;
    @Resource
    private AttendanceUserSettingService attendanceUserSettingService;
    @Resource
    private AttendanceFieldPersonnelMapper attendanceFieldPersonnelMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    @Transactional
    public void save(AttendanceBaseSettingDto attendanceBaseSettingDto) throws HandleException {
        AttendanceBaseSetting attendanceBaseSettingEntity = BeanUtil.toBean(attendanceBaseSettingDto, AttendanceBaseSetting.class);
        UserInfo user = UserProvider.getUser();
        List<AttendanceBaseSetting> list = lambdaQuery().eq(AttendanceBaseSetting::getGroupId, attendanceBaseSettingEntity.getGroupId())
                .eq(AttendanceBaseSetting::getDeleteMark, Boolean.FALSE).list();
        AttendanceGroupStatusDto attendanceGroupStatusDto = attendanceGroupService.groupSettingStatus(attendanceBaseSettingDto.getGroupId(), Boolean.FALSE);
        if (Objects.isNull(attendanceGroupStatusDto)) {
            throw new HandleException("当前考勤组未查到");
        }
        Integer enable = attendanceGroupStatusDto.getBaseSetting();
        if (CollUtil.isEmpty(list)) {
            attendanceBaseSettingEntity.setCreatorTime(new Date());
            attendanceBaseSettingEntity.setCreatorUserId(user.getUserId());
            attendanceBaseSettingEntity.setTenantId(user.getTenantId());
            attendanceBaseSettingEntity.setId(RandomUtil.uuId());
            attendanceBaseSettingEntity.setEnable(enable);
            attendanceBaseSettingEntity.setDeleteMark(0);
        } else {
            AttendanceBaseSetting attendanceBaseSetting = list.get(0);
            attendanceBaseSettingEntity.setId(attendanceBaseSetting.getId());
            attendanceBaseSettingEntity.setLastModifyTime(new Date());
            attendanceBaseSettingEntity.setLastModifyUserId(UserProvider.getLoginUserId());
            attendanceBaseSettingEntity.setEnable(enable);
            attendanceBaseSettingEntity.setDeleteMark(0);
            //  非超级管理员没有不能修改出勤换算比
            UserInfo userInfo = userProvider.get();
            Boolean isAdmin = attendanceSuperAdminService.isSuperAdmin(userInfo.getUserId());
            if (!isAdmin && !userProvider.get().getIsAdministrator()) {
                // 不是系统超级管理员及考勤组超级管理员  不能修改出勤换算比 和 是否需要拍照 1.8.2 新加人脸识别
                attendanceBaseSettingEntity.setAttendanceRatio(null);
                attendanceBaseSettingEntity.setAttendancePhoto(null);
                attendanceBaseSettingEntity.setFace(null);
            }
            sendNotice(attendanceBaseSettingEntity.getGroupId(), attendanceBaseSetting.getFieldClockStatus(), attendanceBaseSettingEntity.getFieldClockStatus(), AttendanceNoticeEnum.RULE_CHANGE_FIELD);
            sendNotice(attendanceBaseSettingEntity.getGroupId(), attendanceBaseSetting.getPatchClockStatus(), attendanceBaseSettingEntity.getPatchClockStatus(), AttendanceNoticeEnum.RULE_CHANGE_CARD_MAKEUP);
        }
        saveOrUpdate(attendanceBaseSettingEntity);
        redisTemplate.delete(String.format(ATTENDANCE_BASE_SETTING_CACHE_KEY,UserProvider.getUser().getTenantId()));
        // 指定成员
        if (2 == attendanceBaseSettingDto.getFilePersonnelType()) {
            // 保存外勤人员列表 V1.8.1 版本新增
            attendanceFieldPersonnelMapper.deleteByGroupId(attendanceBaseSettingDto.getGroupId());
            if (StringUtil.isNotEmpty(attendanceBaseSettingDto.getFieldPersonnelIds())) {
                List<AttendanceFieldPersonnel> fieldPersonnelList = new ArrayList<>();
                attendanceBaseSettingDto.getFieldPersonnelIds().forEach(v -> {
                    AttendanceFieldPersonnel attendanceFieldPersonnel = new AttendanceFieldPersonnel();
                    attendanceFieldPersonnel.setId(FtbUtil.getId());
                    attendanceFieldPersonnel.setGroupId(attendanceBaseSettingDto.getGroupId());
                    attendanceFieldPersonnel.setUserId(v);
                    fieldPersonnelList.add(attendanceFieldPersonnel);
                });
                attendanceFieldPersonnelMapper.insertList(fieldPersonnelList);
            }
        }
    }
    @Override
    public void hisBaseSetting(Map<String, String> group2orgMap, Map<String, String> org2groupMap){
        List<AttendanceBaseSetting> list = lambdaQuery().eq(AttendanceBaseSetting::getDeleteMark, Boolean.FALSE).list();
        list.forEach(item->item.setGroupId(org2groupMap.getOrDefault(group2orgMap.getOrDefault(item.getGroupId(),item.getGroupId()),item.getGroupId())));
        list.stream().collect(Collectors.groupingBy(AttendanceBaseSetting::getGroupId)).forEach((key,value)->{
            value.stream().skip(1).forEach(vo->vo.setDeleteMark(1));
        });
        updateBatchById(list);
        redisTemplate.delete(String.format(ATTENDANCE_BASE_SETTING_CACHE_KEY,UserProvider.getUser().getTenantId()));
    }

    private void sendNotice(String groupId, Object oldValue, Object newValue, AttendanceNoticeEnum attendanceNoticeEnum) {
        if (Objects.equals(oldValue, newValue)) {
            return;
        }
        List<String> selfAndChildrenGroupIds = attendanceGroupService.getSelfAndChildrenGroupIds(groupId, 1);
        selfAndChildrenGroupIds.forEach(itemGroupId -> sendNoticeItem(itemGroupId, oldValue, newValue, attendanceNoticeEnum));
    }

    private void sendNoticeItem(String groupId, Object oldValue, Object newValue, AttendanceNoticeEnum attendanceNoticeEnum) {
        RuleChangeNoticeModel ruleChangeNoticeModel = new RuleChangeNoticeModel();
        ruleChangeNoticeModel.setIsAdmin(Boolean.FALSE);
        ruleChangeNoticeModel.setOldValue(oldValue);
        ruleChangeNoticeModel.setNewValue(newValue);
        ruleChangeNoticeModel.setGroupId(groupId);
        ruleChangeNoticeModel.setTenantId(UserProvider.getUser().getTenantId());
        ruleChangeNoticeModel.setAttendanceNoticeEnum(attendanceNoticeEnum);
        attendanceNoticeHandler.send(ruleChangeNoticeModel);
        ruleChangeNoticeModel.setIsAdmin(Boolean.TRUE);
        attendanceNoticeHandler.send(ruleChangeNoticeModel);
    }

    @Override
    public AttendanceBaseSettingVo getOne(String groupId) {
        // 处理新增字段 内勤拍照
        Map<String, AttendanceBaseSetting> enableBaseSetting = getEnableBaseSetting(CollUtil.newArrayList(groupId));
        AttendanceBaseSetting attendanceBaseSetting = enableBaseSetting.get(groupId);
        if (Objects.isNull(attendanceBaseSetting)) {
            return null;
        }
        AttendanceBaseSettingVo bean = BeanUtil.toBean(attendanceBaseSetting, AttendanceBaseSettingVo.class);
        //  非超级管理员不能查看出勤换算比
        UserInfo userInfo = userProvider.get();
        Boolean isAdmin = attendanceSuperAdminService.isSuperAdmin(userInfo.getUserId());
        if (isAdmin || userProvider.get().getIsAdministrator()) {
            bean.setAttendanceRatioType(true);
        }
        // 处理新增字段 外勤人员问题，当继承上级设置时且允许外勤打开，外勤人员列表默认为全员
        // 获取选中考勤组的basesetting状态  关闭情况下 为继承上级
        AttendanceGroup attendanceGroup = attendanceGroupService.getGroupSetting(groupId);
        // 本组全员（不含借调）
        List<AttendanceGroupUserVo> groupUserList = attendanceUserService.getGroupUserList(groupId, Boolean.FALSE);
        List<AttendanceGroupUserVo> list = new ArrayList<>();
        List<String> collect = groupUserList.stream().map(AttendanceGroupUserVo::getUserId).collect(Collectors.toList());
        // 是否打开自定义
        if (null != attendanceGroup && 0 == attendanceGroup.getBaseSetting()){
            // 关闭继承上级  上级外勤打开 且设置为全部 子集展示为全部  父级设置为指定，子级默认为空
            if (1 == bean.getFieldClockStatus() && 1 == bean.getFilePersonnelType() ){
                setUser(list, collect);
                bean.setFieldPersonnellist(list);
            }
        } else {
            // 打开自定义 查自己外勤人员列表配置
            // 选择为全部
            // 查询本组绑定的外勤人员
            List<String> userIds = attendanceFieldPersonnelMapper.getUserIdsByGroupId(groupId);
            // 去除不在本组中的成员
            userIds.retainAll(collect);
            setUser(list,userIds);
            bean.setFieldPersonnellist(list);
        }
        return bean;
    }

    /**
     * 获取用户信息集合
     * @param list 用户信息
     * @param userIds 用户ids
     */
    private void setUser(List<AttendanceGroupUserVo> list,List<String> userIds) {
            ActionResult<List<UserBoundVO>> userList = v2UserApi.getUserPrimaryBoundBatch(userIds, null);
            if (200 == userList.getCode() && CollectionUtil.isNotEmpty(userList.getData())) {
                for (UserBoundVO userPrimaryPositionDetailVO : userList.getData()) {
                    AttendanceGroupUserVo groupUserVo = new AttendanceGroupUserVo();
                    groupUserVo.setUserId(userPrimaryPositionDetailVO.getId());
                    groupUserVo.setRealName(userPrimaryPositionDetailVO.getUserName());
                    groupUserVo.setHeadIcon(UploaderUtil.uploaderImg(userPrimaryPositionDetailVO.getHeadIcon()));
                    groupUserVo.setPositionName(userPrimaryPositionDetailVO.getPositionName());
                    list.add(groupUserVo);
                }
            }
    }


    @Override
    public void changeStatus(String groupId, Integer enable, AttendanceBaseSettingVo attendanceBaseSetting) {
        lambdaUpdate().eq(AttendanceBaseSetting::getGroupId, groupId)
                .set(AttendanceBaseSetting::getEnable, enable)
                .update();
        AttendanceBaseSetting attendanceBaseSettingEntity = getEnableBaseSetting(CollUtil.newArrayList(groupId)).get(groupId);
        redisTemplate.delete(String.format(ATTENDANCE_BASE_SETTING_CACHE_KEY,UserProvider.getUser().getTenantId()));
        if(Objects.isNull(attendanceBaseSetting) || Objects.isNull(attendanceBaseSettingEntity)){
            return;
        }
        sendNotice(groupId, attendanceBaseSetting.getFieldClockStatus(), attendanceBaseSettingEntity.getFieldClockStatus(), AttendanceNoticeEnum.RULE_CHANGE_FIELD);
        sendNotice(groupId, attendanceBaseSetting.getPatchClockStatus(), attendanceBaseSettingEntity.getPatchClockStatus(), AttendanceNoticeEnum.RULE_CHANGE_CARD_MAKEUP);
    }

    @Override
    public Map<String, AttendanceBaseSetting> getEnableBaseSetting(List<String> groupIds) {
        Map<String, AttendanceBaseSetting> collect =  getStringAttendanceBaseSettingMap();
        return groupIds.stream().distinct().collect(Collectors.toMap(Function.identity(), collect::get));
    }

    @NotNull
    private Map<String, AttendanceBaseSetting> getStringAttendanceBaseSettingMap() {
        String key = String.format(ATTENDANCE_BASE_SETTING_CACHE_KEY, StringUtil.isEmpty(UserProvider.getUser().getTenantId()) ? DataSourceContextHolder.getDatasourceId() : UserProvider.getUser().getTenantId());
        if(redisTemplate.hasKey(key)){
            String object = redisTemplate.opsForValue().get(key);
            return Objects.requireNonNull(com.alibaba.fastjson.JSONObject.parseObject(object, new TypeReference<>() {
            }));
        }
        List<AttendanceGroup> attendanceGroupVos = attendanceGroupService.queryDropList();
        if (CollUtil.isEmpty(attendanceGroupVos)) {
            log.error("未查询到考勤组信息");
            return Maps.newHashMap();
        }
        Map<String, String> groupIdToParent = attendanceGroupVos.stream().collect(Collectors.toMap(AttendanceGroup::getId, groupVo -> StringUtil.isBlank(groupVo.getParentId()) ? "-1" : groupVo.getParentId()));
        Map<String, Boolean> collect1 = attendanceGroupVos.stream().collect(Collectors.toMap(AttendanceGroup::getId, group -> group.getBaseSetting() == 1));
        List<AttendanceBaseSetting> list = lambdaQuery()
                .eq(AttendanceBaseSetting::getDeleteMark, Boolean.FALSE)
                .orderByDesc(AttendanceBaseSetting::getCreatorTime)
                .list();
        Map<String, AttendanceBaseSetting> baseSettingMap = list.stream().filter(base -> collect1.getOrDefault(base.getGroupId(), Boolean.FALSE)).collect(Collectors.toMap(AttendanceBaseSetting::getGroupId, baseSetting -> baseSetting, (b1, b2) -> b2));
        // ========== 优化4：结果映射 ==========
        Map<String, AttendanceBaseSetting> result = new HashMap<>(collect1.size());
        for (Map.Entry<String, Boolean> entry : collect1.entrySet()) {
            result.put(entry.getKey(), findNextBaseSetting(baseSettingMap, groupIdToParent, entry.getKey()));
        }
        redisTemplate.opsForValue().set(key, com.alibaba.fastjson.JSONObject.toJSONString(result));
        return result;
    }

    @Override
    public Map<String, AttendanceBaseSetting> getEnableBaseSettingAll(List<String> groupIds) {
        List<AttendanceGroup> attendanceGroupVos = attendanceGroupService.queryAllListByIds();
        if (CollUtil.isEmpty(attendanceGroupVos)) {
            log.error("未查询到考勤组信息");
            return Maps.newHashMap();
        }
        Map<String, String> groupIdToParent = attendanceGroupVos.stream().collect(Collectors.toMap(AttendanceGroup::getId, groupVo -> StringUtil.isBlank(groupVo.getParentId()) ? "-1" : groupVo.getParentId()));
        Map<String, Boolean> collect1 = attendanceGroupVos.stream().collect(Collectors.toMap(AttendanceGroup::getId, group -> group.getBaseSetting() == 1));
        List<AttendanceBaseSetting> list = lambdaQuery().orderByDesc(AttendanceBaseSetting::getCreatorTime).list();
        Map<String, AttendanceBaseSetting> collect = list.stream().filter(base -> collect1.getOrDefault(base.getGroupId(), Boolean.FALSE)).collect(Collectors.toMap(AttendanceBaseSetting::getGroupId, baseSetting -> baseSetting, (b1, b2) -> b2));
        Map<String, AttendanceBaseSetting> map = Maps.newHashMap();
        groupIds.forEach(groupId -> map.put(groupId, findNextBaseSetting(collect, groupIdToParent, groupId)));
        return map;
    }


    @Override
    public Map<String, AttendanceBaseSetting> getEnableBaseSetting(List<String> groupIds, List<AttendanceGroup> attendanceGroupVos) {
        List<AttendanceBaseSetting> list = lambdaQuery()
                .eq(AttendanceBaseSetting::getDeleteMark, Boolean.FALSE)
                .orderByDesc(AttendanceBaseSetting::getCreatorTime)
                .list();
        Map<String, Boolean> collect1 = attendanceGroupVos.stream().collect(Collectors.toMap(AttendanceGroup::getId, group -> group.getBaseSetting() == 1));
        Map<String, AttendanceBaseSetting> collect = list.stream().filter(base -> collect1.getOrDefault(base.getGroupId(), Boolean.FALSE)).collect(Collectors.toMap(AttendanceBaseSetting::getGroupId, baseSetting -> baseSetting, (b1, b2) -> b2));
        Map<String, AttendanceBaseSetting> map = Maps.newHashMap();
        Map<String, String> groupIdToParent = attendanceGroupVos.stream().collect(Collectors.toMap(AttendanceGroup::getId, groupVo -> StringUtil.isBlank(groupVo.getParentId()) ? "-1" : groupVo.getParentId()));
        groupIds.forEach(groupId -> map.put(groupId, findNextBaseSetting(collect, groupIdToParent, groupId)));
        return map;
    }

    @Override
    public void initBaseSetting(String groupId) {
        List<AttendanceBaseSetting> list = lambdaQuery().eq(AttendanceBaseSetting::getGroupId, groupId)
                .eq(AttendanceBaseSetting::getDeleteMark, Boolean.FALSE)
                .list();
        if (CollUtil.isNotEmpty(list)) {
            return;
        }
        Map<String, AttendanceBaseSetting> enableBaseSetting = getEnableBaseSetting(CollUtil.newArrayList(groupId));
        AttendanceBaseSetting attendanceBaseSetting = enableBaseSetting.get(groupId);
        if (Objects.nonNull(attendanceBaseSetting)) {
            attendanceBaseSetting.setId(RandomUtil.uuId());
            attendanceBaseSetting.setGroupId(groupId);
            attendanceBaseSetting.setCreatorTime(new Date());
            attendanceBaseSetting.setCreatorUserId(UserProvider.getLoginUserId());
            save(attendanceBaseSetting);
        }
    }

    @Override
    public AttendanceBaseSetting findBaseSetting(String groupId) {
        return lambdaQuery()
                .eq(AttendanceBaseSetting::getDeleteMark, Boolean.FALSE)
                .eq(AttendanceBaseSetting::getGroupId, groupId)
                .orderByDesc(AttendanceBaseSetting::getCreatorTime)
                .last("limit 1")
                .one();
    }

    @Override
    public JSONArray getTakePhotoSetting(UserInfo userInfo) throws QueryException {

        // 根据当前时间用户所在考勤组查询配置
        GroupInfoVo groupInfo = attendanceClockInService.getGroupInfoByDate(new Date(), userInfo);
        JSONArray array = new JSONArray();
        JSONObject json = new JSONObject();
        Integer shouldTakePhoto = groupInfo.getGroupRule().getShouldTakePhoto();
        json.set("state", shouldTakePhoto);
        json.set("name", shouldTakePhoto.equals(ConstantUtil.NUM_TRUE) ? "是" : "否");
        array.put(json);
        return array;
    }

    @Override
    public Integer getNowGroupAttendancePhoto() {
        List<AttendanceGroupUser> attendanceGroupUsers = attendanceUserService.getAttendanceGroupUsersOfSecondment(new Date(), new Date(), List.of(userProvider.get().getUserId()), null);
        if (CollUtil.isNotEmpty(attendanceGroupUsers)) {
            // 注意可能会继承上级配置
            Map<String, AttendanceBaseSetting> enableBaseSetting = getEnableBaseSetting(CollUtil.newArrayList(attendanceGroupUsers.get(0).getGroupId()));
            AttendanceBaseSetting attendanceBaseSetting = enableBaseSetting.get(attendanceGroupUsers.get(0).getGroupId());
            if (Objects.isNull(attendanceBaseSetting)) {
                return 0;
            }
            return 1 == attendanceBaseSetting.getFace() || 1 == attendanceBaseSetting.getAttendancePhoto() ? 1 : 0;
        }
        return 0;
    }

    @Override
    public boolean getFieldClockStatusSelf(AttendanceBaseSetting setting) {
        if (null == setting) {
            return false;
        }
        // 传入考勤组全员（不含借调）
        List<AttendanceGroupUserVo> groupUserList = attendanceUserService.getGroupUserList(setting.getGroupId(), Boolean.FALSE);
        List<String> collect = groupUserList.stream().map(AttendanceGroupUserVo::getUserId).collect(Collectors.toList());
        if (ConstantUtil.NUM_FALSE == setting.getFieldClockStatus()) {
            return false;
        } else {
            if (1 == setting.getFilePersonnelType()) {
                return true;
            } else {
                // 查询本组绑定的外勤人员
                List<String> userIds = attendanceFieldPersonnelMapper.getUserIdsByGroupId(setting.getGroupId());
                // 去除不在本组中的成员
                userIds.retainAll(collect);
                return userIds.contains(userProvider.get().getUserId());
            }
        }
    }

    @Override
    public QuickCheckInVo quickCheckIn() {
        QuickCheckInVo quickCheckInVo = new QuickCheckInVo();
        // 用户上下班打卡信息
        List<UserSettingVo> settingList = attendanceUserSettingService.getSettingList(CollUtil.newArrayList(userProvider.get().getUserId()), 1, Stream.of(UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode(),
                        UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode())
                .collect(Collectors.toList()));
        Optional<UserSettingVo> startOpt = settingList.stream()
                .filter(v -> UserSettingEnum.ATTENDANCE_SETTING_START_SPEED_CHECK.getCode().equals(v.getCode()))
                .findFirst();
        quickCheckInVo.setQuickGoToWork(startOpt.map(UserSettingVo::getStatus).orElse(0));

        Optional<UserSettingVo> endOpt = settingList.stream()
                .filter(v -> UserSettingEnum.ATTENDANCE_SETTING_END_SPEED_CHECK.getCode().equals(v.getCode()))
                .findFirst();
        quickCheckInVo.setQuickOffWork(endOpt.map(UserSettingVo::getStatus).orElse(0));

        List<AttendanceGroupUser> attendanceGroupUsers = attendanceUserService.getAttendanceGroupUsersOfSecondment(new Date(), new Date(), List.of(userProvider.get().getUserId()), null);
        if (CollUtil.isNotEmpty(attendanceGroupUsers)) {
            // 注意可能会继承上级配置
            Map<String, AttendanceBaseSetting> enableBaseSetting = getEnableBaseSetting(CollUtil.newArrayList(attendanceGroupUsers.get(0).getGroupId()));
            AttendanceBaseSetting attendanceBaseSetting = enableBaseSetting.get(attendanceGroupUsers.get(0).getGroupId());
            if (Objects.isNull(attendanceBaseSetting)) {
                quickCheckInVo.setQuickGoToWork(0);
                quickCheckInVo.setQuickOffWork(0);
            } else {
                quickCheckInVo.setQuickGoToWork(( 1 == attendanceBaseSetting.getFace() || 1 == attendanceBaseSetting.getAttendancePhoto()) ? 0 : quickCheckInVo.getQuickGoToWork());
                quickCheckInVo.setQuickOffWork(( 1 == attendanceBaseSetting.getFace() || 1 == attendanceBaseSetting.getAttendancePhoto()) ? 0 : quickCheckInVo.getQuickOffWork());
            }
        }
        return quickCheckInVo;
    }

    @Override
    public boolean getNeedFace(String groupId) {
        Map<String, AttendanceBaseSetting> enableBaseSetting = getEnableBaseSetting(CollUtil.newArrayList(groupId));
        AttendanceBaseSetting attendanceBaseSetting = enableBaseSetting.get(groupId);
        if (Objects.isNull(attendanceBaseSetting)) {
            return false;
        }
        return 1 == attendanceBaseSetting.getFace();
    }

    /**
     * 获取考勤组下个考勤组配置
     *
     * @param collect 考勤组配置
     * @param groupIdToParent 考勤组层级关系
     * @param groupId 考勤组id
     * @return 考勤组配置
     */
    private AttendanceBaseSetting findNextBaseSetting(Map<String, AttendanceBaseSetting> collect, Map<String, String> groupIdToParent, String groupId) {
        if (StringUtil.isBlank(groupId)) {
            return null;
        }
        if (!collect.containsKey(groupId)) {
            return findNextBaseSetting(collect, groupIdToParent, groupIdToParent.get(groupId));
        }
        return collect.get(groupId);
    }
}
