package cn.ey.biz.module.system.service.codeblock;

import cn.ey.biz.common.util.i18n.I18nContextHolder;
import cn.ey.biz.framework.mybatis.core.util.MyBatisUtils;
import cn.ey.biz.module.system.api.codeblock.dto.CodeBlockWideTableReqDTO;
import cn.ey.biz.module.system.controller.admin.codeblock.vo.*;
import cn.ey.biz.module.system.controller.engagement.code.vo.EngagementCodeRuleReqVOList;
import cn.ey.biz.module.system.dal.dataobject.codeblock.*;
import cn.ey.biz.module.system.dal.dataobject.permission.UserDataRoleDO;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.dal.mapper.codeblock.*;
import cn.ey.biz.module.system.dal.mapper.permission.RoleCodeblockMapper;
import cn.ey.biz.module.system.dal.mapper.permission.UserDataRoleMapper;
import cn.ey.biz.module.system.dal.mapper.user.AdminUserMapper;
import cn.ey.biz.module.system.enums.i18n.I18nLangEnum;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.ACCEPT_LANGUAGE_NOT_NULL;
@Service
@Slf4j
public class CodeBlockWideServiceImpl implements CodeBlockWideService {
    @Resource
    private CodeBlockMapper codeBlockMapper;
    @Resource
    private RoleCodeblockMapper roleCodeblockMapper;

    @Resource
    private MgmtRegionMapper mgmtRegionMapper;

    @Resource
    private ServiceLineMapper serviceLineMapper;

    @Resource
    private SubServiceLineMapper subServiceLineMapper;

    @Resource
    private CompetencyMapper competencyMapper;

    @Resource
    private SubMgmtUnitMapper subMgmtUnitMapper;

    @Resource
    private CountryRegionMapper countryRegionMapper;

    @Resource
    private CityCodeMapper cityCodeMapper;

    @Resource
    private BusinessUnitMapper businessUnitMapper;
    @Resource
    private MarketSegmentMapper marketSegmentMapper;
    @Resource
    private AdminUserMapper userMapper;
    @Resource
    UserDataRoleMapper userDataRoleMapper;



    @Override
    public CodeBlockWideTableVO queryCountryRegion(CodeBlockWideTableReqDTO blockingWideTableReq) {

        List<CodeBlockCodeValueVO> countryRegionCodeList = codeBlockMapper.selectDistinctCountryRegionCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> geoRegionCodeList = codeBlockMapper.selectDistinctGeoRegionCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> buCodeList = codeBlockMapper.selectDistinctBuCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> slCodeList = codeBlockMapper.selectDistinctSlCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> sslCodeList = codeBlockMapper.selectDistinctSslCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> competencyCodeList = codeBlockMapper.selectDistinctCompetencyCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> smuCodeList = codeBlockMapper.selectDistinctSmuCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> muCodeList = codeBlockMapper.selectDistinctMuCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> ouCodeList = codeBlockMapper.selectDistinctOuCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> legalEntityList = codeBlockMapper.selectDistinctLegalEntityList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> mrCodeList = codeBlockMapper.selectDistinctMrCodeList(blockingWideTableReq);
        List<CodeBlockCodeValueVO> areaCodeList = codeBlockMapper.selectDistinctAreaCodeList(blockingWideTableReq);
        return CodeBlockWideTableVO.builder()
                .legalEntity(legalEntityList)
                .countryRegionCode(countryRegionCodeList)
                .geoRegionCode(geoRegionCodeList)
                .buCode(buCodeList)
                .slCode(slCodeList)
                .sslCode(sslCodeList)
                .competencyCode(competencyCodeList)
                .smuCode(smuCodeList)
                .muCode(muCodeList)
                .ouCode(ouCodeList).mrCode(mrCodeList).areaCode(areaCodeList)
                .build();
    }

    @Override
    public CodeBlockWideTableVO queryCountryRegionForBack(EngagementCodeRuleReqVOList reqVOList) {
        List<CodeBlockCodeValueVO> smuCodeList = codeBlockMapper.selectDistinctSmuCodeBackList(reqVOList);
        List<CodeBlockCodeValueVO> muCodeList = codeBlockMapper.selectDistinctMuCodeBackList(reqVOList);
        List<CodeBlockCodeValueVO> ouCodeList = codeBlockMapper.selectDistinctOuCodeBackList(reqVOList);
        List<CodeBlockCodeValueVO> legalEntityList = codeBlockMapper.selectDistinctLegalEntityCodeBackList(reqVOList);
        List<CodeBlockCodeValueVO> buCodeList = codeBlockMapper.selectDistinctBuCodeBackList(reqVOList);
        List<CodeBlockCodeValueVO> slCodeList = codeBlockMapper.selectDistinctSlCodeBackList(reqVOList);
        List<CodeBlockCodeValueVO> sslCodeList = codeBlockMapper.selectDistinctSslCodeBackList(reqVOList);
        return CodeBlockWideTableVO.builder()
                .legalEntity(legalEntityList)
                .smuCode(smuCodeList)
                .muCode(muCodeList)
                .ouCode(ouCodeList)
                .buCode(buCodeList)
                .slCode(slCodeList)
                .sslCode(sslCodeList)
                .build();
    }

    @Override
    public IPage<CodeBlockDO> getDictDataPage(CodeBlockPageReqVO reqVO) {
        List<CodeBlockServiceLineVO> codeBlockServiceLineVOAll = new ArrayList<CodeBlockServiceLineVO>();
        List<List<String>> serviceLine = reqVO.getServiceLine();
        if (serviceLine != null && serviceLine.size() > 0) {
            for (List<String> serviceList : serviceLine) {
                CodeBlockServiceLineVO codeBlockServiceLineVO = new CodeBlockServiceLineVO();
                for (int i = 0; i < serviceList.size(); i++) {
                    String service = serviceList.get(i);

                    if (i == 0 && StringUtil.isNotBlank(service)) {
                        codeBlockServiceLineVO.setSl(service);
                    }
                    if (i == 1 && StringUtil.isNotBlank(service)) {
                        codeBlockServiceLineVO.setSsl(service);
                    }
                    if (i == 2 && StringUtil.isNotBlank(service)) {
                        codeBlockServiceLineVO.setCompetency(service);
                    }
                    if (i == 3 && StringUtil.isNotBlank(service)) {
                        codeBlockServiceLineVO.setSmu(service);
                    }
                }
                codeBlockServiceLineVOAll.add(codeBlockServiceLineVO);
            }
        }
        List<CodeBlockAreaVO> codeBlockAreaVOVOAll = new ArrayList<CodeBlockAreaVO>();
        List<List<String>> area = reqVO.getArea();
        if (area != null && area.size() > 0) {
            for (List<String> areaList : area) {
                CodeBlockAreaVO codeBlockAreaVO = new CodeBlockAreaVO();
                for (int i = 0; i < areaList.size(); i++) {
                    String service = areaList.get(i);
                    if (i == 0 && StringUtil.isNotBlank(service)) {
                        codeBlockAreaVO.setMarketSegmentCode(service);
                    }
                    if (i == 1 && StringUtil.isNotBlank(service)) {
                        codeBlockAreaVO.setCityCode(service);
                    }
                    if (i == 2 && StringUtil.isNotBlank(service)) {
                        codeBlockAreaVO.setBuCode(service);
                    }
                }
                codeBlockAreaVOVOAll.add(codeBlockAreaVO);
            }
        }
        IPage page = MyBatisUtils.buildPage(reqVO);
        return codeBlockMapper.filterListPage(page, reqVO, codeBlockServiceLineVOAll, codeBlockAreaVOVOAll);
    }

    @Override
    public List<CodeBlockUserGpnVO> getCodeblockGpn(String app) {
        List<CodeBlockDO> codeblockdos = codeBlockMapper.selectPageCodeBlockCode();
        List<String> codeBlocks = codeblockdos.stream().map(CodeBlockDO::getCodeBlockCode).collect(Collectors.toList());
//        List<RoleCodeblockDO> roleCodeBlockDOs = roleCodeblockMapper.selectList(new LambdaQueryWrapper<RoleCodeblockDO>()
//                .in(RoleCodeblockDO::getCodeBlock, codeBlocks));
        if (CollectionUtil.isNotEmpty(codeBlocks)) {
            List<CodeBlockUserGpnVO> codeBlockUserGpnVOLi = roleCodeblockMapper.getCodeLockUserGpnList(codeBlocks, app);
            return codeBlockUserGpnVOLi;
        }
        return null;
    }

    /**
     * 地区树查询
     *
     * @return
     */
    @Override
    public List<CodeBlockTreeValueVO> treeCityCodes() {
        //dict_business_unit
        //marketSegmentCode、cityCode、buCode
        String acceptLanguage = I18nContextHolder.getLangId();
        if (StringUtils.isEmpty(acceptLanguage)) {
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        QueryWrapper<BusinessUnitDO> businessUnitDOQueryWrapper = new QueryWrapper<>();
        businessUnitDOQueryWrapper.eq("deleted", false);
        businessUnitDOQueryWrapper.eq("status", 0);
        List<BusinessUnitDO> businessUnitDOS = businessUnitMapper.selectList(businessUnitDOQueryWrapper);
        if (CollectionUtil.isEmpty(businessUnitDOS)) {
            return null;
        }
        List<CodeBlockTreeValueVO> marketSegmentCodeTreeList = new ArrayList<>();
        List<String> marketSegmentCodeDistinct = businessUnitDOS.stream().sorted(Comparator.comparing(BusinessUnitDO::getBuId).reversed()).map(BusinessUnitDO::getMarketSegmentCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
        for (String marketSegmentCode : marketSegmentCodeDistinct) {
            CodeBlockTreeValueVO marketSegmentCodeTree = new CodeBlockTreeValueVO();
            if (StringUtils.isBlank(marketSegmentCode)) {
                continue;
            }
            List<BusinessUnitDO> collect = businessUnitDOS.stream().filter(c -> marketSegmentCode.equals(c.getMarketSegmentCode())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(collect)) {
                BusinessUnitDO marketSegmentCodeBlockDO = collect.get(0);
                marketSegmentCodeTree.setLabel(marketSegmentCodeBlockDO.getMarketSegmentName());
            }
            marketSegmentCodeTree.setValue(marketSegmentCode);
            List<CodeBlockTreeValueVO> cityCodeTreeList = new ArrayList<>();
            List<String> cityCodeDistinct = businessUnitDOS.stream().filter(c -> marketSegmentCode.equals(c.getMarketSegmentCode())).sorted(Comparator.comparing(BusinessUnitDO::getBuId).reversed()).map(BusinessUnitDO::getCityCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
            for (String cityCode : cityCodeDistinct) {
                if (StringUtils.isBlank(cityCode)) {
                    continue;
                }
                CodeBlockTreeValueVO cityCodeTree = new CodeBlockTreeValueVO();
                List<BusinessUnitDO> collect1 = businessUnitDOS.stream().filter(c -> cityCode.equals(c.getCityCode())).sorted(Comparator.comparing(BusinessUnitDO::getBuId).reversed()).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(collect1)) {
                    cityCodeTree.setLabel(collect1.get(0).getCityName());
                }
                cityCodeTree.setValue(cityCode);
                List<CodeBlockTreeValueVO> buCodeTreeList = new ArrayList<>();
                List<String> buCodeDistinct = businessUnitDOS.stream().filter(b -> marketSegmentCode.equals(b.getMarketSegmentCode())
                        && b.getCityCode().equals(cityCode)).sorted(Comparator.comparing(BusinessUnitDO::getBuId).reversed()).map(BusinessUnitDO::getBuCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(buCodeDistinct)) {
                    for (String buCode : buCodeDistinct) {
                        if (StringUtils.isBlank(buCode)) {
                            continue;
                        }
                        CodeBlockTreeValueVO buCodeTree = new CodeBlockTreeValueVO();
                        List<BusinessUnitDO> collect2 = businessUnitDOS.stream().filter(c -> buCode.equals(c.getBuCode())).collect(Collectors.toList());
                        if (CollectionUtil.isNotEmpty(collect2)) {
                            buCodeTree.setLabel(collect2.get(0).getBuName());
                            if (acceptLanguage.equals(I18nLangEnum.EN_US.getType())) {
                                buCodeTree.setLabel(collect2.get(0).getBuValue());
                            } else {
                                buCodeTree.setLabel(collect2.get(0).getBuName());
                            }

                        }
                        buCodeTree.setValue(buCode);
                        buCodeTree.setChildren(null);
                        buCodeTreeList.add(buCodeTree);
                    }
                }
                if (CollectionUtil.isNotEmpty(buCodeTreeList)) {
                    cityCodeTree.setChildren(buCodeTreeList);
                } else {
                    cityCodeTree.setChildren(null);
                }
                cityCodeTreeList.add(cityCodeTree);
            }
            if (CollectionUtil.isNotEmpty(cityCodeTreeList)) {
                marketSegmentCodeTree.setChildren(cityCodeTreeList);
            } else {
                marketSegmentCodeTree.setChildren(null);
            }
            marketSegmentCodeTreeList.add(marketSegmentCodeTree);
        }
        return marketSegmentCodeTreeList;
    }


    /**
     * 业务线树查询
     *
     * @return
     */
    @Override
    public List<CodeBlockTreeValueVO> treeCodeList() {
        //sl_code->ssl_code->competency_code->smu_code
        QueryWrapper<ServiceLineDO> serviceLineDOQueryWrapper = new QueryWrapper<>();
        serviceLineDOQueryWrapper.eq("deleted", false);
        serviceLineDOQueryWrapper.eq("status", 0);
        List<ServiceLineDO> serviceLineDOS = serviceLineMapper.selectList(serviceLineDOQueryWrapper);
        if (CollectionUtil.isEmpty(serviceLineDOS)) {
            return null;
        }
        QueryWrapper<SubServiceLineDO> subServiceLineDOQueryWrapper = new QueryWrapper<>();
        subServiceLineDOQueryWrapper.eq("deleted", false);
        subServiceLineDOQueryWrapper.eq("status", 0);
        List<SubServiceLineDO> subServiceLineDOS = subServiceLineMapper.selectList(subServiceLineDOQueryWrapper);

        QueryWrapper<CompetencyDO> competencyDOQueryWrapper = new QueryWrapper<>();
        competencyDOQueryWrapper.eq("deleted", false);
        competencyDOQueryWrapper.eq("status", 0);
        List<CompetencyDO> competencyDOS = competencyMapper.selectList(competencyDOQueryWrapper);

        QueryWrapper<SubMgmtUnitDO> subMgmtUnitDOQueryWrapper = new QueryWrapper<>();
        subMgmtUnitDOQueryWrapper.eq("deleted", false);
        subMgmtUnitDOQueryWrapper.eq("status", 0);
        List<SubMgmtUnitDO> subMgmtUnitDOS = subMgmtUnitMapper.selectList(subMgmtUnitDOQueryWrapper);

        List<CodeBlockTreeValueVO> slCodeTreeList = new ArrayList<>();
        List<String> slCodeDistinct = serviceLineDOS.stream().sorted(Comparator.comparing(ServiceLineDO::getSlId).reversed()).map(ServiceLineDO::getSlCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
        for (String slCode : slCodeDistinct) {
            if (StringUtils.isBlank(slCode)) {
                continue;
            }
            CodeBlockTreeValueVO slCodeTree = new CodeBlockTreeValueVO();
            List<ServiceLineDO> collect = serviceLineDOS.stream().filter(c -> slCode.equals(c.getSlCode())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(collect)) {
                slCodeTree.setLabel(collect.get(0).getSlName());
            }
            slCodeTree.setValue(slCode);
            List<CodeBlockTreeValueVO> sslCodeTreeList = new ArrayList<>();
            if (CollectionUtil.isNotEmpty(subServiceLineDOS)) {
                List<String> sslCodeDistinct = subServiceLineDOS.stream().filter(c -> slCode.equals(c.getSlCode())).sorted(Comparator.comparing(SubServiceLineDO::getSslId).reversed()).map(SubServiceLineDO::getSslCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(sslCodeDistinct)) {
                    for (String sslCode : sslCodeDistinct) {
                        if (StringUtils.isBlank(sslCode)) {
                            continue;
                        }
                        CodeBlockTreeValueVO sslCodeTree = new CodeBlockTreeValueVO();
                        List<SubServiceLineDO> collect1 = subServiceLineDOS.stream().filter(c -> sslCode.equals(c.getSslCode())).collect(Collectors.toList());
                        if (CollectionUtil.isNotEmpty(collect1)) {
                            sslCodeTree.setLabel(collect1.get(0).getSslName());
                        }
                        sslCodeTree.setValue(sslCode);
                        List<CodeBlockTreeValueVO> competencyCodeTreeList = new ArrayList<>();
                        if (CollectionUtil.isNotEmpty(competencyDOS)) {
                            List<String> competencyCodeDistinct = competencyDOS.stream().filter(c -> sslCode.equals(c.getSslCode())).sorted(Comparator.comparing(CompetencyDO::getCompetencyId).reversed()).map(CompetencyDO::getCompetencyCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                            if (CollectionUtil.isNotEmpty(competencyCodeDistinct)) {
                                for (String competencyCode : competencyCodeDistinct) {
                                    if (StringUtils.isBlank(competencyCode)) {
                                        continue;
                                    }
                                    CodeBlockTreeValueVO competencyCodeTree = new CodeBlockTreeValueVO();
                                    List<CompetencyDO> collect2 = competencyDOS.stream().filter(c -> competencyCode.equals(c.getCompetencyCode())).collect(Collectors.toList());
                                    if (CollectionUtil.isNotEmpty(collect2)) {
                                        competencyCodeTree.setLabel(collect2.get(0).getCompetencyName());
                                    }
                                    competencyCodeTree.setValue(competencyCode);
                                    List<CodeBlockTreeValueVO> smuCodeTreeList = new ArrayList<>();
                                    if (CollectionUtil.isNotEmpty(subMgmtUnitDOS)) {
                                        List<String> smuCodeDistinct = subMgmtUnitDOS.stream().filter(c -> competencyCode.equals(c.getCompetencyCode())).sorted(Comparator.comparing(SubMgmtUnitDO::getSmuId).reversed()).map(SubMgmtUnitDO::getSmuCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                                        if (CollectionUtil.isNotEmpty(smuCodeDistinct)) {
                                            for (String smuCode : smuCodeDistinct) {
                                                if (StringUtils.isBlank(smuCode)) {
                                                    continue;
                                                }
                                                CodeBlockTreeValueVO smuCodeTree = new CodeBlockTreeValueVO();
                                                List<SubMgmtUnitDO> collect3 = subMgmtUnitDOS.stream().filter(c -> smuCode.equals(c.getSmuCode())).collect(Collectors.toList());
                                                if (CollectionUtil.isNotEmpty(collect3)) {
                                                    smuCodeTree.setLabel(collect3.get(0).getSmuName());
                                                }
                                                smuCodeTree.setValue(smuCode);
                                                smuCodeTree.setChildren(null);
                                                smuCodeTreeList.add(smuCodeTree);
                                            }
                                        }
                                    }
                                    if (CollectionUtil.isNotEmpty(smuCodeTreeList)) {
                                        competencyCodeTree.setChildren(smuCodeTreeList);
                                    } else {
                                        competencyCodeTree.setChildren(null);
                                    }
                                    competencyCodeTreeList.add(competencyCodeTree);
                                }
                            }
                        }
                        if (CollectionUtil.isNotEmpty(competencyCodeTreeList)) {
                            sslCodeTree.setChildren(competencyCodeTreeList);
                        } else {
                            sslCodeTree.setChildren(null);
                        }
                        sslCodeTreeList.add(sslCodeTree);
                    }
                }
            }
            if (CollectionUtil.isNotEmpty(sslCodeTreeList)) {
                slCodeTree.setChildren(sslCodeTreeList);
            } else {
                slCodeTree.setChildren(null);
            }
            slCodeTreeList.add(slCodeTree);
        }
        return slCodeTreeList;
    }

    /**
     * 商机业务线树形列表取宽表数据
     *
     * @return
     */
    @Override
    public List<CodeBlockTreeValueVO> getBusinessTreeList() {
        //sl_code->ssl_code->competency_code->smu_code
        QueryWrapper<CodeBlockDO> serviceLineDOQueryWrapper = new QueryWrapper<>();
        serviceLineDOQueryWrapper.eq("deleted", false);
        serviceLineDOQueryWrapper.eq("status", 0);
        List<CodeBlockDO> serviceLineDOS = codeBlockMapper.selectList(serviceLineDOQueryWrapper);
        if (CollectionUtil.isEmpty(serviceLineDOS)) {
            return null;
        }

        List<CodeBlockTreeValueVO> slCodeTreeList = new ArrayList<>();
        List<String> slCodeDistinct = serviceLineDOS.stream().sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getSlCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
        for (String slCode : slCodeDistinct) {
            if (StringUtils.isBlank(slCode)) {
                continue;
            }
            CodeBlockTreeValueVO slCodeTree = new CodeBlockTreeValueVO();
            List<CodeBlockDO> collect = serviceLineDOS.stream().filter(c -> slCode.equals(c.getSlCode())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(collect)) {
                slCodeTree.setLabel(collect.get(0).getSlName());
            }
            slCodeTree.setValue(slCode);
            List<CodeBlockTreeValueVO> sslCodeTreeList = new ArrayList<>();
            List<String> sslCodeDistinct = serviceLineDOS.stream().filter(c -> slCode.equals(c.getSlCode())).sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getSslCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(sslCodeDistinct)) {
                for (String sslCode : sslCodeDistinct) {
                    if (StringUtils.isBlank(sslCode)) {
                        continue;
                    }
                    CodeBlockTreeValueVO sslCodeTree = new CodeBlockTreeValueVO();
                    List<CodeBlockDO> collect1 = serviceLineDOS.stream().filter(c -> sslCode.equals(c.getSslCode())).collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(collect1)) {
                        sslCodeTree.setLabel(collect1.get(0).getSslName());
                    }
                    sslCodeTree.setValue(sslCode);
                    List<CodeBlockTreeValueVO> competencyCodeTreeList = new ArrayList<>();
                    List<String> competencyCodeDistinct = serviceLineDOS.stream().filter(c -> sslCode.equals(c.getSslCode()) && slCode.equals(c.getSlCode())).sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getCompetencyCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                    if (CollectionUtil.isNotEmpty(competencyCodeDistinct)) {
                        for (String competencyCode : competencyCodeDistinct) {
                            if (StringUtils.isBlank(competencyCode)) {
                                continue;
                            }
                            CodeBlockTreeValueVO competencyCodeTree = new CodeBlockTreeValueVO();
                            List<CodeBlockDO> collect2 = serviceLineDOS.stream().filter(c -> competencyCode.equals(c.getCompetencyCode())).collect(Collectors.toList());
                            if (CollectionUtil.isNotEmpty(collect2)) {
                                competencyCodeTree.setLabel(collect2.get(0).getCompetencyName());
                            }
                            competencyCodeTree.setValue(competencyCode);
                            List<CodeBlockTreeValueVO> smuCodeTreeList = new ArrayList<>();
                            List<String> smuCodeDistinct = serviceLineDOS.stream().filter(c -> competencyCode.equals(c.getCompetencyCode()) && sslCode.equals(c.getSslCode()) && slCode.equals(c.getSlCode())).sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getSmuCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                            if (CollectionUtil.isNotEmpty(smuCodeDistinct)) {
                                for (String smuCode : smuCodeDistinct) {
                                    if (StringUtils.isBlank(smuCode)) {
                                        continue;
                                    }
                                    CodeBlockTreeValueVO smuCodeTree = new CodeBlockTreeValueVO();
                                    List<CodeBlockDO> collect3 = serviceLineDOS.stream().filter(c -> smuCode.equals(c.getSmuCode())).collect(Collectors.toList());
                                    if (CollectionUtil.isNotEmpty(collect3)) {
                                        smuCodeTree.setLabel(collect3.get(0).getSmuName());
                                    }
                                    smuCodeTree.setValue(smuCode);
                                    smuCodeTree.setChildren(null);
                                    smuCodeTreeList.add(smuCodeTree);
                                }
                            }
                            if (CollectionUtil.isNotEmpty(smuCodeTreeList)) {
                                competencyCodeTree.setChildren(smuCodeTreeList);
                            } else {
                                competencyCodeTree.setChildren(null);
                            }
                            competencyCodeTreeList.add(competencyCodeTree);
                        }
                    }
                    if (CollectionUtil.isNotEmpty(competencyCodeTreeList)) {
                        sslCodeTree.setChildren(competencyCodeTreeList);
                    } else {
                        sslCodeTree.setChildren(null);
                    }
                    sslCodeTreeList.add(sslCodeTree);
                }
            }
            if (CollectionUtil.isNotEmpty(sslCodeTreeList)) {
                slCodeTree.setChildren(sslCodeTreeList);
            } else {
                slCodeTree.setChildren(null);
            }
            slCodeTreeList.add(slCodeTree);
        }
        return slCodeTreeList;
    }

    /**
     * 商机区域树形列表取宽表数据
     *
     * @return
     */
    @Override
    public List<CodeBlockTreeValueVO> getBusinessTreeCityCodeList() {
        //dict_business_unit
        //marketSegmentCode、cityCode、buCode
        QueryWrapper<CodeBlockDO> businessUnitDOQueryWrapper = new QueryWrapper<>();
        businessUnitDOQueryWrapper.eq("deleted", false);
        businessUnitDOQueryWrapper.eq("status", 0);
        List<CodeBlockDO> businessUnitDOS = codeBlockMapper.selectList(businessUnitDOQueryWrapper);
        if (CollectionUtil.isEmpty(businessUnitDOS)) {
            return null;
        }
        List<CodeBlockTreeValueVO> marketSegmentCodeTreeList = new ArrayList<>();
        List<String> marketSegmentCodeDistinct = businessUnitDOS.stream().sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getMarketSegmentCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
        for (String marketSegmentCode : marketSegmentCodeDistinct) {
            CodeBlockTreeValueVO marketSegmentCodeTree = new CodeBlockTreeValueVO();
            if (StringUtils.isBlank(marketSegmentCode)) {
                continue;
            }
            List<CodeBlockDO> collect = businessUnitDOS.stream().filter(c -> marketSegmentCode.equals(c.getMarketSegmentCode())).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(collect)) {
                CodeBlockDO marketSegmentCodeBlockDO = collect.get(0);
                marketSegmentCodeTree.setLabel(marketSegmentCodeBlockDO.getMarketSegmentName());
            }
            marketSegmentCodeTree.setValue(marketSegmentCode);
            List<CodeBlockTreeValueVO> cityCodeTreeList = new ArrayList<>();
            List<String> cityCodeDistinct = businessUnitDOS.stream().filter(c -> marketSegmentCode.equals(c.getMarketSegmentCode())).sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getCityCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
            for (String cityCode : cityCodeDistinct) {
                if (StringUtils.isBlank(cityCode)) {
                    continue;
                }
                CodeBlockTreeValueVO cityCodeTree = new CodeBlockTreeValueVO();
                List<CodeBlockDO> collect1 = businessUnitDOS.stream().filter(c -> cityCode.equals(c.getCityCode())).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(collect1)) {
                    cityCodeTree.setLabel(collect1.get(0).getCityName());
                }
                cityCodeTree.setValue(cityCode);
                List<CodeBlockTreeValueVO> buCodeTreeList = new ArrayList<>();
                List<String> buCodeDistinct = businessUnitDOS.stream().filter(b -> marketSegmentCode.equals(b.getMarketSegmentCode())
                        && b.getCityCode().equals(cityCode)).sorted(Comparator.comparing(CodeBlockDO::getCodeBlockId).reversed()).map(CodeBlockDO::getBuCode).filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(buCodeDistinct)) {
                    for (String buCode : buCodeDistinct) {
                        if (StringUtils.isBlank(buCode)) {
                            continue;
                        }
                        CodeBlockTreeValueVO buCodeTree = new CodeBlockTreeValueVO();
                        List<CodeBlockDO> collect2 = businessUnitDOS.stream().filter(c -> buCode.equals(c.getBuCode())).collect(Collectors.toList());
                        if (CollectionUtil.isNotEmpty(collect2)) {
                            buCodeTree.setLabel(collect2.get(0).getBuName());
                        }
                        buCodeTree.setValue(buCode);
                        buCodeTree.setChildren(null);
                        buCodeTreeList.add(buCodeTree);
                    }
                }
                if (CollectionUtil.isNotEmpty(buCodeTreeList)) {
                    cityCodeTree.setChildren(buCodeTreeList);
                } else {
                    cityCodeTree.setChildren(null);
                }
                cityCodeTreeList.add(cityCodeTree);
            }
            if (CollectionUtil.isNotEmpty(cityCodeTreeList)) {
                marketSegmentCodeTree.setChildren(cityCodeTreeList);
            } else {
                marketSegmentCodeTree.setChildren(null);
            }
            marketSegmentCodeTreeList.add(marketSegmentCodeTree);
        }
        return marketSegmentCodeTreeList;
    }

    @Override
    public List<CodeBlockDO> getCodeBlockListByGpn(String app, String gpn,String configType) {
        LambdaQueryWrapper<AdminUserDO> wrappers = Wrappers.lambdaQuery(AdminUserDO.class)
                .eq(AdminUserDO::getGpn, gpn);
        List<AdminUserDO> adminUserLi = userMapper.selectList(wrappers);
        if (adminUserLi != null && adminUserLi.size() > 0) {
            LambdaQueryWrapper<UserDataRoleDO> udrWrappers = Wrappers.lambdaQuery(UserDataRoleDO.class)
                    .eq(UserDataRoleDO::getUserId, adminUserLi.get(0).getId());
            List<UserDataRoleDO> udrList=userDataRoleMapper.selectList(udrWrappers);
            List<Long> roleIds=udrList.stream().map(UserDataRoleDO ::getRoleId).collect(Collectors.toList());
            if(CollectionUtil.isNotEmpty(roleIds)){
                List<CodeBlockDO> codeBlockDOList = codeBlockMapper.getCodeBlockListByGpnUpgrade(app, roleIds,configType);
                return codeBlockDOList;
            }
        }
        return null;
    }

    @Override
    public List<CodeBlockDO> getCodeBlockDetailList(Collection<String> codeBlocks) {
       return  codeBlockMapper.selectList(CodeBlockDO::getCodeBlockCode, codeBlocks);
    }

    @Override
    public List<String> getGpnListByCodeblock(String app, String codeBlock, String configType) {
        List<String> gpnList=codeBlockMapper.getGpnListByCodeBlock(app,codeBlock,configType);
        return gpnList;
    }

}
