带分页的列表移动排序(交换排序位置)、拖拽排序示例
/**
* 排序 上下移动
*
* @param dto
*/
@Transactional(rollbackFor = Exception.class)
public void moveElectricBalanceDevice(ElectricBalanceMoveDto dto) {
// 獲取稱菜機列表,找出移動的稱菜機位置
String productKey = iotProductKey.getElectricBalance();
String storeId = dto.getStoreId();
if (StringUtils.isEmpty(productKey)) {
log.error("请联系管理员确认产品——称菜机productKey");
return;
}
Map<String, Map<String, String>> devicePropertiesMap = new HashMap<>();
int page = dto.getCurrentPage();
int size = dto.getCurrentSize();
PageVO<DeviceVO> devicePage = weighDeviceService.queryUserDevice4Sort(productKey, page, size, storeId, devicePropertiesMap);
List<DeviceVO> collect = devicePage.getContent().stream().sorted(Comparator.comparingInt(com.ut.cook.catering.basic.DeviceListVO::getIndex)).collect(Collectors.toList());
if (CollectionUtils.isEmpty(collect) || devicePage.getTotalElements() < 2) {
log.error("该门店无足够可用打菜机");
return;
}
// 当前处理称菜机在数组中的位置,处理前端所传的全局index
int index = dto.getIndex() % size;
long sourceIndex = collect.get(index).getIndex();
DeviceVO sourceVo = collect.get(index);
String deviceName = sourceVo.getDeviceName();
long aimIndex;
// 最后一页最后一个向下,前端已做处理(置灰),后端处理需前端传totalPage
DeviceVO affected;
if (dto.getMove()) {
if (0 == index) {
if (page == 1) {
log.error("已在顶部");
return;
}
// 非第一页的第0个,向上的话受影响的是前一页的最后一个
PageVO<DeviceVO> devicePageAffected = weighDeviceService.queryUserDevice4Sort(productKey, page - 1, size, storeId, devicePropertiesMap);
affected = devicePageAffected.getContent().get(size - 1);
log.info("受影响称菜机:{}", affected.getDeviceCgid());
aimIndex = affected.getIndex();
} else {
affected = collect.get(index - 1);
aimIndex = affected.getIndex();
}
} else {
if ((size - 1) == index) {
PageVO<DeviceVO> devicePageAffected = weighDeviceService.queryUserDevice4Sort(productKey, page + 1, size, storeId, devicePropertiesMap);
affected = devicePageAffected.getContent().get(0);
aimIndex = affected.getIndex();
} else {
affected = collect.get(index + 1);
aimIndex = affected.getIndex();
}
}
String aimDeviceName = affected.getDeviceName();
// 交換排序Index,修改設備的排序索引 传期望值(数组)修改属性
BatchUpdateDto indexDto = new BatchUpdateDto();
BatchUpdateDto.UpdateDeviceInfo updateDeviceInfo = new BatchUpdateDto.UpdateDeviceInfo();
updateDeviceInfo.setDeviceName(deviceName).setIndex(aimIndex).setProductKey(productKey);
BatchUpdateDto.UpdateDeviceInfo updateDeviceInfo1 = new BatchUpdateDto.UpdateDeviceInfo();
updateDeviceInfo1.setDeviceName(aimDeviceName).setIndex(sourceIndex).setProductKey(productKey);
List<BatchUpdateDto.UpdateDeviceInfo> list = new ArrayList<>();
list.add(updateDeviceInfo);
list.add(updateDeviceInfo1);
indexDto.setUpdateDeviceInfos(list);
try {
iotServer.batchUpdate(indexDto);
} catch (Exception e) {
log.error("排序操作失败");
throw new BaseRetException(BaseRet.createFailureRet("IOT批量更新失败"));
}
}
拖拽排序
@Transactional
public Result dragSort(Map<String, Object> condition) {
String dragId = Convert.toStr(condition.get("dragId"));//拖动的id
String goalId = Convert.toStr(condition.get("goalId"));//目标位置id下面
Personnel dragPersonnel = new Personnel().selectById(dragId);//需要移动的演职人员
Personnel goalPersonnel = new Personnel().selectById(goalId);//移动目标位置的上一个演职人员,没有则是置顶
DateTime date = DateUtil.date();
if (dragPersonnel != null){
condition.put("companyId",dragPersonnel.getCompanyId());
if (goalPersonnel == null){//置顶
condition.put("isMoveUp",2);//上移
condition.put("id",dragPersonnel.getId());//移动演职人员的id
condition.put("type",dragPersonnel.getTypeId());//移动演职人员的类型
//末位sort
Integer lastValueSort = baseMapper.lastSort(condition);
condition.put("start",lastValueSort);//开始序号
condition.put("end",dragPersonnel.getSort());//结束序号
baseMapper.updateSort(condition);
dragPersonnel.setSort(lastValueSort);
dragPersonnel.setUpdateTime(date);
dragPersonnel.updateById();
}else {
//上移
if (dragPersonnel.getSort() < goalPersonnel.getSort()){
condition.put("isMoveUp",2);//上移
condition.put("id",dragPersonnel.getId());//移动演职人员的id
condition.put("type",dragPersonnel.getTypeId());//移动演职人员的类型
condition.put("start",goalPersonnel.getSort()-1);//开始序号
condition.put("end",dragPersonnel.getSort());//结束序号
baseMapper.updateSort(condition);
dragPersonnel.setSort(goalPersonnel.getSort()-1);//目标位置演职人员下一个序号
dragPersonnel.setUpdateTime(date);
dragPersonnel.updateById();
}else {//下移
condition.put("isMoveUp",1);//下移
condition.put("id",dragPersonnel.getId());//移动演职人员的id
condition.put("type",dragPersonnel.getTypeId());//移动演职人员的类型
condition.put("start",dragPersonnel.getSort()-1);//开始序号
condition.put("end",goalPersonnel.getSort()-1);//结束序号
baseMapper.updateSort(condition);
dragPersonnel.setSort(goalPersonnel.getSort());//目标位置演职人员序号
dragPersonnel.setUpdateTime(date);
dragPersonnel.updateById();
}
}
return ResultGenerator.genOkResult("排序成功");
}
return ResultGenerator.genFailedResult("找不到需要排序的演职人员!");
}
<update id="updateSort">
UPDATE
mst_personnel a
SET
<choose>
<when test="isMoveUp == 1"> <!-- 上移 -->
a.sort = IFNULL(a.sort,0)-1
</when>
<when test="isMoveUp == 0"> <!-- 下移 -->
a.sort = IFNULL(a.sort,0)+1
</when>
<otherwise>
a.sort = IFNULL(a.sort,0)-1
</otherwise>
</choose>
,a.update_time = NOW() <!-- 修改时间 -->
<where>
a.id != #{id} <!-- 除去本身 -->
AND a.type_id = #{type} <!-- 同类型 -->
AND a.sort <![CDATA[<=]]> #{start} <!-- 开始位置 -->
AND a.sort > #{end} <!-- 结束位置 -->
AND a.company_id = #{companyId}
</where>
</update>
拖拽位置、受影响的区间
update table1 set index = index + 1 where index between 4 and 8