package org.springblade.manager.service.impl;


import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.manager.dto.FormData;
import org.springblade.manager.entity.Formula;
import org.springblade.manager.entity.WbsFormElement;
import org.springblade.manager.entity.WbsTree;
import org.springblade.manager.mapper.FormulaMapper;
import org.springblade.manager.service.IFormulaService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author yangyj
 * @Date 2022/6/9 14:29
 * @description TODO
 */
@Service
public class FormulaServiceImpl extends BaseServiceImpl<FormulaMapper, Formula> implements IFormulaService {
    /**
     * @Description
     * @Param [variables, list：表单数据, formulas:同一个wbsId]
     * @return void
     * @Author yangyj
     * @Date 2022.06.15 16:34
     **/
    public void execute(Map variables, List<FormData> list,List<Formula> formulas ){
             Map<Boolean,List<FormData>> map = list.stream().collect(Collectors.partitioningBy(e->e!=null&&e.getFormula().getFormula().contains("E[")));
            List<FormData>total = new ArrayList<>();
            List<WbsTree> trees = new ArrayList<>();
            for(int i=0;i<6;i++){
                 WbsTree t= new WbsTree();
                 t.setFullName("第"+i+"层");
            }
             /*没有依赖的*/
             List<FormData> simple=map.get(false);
             if(CollectionUtil.isNotEmpty(simple)){
                 total.addAll(simple);
             }
            /*有依赖的*/
             List<FormData> rely= map.get(true);
             if(CollectionUtil.isNotEmpty(rely)){
                  sort(rely,((rely.size()+1)/2)*rely.size());
                  total.addAll(rely);
             }
            if(CollectionUtil.isNotEmpty(total)){
                /*预处理*/
                for(FormData fd:total){

                }
            }
    }




    /**
     * 依赖排序
     */
    public static final String ELE_CODE_REG= "(?<=E\\[)[^]]+(?=\\])";
    public static final  Pattern P = Pattern.compile(ELE_CODE_REG);
    public  static void sort( List<FormData> list,int n){
        System.out.println("剩余计算次数："+n+"次");
        if(move(list)&&n>0){
            sort(list,--n);
        }
    }
    public  static Boolean move( List<FormData> list){
        for(int i=0;i<list.size();i++){
            FormData f=list.get(i);
            Matcher m =P.matcher(f.getFormula().getFormula());
            List<String> cp =  new ArrayList<>();
            while (m.find()){
                cp.add(m.group());
            }
            Map<Boolean,List<FormData>>  map= list.stream().skip(i+1).collect(Collectors.partitioningBy(e->cp.contains(e.getEKey())));
            List<FormData> match =map.get(true);
            if(CollectionUtil.isNotEmpty(match)){
                for(FormData r:match){
                    list.remove(r);
                }
                list.addAll(0,match);
                return true;
            }
        }
        return false;

    }



}










