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.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> rely= map.get(true);
             /*没有依赖的*/
             List<FormData> simple=map.get(false);

    }


    public static final String ELE_CODE_REG= "(?<=E\\[)[^]]+(?=\\])";

    /**
     * 依赖排序
     */
    public   void sort( List<FormData> list,List<FormData> data,List<FormData> queue,FormData current){
        if(current!=null){
            Pattern p = Pattern.compile(ELE_CODE_REG);
            Matcher m =p.matcher(current.getFormula().getFormula());
            List<String> cp =  new ArrayList<>();
            while (m.find()){
                cp.add(m.group());
            }
           Map<Boolean,List<FormData>>  map= list.stream().collect(Collectors.partitioningBy(e->cp.contains(e.getEKey())));
            List<FormData> match =map.get(true);
            if(CollectionUtil.isNotEmpty(match)){
                data.addAll(match);
            }
            List<FormData> remain =map.get(false);
            if(CollectionUtil.isNotEmpty(remain)){
                list.clear();
                list.addAll(remain);
            }
        }
    }

}
