/** * @param mustChange 需要移动的元素下表 * @param beChange 需要移动到的下表 * @return */ @Override public ResponseEntity<JsonResultEntity> sortBanner(Integer mustChange, Integer beChange) { beChange = beChange - 1; mustChange = mustChange - 1; Example example = new Example(BannersEntity.class); Example.Criteria criteria = example.createCriteria(); criteria.andNotEqualTo("status", BannersEntity.StatusEnum.DEL.getCode()); example.setOrderByClause("sort_id ASC"); List<BannersEntity> bannersEntities = bannersMapper.selectByExample(example); if (bannersEntities == null || mustChange.equals(beChange)) { return ResponseEntity.ok(JsonResultUtil.success()); } if (mustChange.compareTo(bannersEntities.size()) > 0 || mustChange.compareTo(0) < 0) { return ResponseEntity.ok(JsonResultUtil.success("需要移动的元素下标越界!")); } if (beChange.compareTo(bannersEntities.size()) > 0 || beChange.compareTo(0) < 0) { return ResponseEntity.ok(JsonResultUtil.success("需要移动的元素下标越界!")); } if (mustChange < beChange) { //当需要移动的下标大于被移动到的位置的下标的时候,则区间整体下移 for (int i = mustChange; i < beChange + 1; i++) { if (i == mustChange) { bannersEntities.get(mustChange).setSortId(bannersEntities.get(beChange).getSortId()); bannersMapper.updateByPrimaryKeySelective(bannersEntities.get(mustChange)); } else { bannersEntities.get(i).setSortId(bannersEntities.get(i).getSortId() - 1); bannersMapper.updateByPrimaryKeySelective(bannersEntities.get(i)); } } } else { bannersEntities.get(mustChange).setSortId(bannersEntities.get(beChange).getSortId()); bannersMapper.updateByPrimaryKeySelective(bannersEntities.get(mustChange)); //当需要移动的下标小于被移动到的位置的下标的时候,则区间整体上移 for (int i = beChange; i < mustChange; i++) { bannersEntities.get(i).setSortId(bannersEntities.get(i).getSortId() + 1); bannersMapper.updateByPrimaryKeySelective(bannersEntities.get(i)); } } getSortBanner(); return ResponseEntity.ok(JsonResultUtil.success()); } public void getSortBanner() { Example example = new Example(BannersEntity.class); Example.Criteria criteria = example.createCriteria(); criteria.andNotEqualTo("status", BannersEntity.StatusEnum.DEL.getCode()); example.setOrderByClause("sort_id ASC"); List<BannersEntity> bannersEntities = bannersMapper.selectByExample(example); int min = bannersEntities.stream().mapToInt(BannersEntity::getSortId).summaryStatistics().getMin(); int max = bannersEntities.stream().mapToInt(BannersEntity::getSortId).summaryStatistics().getMax(); //如果有一个的sortId不为0则代表后面没有sortId为0的了 if (bannersEntities != null && bannersEntities.size() > 0) { if (min != 0 && max == bannersEntities.size()) { return; } else if ((min != 0 && min != 1) || (min != 0 && max != bannersEntities.size())) { for (int i = 0; i < bannersEntities.size(); i++) { bannersEntities.get(i).setSortId(i + 1); bannersMapper.updateByPrimaryKeySelective(bannersEntities.get(i)); } } else if (min == 0) { int index = 1; for (int i = 0; i < bannersEntities.size(); i++) { if (bannersEntities.get(i).getSortId() == 0) { bannersEntities.get(i).setSortId(bannersEntities.get(bannersEntities.size() - 1).getSortId() + index); index++; bannersMapper.updateByPrimaryKeySelective(bannersEntities.get(i)); } } getSortBanner(); } } }