package com.lideeyunji.core.framework.config.excel.check.strategy;

import com.lideeyunji.core.framework.config.excel.check.IExcelCheckStrategy;
import com.lideeyunji.core.framework.config.excel.check.param.ExcelCheckParams;
import com.lideeyunji.core.framework.entity.FormFieldEntity;
import com.lideeyunji.core.framework.params.vo.DbFormConfigVo;
import com.lideeyunji.core.framework.utils.Func;
import com.lideeyunji.tool.framework.yunji.enums.LideeYunJiFieldTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * 数据类型-校验
 */
@Component
@Slf4j
public class ExcelDataLengthStrategy implements IExcelCheckStrategy {

    //执行
    @Override
    public void check(ExcelCheckParams checkParams) throws Exception {
        Map<String, Object> dataMap = checkParams.getDataMap();
        DbFormConfigVo vo = checkParams.getDbFormConfigVo();

        List<FormFieldEntity> fieldList = vo.getFieldList();

        for(FormFieldEntity fieldEntity : fieldList){
            String fieldName = fieldEntity.getFieldName();
            String fieldCode = fieldEntity.getFieldCode();
            String fieldType = fieldEntity.getFieldType();
            int fieldLen = fieldEntity.getFieldLen();

            Object obj = dataMap.get(fieldCode);
            if(Func.isEmpty( obj)){
                continue;
            }
            LideeYunJiFieldTypeEnum fieldTypeEnum = LideeYunJiFieldTypeEnum.getByFieldType(fieldType);
            if(Func.isEmpty(fieldTypeEnum)){
                throw new Exception("字段类型错误");
            }
            //如果是字符串才有长度校验
            if(Func.equals(fieldType,"String") || Func.equals(fieldType,"Text") || Func.equals(fieldType,"LongText")){
               if(Func.toStr( obj).length()>fieldLen){
                   throw new Exception("字段【"+fieldName+"】长度超过限制");
               }
            }
        }


    }


}
