Java案例树形数据结构及构建
@Data
@AllArgsConstructor
@NoArgsConstructor
public class SysDataDictTreeResp extends SysDataDict {
@ApiModelProperty(value = "子集")
List<SysDataDictTreeResp> childrenList;
}
//构建树
private List<SysDataDictTreeResp> generateDictTree(Map<String, List<SysDataDict>> dataGroupMap,
List<SysDataDict> defaultGroupSonKeys) {
return defaultGroupSonKeys.stream().map(data -> {
SysDataDictTreeResp resp = new SysDataDictTreeResp();
BeanUtils.copyProperties(data, resp);
resp.setChildrenList(generateChildrenDict(dataGroupMap, data.getDataKey()));
return resp;
}).collect(Collectors.toList());
}
private List<SysDataDictTreeResp> generateChildrenDict(Map<String, List<SysDataDict>> dataGroupMap, String dataKey) {
if (dataGroupMap.containsKey(dataKey)) {
List<SysDataDict> mapValues = dataGroupMap.get(dataKey);
return generateDictTree(dataGroupMap, mapValues);
}
return Collections.emptyList();
}
数据结构思路优化方案
数据结构优化方案
@Override
public Boolean setOrderCustomerServiceStatus(Long id, Integer status) {
LOGGER.info("id:{}, status:{}", id, status);
OrderCustomerService orderCustomerService = orderCustomerServiceMapper.getById(id);
if (null == orderCustomerService) {
throw new OrderException(OrderExceptionResult.ORDER_CUSTOMER_SERVICE_NOT_EXITS);
}
if (OrderConstant.ORDER_CUSTOMER_SERVICE_STATUS_PROCESSED.equals(orderCustomerService.getStatus())) {
LOGGER.info("备注已处理,不能更新");
throw new OrderException(OrderExceptionResult.ORDER_CUSTOMER_SERVICE_STATUS_PROCESSED);
}
if (status.equals(orderCustomerService.getStatus())) {
LOGGER.info("订单备注状态重复");
throw new OrderException(OrderExceptionResult.ORDER_CUSTOMER_SERVICE_STATUS_REPEAT);
}
Date now = DateUtils.getNow();
orderCustomerServiceMapper.setOrderCustomerServiceStatus(id, status, now);
return true;
}
考虑到不同的情况,做一个处理
如不存在
如备注已处理,不能更新
如订单备注状态和前端传入的值一样
以上情况都提示
将被下面代码处理
public enum OrderExceptionResult implements IExceptionResult {
。。。。。
ORDER_CUSTOMER_SERVICE_STATUS_PROCESSED(1025, "订单备注状态已处理"),
ORDER_CUSTOMER_SERVICE_NOT_EXITS(1026, "订单备注不存在"),
ORDER_CUSTOMER_SERVICE_STATUS_REPEAT(1027, "订单备注状态重复"),
;
private int code;
private String msg;
OrderExceptionResult(int code, String msg) {
this.code = code;
this.msg = msg;
}
@Override
public int getCode() {
return code;
}
@Override
public String getMsg() {
return msg;
}
public IExceptionResult buildParamErrorMsg(String paramErrorMsg) {
if (paramErrorMsg == null || "".equals(paramErrorMsg)) {
paramErrorMsg = "";
}
if (this.code == PARAM_ERROR.code) {
this.msg = "参数 " + paramErrorMsg + " 不能为空";
}
return this;
}