• 项目经理的特殊需求,对象的移动,


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

      

  • 相关阅读:
    心怀梦想
    一年三篇IF大于7的牛人告诉你怎么写SCI
    保护眼睛,绿豆沙颜色的RGB值和HSL值
    优美的句子
    MATLAB中imshow()和image()
    MATLAB中求矩阵非零元的坐标
    最小二乘法(一维)
    关于论文
    机器学习中的数学(1)-回归(regression)、梯度下降(gradient descent)
    Go语言基础之操作Redis
  • 原文地址:https://www.cnblogs.com/hahahehexixihoho/p/11102511.html
Copyright © 2020-2023  润新知