package com.hz.pm.api.common.helper.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.common.helper.UserInfoHelper;
import com.hz.pm.api.common.util.StrUtils;
import com.hz.pm.api.sys.mapper.RoleMapper;
import com.hz.pm.api.sys.model.entity.Role;
import com.hz.pm.api.sys.model.entity.UserRole;
import com.hz.pm.api.sys.service.IUserRoleService;
import com.hz.pm.api.user.model.entity.UserInfo;
import com.hz.pm.api.user.model.enumeration.RoleEnum;
import com.hz.pm.api.user.model.enumeration.UserAvailableEnum;
import com.hz.pm.api.user.security.model.UserFullInfoDTO;
import com.hz.pm.api.user.service.IUserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2023/2/10 下午4:40
 */
@Component
@RequiredArgsConstructor
public class UserInfoHelperImpl implements UserInfoHelper {


    private final IUserInfoService userInfoService;
    private final IUserRoleService iUserRoleService;
    private final RoleMapper roleMapper;

    @Override
    public UserFullInfoDTO getUserFullInfoByMhUserIdOrOpenId(String mhUserId) {
        LambdaQueryWrapper<UserInfo> query = Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getMhUserId, mhUserId)
                .or(q1 -> q1.eq(UserInfo::getWechatOpenId, mhUserId));
        UserInfo userInfo = userInfoService.getOne(query);
        if (Objects.isNull(userInfo)) {
            return null;
        }
        // 返回用户全量信息
        return getUserFullInfo(userInfo);
    }

    @Override
    public UserFullInfoDTO getUserFullInfo(Long userId) {
        UserInfo userInfo = userInfoService.getById(userId);
        if (Objects.isNull(userInfo)) {
            return null;
        }
        // 返回用户全量信息
        return getUserFullInfo(userInfo);
    }

    @Override
    public UserFullInfoDTO getUserFullInfo(UserInfo userInfo) {
        UserFullInfoDTO userFullInfo = new UserFullInfoDTO();
        // 获取用户角色列表信息
        List<UserRole> userRoleList = iUserRoleService.listByUserId(userInfo.getId());
        if (CollUtil.isNotEmpty(userRoleList)) {
            List<Long> roleIdList = userRoleList.stream()
                    .map(UserRole::getRoleId).distinct()
                    .collect(Collectors.toList());
            userFullInfo.setUserRoleList(roleMapper.selectBatchIds(roleIdList));
        }

        // 装配返回
        userFullInfo.setUserId(userInfo.getId());
        userFullInfo.setMhUserId(userInfo.getMhUserId());
        userFullInfo.setIdentifier(userInfo.getRealName());
        userFullInfo.setRealName(userInfo.getRealName());
        userFullInfo.setMhUnitId(userInfo.getMhUnitId());
        userFullInfo.setMhUnitName(userInfo.getMhUnitName());
        userFullInfo.setUsername(userInfo.getRealName());
        userFullInfo.setMobile(userInfo.getMobile());
        userFullInfo.setAccountId(userInfo.getAccountId());
        String available = userInfo.getAvailable();
        if (StringUtils.isNotBlank(available)) {
            userFullInfo.setAvailable(UserAvailableEnum.valueOf(available));
        }
        return userFullInfo;
    }

    /**
     * 批量查询 这里 会没有角色
     *
     * @param userInfos \
     * @return \
     */
    @Override
    public List<UserFullInfoDTO> getUserFullInfos(List<UserInfo> userInfos) {
        if (CollUtil.isEmpty(userInfos)) {
            return Collections.emptyList();
        }
        return userInfos.stream().map(userInfo -> {
            UserFullInfoDTO userFullInfo = new UserFullInfoDTO();
            // 装配返回
            userFullInfo.setMhUnitName(userInfo.getMhUnitName());
            userFullInfo.setMhUnitId(userInfo.getMhUnitId());
            userFullInfo.setUserId(userInfo.getId());
            userFullInfo.setIdentifier(userInfo.getRealName());
            userFullInfo.setRealName(userInfo.getRealName());
            userFullInfo.setUsername(userInfo.getRealName());
            userFullInfo.setMobile(userInfo.getMobile());
            userFullInfo.setAccountId(userInfo.getAccountId());
            String available = userInfo.getAvailable();
            if (StrUtils.isNotBlank(available)) {
                userFullInfo.setAvailable(UserAvailableEnum.valueOf(available));
            }
            return userFullInfo;
        }).collect(Collectors.toList());
    }

    @Override
    public String getUserName(Long userId) {
        UserFullInfoDTO userFullInfo = getUserFullInfo(userId);
        return userFullInfo.getRealName();
    }

    @Override
    public boolean isSuperOrRegionAdmin(Long userId) {
        UserFullInfoDTO userFullInfo = getUserFullInfo(userId);
        if (CollUtil.isNotEmpty(userFullInfo.getUserRoleList())) {
            for (Role role : userFullInfo.getUserRoleList()) {
                if (RoleEnum.SUPER_ADMIN.name().equals(role.getCode()) ||
                        RoleEnum.REGION_MANAGER.name().equals(role.getCode())) {
                    return Boolean.TRUE;
                }
            }
        }
        return Boolean.FALSE;
    }

    @Override
    public List<UserFullInfoDTO> listUserFullInfoByUserIds(Collection<Long> userIds) {
        List<UserInfo> userInfos = userInfoService.listByIds(userIds);
        return getUserFullInfos(userInfos);
    }

}
