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

com.mobaijun.easyexcel.core.DropDownOptions Maven / Gradle / Ivy

There is a newer version: 3.0.9
Show newest version
/*
 * Copyright (C) 2022 [www.mobaijun.com]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mobaijun.easyexcel.core;

import cn.hutool.core.util.StrUtil;
import com.mobaijun.core.exception.ServiceException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

/**
 * Description: 

Excel下拉可选项

* 注意:为确保下拉框解析正确,传值务必使用createOptionValue()做为值的拼接 * Author: [mobaijun] * Date: [2024/8/20 18:20] * IntelliJ IDEA Version: [IntelliJ IDEA 2023.1.4] */ @Getter @Setter @ToString @AllArgsConstructor @NoArgsConstructor @SuppressWarnings("unused") public class DropDownOptions { /** * 分隔符 */ private static final String DELIMITER = "_"; /** * 一级下拉所在列index,从0开始算 */ private int index = 0; /** * 二级下拉所在的index,从0开始算,不能与一级相同 */ private int nextIndex = 0; /** * 一级下拉所包含的数据 */ private List options = new ArrayList<>(); /** * 二级下拉所包含的数据Map *

以每一个一级选项值为Key,每个一级选项对应的二级数据为Value

*/ private Map> nextOptions = new HashMap<>(); /** * 创建只有一级的下拉选 */ public DropDownOptions(int index, List options) { this.index = index; this.options = options; } /** *

创建每个选项可选值

*

注意:不能以数字,特殊符号开头,选项中不可以包含任何运算符号

* * @param vars 可选值内包含的参数 * @return 合规的可选值 */ public static String createOptionValue(Object... vars) { StringBuilder stringBuffer = new StringBuilder(); String regex = "^[\\S\\d\\u4e00-\\u9fa5]+$"; for (int i = 0; i < vars.length; i++) { String var = StrUtil.trimToEmpty(String.valueOf(vars[i])); if (!var.matches(regex)) { throw new ServiceException("选项数据不符合规则,仅允许使用中英文字符以及数字"); } stringBuffer.append(var); if (i < vars.length - 1) { // 直至最后一个前,都以_作为切割线 stringBuffer.append(DELIMITER); } } if (stringBuffer.toString().matches("^\\d_*$")) { throw new ServiceException("禁止以数字开头"); } return stringBuffer.toString(); } /** * 将处理后合理的可选值解析为原始的参数 * * @param option 经过处理后的合理的可选项 * @return 原始的参数 */ public static List analyzeOptionValue(String option) { return StrUtil.split(option, DELIMITER, true, true); } /** * 创建级联下拉选项 * * @param parentList 父实体可选项原始数据 * @param parentIndex 父下拉选位置 * @param sonList 子实体可选项原始数据 * @param sonIndex 子下拉选位置 * @param parentHowToGetIdFunction 父类如何获取唯一标识 * @param sonHowToGetParentIdFunction 子类如何获取父类的唯一标识 * @param howToBuildEveryOption 如何生成下拉选内容 * @return 级联下拉选项 */ public static DropDownOptions buildLinkedOptions(List parentList, int parentIndex, List sonList, int sonIndex, Function parentHowToGetIdFunction, Function sonHowToGetParentIdFunction, Function howToBuildEveryOption) { DropDownOptions parentLinkSonOptions = new DropDownOptions(); // 先创建父类的下拉 parentLinkSonOptions.setIndex(parentIndex); parentLinkSonOptions.setOptions( parentList.stream() .map(howToBuildEveryOption) .collect(Collectors.toList()) ); // 提取父-子级联下拉 Map> sonOptions = new HashMap<>(); // 父级依据自己的ID分组 Map> parentGroupByIdMap = parentList.stream().collect(Collectors.groupingBy(parentHowToGetIdFunction)); // 遍历每个子集,提取到Map中 sonList.forEach(everySon -> { if (parentGroupByIdMap.containsKey(sonHowToGetParentIdFunction.apply(everySon))) { // 找到对应的上级 T parentObj = parentGroupByIdMap.get(sonHowToGetParentIdFunction.apply(everySon)).get(0); // 提取名称和ID作为Key String key = howToBuildEveryOption.apply(parentObj); // Key对应的Value List thisParentSonOptionList; if (sonOptions.containsKey(key)) { thisParentSonOptionList = sonOptions.get(key); } else { thisParentSonOptionList = new ArrayList<>(); sonOptions.put(key, thisParentSonOptionList); } // 往Value中添加当前子集选项 thisParentSonOptionList.add(howToBuildEveryOption.apply(everySon)); } }); parentLinkSonOptions.setNextIndex(sonIndex); parentLinkSonOptions.setNextOptions(sonOptions); return parentLinkSonOptions; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy