All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.qdbp.filling.biz.BaseEntityDataFillFieldValueBasic Maven / Gradle / Ivy

package com.gitee.qdbp.filling.biz;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gitee.qdbp.able.beans.KeyString;
import com.gitee.qdbp.able.beans.KeyValue;
import com.gitee.qdbp.filling.api.EntityDataTypedFillService;
import com.gitee.qdbp.tools.utils.ConvertTools;
import com.gitee.qdbp.tools.utils.NamingTools;
import com.gitee.qdbp.tools.utils.StringTools;
import com.gitee.qdbp.tools.utils.VerifyTools;

/**
 * 实体数据填充单一类型处理基础实现类
 *
 * @author zhaohuihua
 * @version 20210113
 */
public abstract class BaseEntityDataFillFieldValueBasic implements EntityDataTypedFillService {

    protected boolean skipDataFill(Map data) {
        return false;
    }

    protected boolean skipSourceValue(Serializable sourceValue) {
        return false;
    }

    // originalData: key=sourceValue
    // valueMaps: key=sourceValue, value=targetValue
    protected void fillValueToOriginalData(Map> originalData, Map valueMaps) {
        // # 将targetValue填充至originalData
        for (Map.Entry> entry : originalData.entrySet()) {
            Serializable sourceValue = entry.getKey();
            if (valueMaps.containsKey(sourceValue)) {
                Object targetValue = valueMaps.get(sourceValue);
                List fillors = entry.getValue();
                for (DataFillor fillor : fillors) {
                    fillor.fillValues(targetValue);
                }
            }
        }
    }

    /**
     * options多个字段以逗号分隔, 默认的填充字段为源字段去掉Id/Code后缀, 加上Name
* -- createUser,updateUser
* -- 等于createUser-createUserName,updateUser-updateUserName
* 也可以自定义转换关系
* -- creatorId-createUser,updatorId-updateUser
* @param options 转换规则 * @return 解析后的规则, key=fieldName, value=targetName */ protected List parseOptions(String options) { List list = new ArrayList<>(); if (options == null || options.trim().length() == 0) { return list; } String[] array = StringTools.split(options, ','); for (String option : array) { option = option.trim(); int index = option.indexOf('-'); if (index < 0) { list.add(new KeyString(option, convertToTargetName(option))); } else if (index > 0) { String source = option.substring(0, index).trim(); String target = option.substring(index + 1).trim(); list.add(new KeyString(source, target)); // } else { // 横框开头? 忽略! } } return list; } protected Map> collectFillInfos(Iterable> list, List options) { Map> sourceMaps = new HashMap<>(); for (Map map : list) { if (skipDataFill(map)) { continue; } DataFillor fillor = collectDataFillor(map, options); if (fillor.getFillFields().isEmpty()) { continue; } // key=targetName, value=sourceValue for (KeyValue item : fillor.getFillFields()) { if (sourceMaps.containsKey(item.getValue())) { sourceMaps.get(item.getValue()).add(fillor); } else { List fillors = ConvertTools.toList(fillor); sourceMaps.put(item.getValue(), fillors); } } } return sourceMaps; } protected DataFillor collectDataFillor(Map data, List options) { DataFillor fillor = new DataFillor(data); for (KeyString item : options) { String sourceName = item.getKey(); String targetName = item.getValue(); // # 先按入参指定的字段名, 判断是否存在字段值 Object sourceValue = data.get(sourceName); if (VerifyTools.isNotBlank(sourceValue) && data.get(targetName) == null) { if (sourceValue instanceof Serializable) { Serializable value = (Serializable) sourceValue; if (!skipSourceValue(value)) { fillor.addFillField(targetName, value); } } continue; } // # 如果不存在, 转换为驼峰命名或下划线命名再次判断 String convertedSourceName; String convertedTargetName; if (isCamelNaming(sourceName)) { // 入参是驼峰命名, 转换为下划线命名 convertedSourceName = NamingTools.toUnderlineString(sourceName).toUpperCase(); convertedTargetName = NamingTools.toUnderlineString(targetName).toUpperCase(); } else { // 入参是下划线命名, 转换为驼峰命名 convertedSourceName = NamingTools.toCamelString(sourceName); convertedTargetName = NamingTools.toCamelString(targetName); } Object convertedSourceValue = data.get(convertedSourceName); if (VerifyTools.isNotBlank(convertedSourceValue) && data.get(convertedTargetName) == null) { if (convertedSourceValue instanceof Serializable) { Serializable value = (Serializable) convertedSourceValue; if (!skipSourceValue(value)) { fillor.addFillField(convertedTargetName, value); } } continue; } } return fillor; } protected String convertToTargetName(String source) { if (isCamelNaming(source)) { String target = source; if (target.endsWith("Id")) { target = StringTools.removeSuffix(target, "Id"); } else if (target.endsWith("Code")) { target = StringTools.removeSuffix(target, "Code"); } return target + "Name"; } else { String target = source; if (target.endsWith("_ID")) { target = StringTools.removeSuffix(target, "_ID"); } else if (target.endsWith("_CODE")) { target = StringTools.removeSuffix(target, "_CODE"); } return target + "_NAME"; } } protected boolean isCamelNaming(String source) { char[] chars = source.toCharArray(); for (int i = 0, z = chars.length; i < z; i++) { char c = source.charAt(i); if (c >= 'a' && c <= 'z') { return true; // 存在小写字母, 即判定为驼峰命名 } } return false; } protected static class DataFillor { // key=targetName, value=sourceValue private final List> fillFields; private final Map originalData; public DataFillor(Map originalData) { this.originalData = originalData; this.fillFields = new ArrayList<>(); } protected void addFillField(String targetName, Serializable sourceValue) { this.fillFields.add(new KeyValue<>(targetName, sourceValue)); } public List> getFillFields() { return this.fillFields; } public void fillValues(Object targetValue) { for (KeyValue item : fillFields) { this.originalData.put(item.getKey(), targetValue); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy