package org.jeecg.modules.stm.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.stm.base.domain.entity.StDomain;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: yzh
 * @Date 2023/4/28 20:20
 * @Description:
 */
public class QueryDomainParamsUtlis {

    /***
     * 等到领域代码
     * @param parameterMap
     * @return
     */
    public static String getDomainCode(Map<String, String[]> parameterMap){
        String [] param = parameterMap.get("domainCodeMore");
        if(param!=null && param.length==1){
            return param[0];
        }
        return null;
    }

    private static List<String> getNextDomainCodes(String code){
        if(StringUtils.isEmpty(code)){
            return null;
        }
        IStDomainService iStDomainService = SpringContextUtils.getBean(IStDomainService.class);
        QueryWrapper<StDomain> stDomainQueryWrapper = new QueryWrapper<>();
        stDomainQueryWrapper.like("DOMAIN_CODE",code+"%");
        List<StDomain> stDomainList = iStDomainService.list(stDomainQueryWrapper);
        if(CollectionUtils.isEmpty(stDomainList)){
            return Arrays.asList("NotExist");
        }
        return stDomainList.stream().map(StDomain::getDomainCode).collect(Collectors.toList());
    }

    public static List<String> getDomainCodes(Map<String, String[]> parameterMap){
        String code = getDomainCode(parameterMap);
        return getNextDomainCodes(code);
    }
}
