package com.lideeyunji.core.framework.config.btncommand.filter;

import com.lideeyunji.core.framework.params.vo.role.DbFormRoleFieldVo;
import com.lideeyunji.core.framework.service.IDbFormRoleService;
import com.lideeyunji.core.framework.service.IFormService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.adapter.IlideeYunjiAdapter;
import com.lideeyunji.tool.framework.yunji.enums.YNEnum;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;
import com.lideeyunji.tool.framework.yunji.utils.FuncBase;

import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

/**
 * 移除列表不显示的字段
 */
public class RemoveFieldCriteriaList implements CriteriaList{

    //处理字段凭接（虚拟化字段）
    @Override
    public void meetCriteria(Long dbFormId,List<Map<String, Object>> records) {
        IDbFormRoleService dbFormRoleService = SpringUtils.getBean(IDbFormRoleService.class);
        IlideeYunjiAdapter lideeYunJiAdapter = SpringUtils.getBean(IlideeYunjiAdapter.class);
        Long tenantId = lideeYunJiAdapter.getTenantId();

        List<DbFormRoleFieldVo> roleFieldVoList = dbFormRoleService.listRoleField(tenantId, dbFormId,false);
        removeNotWebView(dbFormId,records,roleFieldVoList);
    }

    /**
     *
     * 移除不是界面显示的列表-列表数据
     *
     * @param records
     */
    public void removeNotWebView(Long dbFormId, List<Map<String, Object>> records,List<DbFormRoleFieldVo> roleFieldVoList) {
        if (Func.isEmpty(records)) {
            return;
        }
        Map<String, DbFormRoleFieldVo> roleDisableMap =null;
        if(Func.isNotEmpty(roleFieldVoList)){
            //转为map
            roleDisableMap = roleFieldVoList.stream()
                    .collect(Collectors.toMap(DbFormRoleFieldVo::getFieldCode, entity -> entity));
        }

        IFormService formService = SpringUtils.getBean(IFormService.class);
        Set<String> webViewFieldSet = new HashSet<>(formService.getWebViewFieldList(dbFormId));

        Map<String, DbFormRoleFieldVo> finalRoleDisableMap = roleDisableMap;
        ForkJoinPool pool = null;
        try {
            pool = FuncBase.lideeyunjiForkJoinPool();
            pool.submit(() -> records.parallelStream().forEach(recordMap -> recordMap.keySet().removeIf(key -> {
                if(Func.equals(key,"lideeyunji_subtable_data") || Func.equals(key,"hasChildren") || Func.equals(key,"leaf")){
                    return false;
                }
                boolean noRoleFlag=false;
                if(Func.isNotEmpty(finalRoleDisableMap) && finalRoleDisableMap.containsKey(key)){
                    DbFormRoleFieldVo roleVo = finalRoleDisableMap.get(key);
                    //如果全部为空话，看最大的那个,最大的那个为N说明是没有权限，需要剔除
                    if(Func.isEmpty(roleVo.getFormIsEdit()) && Func.isEmpty(roleVo.getFormIsView()) && Func.isEmpty(roleVo.getListIsView())&&Func.equals(roleVo.getEnableState(),YNEnum.N.getCode())){
                        noRoleFlag=true;
                    }
                }
                return !webViewFieldSet.contains(key) || noRoleFlag;
            }))).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            if (pool != null) {
                pool.shutdown();
            }
        }
    }



}
