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

import com.hz.pm.api.common.helper.RegionCacheHelper;
import com.hz.pm.api.common.helper.basic.AbstractRegionLimitHelper;
import com.hz.pm.api.expert.service.IExpertUserFullInfoService;
import com.hz.pm.api.meta.model.bo.RegionContainsBO;
import com.hz.pm.api.sys.service.IRegionService;
import com.hz.pm.api.user.service.IUserInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * @author liuxinxin
 * @date 2022/8/1 下午2:27
 */
@Component
public class RegionLimitHelperImpl extends AbstractRegionLimitHelper {

    public RegionLimitHelperImpl(RegionCacheHelper regionCache, IExpertUserFullInfoService expertUserFullInfoService
            , IUserInfoService iUserInfoService, IRegionService regionService) {
        super(regionCache, expertUserFullInfoService, iUserInfoService, regionService);
    }

    public static Boolean contains(Integer adminRegionLevel, List<String> adminAllContainsRegionCodes
            , Integer expertRegionLevel, String expertRegionCode) {
        for (String adminAllContainsRegionCode : adminAllContainsRegionCodes) {
            if (adminAllContainsRegionCode.equals(expertRegionCode) && adminRegionLevel >= expertRegionLevel) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean regionContains(List<RegionContainsBO> regionContainsBOList, String regionCode, Integer regionLevel) {
        for (RegionContainsBO regionContainsBO : regionContainsBOList) {
            Integer parentRegionTreeLevel = regionContainsBO.getParentRegionTreeLevel();
            List<String> containsRegionCodeList = regionContainsBO.getContainsRegionCodeList();
            for (String containsRegionCode : containsRegionCodeList) {
                if (containsRegionCode.equals(regionCode) && parentRegionTreeLevel <= regionLevel) {
                    return true;
                }
            }
        }
        return false;
    }


    @Override
    public List<RegionContainsBO> queryContainsRegionAssembler(RegionContainsBO queryRegionContainBo, List<RegionContainsBO> expertAdminContainsRegionList) {
        List<RegionContainsBO> resultBoList = new ArrayList<>();
        if (Objects.nonNull(queryRegionContainBo)) {
            List<String> containsRegionCodeList = queryRegionContainBo.getContainsRegionCodeList();
            // 查询取并级
            if (CollectionUtils.isNotEmpty(expertAdminContainsRegionList)) {
                for (RegionContainsBO regionContainsBO : expertAdminContainsRegionList) {
                    List<String> regionCodeList = regionContainsBO.getContainsRegionCodeList();
                    regionCodeList.retainAll(containsRegionCodeList);
                    if (CollectionUtils.isNotEmpty(regionCodeList)) {
                        regionContainsBO.setContainsRegionCodeList(regionCodeList);
                        resultBoList.add(regionContainsBO);
                    }
                }
            }
            return resultBoList;
        } else {
            return expertAdminContainsRegionList;
        }
    }

    @Override
    public RegionContainsBO getContainsRegionBo(Integer regionLevel, String regionCode) {
        Collection<String> regionCodes = regionCache.listChildRegionCodeList(regionCode, regionLevel);
        RegionContainsBO regionContains = new RegionContainsBO();
        regionContains.setContainsRegionCodeList(new ArrayList<>(regionCodes));
        regionContains.setParentRegionTreeLevel(regionLevel);
        return regionContains;
    }

}
