commit
7cdeb3d3d7
@ -10,6 +10,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <h1>Excel下拉可选项</h1>
|
* <h1>Excel下拉可选项</h1>
|
||||||
@ -88,4 +90,60 @@ public class DropDownOptions {
|
|||||||
public static List<String> analyzeOptionValue(String option) {
|
public static List<String> analyzeOptionValue(String option) {
|
||||||
return StrUtil.split(option, DELIMITER, true, true);
|
return StrUtil.split(option, DELIMITER, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建级联下拉选项
|
||||||
|
*
|
||||||
|
* @param parentList 父实体可选项原始数据
|
||||||
|
* @param parentIndex 父下拉选位置
|
||||||
|
* @param sonList 子实体可选项原始数据
|
||||||
|
* @param sonIndex 子下拉选位置
|
||||||
|
* @param parentHowToGetIdFunction 父类如何获取唯一标识
|
||||||
|
* @param sonHowToGetParentIdFunction 子类如何获取父类的唯一标识
|
||||||
|
* @param howToBuildEveryOption 如何生成下拉选内容
|
||||||
|
* @return 级联下拉选项
|
||||||
|
*/
|
||||||
|
public static <T> DropDownOptions buildLinkedOptions(List<T> parentList,
|
||||||
|
int parentIndex,
|
||||||
|
List<T> sonList,
|
||||||
|
int sonIndex,
|
||||||
|
Function<T, Number> parentHowToGetIdFunction,
|
||||||
|
Function<T, Number> sonHowToGetParentIdFunction,
|
||||||
|
Function<T, String> howToBuildEveryOption) {
|
||||||
|
DropDownOptions parentLinkSonOptions = new DropDownOptions();
|
||||||
|
// 先创建父类的下拉
|
||||||
|
parentLinkSonOptions.setIndex(parentIndex);
|
||||||
|
parentLinkSonOptions.setOptions(
|
||||||
|
parentList.stream()
|
||||||
|
.map(howToBuildEveryOption)
|
||||||
|
.collect(Collectors.toList())
|
||||||
|
);
|
||||||
|
// 提取父-子级联下拉
|
||||||
|
Map<String, List<String>> sonOptions = new HashMap<>();
|
||||||
|
// 父级依据自己的ID分组
|
||||||
|
Map<Number, List<T>> 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<String> 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,56 +51,36 @@ public class ExportExcelServiceImpl implements IExportExcelService {
|
|||||||
|
|
||||||
// 首先从数据库中查询下拉框内的可选项
|
// 首先从数据库中查询下拉框内的可选项
|
||||||
// 这里模拟查询结果
|
// 这里模拟查询结果
|
||||||
List<DemoCityData> provinceList = getProvinceList();
|
List<DemoCityData> provinceList = getProvinceList(),
|
||||||
List<DemoCityData> cityList = getCityList(provinceList);
|
cityList = getCityList(provinceList),
|
||||||
List<DemoCityData> areaList = getAreaList(cityList);
|
areaList = getAreaList(cityList);
|
||||||
|
int provinceIndex = 5, cityIndex = 6, areaIndex = 7;
|
||||||
|
|
||||||
// 把所有的结果提取为规范的下拉选可选项
|
DropDownOptions provinceToCity = DropDownOptions.buildLinkedOptions(
|
||||||
// 规范的一级省,用于级联省-市
|
provinceList,
|
||||||
List<String> provinceOptions = StreamUtils.toList(provinceList, everyProvince ->
|
provinceIndex,
|
||||||
DropDownOptions.createOptionValue(everyProvince.getName(), everyProvince.getId()));
|
cityList,
|
||||||
// 规范的二级市,用于级联省-市
|
cityIndex,
|
||||||
Map<String, List<String>> provinceToCityOptions = new HashMap<>();
|
DemoCityData::getId,
|
||||||
StreamUtils.groupByKey(cityList, DemoCityData::getPData)
|
DemoCityData::getPid,
|
||||||
.forEach((province, thisProvinceCityList) -> {
|
everyOptions -> DropDownOptions.createOptionValue(
|
||||||
// 每个省下二级的市可选项
|
everyOptions.getName(),
|
||||||
String optionValue = DropDownOptions.createOptionValue(province.getName(), province.getId());
|
everyOptions.getId()
|
||||||
List<String> list = StreamUtils.toList(thisProvinceCityList, everyCity ->
|
)
|
||||||
DropDownOptions.createOptionValue(everyCity.getName(), everyCity.getId()));
|
);
|
||||||
provinceToCityOptions.put(optionValue, list);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 规范的一级市,用于级联市-县
|
DropDownOptions cityToArea = DropDownOptions.buildLinkedOptions(
|
||||||
List<String> cityOptions = StreamUtils.toList(cityList, everyCity ->
|
cityList,
|
||||||
DropDownOptions.createOptionValue(everyCity.getName(), everyCity.getId()));
|
cityIndex,
|
||||||
// 规范的二级县,用于级联市-县
|
areaList,
|
||||||
Map<String, List<String>> cityToAreaOptions = new HashMap<>();
|
areaIndex,
|
||||||
StreamUtils.groupByKey(areaList, DemoCityData::getPData)
|
DemoCityData::getId,
|
||||||
.forEach((city, thisCityAreaList) -> {
|
DemoCityData::getPid,
|
||||||
// 每个市下二级的县可选项
|
everyOptions -> DropDownOptions.createOptionValue(
|
||||||
String optionValue = DropDownOptions.createOptionValue(city.getName(), city.getId());
|
everyOptions.getName(),
|
||||||
List<String> list = StreamUtils.toList(thisCityAreaList, everyCity ->
|
everyOptions.getId()
|
||||||
DropDownOptions.createOptionValue(everyCity.getName(), everyCity.getId()));
|
)
|
||||||
cityToAreaOptions.put(optionValue, list);
|
);
|
||||||
});
|
|
||||||
|
|
||||||
// 因为省市县三个都是联动,省级联市,市级联县,因此需要创建两个级联下拉,分别以省和市为判断依据创建
|
|
||||||
// 创建省-市级联
|
|
||||||
DropDownOptions provinceToCity = new DropDownOptions();
|
|
||||||
// 以省为一级
|
|
||||||
provinceToCity.setIndex(5);
|
|
||||||
// 以市为二级
|
|
||||||
provinceToCity.setNextIndex(6);
|
|
||||||
// 补充省的内容以及市的内容
|
|
||||||
provinceToCity.setOptions(provinceOptions);
|
|
||||||
provinceToCity.setNextOptions(provinceToCityOptions);
|
|
||||||
|
|
||||||
// 创建市-县级联
|
|
||||||
DropDownOptions cityToArea = new DropDownOptions();
|
|
||||||
cityToArea.setIndex(6);
|
|
||||||
cityToArea.setNextIndex(7);
|
|
||||||
cityToArea.setOptions(cityOptions);
|
|
||||||
cityToArea.setNextOptions(cityToAreaOptions);
|
|
||||||
|
|
||||||
// 把所有的下拉框存储
|
// 把所有的下拉框存储
|
||||||
List<DropDownOptions> options = new ArrayList<>();
|
List<DropDownOptions> options = new ArrayList<>();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user