zoukankan      html  css  js  c++  java
  • 项目经理的特殊需求,对象的移动,

    /**
         * @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();
                }
            }
        }
    

      

  • 相关阅读:
    WPF e.Systemkey的一个坑
    Codeforces Round #375 (Div. 2) ABCDE
    主席树模板(poj2104)
    hdu5909-Tree Cutting(树形dp)
    HYSBZ 2243-染色 (树链剖分)
    POJ3237-Tree (树链剖分,线段树区间更新+点更新+区间查询)
    POJ2763-Housewife Wind(树链剖分)
    2016东北四省赛 小结
    HDU3966-Aragorn's Story(树链剖分)
    POJ 2749--Building roads(2-SAT)
  • 原文地址:https://www.cnblogs.com/hahahehexixihoho/p/11102511.html
Copyright © 2011-2022 走看看