package com.ningdatech.pmapi.common.helper.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.common.helper.RegionCacheHelper;
import com.ningdatech.pmapi.common.helper.basic.AbstractRegionLimitHelper;
import com.ningdatech.pmapi.expert.service.IExpertUserFullInfoService;
import com.ningdatech.pmapi.meta.model.bo.RegionContainsBO;
import com.ningdatech.pmapi.sys.model.entity.Region;
import com.ningdatech.pmapi.sys.service.IRegionService;
import com.ningdatech.pmapi.user.entity.UserInfo;
import com.ningdatech.pmapi.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 {

    private static final Logger logger = LoggerFactory.getLogger(RegionLimitHelperImpl.class);

    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;
    }


    @Override
    public List<RegionContainsBO> getExpertAdminContainsRegion(Long expertAdminUserId) {
        if (Objects.isNull(expertAdminUserId)) {
            logger.error("getExpertAdminContainsRegion expertAdminUserId is null");
            return new ArrayList<>();
        }

        UserInfo userInfo = iUserInfoService.getById(expertAdminUserId);

        if (Objects.isNull(userInfo) || StringUtils.isBlank(userInfo.getRegionCode())) {
            logger.error("getExpertAdminContainsRegion userInfo is null Or regionCode is null");
            return new ArrayList<>();
        }

        String regionCode = userInfo.getRegionCode();
        List<Region> regionList = regionService.list(Wrappers.lambdaQuery(Region.class)
                .eq(Region::getRegionCode, regionCode)
                .orderByDesc(Region::getRegionLevel));

        Region region = regionList.get(0);
        Collection<String> regionCodes = regionCache
                .listChildRegionCodeList(region.getRegionCode(), region.getRegionLevel());
        RegionContainsBO regionContainsBO = new RegionContainsBO();
        regionContainsBO.setContainsRegionCodeList(new ArrayList<>(regionCodes));
        regionContainsBO.setParentRegionTreeLevel(region.getRegionLevel());

        ArrayList<RegionContainsBO> regionContainsBOS = CollectionUtil.toList(regionContainsBO);

        return regionContainsBOS;
    }


}
