sql中上下移置顶置底置换的排序实现(spring cloud+mybatis plus架构中) --菜鸟小回

sql中上下移置顶置底置换的排序实现

// controller
 	@ApiOperation(value = "置顶 ")
    @PostMapping("/top")
    public ResultObject head(@RequestBody Map map) {
        iDyMallSpuService.head(map);
        return new ResultObject(StatusCode.OK, "置顶成功");
    }

    @ApiOperation(value = "置底 ")
    @PostMapping("/bottom")
    public ResultObject tail(@RequestBody Map map) {
       iDyMallSpuService.tail(map);
        return new ResultObject(StatusCode.OK, "置底成功");
    }

    @ApiOperation(value = "互换 ")
    @PostMapping("/exchange")
    public ResultObject exchange(@RequestBody  Map map) {
        Integer ownerId = jsonObject.getInt("ownerId");
        Integer otherId = jsonObject.getInt("otherId");
        if (iDyMallSpuService.exchange(ownerId, otherId)) {
            return new ResultObject(StatusCode.OK, "互换位置成功");
        } else {
            return new ResultObject(StatusCode.ERROR, "互换位置失败");
        }
    }

    @ApiOperation(value = "上移 ")
    @PostMapping("/upper")
    public ResultObject increase(@RequestBody Map map) {
        Long presentId = MapUtil.getLong(map,"id");
        if (iDyMallSpuService.presentToBefore(presentId)) {
            return new ResultObject(StatusCode.OK, "上移成功");
        } else {
            return new ResultObject(StatusCode.ERROR, "上移失败");
        }
    }

    @ApiOperation(value = "下移 ")
    @PostMapping("/lower")
    public ResultObject lower(@RequestBody Map map) {
        Long presentId = MapUtil.getLong(map,"id");
        if (iDyMallSpuService.presentToAfter(presentId)) {
            return new ResultObject(StatusCode.OK, "下移成功");
        } else {
            return new ResultObject(StatusCode.ERROR, "下移失败");
        }
    }
//service
 @Override
    public Boolean head(Map map){
        Long id = MapUtil.getLong(map, "id");
        Long shopId = dySkuMapper.selectShopIdBySkuId(id);
        DySku dySku = iDySkuService.selectById(id);
        Long min = dySkuMapper.minSort(shopId);
        dySku.setSort(min - 1);
        return iDySkuService.updateById(dySku);
    }
    @Override
    public Boolean tail(Map map){
        Long id = MapUtil.getLong(map, "id");
        DySku dySku = iDySkuService.selectById(id);
        Long shopId = dySkuMapper.selectShopIdBySkuId(id);
        Long max = dySkuMapper.maxSort(shopId);
        dySku.setSort(max + 1);
        return iDySkuService.updateById(dySku);
    }
    @Override
    public Boolean exchange(Integer ownerId, Integer otherId){
        DySku owner = iDySkuService.selectById(ownerId);
        DySku other = iDySkuService.selectById(otherId);
        long ownerSort = owner.getSort();
        long otherSort = other.getSort();
        owner.setSort(otherSort);
        other.setSort(ownerSort);
        return iDySkuService.insertOrUpdateBatch(Lists.newArrayList(owner, other));
    }
    @Override
    public Boolean presentToBefore(Long presentId){
        Long shopId = dySkuMapper.selectShopIdBySkuId(presentId);
        List dySkus = dySkuMapper.presentAndBefore(presentId,shopId);
        DySku present = dySkus.get(0);
        DySku before = dySkus.get(1);
        long ownerSort = present.getSort();
        long otherSort = before.getSort();
        present.setSort(otherSort);
        before.setSort(ownerSort);
        return iDySkuService.insertOrUpdateBatch(Lists.newArrayList(present, before));
    }
    @Override
    public Boolean presentToAfter(Long presentId){
        Long shopId = dySkuMapper.selectShopIdBySkuId(presentId);
        List dySkus = dySkuMapper.presentAndAfter(presentId,shopId);
        DySku present = dySkus.get(0);
        DySku after = dySkus.get(1);
        long ownerSort = present.getSort();
        long otherSort = after.getSort();
        present.setSort(otherSort);
        after.setSort(ownerSort);
        return iDySkuService.insertOrUpdateBatch(Lists.newArrayList(present, after));
    }
//mapper
@Select("SELECT p.shop_id shopId FROM dy_sku k LEFT JOIN dy_spu p ON k.spu_id=p.id where k.id=#{skuId}")
Long selectShopIdBySkuId(@Param("skuId") Long skuId);

@Select("SELECT MIN( k.sort ) FROM dy_sku k LEFT JOIN dy_spu p ON k.spu_id=p.id WHERE p.shop_id =#{shopId}")
Long minSort(@Param("shopId")Long shopId);

@Select("SELECT MAX( k.sort ) FROM dy_sku k LEFT JOIN dy_spu p ON k.spu_id=p.id WHERE p.shop_id =#{shopId}")
Long maxSort(@Param("shopId")Long shopId);

List presentAndBefore(@Param("ownerId")Long ownerId,@Param("shopId")Long shopId);

List presentAndAfter(@Param("ownerId")Long ownerId,@Param("shopId")Long shopId);
//xml


你可能感兴趣的:(Sring,Cloud,MySQL,mysql,java,spring)