拖动排序
This commit is contained in:
@@ -18,6 +18,7 @@ import java.util.List;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付方式管理
|
* 支付方式管理
|
||||||
|
*
|
||||||
* @author Administrator
|
* @author Administrator
|
||||||
*/
|
*/
|
||||||
@RestController
|
@RestController
|
||||||
@@ -60,7 +61,19 @@ public class ShopPayTypeController {
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
public CzgResult<Boolean> add(@RequestBody @Validated(InsertGroup.class) ShopPayTypeAddDTO shopPayTypeAddDTO) {
|
public CzgResult<Boolean> add(@RequestBody @Validated(InsertGroup.class) ShopPayTypeAddDTO shopPayTypeAddDTO) {
|
||||||
ShopPayType shopPayType = BeanUtil.copyProperties(shopPayTypeAddDTO, ShopPayType.class);
|
ShopPayType shopPayType = BeanUtil.copyProperties(shopPayTypeAddDTO, ShopPayType.class);
|
||||||
return CzgResult.success(shopPayTypeService.add(StpKit.USER.getShopId(),shopPayType));
|
return CzgResult.success(shopPayTypeService.add(StpKit.USER.getShopId(), shopPayType));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支付类型拖动排序
|
||||||
|
*
|
||||||
|
* @param shopId 店铺ID
|
||||||
|
* @param id 拖动的支付类型ID
|
||||||
|
* @param targetSort 目标位置排序号
|
||||||
|
*/
|
||||||
|
@PostMapping("/sort")
|
||||||
|
public CzgResult<Void> sort(@RequestParam Long shopId, @RequestParam Integer id, @RequestParam Integer targetSort) {
|
||||||
|
shopPayTypeService.updateSort(shopId, id, targetSort);
|
||||||
|
return CzgResult.success();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,12 @@ public class ShopPayType implements Serializable {
|
|||||||
@Id(keyType = KeyType.Auto)
|
@Id(keyType = KeyType.Auto)
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图标
|
||||||
|
*/
|
||||||
|
private String icon;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付类型cash,alipay,weixin,deposit,arrears,virtual,member-account
|
* 支付类型cash,alipay,weixin,deposit,arrears,virtual,member-account
|
||||||
*/
|
*/
|
||||||
@@ -57,26 +63,6 @@ public class ShopPayType implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private Long shopId;
|
private Long shopId;
|
||||||
|
|
||||||
/**
|
|
||||||
* 0允许退款 1-不允许退款
|
|
||||||
*/
|
|
||||||
private Integer isRefundable;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 是否打开钱箱
|
|
||||||
*/
|
|
||||||
private Integer isOpenCashDrawer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 0不是 1是 [系统级支付]
|
|
||||||
*/
|
|
||||||
private Integer isSystem;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 0-非虚拟 1虚拟 virtual
|
|
||||||
*/
|
|
||||||
private Integer isIdeal;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 0-不显示1显示
|
* 0-不显示1显示
|
||||||
*/
|
*/
|
||||||
@@ -91,10 +77,4 @@ public class ShopPayType implements Serializable {
|
|||||||
private LocalDateTime createTime;
|
private LocalDateTime createTime;
|
||||||
@Column(onInsertValue = "now()")
|
@Column(onInsertValue = "now()")
|
||||||
private LocalDateTime updateTime;
|
private LocalDateTime updateTime;
|
||||||
|
|
||||||
/**
|
|
||||||
* 图标
|
|
||||||
*/
|
|
||||||
private String icon;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,4 +21,12 @@ public interface ShopPayTypeService extends IService<ShopPayType> {
|
|||||||
Boolean edit(Long shopId, ShopPayTypeDTO shopPayTypeDTO);
|
Boolean edit(Long shopId, ShopPayTypeDTO shopPayTypeDTO);
|
||||||
|
|
||||||
void addInfo(Long shopId);
|
void addInfo(Long shopId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 拖动排序
|
||||||
|
* @param shopId 店铺ID
|
||||||
|
* @param payTypeId 当前拖动的支付类型ID
|
||||||
|
* @param targetSort 目标排序号
|
||||||
|
*/
|
||||||
|
void updateSort(Long shopId, Integer payTypeId, Integer targetSort);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import com.czg.account.entity.ShopPayType;
|
|||||||
import com.czg.account.service.ShopPayTypeService;
|
import com.czg.account.service.ShopPayTypeService;
|
||||||
import com.czg.service.account.mapper.ShopPayTypeMapper;
|
import com.czg.service.account.mapper.ShopPayTypeMapper;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -24,11 +25,11 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class ShopPayTypeServiceImpl extends ServiceImpl<ShopPayTypeMapper, ShopPayType> implements ShopPayTypeService{
|
public class ShopPayTypeServiceImpl extends ServiceImpl<ShopPayTypeMapper, ShopPayType> implements ShopPayTypeService{
|
||||||
private final ArrayList<ShopPayType> payTypeArrayList = new ArrayList<>(){{
|
private final ArrayList<ShopPayType> payTypeArrayList = new ArrayList<>(){{
|
||||||
add(new ShopPayType().setPayType("cash").setPayName("现金").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/319005ffef734aa6a88d432459fa59d8.png"));
|
add(new ShopPayType().setPayType("cash").setPayName("现金").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/319005ffef734aa6a88d432459fa59d8.png").setSorts(1));
|
||||||
add(new ShopPayType().setPayType("bank").setPayName("银行卡").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/98e6199cc61a480b9c63617600b2f2fe.png"));
|
add(new ShopPayType().setPayType("bank").setPayName("银行卡").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/98e6199cc61a480b9c63617600b2f2fe.png").setSorts(2));
|
||||||
add(new ShopPayType().setPayType("scanCode").setPayName("扫码支付").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/3a344b46638547df81d5857a3c8cac66.png"));
|
add(new ShopPayType().setPayType("scanCode").setPayName("扫码支付").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/3a344b46638547df81d5857a3c8cac66.png").setSorts(3));
|
||||||
add(new ShopPayType().setPayType("deposit").setPayName("储值卡").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/6f778df10ea74142a6cba310c2a19063.png"));
|
add(new ShopPayType().setPayType("deposit").setPayName("储值卡").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/6f778df10ea74142a6cba310c2a19063.png").setSorts(4));
|
||||||
add(new ShopPayType().setPayType("virtual").setPayName("挂账支付").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/6f778df10ea74142a6cba310c2a19063.png").setIsShowShortcut(0));
|
add(new ShopPayType().setPayType("virtual").setPayName("挂账支付").setIcon("https://cashier-oss.oss-cn-beijing.aliyuncs.com/upload/3/6f778df10ea74142a6cba310c2a19063.png").setIsShowShortcut(0).setSorts(5));
|
||||||
}};
|
}};
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -44,6 +45,50 @@ public class ShopPayTypeServiceImpl extends ServiceImpl<ShopPayTypeMapper, ShopP
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public void updateSort(Long shopId, Integer payTypeId, Integer targetSort) {
|
||||||
|
// 1. 查询当前拖动的项
|
||||||
|
ShopPayType current = getById(payTypeId);
|
||||||
|
if (current == null) {
|
||||||
|
throw new RuntimeException("支付类型不存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer oldSort = current.getSorts();
|
||||||
|
// 如果位置没变,不处理
|
||||||
|
if (oldSort.equals(targetSort)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 查询同店铺下所有支付类型(按排序升序)
|
||||||
|
QueryWrapper wrapper = QueryWrapper.create()
|
||||||
|
.eq(ShopPayType::getShopId, shopId)
|
||||||
|
.orderBy(ShopPayType::getSorts, true);
|
||||||
|
|
||||||
|
List<ShopPayType> list = list(wrapper);
|
||||||
|
|
||||||
|
// 3. 重新排序(核心算法)
|
||||||
|
for (int i = 0; i < list.size(); i++) {
|
||||||
|
ShopPayType item = list.get(i);
|
||||||
|
if (item.getId().equals(payTypeId)) {
|
||||||
|
// 当前拖动项,设置目标排序
|
||||||
|
item.setSorts(targetSort);
|
||||||
|
} else if (oldSort < targetSort) {
|
||||||
|
// 向下拖动:原排序 < 目标排序 → 中间项排序 -1
|
||||||
|
if (item.getSorts() > oldSort && item.getSorts() <= targetSort) {
|
||||||
|
item.setSorts(item.getSorts() - 1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 向上拖动:原排序 > 目标排序 → 中间项排序 +1
|
||||||
|
if (item.getSorts() < oldSort && item.getSorts() >= targetSort) {
|
||||||
|
item.setSorts(item.getSorts() + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 4. 批量更新排序
|
||||||
|
updateBatch(list);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<ShopPayType> getList(Long shopId) {
|
public List<ShopPayType> getList(Long shopId) {
|
||||||
List<ShopPayType> list = list(new QueryWrapper().eq(ShopPayType::getShopId, StpKit.USER.getShopId()));
|
List<ShopPayType> list = list(new QueryWrapper().eq(ShopPayType::getShopId, StpKit.USER.getShopId()));
|
||||||
|
|||||||
Reference in New Issue
Block a user