package com.cool.store.dao;

import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.openPreparation.UserNameDTO;
import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.mapper.EnterpriseUserMapper;
import com.cool.store.utils.StringUtil;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author zhangchenbiao
 * @date 2023-05-19 02:58
 */
@Repository
public class EnterpriseUserDAO {

    @Resource
    private EnterpriseUserMapper enterpriseUserMapper;


    public EnterpriseUserDO getUserInfoById(String userId){
        if(StringUtils.isBlank(userId)){
            return null;
        }
        return enterpriseUserMapper.getUserInfoById( userId);
    }

    public EnterpriseUserDO getUserInfoByJobnumber(String jobnumber){
        if(StringUtils.isBlank(jobnumber)){
            return null;
        }
        return enterpriseUserMapper.getUserInfoByJobnumber(jobnumber);
    }

    public EnterpriseUserDO getUserInfoByThirdOaUniqueFlag(String thirdOaUniqueFlag){
        if(StringUtils.isBlank(thirdOaUniqueFlag)){
            return null;
        }
        return enterpriseUserMapper.getUserInfoByThirdOaUniqueFlag(thirdOaUniqueFlag);
    }

    public List<EnterpriseUserDO> getUserInfoByUserIds(List<String> userIdList){
        if(CollectionUtils.isEmpty(userIdList)){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.getUserInfoByUserIds( userIdList);
    }

    public List<EnterpriseUserDO> searchUserByRegionIdsAndKeyword(List<String> regionIds, String keyword, List<String> leaderRegionIds){
        if(CollectionUtils.isEmpty(regionIds)){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.searchUserByRegionIdsAndKeyword( regionIds, keyword, leaderRegionIds);
    }

    public boolean isExistDeptUser(String regionId){
        if(StringUtils.isBlank(regionId)){
            return false;
        }
        return enterpriseUserMapper.getUserCountByRegionId( regionId) > CommonConstants.ZERO;
    }

    /**
     * 获取部门负责人
     * @param regionId
     * @return
     */
    public List<EnterpriseUserDO> getUserListByDeptLeader(String regionId){
        if(StringUtils.isBlank(regionId)){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.getUserListByDeptLeader( regionId);
    }

    public List<EnterpriseUserDO> getUserListByDeptLeaders(List<String> regionIds){
        if(CollectionUtils.isEmpty(regionIds)){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.getUserListByDeptLeaders( regionIds);
    }


    public List<EnterpriseUserDO> getUserListByRegionIds(List<String> regionIds){
        if(CollectionUtils.isEmpty(regionIds)){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.getUserListByRegionIds( regionIds);
    }

    public List<String> getUserIdListByRegionId(Long regionId, List<String> userIdList){
        if(CollectionUtils.isEmpty(userIdList) || regionId == null){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.getUserIdListByRegionId(regionId, userIdList);
    }

    public Map<String, String> getUserNameAndMobile(List<String> userIds){
        List<EnterpriseUserDO> userList = getUserInfoByUserIds(userIds);
        return userList.stream().filter(o->!StringUtils.isAnyBlank(o.getMobile())).collect(Collectors.toMap(k -> k.getUserId(), v -> v.getName() + " " + v.getMobile()));
    }

    public Map<String, String> getUserNameMap(List<String> userIds){
        List<EnterpriseUserDO> userList = getUserInfoByUserIds(userIds);
        return userList.stream().filter(o->!StringUtils.isAnyBlank(o.getName())).collect(Collectors.toMap(k -> k.getUserId(), v -> v.getName()));
    }

    public Map<String, EnterpriseUserDO> getUserMap(List<String> userIds){
        List<EnterpriseUserDO> userList = getUserInfoByUserIds(userIds);
        return userList.stream().filter(o->!StringUtils.isAnyBlank(o.getMobile(), o.getName())).collect(Collectors.toMap(k -> k.getUserId(), Function.identity()));
    }

    public Map<String, EnterpriseUserDO> getUserInfoMap(List<String> userIds){
        List<EnterpriseUserDO> userList = getUserInfoByUserIds(userIds);
        return userList.stream().collect(Collectors.toMap(k -> k.getUserId(), Function.identity()));
    }

    public EnterpriseUserDO selectByMobile(String mobile) {
        return  enterpriseUserMapper.selectByMobile( mobile);
    }

    public String getUserName(String userId){
        EnterpriseUserDO userInfo = getUserInfoById(userId);
        return Optional.ofNullable(userInfo).map(EnterpriseUserDO::getName).orElse(StringUtils.EMPTY);
    }

    public EnterpriseUserDO selectByInvestmentManager(String investmentManager) {
        if (StringUtil.isEmpty(investmentManager)) {
            return null;
        }
        return enterpriseUserMapper.selectByInvestmentManager( investmentManager);
    }
    public List<UserNameDTO> getNameByUserId(List<String> userIdList){
        if (CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>();
        }
        return enterpriseUserMapper.selectNameByUserId(userIdList);
    }

    public List<EnterpriseUserDO> getUserListByRegionId(Long regionId){
        if(regionId == null){
            return Lists.newArrayList();
        }
        return enterpriseUserMapper.getUserListByRegionId(regionId);
    }
}