package org.jeecg.modules.stm.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainLableService;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.jeecg.modules.stm.compliane.item.entity.StComplianeItem;
import org.jeecg.modules.stm.compliane.item.service.IStComplianeItemService;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @Author: yzh
 * @Date 2023/4/9 13:38
 * @Description:
 */
public class QueryCaManParamsUtlis {

    private static String getCaName(Map<String, String[]> parameterMap){
        String [] param = parameterMap.get("caName");
        if(param!=null && param.length==1){
            return param[0];
        }
        return null;
    }

    public static List<String> getCaNos(Map<String, String[]> parameterMap){
        List<String> caMainCodes = null;
        List<String> labelCodes = QueryLableParamsUtlis.getLableCodes(parameterMap);
        if(!CollectionUtils.isEmpty(labelCodes)){
            if(labelCodes.size()==1 && "notExist".equals(labelCodes.get(0))){
                caMainCodes = labelCodes;
            }else {
                IStCaMainLableService iStCaMainLableService = SpringContextUtils.getBean(IStCaMainLableService.class);
                caMainCodes = iStCaMainLableService.listByLabelCodes(labelCodes);
            }
        }
        QueryWrapper<StCaMain> queryWrapper = new QueryWrapper<>();
        boolean caQueryFlag = false;
        if(!CollectionUtils.isEmpty(caMainCodes)){
            if(caMainCodes.size()==1 && "notExist".equals(caMainCodes.get(0))) {
                return Arrays.asList("notExist");
            }
            queryWrapper.in("CA_NO",caMainCodes);
            caQueryFlag = true;
        }
        String caName = getCaName(parameterMap);
        if(StringUtils.hasText(caName)) {
            queryWrapper.like("CA_NAME", "%" +caName+"%");
            caQueryFlag = true;
        }
        String domainCode = QueryDomainParamsUtlis.getDomainCode(parameterMap);
        if(StringUtils.hasText(domainCode)) {
            queryWrapper.likeRight("DOMAIN_CODE", domainCode);
            caQueryFlag = true;
        }
        List<String> caNos = null;
        if(caQueryFlag) {
            IStCaMainService iStCaMainService = SpringContextUtils.getBean(IStCaMainService.class);
            List<StCaMain> stCaMains = iStCaMainService.list(queryWrapper);
            if(!CollectionUtils.isEmpty(stCaMains)){
                 caNos = stCaMains.stream().map(StCaMain::getCaNo).collect(Collectors.toList());
            }else{
                caNos = Arrays.asList("notExist");
            }
        }
        String [] param = parameterMap.get("caNo");
        if(param!=null && param.length==1){
            if(caNos!=null&& !"notExist".equals(caNos.get(0))){
                caNos.add(param[0]);
            }else {
                caNos = Arrays.asList(param[0]);
            }
        }
        return caNos;
    }

    public static List<String> getFilterCaNos(Map<String, String[]> parameterMap) {
        String [] param = parameterMap.get("isFilte");
        if(param!=null && param.length==1 && "true".equals(param[0])){
            String [] bizType = parameterMap.get("bizType");
            String [] bizNo = parameterMap.get("bizNo");
            if(bizType!=null && bizType.length==1 && bizNo!=null && bizNo.length==1){
                // 合规关联
                if("hg".equals(bizType[0])){
                    IStComplianeItemService iStComplianeItemService = SpringContextUtils.getBean(IStComplianeItemService.class);
                    QueryWrapper<StComplianeItem> queryWrapper = new QueryWrapper<>();
                    queryWrapper.select("CA_NO");
                    queryWrapper.eq("COMPLIANE_NO",bizNo[0]);
                    List<StComplianeItem> list = iStComplianeItemService.list(queryWrapper);
                    if(!CollectionUtils.isEmpty(list)){
                      List<String> caNos =  list.stream().map(StComplianeItem::getCaNo).collect(Collectors.toList());
                      return caNos;
                    }
                }
            }
        }
        return null;
    }
}
