feat(box): 添加纸箱品牌和产品拖拽排序功能

- 新增 BoxBrandDragCmd 和 BoxProductDragCmd 命令类
- 在 BoxBrandController 和 BoxProductController 中增加 dragBoxBrand 和 dragBoxProduct 接口
- 在 BoxBrandServiceI 和 BoxProductServiceI 接口中定义 drag 方法
- 实现 BoxBrandServiceImpl 和 BoxProductServiceImpl 的 drag 方法逻辑
- 在 BoxBrandGatewayImpl 和 BoxProductGatewayImpl 中实现拖拽排序的核心算法
- 为 BoxBrandDO 和 BoxProductDO 添加 sort 字段及数据库映射
- 提供批量重置排序的 SQL 注解方式实现
- 调整查询逻辑以支持按 sort 升序排列
- 补充 BoxProductListQry 查询条件字段并优化查询顺序
This commit is contained in:
shenyifei 2025-11-10 00:00:57 +08:00
parent ebfdedc01a
commit 03e24c868f
21 changed files with 361 additions and 39 deletions

View File

@ -8,6 +8,7 @@ import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.biz.api.BoxBrandServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandPageQry;
@ -82,4 +83,13 @@ public class BoxBrandController {
boxBrandService.destroy(boxBrandDestroyCmd);
return Response.buildSuccess();
}
@SaCheckLogin
// @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_BOX_BRAND_UPDATE})
@RequestMapping(value = "dragBoxBrand", method = {RequestMethod.PATCH, RequestMethod.PUT})
@Operation(summary = "纸箱品牌拖拽排序", method = "PATCH")
public Response dragBoxBrand(@RequestBody @Validated BoxBrandDragCmd boxBrandDragCmd) {
boxBrandService.drag(boxBrandDragCmd);
return Response.buildSuccess();
}
}

View File

@ -8,6 +8,7 @@ import com.alibaba.cola.dto.SingleResponse;
import com.xunhong.erp.turbo.api.biz.api.BoxProductServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductPageQry;
@ -82,4 +83,13 @@ public class BoxProductController {
boxProductService.destroy(boxProductDestroyCmd);
return Response.buildSuccess();
}
@SaCheckLogin
// @SaCheckPermission(value = {PermissionConstant.MDB_BUSINESS_BOX_PRODUCT_UPDATE})
@RequestMapping(value = "dragBoxProduct", method = {RequestMethod.PATCH, RequestMethod.PUT})
@Operation(summary = "纸箱产品拖拽排序", method = "PATCH")
public Response dragBoxProduct(@RequestBody @Validated BoxProductDragCmd boxProductDragCmd) {
boxProductService.drag(boxProductDragCmd);
return Response.buildSuccess();
}
}

View File

@ -0,0 +1,21 @@
package com.xunhong.erp.turbo.biz.app.executor.query;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDragCmd;
import com.xunhong.erp.turbo.biz.domain.gateway.BoxBrandGateway;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author shenyifei
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class BoxBrandDragCmdExe {
private final BoxBrandGateway boxBrandGateway;
public void execute(BoxBrandDragCmd boxBrandDragCmd) {
boxBrandGateway.drag(boxBrandDragCmd);
}
}

View File

@ -0,0 +1,21 @@
package com.xunhong.erp.turbo.biz.app.executor.query;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDragCmd;
import com.xunhong.erp.turbo.biz.domain.gateway.BoxProductGateway;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* @author shenyifei
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class BoxProductDragCmdExe {
private final BoxProductGateway boxProductGateway;
public void execute(BoxProductDragCmd boxProductDragCmd) {
boxProductGateway.drag(boxProductDragCmd);
}
}

View File

@ -3,6 +3,7 @@ package com.xunhong.erp.turbo.biz.app.service;
import com.xunhong.erp.turbo.api.biz.api.BoxBrandServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandPageQry;
@ -12,6 +13,7 @@ import com.xunhong.erp.turbo.base.dto.PageDTO;
import com.xunhong.erp.turbo.biz.app.executor.cmd.BoxBrandCreateCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.cmd.BoxBrandDestroyCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.cmd.BoxBrandUpdateCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxBrandDragCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxBrandListQryExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxBrandPageQryExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxBrandShowQryExe;
@ -33,10 +35,11 @@ public class BoxBrandServiceImpl implements BoxBrandServiceI {
private final BoxBrandCreateCmdExe boxBrandCreateCmdExe;
private final BoxBrandUpdateCmdExe boxBrandUpdateCmdExe;
private final BoxBrandPageQryExe boxBrandPageQryExe;
private final BoxBrandListQryExe boxBrandListQryExe;
private final BoxBrandShowQryExe boxBrandShowQryExe;
private final BoxBrandDestroyCmdExe boxBrandDestroyCmdExe;
private final BoxBrandPageQryExe boxBrandPageQryExe;
private final BoxBrandListQryExe boxBrandListQryExe;
private final BoxBrandDragCmdExe boxBrandDragCmdExe;
@Override
public BoxBrandVO create(BoxBrandCreateCmd boxBrandCreateCmd) {
@ -67,5 +70,9 @@ public class BoxBrandServiceImpl implements BoxBrandServiceI {
public void destroy(BoxBrandDestroyCmd boxBrandDestroyCmd) {
boxBrandDestroyCmdExe.execute(boxBrandDestroyCmd);
}
}
@Override
public void drag(BoxBrandDragCmd boxBrandDragCmd) {
boxBrandDragCmdExe.execute(boxBrandDragCmd);
}
}

View File

@ -3,6 +3,7 @@ package com.xunhong.erp.turbo.biz.app.service;
import com.xunhong.erp.turbo.api.biz.api.BoxProductServiceI;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductPageQry;
@ -12,6 +13,7 @@ import com.xunhong.erp.turbo.base.dto.PageDTO;
import com.xunhong.erp.turbo.biz.app.executor.cmd.BoxProductCreateCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.cmd.BoxProductDestroyCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.cmd.BoxProductUpdateCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxProductDragCmdExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxProductListQryExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxProductPageQryExe;
import com.xunhong.erp.turbo.biz.app.executor.query.BoxProductShowQryExe;
@ -33,10 +35,11 @@ public class BoxProductServiceImpl implements BoxProductServiceI {
private final BoxProductCreateCmdExe boxProductCreateCmdExe;
private final BoxProductUpdateCmdExe boxProductUpdateCmdExe;
private final BoxProductPageQryExe boxProductPageQryExe;
private final BoxProductListQryExe boxProductListQryExe;
private final BoxProductShowQryExe boxProductShowQryExe;
private final BoxProductDestroyCmdExe boxProductDestroyCmdExe;
private final BoxProductPageQryExe boxProductPageQryExe;
private final BoxProductListQryExe boxProductListQryExe;
private final BoxProductDragCmdExe boxProductDragCmdExe;
@Override
public BoxProductVO create(BoxProductCreateCmd boxProductCreateCmd) {
@ -67,5 +70,9 @@ public class BoxProductServiceImpl implements BoxProductServiceI {
public void destroy(BoxProductDestroyCmd boxProductDestroyCmd) {
boxProductDestroyCmdExe.execute(boxProductDestroyCmd);
}
}
@Override
public void drag(BoxProductDragCmd boxProductDragCmd) {
boxProductDragCmdExe.execute(boxProductDragCmd);
}
}

View File

@ -5,6 +5,7 @@ import com.alibaba.cola.dto.DTO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
@ -41,6 +42,11 @@ public class BoxBrand extends DTO {
*/
private Boolean status;
/**
* 排序
*/
private BigDecimal sort;
/**
* 产品列表
*/
@ -52,4 +58,3 @@ public class BoxBrand extends DTO {
private LocalDateTime createdAt;
}

View File

@ -62,10 +62,14 @@ public class BoxProduct extends DTO {
*/
private Boolean status;
/**
* 排序
*/
private BigDecimal sort;
/**
* 创建时间
*/
private LocalDateTime createdAt;
}

View File

@ -3,6 +3,7 @@ package com.xunhong.erp.turbo.biz.domain.gateway;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandPageQry;
@ -26,5 +27,6 @@ public interface BoxBrandGateway {
BoxBrand show(BoxBrandShowQry boxBrandShowQry);
void destroy(BoxBrandDestroyCmd boxBrandDestroyCmd);
}
void drag(BoxBrandDragCmd boxBrandDragCmd);
}

View File

@ -3,6 +3,7 @@ package com.xunhong.erp.turbo.biz.domain.gateway;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductPageQry;
@ -26,5 +27,6 @@ public interface BoxProductGateway {
BoxProduct show(BoxProductShowQry boxProductShowQry);
void destroy(BoxProductDestroyCmd boxProductDestroyCmd);
}
void drag(BoxProductDragCmd boxProductDragCmd);
}

View File

@ -9,6 +9,7 @@ import com.xunhong.erp.turbo.datasource.domain.entity.BaseDO;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.math.BigDecimal;
import java.util.List;
/**
@ -55,8 +56,13 @@ public class BoxBrandDO extends BaseDO<BoxBrandDO> {
@TableField(value = "status")
private Boolean status;
/**
* 排序
*/
@TableField(value = "sort")
private BigDecimal sort;
@TableField(exist = false)
private List<BoxProductDO> boxProductDOList;
}

View File

@ -80,4 +80,10 @@ public class BoxProductDO extends BaseDO<BoxProductDO> {
@TableField(value = "status")
private Boolean status;
/**
* 排序
*/
@TableField(value = "sort")
private BigDecimal sort;
}

View File

@ -2,11 +2,13 @@ package com.xunhong.erp.turbo.biz.infrastructure.gateway;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandPageQry;
@ -20,9 +22,11 @@ import com.xunhong.erp.turbo.biz.infrastructure.mapper.BoxBrandMapper;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.BoxProductMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @author shenyifei
@ -32,7 +36,6 @@ import java.util.Objects;
public class BoxBrandGatewayImpl implements BoxBrandGateway {
private final BoxBrandMapper boxBrandMapper;
private final BoxBrandConvert boxBrandConvert;
private final BoxProductMapper boxProductMapper;
@Override
@ -49,11 +52,27 @@ public class BoxBrandGatewayImpl implements BoxBrandGateway {
queryWrapper.eq(Objects.nonNull(boxBrandPageQry.getStatus()), BoxBrandDO::getStatus, boxBrandPageQry.getStatus());
queryWrapper.eq(Objects.nonNull(boxBrandPageQry.getBrandId()), BoxBrandDO::getBrandId, boxBrandPageQry.getBrandId());
queryWrapper.eq(Objects.nonNull(boxBrandPageQry.getType()), BoxBrandDO::getType, boxBrandPageQry.getType());
queryWrapper.eq(Objects.nonNull(boxBrandPageQry.getStatus()), BoxBrandDO::getStatus, boxBrandPageQry.getStatus());
queryWrapper.orderByAsc(BoxBrandDO::getSort);
queryWrapper.orderByDesc(BoxBrandDO::getCreatedAt);
IPage<BoxBrandDO> page = new Page<>(boxBrandPageQry.getPageIndex(), boxBrandPageQry.getPageSize());
page = boxBrandMapper.selectPage(page, queryWrapper);
List<Long> brandIdList = page.getRecords().stream().map(BoxBrandDO::getBrandId).toList();
if (CollUtil.isNotEmpty(brandIdList)) {
LambdaQueryWrapper<BoxProductDO> queryWrapper1 = Wrappers.lambdaQuery(BoxProductDO.class);
queryWrapper1.in(BoxProductDO::getBrandId, brandIdList);
List<BoxProductDO> boxProductDOList = boxProductMapper.selectList(queryWrapper1);
page.getRecords().forEach(boxBrandDO -> {
boxBrandDO.setBoxProductDOList(boxProductDOList.stream()
.filter(boxProductDO -> boxProductDO.getBrandId().equals(boxBrandDO.getBrandId()))
.toList());
});
}
return page.convert(boxBrandConvert::toBoxBrand);
}
@ -63,6 +82,9 @@ public class BoxBrandGatewayImpl implements BoxBrandGateway {
queryWrapper.eq(Objects.nonNull(boxBrandListQry.getStatus()), BoxBrandDO::getStatus, boxBrandListQry.getStatus());
queryWrapper.eq(Objects.nonNull(boxBrandListQry.getBrandId()), BoxBrandDO::getBrandId, boxBrandListQry.getBrandId());
queryWrapper.eq(Objects.nonNull(boxBrandListQry.getType()), BoxBrandDO::getType, boxBrandListQry.getType());
queryWrapper.eq(Objects.nonNull(boxBrandListQry.getStatus()), BoxBrandDO::getStatus, boxBrandListQry.getStatus());
queryWrapper.orderByAsc(BoxBrandDO::getSort);
queryWrapper.orderByDesc(BoxBrandDO::getCreatedAt);
List<BoxBrandDO> boxBrandDOList = boxBrandMapper.selectList(queryWrapper);
@ -115,5 +137,50 @@ public class BoxBrandGatewayImpl implements BoxBrandGateway {
BoxBrandDO boxBrandDO = boxBrandMapper.selectOne(queryWrapper);
boxBrandDO.deleteById();
}
@Override
@Transactional
public void drag(BoxBrandDragCmd boxBrandDragCmd) {
reorderTask(boxBrandDragCmd.getCurrentId(), boxBrandDragCmd.getPrevId(), boxBrandDragCmd.getNextId());
}
// 拖拽排序核心方法
public void reorderTask(Long currentId, Long prevId, Long nextId) {
// 获取相邻元素的sort值
Double prevSort = Optional.ofNullable(prevId)
.map(id -> boxBrandMapper.selectById(id).getSort().doubleValue())
.orElse(null);
Double nextSort = Optional.ofNullable(nextId)
.map(id -> boxBrandMapper.selectById(id).getSort().doubleValue())
.orElse(null);
// 计算新sort值
Double newSort = calculateNewSort(prevSort, nextSort);
// 检查是否需要重整排序
if (needResetSort(prevSort, nextSort)) {
boxBrandMapper.batchResetSort(boxBrandMapper.selectById(currentId).getBrandId());
} else {
// 更新当前任务sort值
boxBrandMapper.update(null, new LambdaUpdateWrapper<BoxBrandDO>()
.eq(BoxBrandDO::getBrandId, currentId)
.set(BoxBrandDO::getSort, newSort)
);
}
}
private Double calculateNewSort(Double prevSort, Double nextSort) {
if (prevSort == null && nextSort != null) {
return nextSort - 1000; // 插入到开头
} else if (nextSort == null && prevSort != null) {
return prevSort + 1000; // 插入到末尾
} else if (prevSort == null && nextSort == null) {
return 0.0;
} else {
return (prevSort + nextSort) / 2; // 插入中间
}
}
private boolean needResetSort(Double prevSort, Double nextSort) {
return prevSort != null && nextSort != null
&& (nextSort - prevSort) < 1.0; // 判断间隙是否耗尽
}
}

View File

@ -2,11 +2,13 @@ package com.xunhong.erp.turbo.biz.infrastructure.gateway;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductPageQry;
@ -18,9 +20,11 @@ import com.xunhong.erp.turbo.biz.infrastructure.entity.BoxProductDO;
import com.xunhong.erp.turbo.biz.infrastructure.mapper.BoxProductMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
/**
* @author shenyifei
@ -43,12 +47,11 @@ public class BoxProductGatewayImpl implements BoxProductGateway {
public IPage<BoxProduct> page(BoxProductPageQry boxProductPageQry) {
LambdaQueryWrapper<BoxProductDO> queryWrapper = Wrappers.lambdaQuery(BoxProductDO.class);
queryWrapper.like(StrUtil.isNotBlank(boxProductPageQry.getName()), BoxProductDO::getName, boxProductPageQry.getName());
queryWrapper.like(StrUtil.isNotBlank(boxProductPageQry.getRemark()), BoxProductDO::getRemark, boxProductPageQry.getRemark());
queryWrapper.eq(Objects.nonNull(boxProductPageQry.getStatus()), BoxProductDO::getStatus, boxProductPageQry.getStatus());
queryWrapper.eq(Objects.nonNull(boxProductPageQry.getBrandId()), BoxProductDO::getBrandId, boxProductPageQry.getBrandId());
queryWrapper.eq(Objects.nonNull(boxProductPageQry.getSpecType()), BoxProductDO::getSpecType, boxProductPageQry.getSpecType());
queryWrapper.eq(Objects.nonNull(boxProductPageQry.getType()), BoxProductDO::getType, boxProductPageQry.getType());
queryWrapper.eq(Objects.nonNull(boxProductPageQry.getBrandId()), BoxProductDO::getBrandId, boxProductPageQry.getBrandId());
queryWrapper.eq(Objects.nonNull(boxProductPageQry.getStatus()), BoxProductDO::getStatus, boxProductPageQry.getStatus());
queryWrapper.orderByAsc(BoxProductDO::getSort);
queryWrapper.orderByDesc(BoxProductDO::getCreatedAt);
IPage<BoxProductDO> page = new Page<>(boxProductPageQry.getPageIndex(), boxProductPageQry.getPageSize());
@ -60,7 +63,13 @@ public class BoxProductGatewayImpl implements BoxProductGateway {
@Override
public List<BoxProduct> list(BoxProductListQry boxProductListQry) {
LambdaQueryWrapper<BoxProductDO> queryWrapper = Wrappers.lambdaQuery(BoxProductDO.class);
queryWrapper.eq(Objects.nonNull(boxProductListQry.getType()), BoxProductDO::getType, boxProductListQry.getType());
queryWrapper.like(StrUtil.isNotBlank(boxProductListQry.getName()), BoxProductDO::getName, boxProductListQry.getName());
queryWrapper.eq(Objects.nonNull(boxProductListQry.getSpecType()), BoxProductDO::getSpecType, boxProductListQry.getSpecType());
queryWrapper.eq(Objects.nonNull(boxProductListQry.getBrandId()), BoxProductDO::getBrandId, boxProductListQry.getBrandId());
queryWrapper.eq(Objects.nonNull(boxProductListQry.getStatus()), BoxProductDO::getStatus, boxProductListQry.getStatus());
queryWrapper.orderByAsc(BoxProductDO::getSort);
queryWrapper.orderByDesc(BoxProductDO::getCreatedAt);
List<BoxProductDO> boxProductDOList = boxProductMapper.selectList(queryWrapper);
return boxProductDOList.stream().map(boxProductConvert::toBoxProduct).toList();
}
@ -98,4 +107,50 @@ public class BoxProductGatewayImpl implements BoxProductGateway {
BoxProductDO boxProductDO = boxProductMapper.selectOne(queryWrapper);
boxProductDO.deleteById();
}
@Override
@Transactional
public void drag(BoxProductDragCmd boxProductDragCmd) {
reorderTask(boxProductDragCmd.getCurrentId(), boxProductDragCmd.getPrevId(), boxProductDragCmd.getNextId());
}
// 拖拽排序核心方法
public void reorderTask(Long currentId, Long prevId, Long nextId) {
// 获取相邻元素的sort值
Double prevSort = Optional.ofNullable(prevId)
.map(id -> boxProductMapper.selectById(id).getSort().doubleValue())
.orElse(null);
Double nextSort = Optional.ofNullable(nextId)
.map(id -> boxProductMapper.selectById(id).getSort().doubleValue())
.orElse(null);
// 计算新sort值
Double newSort = calculateNewSort(prevSort, nextSort);
// 检查是否需要重整排序
if (needResetSort(prevSort, nextSort)) {
boxProductMapper.batchResetSort(boxProductMapper.selectById(currentId).getProductId());
} else {
// 更新当前任务sort值
boxProductMapper.update(null, new LambdaUpdateWrapper<BoxProductDO>()
.eq(BoxProductDO::getProductId, currentId)
.set(BoxProductDO::getSort, newSort)
);
}
}
private Double calculateNewSort(Double prevSort, Double nextSort) {
if (prevSort == null && nextSort != null) {
return nextSort - 1000; // 插入到开头
} else if (nextSort == null && prevSort != null) {
return prevSort + 1000; // 插入到末尾
} else if (prevSort == null && nextSort == null) {
return 0.0;
} else {
return (prevSort + nextSort) / 2; // 插入中间
}
}
private boolean needResetSort(Double prevSort, Double nextSort) {
return prevSort != null && nextSort != null
&& (nextSort - prevSort) < 1.0; // 判断间隙是否耗尽
}
}

View File

@ -3,11 +3,19 @@ package com.xunhong.erp.turbo.biz.infrastructure.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xunhong.erp.turbo.biz.infrastructure.entity.BoxBrandDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* @author shenyifei
*/
@Mapper
public interface BoxBrandMapper extends BaseMapper<BoxBrandDO> {
// 批量更新排序值用于间隙重整
@Update("UPDATE box_brand t " +
"JOIN (SELECT brand_id, (ROW_NUMBER() OVER (ORDER BY sort) - 1) * 1000 AS new_sort " +
"FROM box_brand WHERE brand_id = #{brandId}) AS sorted_rows " +
"ON t.brand_id = sorted_rows.brand_id " +
"SET t.sort = sorted_rows.new_sort WHERE t.brand_id = #{brandId}")
void batchResetSort(@Param("brandId") Long brandId);
}

View File

@ -3,11 +3,19 @@ package com.xunhong.erp.turbo.biz.infrastructure.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xunhong.erp.turbo.biz.infrastructure.entity.BoxProductDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
/**
* @author shenyifei
*/
@Mapper
public interface BoxProductMapper extends BaseMapper<BoxProductDO> {
// 批量更新排序值用于间隙重整
@Update("UPDATE box_product t " +
"JOIN (SELECT product_id, (ROW_NUMBER() OVER (ORDER BY sort) - 1) * 1000 AS new_sort " +
"FROM box_product WHERE product_id = #{productId}) AS sorted_rows " +
"ON t.product_id = sorted_rows.product_id " +
"SET t.sort = sorted_rows.new_sort WHERE t.product_id = #{productId}")
void batchResetSort(@Param("productId") Long productId);
}

View File

@ -2,6 +2,7 @@ package com.xunhong.erp.turbo.api.biz.api;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxBrandUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxBrandPageQry;
@ -26,5 +27,6 @@ public interface BoxBrandServiceI {
BoxBrandVO show(BoxBrandShowQry boxBrandShowQry);
void destroy(BoxBrandDestroyCmd boxBrandDestroyCmd);
}
void drag(BoxBrandDragCmd boxBrandDragCmd);
}

View File

@ -2,6 +2,7 @@ package com.xunhong.erp.turbo.api.biz.api;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductCreateCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDestroyCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductDragCmd;
import com.xunhong.erp.turbo.api.biz.dto.cmd.BoxProductUpdateCmd;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductListQry;
import com.xunhong.erp.turbo.api.biz.dto.qry.BoxProductPageQry;
@ -26,5 +27,6 @@ public interface BoxProductServiceI {
BoxProductVO show(BoxProductShowQry boxProductShowQry);
void destroy(BoxProductDestroyCmd boxProductDestroyCmd);
}
void drag(BoxProductDragCmd boxProductDragCmd);
}

View File

@ -0,0 +1,33 @@
package com.xunhong.erp.turbo.api.biz.dto.cmd;
import com.xunhong.erp.turbo.base.dto.Command;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author shenyifei
*/
@Data
@Schema(title = "纸箱品牌拖拽")
@EqualsAndHashCode(callSuper = true)
public class BoxBrandDragCmd extends Command {
/**
* 相邻元素
*/
@Schema(title = "相邻元素前")
private Long prevId;
/**
* 相邻元素
*/
@Schema(title = "相邻元素后")
private Long nextId;
/**
* 当前元素
*/
@Schema(title = "当前元素")
private Long currentId;
}

View File

@ -0,0 +1,33 @@
package com.xunhong.erp.turbo.api.biz.dto.cmd;
import com.xunhong.erp.turbo.base.dto.Command;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author shenyifei
*/
@Data
@Schema(title = "纸箱产品拖拽")
@EqualsAndHashCode(callSuper = true)
public class BoxProductDragCmd extends Command {
/**
* 相邻元素
*/
@Schema(title = "相邻元素前")
private Long prevId;
/**
* 相邻元素
*/
@Schema(title = "相邻元素后")
private Long nextId;
/**
* 当前元素
*/
@Schema(title = "当前元素")
private Long currentId;
}

View File

@ -1,6 +1,7 @@
package com.xunhong.erp.turbo.api.biz.dto.qry;
import com.xunhong.erp.turbo.api.biz.dto.enums.BoxBrandTypeEnum;
import com.xunhong.erp.turbo.api.biz.dto.enums.BoxProductSpecTypeEnum;
import com.xunhong.erp.turbo.base.dto.Query;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@ -17,6 +18,18 @@ public class BoxProductListQry extends Query {
@Schema(title = "纸箱产品ID", type = "string")
private Long productId;
@Schema(title = "纸箱产品名称", type = "string")
private String name;
@Schema(title = "纸箱品牌ID", type = "string")
private String brandId;
/**
* 规格1_2粒装2_4粒装
*/
@Schema(title = "规格1_2粒装2_4粒装")
private BoxProductSpecTypeEnum specType;
/**
* 品牌类型1_我方纸箱2_瓜农纸箱3_第三方纸箱
*/