复选框值存数据库 存取问题

添加,方便判断是否包含 
<input type="checkbox" name="producacategoryParentIds" value="${productCategory.id}"
[#if storeShopCategory.producacategoryParentIds.contains(","+productCategory.id.toString()+",")]checked[/#if]>${productCategory.name}


/**
* 更新
*/
@PostMapping("/update")
public ResponseEntity<?> update(StoreShopCategory storeShopCategory) {
storeShopCategory.setProducacategoryParentIds(","+storeShopCategory.getProducacategoryParentIds()+",");
if (null==storeShopCategory.getId()){
storeShopCategoryService.save(storeShopCategory);
}else {
storeShopCategoryService.update(storeShopCategory);
}
return Results.OK;
}
/**
* 添加
*/
@GetMapping("/add")
public String add(@CurrentStore Store currentStore, ModelMap model) {
if (currentStore == null) {
return UNPROCESSABLE_ENTITY_VIEW;
}
List<ProductCategory> productCategories = storeService.findProductCategoryList(currentStore, CategoryApplication.Status.PENDING);
List<ProductCategory> trees = productCategoryService.findTree();
List<StoreShopCategory> all = storeShopCategoryService.findAll();
List<Long> list = new ArrayList<>();
for (int i = 0; i <all.size() ; i++) {
if (currentStore.getIsInternational()==all.get(i).getShopStoreTypeId()){
String[] split = StringUtil.trimFirstAndLastChar(all.get(i).getProducacategoryParentIds(),",").split(",");
for (int j = 0; j <split.length ; j++) {
list.add(Long.parseLong(split[j]));
}
}
}
// List<Long> list = new ArrayList<>();
List<ProductCategory> tree = ProductCategory.getTree(new ArrayList<ProductCategory>(), trees, list, 0);
ProductCategory.sort(tree);
model.addAttribute("productCategoryTree", tree);
model.addAttribute("appliedProductCategories", productCategories);
return "business/category_application/add";
}
/**
* 传入所有的商品分类集合,和id返回一个集合
*/
public static List<ProductCategory> getTree(List<ProductCategory> treesNull,List<ProductCategory> trees,List<Long> list,int index){
for (int j = 0; j <trees.size() ; j++) {
if (list.contains(trees.get(j).getId())){
treesNull.add(trees.get(j));
}
}
if (index!=3){
index++;
List<ProductCategory> list1 = new ArrayList<>();
List<Long> collect = new ArrayList<>();
for (int i = 0; i <trees.size(); i++) {
for (int j = 0; j <treesNull.size(); j++) {
if (trees.get(i).getGrade()==index&&trees.get(i).getParent().getId()==treesNull.get(j).getId()){
list1.add(trees.get(i));
collect.add(trees.get(i).getId());
}
}
}
// List<Long> collect = list1.stream().map(ProductCategory::getId).collect(Collectors.toList());
getTree(treesNull,trees,collect,index);
}else {
return treesNull;
}
return treesNull;
}

public static void sort(List<ProductCategory> productCategories) {
if (CollectionUtils.isEmpty(productCategories)) {
return;
}
final Map<Long, Integer> orderMap = new HashMap<>();
for (ProductCategory productCategory : productCategories) {
orderMap.put(productCategory.getId(), productCategory.getOrder());
}
Collections.sort(productCategories, new Comparator<ProductCategory>() {
@Override
public int compare(ProductCategory productCategory1, ProductCategory productCategory2) {
Long[] ids1 = (Long[]) ArrayUtils.add(productCategory1.getParentIds(), productCategory1.getId());
Long[] ids2 = (Long[]) ArrayUtils.add(productCategory2.getParentIds(), productCategory2.getId());
Iterator<Long> iterator1 = Arrays.asList(ids1).iterator();
Iterator<Long> iterator2 = Arrays.asList(ids2).iterator();
CompareToBuilder compareToBuilder = new CompareToBuilder();
while (iterator1.hasNext() && iterator2.hasNext()) {
Long id1 = iterator1.next();
Long id2 = iterator2.next();
Integer order1 = orderMap.get(id1);
Integer order2 = orderMap.get(id2);
compareToBuilder.append(order1, order2).append(id1, id2);
if (!iterator1.hasNext() || !iterator2.hasNext()) {
compareToBuilder.append(productCategory1.getGrade(), productCategory2.getGrade());
}
}
return compareToBuilder.toComparison();
}
});
}


原文地址:https://www.cnblogs.com/-mzh/p/11170275.html