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

import com.lideeyunji.core.framework.config.virtualization.VirtualizationFieldPluginManager;
import com.lideeyunji.core.framework.entity.FormFieldEntity;
import com.lideeyunji.core.framework.entity.FormFieldWebEntity;
import com.lideeyunji.core.framework.mapper.FormFieldMapper;
import com.lideeyunji.core.framework.mapper.FormFieldWebMapper;
import com.lideeyunji.core.framework.params.model.WebFormatConfigModel;
import com.lideeyunji.core.framework.service.IFormService;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.enums.YNEnum;
import com.lideeyunji.tool.framework.yunji.tool.spring.SpringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 虚拟化字段显示
 */
public class ViewFieldCriteriaList implements CriteriaList{

    //处理字段凭接（虚拟化字段）
    @Override
    public void meetCriteria(Long dbFormId,List<Map<String, Object>> records) {
        FormFieldWebMapper fieldWebMapper = SpringUtils.getBean(FormFieldWebMapper.class);
        FormFieldMapper fieldMapper = SpringUtils.getBean(FormFieldMapper.class);

        // 获取表单的字段对应的字典
        IFormService formService = SpringUtils.getBean(IFormService.class);
        Map<String, Map<String, Object>> fieldDictMap = formService.getFieldDict(dbFormId);

        //表单map
        Map<String, FormFieldEntity> dbFormMap = fieldMapper.getByDbFormMap(dbFormId);

        List<FormFieldWebEntity> webEntityList = fieldWebMapper.webEntityListAndFormatConfigIsNotNull(dbFormId);
        webEntityList.stream().forEach(web->{
            String fieldCode = web.getFieldCode();
            String formatConfig = web.getFormatConfig();
            if(Func.isEmpty(formatConfig)){
                return;
            }
            FormFieldEntity formFieldEntity = dbFormMap.get(fieldCode);
            if(Func.isEmpty(formFieldEntity)){
                return;
            }
            String isDb = formFieldEntity.getIsDb();
            if(Func.equals(isDb, YNEnum.Y.getCode())){
                return;
            }


            WebFormatConfigModel webFormatConfigModel = Func.json2Bean(formatConfig, WebFormatConfigModel.class);
            if(Func.isEmpty(webFormatConfigModel)){
                return;
            }
            String formatType = webFormatConfigModel.getFormatType();
            if(Func.isEmpty(formatType) || !Func.equals(formatType,"java")){
                return;
            }
            WebFormatConfigModel.Json  formatJson= webFormatConfigModel.getFormatJson();
            WebFormatConfigModel.JavaModel javaModel = formatJson.getJava();
            String valueType = javaModel.getValueType();
            List<WebFormatConfigModel.JavaGroup> groupList = javaModel.getGroup();
            WebFormatConfigModel.JavaCustom custom = javaModel.getCustom();
            switch (valueType){
                case "group"://表达式
                    records.stream().forEach(record -> {
                        record.put(fieldCode, this.getGroupVal(fieldDictMap, groupList, new HashMap<>(record)));
                    });
                    break;
                case "custom":
                    records.stream().forEach(record -> {
                        //运行插件
                        String result = VirtualizationFieldPluginManager.executePlugin(custom.getJavaPath(), new HashMap<>(record));
                        record.put(fieldCode, result);
                    });
                    break;
            }
        });
    }

    //获取分组
    private String getGroupVal(Map<String, Map<String, Object>> fieldDictMap, List<WebFormatConfigModel.JavaGroup> groupList, Map<String, Object> dataMap) {
        this.replaceDict(fieldDictMap, dataMap);// 替换字典值
        String formatVal = ""; // 用于存储格式化后的值
        for (WebFormatConfigModel.JavaGroup group : groupList) {
            String value = group.getValue();
            String type = group.getType();
            if(Func.isEmpty(value)){
                continue;
            }
            switch (type) {
                case "CONCAT":
                    formatVal += value;
                    break;
                case "CALCULATE":
                    formatVal += Func.executeJavaExpress(value, dataMap);
                    break;
            }
        }
        return formatVal;
    }

    /**
     * 替换字典值
     */
    private void replaceDict(Map<String, Map<String, Object>> fieldDictMap, Map<String, Object> dataMap){
        if (Func.isEmpty(fieldDictMap) || Func.isEmpty(dataMap)) {
            return;
        }
        fieldDictMap.keySet().stream().forEach(field -> {
            Object oldValue = dataMap.get(field);
            if (Func.isEmpty(oldValue)) {
                return;
            }
            Map<String, Object> dictMap = fieldDictMap.get(field);
            if (Func.isEmpty(dictMap)) {
                return;
            }
            Object newValue = dictMap.get(oldValue.toString());
            if (Func.isEmpty(newValue)) {
                return;
            }
            dataMap.put(field, newValue);
        });
    }
}
