ssm-springboot实现修排名再顺序排的主要代码

 

 

排序改为2,结果如下:

 

 

package com.zhetang.controller;

import com.core.vo.JsonResult;
import com.utils.PageResult;
import com.zhetang.model.MallCarouseManage;
import com.zhetang.model.MallItemPic;
import com.zhetang.service.CarouselService;
import com.zhetang.service.ItemPicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
public class CarouselController {

    @Autowired
    private CarouselService carouselServiceImpl;

    @Autowired
    private ItemPicService itemPicServiceImpl;

    @RequestMapping("/carousel/add")
    public JsonResult addCarousel(@RequestBody  MallCarouseManage mallCarouseManage ){

        System.out.println(mallCarouseManage);
        MallItemPic pic1=new MallItemPic();
        mallCarouseManage.setCarouselId(UUID.randomUUID().toString().replaceAll("\\-",""));


        List<Map> list= mallCarouseManage.getPic();
        System.out.println(list);
        Map map = list.get(0);
        System.out.println(map+"df");
        //Collection values = map.values();
        Set<Map.Entry<String,String>> set = map.entrySet();
        Iterator<Map.Entry<String, String>> it = set.iterator();

        List<String> list1=new ArrayList<>();
        while (it.hasNext()){
            Map.Entry<String, String> e = it.next();


            list1.add(e.getValue());


        }
        pic1.setItemPic(list1.get(1));
        pic1.setPicName(list1.get(0));
        pic1.setPicId(UUID.randomUUID().toString().replaceAll("\\-",""));

        pic1.setCarouselId(mallCarouseManage.getCarouselId());
        System.out.println(pic1+"1111111");
        itemPicServiceImpl.addPic(pic1);

             mallCarouseManage.setCarouselPic(list1.get(1));

        System.out.println(mallCarouseManage+"2222222");
             carouselServiceImpl.addCarouse(mallCarouseManage);


             return JsonResult.ok();
    }
//List<MallCarouseManage>
    @RequestMapping("/carousel/queryAll")
    public  JsonResult selAll(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5")int pageSize){
        PageResult<MallCarouseManage> list = carouselServiceImpl.selAll(pageNum, pageSize);
        return  JsonResult.ok(list);
    }

    @RequestMapping("/carousel/query")
    public JsonResult selCarousel(String carousel_id){
        carouselServiceImpl.selCarouse(carousel_id);
        return  JsonResult.ok();
    }


    @RequestMapping("/carousel/delete")
    public  JsonResult deleteCarousel(String carouselId,int sort){
        carouselServiceImpl.deleteCarouse(carouselId);

        PageResult<MallCarouseManage> list2 = carouselServiceImpl.selAll1(1, 5);
        sort3(list2.getRows(),sort);
        return  JsonResult.ok();
    }

    @RequestMapping("/carousel/update")
    public  JsonResult  updateCarousel(@RequestBody MallCarouseManage mallCarouseManage){
        int sort = mallCarouseManage.getSort();
        Date date=new Date();
        mallCarouseManage.setCarouselUpdate(date);
        System.out.println(mallCarouseManage);
        MallItemPic pic1=new MallItemPic();



        List<Map> list= mallCarouseManage.getPic();
        System.out.println(list);
        Map map = list.get(0);
        System.out.println(map+"df");
        //Collection values = map.values();
        Set<Map.Entry<String,String>> set = map.entrySet();
        Iterator<Map.Entry<String, String>> it = set.iterator();

        List<String> list1=new ArrayList<>();
        while (it.hasNext()){
            Map.Entry<String, String> e = it.next();


            list1.add(e.getValue());


        }
        pic1.setItemPic(list1.get(1));
        pic1.setPicName(list1.get(0));
        pic1.setCarouselId(mallCarouseManage.getCarouselId());
        itemPicServiceImpl.updatePic(pic1);

        System.out.println(pic1);


        mallCarouseManage.setCarouselPic(list1.get(1));


        carouselServiceImpl.updateCarouse(mallCarouseManage);
        updateSort(sort,mallCarouseManage);
        return  JsonResult.ok();
    }

    /**
     * 从前往后排序
     */
    public  void sort1(List<MallCarouseManage> list2){

       for(int i=0;i<list2.size();i++){
           for(int j=i+1;j<list2.size();j++){
               if (list2.get(i).getCarouselSort()==list2.get(j).getCarouselSort()){
                   list2.get(j).setCarouselSort((list2.get(j).getCarouselSort())+1);
                   carouselServiceImpl.updateCarouse(list2.get(j));
                   break;
               }
           }
       }

    }

    /**
     * 从后往前
     */
    public  void sort2(List<MallCarouseManage> list2){

        for(int i=list2.size()-1;i>=0;i--){
            for(int j=i-1;j>=0;j--){
                if (list2.get(i).getCarouselSort()==list2.get(j).getCarouselSort()){
                    list2.get(i).setCarouselSort((list2.get(i).getCarouselSort())-1);
                    carouselServiceImpl.updateCarouse(list2.get(i));
                    break;
                }
            }
        }

    }

    public  void sort3(List<MallCarouseManage> list2,int sort){

        for(int i=list2.size()-1;i>=sort-1;i--){
            list2.get(i).setCarouselSort((list2.get(i).getCarouselSort())-1);
            carouselServiceImpl.updateCarouse(list2.get(i));
        }

    }


    /**
     * 修改排序
     * @param sort
     * @param mallCarouseManage
     */
    public void updateSort(int sort,MallCarouseManage mallCarouseManage){
        /**
         * 查询所有数据,然后和修改的sort进行比较,如果有,就修改加一
         */
        PageResult<MallCarouseManage> list2 = carouselServiceImpl.selAll1(1, 5);
        if(sort>mallCarouseManage.getCarouselSort()){
            sort1(list2.getRows());
        }
        if(sort<mallCarouseManage.getCarouselSort()){
            sort2(list2.getRows());
        }
    }

}
View Code

 

 

主要逻辑就是,1,2,3,4修改后变成1,2,2,3,

两层for循环,一个一个的比较,相等,后一个序号加一。

 

从前往后排是,落后排名改成前面的排名,

从后往前排是,前面的排名改成落后的排名。

 /**
     * 从前往后排序
     */
    public  void sort1(List<MallCarouseManage> list2){

       for(int i=0;i<list2.size();i++){
           for(int j=i+1;j<list2.size();j++){
               if (list2.get(i).getCarouselSort()==list2.get(j).getCarouselSort()){
                   list2.get(j).setCarouselSort((list2.get(j).getCarouselSort())+1);
                   carouselServiceImpl.updateCarouse(list2.get(j));
                   break;
               }
           }
       }

    }

    /**
     * 从后往前
     */
    public  void sort2(List<MallCarouseManage> list2){

        for(int i=list2.size()-1;i>=0;i--){
            for(int j=i-1;j>=0;j--){
                if (list2.get(i).getCarouselSort()==list2.get(j).getCarouselSort()){
                    list2.get(i).setCarouselSort((list2.get(i).getCarouselSort())-1);
                    carouselServiceImpl.updateCarouse(list2.get(i));
                    break;
                }
            }
        }

    }

mybatis查询sql语句

 

 数据库字段:

posted @ 2019-12-11 14:01  wq9  阅读(728)  评论(0编辑  收藏  举报