package com.hz.pm.api.organization.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hz.pm.api.organization.mapper.DingOrganizationMapper;
import com.hz.pm.api.organization.model.entity.DingEmployeeInfo;
import com.hz.pm.api.organization.model.entity.DingOrganization;
import com.hz.pm.api.organization.service.IDingOrganizationService;
import com.ningdatech.basic.util.CollUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Lierbao
 * @since 2023-02-09
 */
@Service
public class DingOrganizationServiceImpl extends ServiceImpl<DingOrganizationMapper, DingOrganization> implements IDingOrganizationService {

    @Override
    public DingOrganization getByOrgCode(String orgCode) {
        if (StringUtils.isBlank(orgCode)) {
            return null;
        }
        return this.getOne(Wrappers.lambdaQuery(DingOrganization.class)
                .eq(DingOrganization::getOrganizationCode, orgCode));
    }

    @Override
    public DingOrganization getParentOrganization(String orgCode) {
        DingOrganization dingOrganization = getByOrgCode(orgCode);
        if (dingOrganization == null) {
            return null;
        }
        String parentCode = dingOrganization.getParentCode();
        return getByOrgCode(parentCode);
    }

    @Override
    public List<DingOrganization> listByCodes(Collection<String> codes) {
        if (CollectionUtil.isEmpty(codes)) {
            return new ArrayList<>();
        }
        LambdaQueryWrapper<DingOrganization> query = Wrappers.lambdaQuery(DingOrganization.class)
                .in(DingOrganization::getOrganizationCode, codes);
        return list(query);
    }

    @Override
    public List<String> listNameByCodes(Collection<String> codes) {
        LambdaQueryWrapper<DingOrganization> query = Wrappers.lambdaQuery(DingOrganization.class)
                .select(DingOrganization::getOrganizationName)
                .in(DingOrganization::getOrganizationCode, codes);
        return CollUtils.fieldList(list(query), DingOrganization::getOrganizationName);
    }

    @Override
    public Map<String, String> getOrgCodeAndNameMap(List<String> orgCodeList) {
        orgCodeList = orgCodeList.stream().distinct().collect(Collectors.toList());
        List<DingOrganization> dingOrganizationList = listByCodes(orgCodeList);
        return dingOrganizationList.stream()
                .filter(r -> StringUtils.isNotBlank(r.getOrganizationCode()) && StringUtils.isNotBlank(r.getOrganizationName()))
                .collect(Collectors.toMap(DingOrganization::getOrganizationCode, DingOrganization::getOrganizationName));
    }

    @Override
    public Map<String, DingOrganization> getOrgMap(List<String> orgCodeList) {
        orgCodeList = orgCodeList.stream().distinct().collect(Collectors.toList());
        if (CollectionUtil.isEmpty(orgCodeList)) {
            return new HashMap<>();
        }
        List<DingOrganization> dingOrganizationList = listByCodes(orgCodeList);
        return dingOrganizationList.stream()
                .filter(r -> StringUtils.isNotBlank(r.getOrganizationCode()))
                .collect(Collectors.toMap(DingOrganization::getOrganizationCode, r -> r));
    }

    @Override
    public Map<String, DingOrganization> getEmpCodeOrgMap(List<DingEmployeeInfo> employeeInfoList) {
        List<String> orgCodeList = employeeInfoList.stream()
                .map(DingEmployeeInfo::getEmpPosUnitCode)
                .distinct().collect(Collectors.toList());

        Map<String, DingOrganization> orgMap = this.getOrgMap(orgCodeList);

        Map<String, DingOrganization> empCodeOrgMap = new HashMap<>();
        for (DingEmployeeInfo dingEmployeeInfo : employeeInfoList) {
            DingOrganization dingOrganization = orgMap.get(dingEmployeeInfo.getEmpPosUnitCode());
            if (Objects.nonNull(dingOrganization)) {
                empCodeOrgMap.put(dingEmployeeInfo.getEmployeeCode(), dingOrganization);
            }
        }
        return empCodeOrgMap;
    }
}
