diff --git a/modules/visual-design/src/main/java/com/thing/visual/group/dto/AdjustGroupDto.java b/modules/visual-design/src/main/java/com/thing/visual/group/dto/AdjustGroupDto.java index 2b780c3..dfacef0 100644 --- a/modules/visual-design/src/main/java/com/thing/visual/group/dto/AdjustGroupDto.java +++ b/modules/visual-design/src/main/java/com/thing/visual/group/dto/AdjustGroupDto.java @@ -8,12 +8,13 @@ import lombok.Data; @Schema(description = "调整组排序入参对象") public class AdjustGroupDto { - private String currentName; - private Long currentSort; + private String name; - private String upName; + private Integer currentSort; - private String upSort; + private Integer upSort; + + private String type; } diff --git a/modules/visual-design/src/main/java/com/thing/visual/group/dto/NameSort.java b/modules/visual-design/src/main/java/com/thing/visual/group/dto/NameSort.java new file mode 100644 index 0000000..9172a90 --- /dev/null +++ b/modules/visual-design/src/main/java/com/thing/visual/group/dto/NameSort.java @@ -0,0 +1,13 @@ +package com.thing.visual.group.dto; + + +import lombok.Data; + +@Data +public class NameSort { + + private String name; + + private Long sort; + +} diff --git a/modules/visual-design/src/main/java/com/thing/visual/group/service/impl/IotVisualGroupServiceImpl.java b/modules/visual-design/src/main/java/com/thing/visual/group/service/impl/IotVisualGroupServiceImpl.java index 6b9c64a..85e64e6 100644 --- a/modules/visual-design/src/main/java/com/thing/visual/group/service/impl/IotVisualGroupServiceImpl.java +++ b/modules/visual-design/src/main/java/com/thing/visual/group/service/impl/IotVisualGroupServiceImpl.java @@ -1,16 +1,20 @@ package com.thing.visual.group.service.impl; import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.update.UpdateChain; +import com.mybatisflex.core.update.UpdateWrapper; import com.thing.common.orm.service.impl.BaseServiceImpl; import com.thing.visual.group.dto.AdjustGroupDto; import com.thing.visual.group.dto.GroupSortInfo; import com.thing.visual.group.dto.IotVisualGroupDTO; +import com.thing.visual.group.dto.NameSort; import com.thing.visual.group.mapper.IotVisualGroupMapper; import com.thing.visual.group.entity.IotVisualGroupEntity; import com.thing.visual.group.service.IotVisualGroupService; import org.apache.commons.lang3.ObjectUtils; import org.springframework.stereotype.Service; +import java.util.List; import java.util.Map; import static com.mybatisflex.core.query.QueryMethods.max; @@ -69,6 +73,80 @@ public class IotVisualGroupServiceImpl extends BaseServiceImpl sortInfo = this.getNameSort(dto.getName()); + //修改排序 + moveItem(sortInfo,dto.getCurrentSort()-1,dto.getUpSort()-1); + //重置sort字段 + updateSortValues(sortInfo); + + if(ObjectUtils.isNotEmpty(dto.getName())){ + //组内排序 + sortInfo.forEach(temp->{ + UpdateChain.of(IotVisualGroupEntity.class) + .set(IotVisualGroupEntity::getSort, temp.getSort()) + .where(IotVisualGroupEntity::getName).eq(dto.getName()).eq(IotVisualGroupEntity::getBusinessName,temp.getName()) + .update(); + }); + }else { + //组排序 + sortInfo.forEach(temp->{ + UpdateChain.of(IotVisualGroupEntity.class) + .set(IotVisualGroupEntity::getSort, temp.getSort()) + .where(IotVisualGroupEntity::getName).eq(temp.getName()) + .update(); + }); + } + return "ok"; } + + + + + + /** + * 获取所有信息,看没有没有穿name,传name了,就是组内排序,没传name就是组排序 + * @param name + * @return + */ + private List getNameSort(String name){ + QueryWrapper wrapper = new QueryWrapper(); + if(ObjectUtils.isNotEmpty(name)){ + wrapper.select(max(IOT_VISUAL_GROUP_ENTITY.SORT).as("sort"), IOT_VISUAL_GROUP_ENTITY.BUSINESS_NAME.as("name")) + .from(IOT_VISUAL_GROUP_ENTITY); + wrapper.eq(IotVisualGroupEntity::getName,name).groupBy(IOT_VISUAL_GROUP_ENTITY.NAME).orderBy(IOT_VISUAL_GROUP_ENTITY.SORT,true); + }else { + wrapper.select(max(IOT_VISUAL_GROUP_ENTITY.SORT).as("sort"), IOT_VISUAL_GROUP_ENTITY.NAME.as("name")) + .from(IOT_VISUAL_GROUP_ENTITY); + wrapper.groupBy(IOT_VISUAL_GROUP_ENTITY.NAME).orderBy(IOT_VISUAL_GROUP_ENTITY.SORT,true);; + } + return this.mapper.selectListByQueryAs(wrapper,NameSort.class); + } + + + /** + * 删除原坐标,在新的坐标插入我的对象 + * @param list + * @param fromIndex + * @param toIndex + */ + private static void moveItem(List list, int fromIndex, int toIndex) { + if (fromIndex < 0 || fromIndex >= list.size() || toIndex < 0 || toIndex >= list.size()) { + throw new IndexOutOfBoundsException("Invalid index"); + } + NameSort item = list.remove(fromIndex); + list.add(toIndex, item); + updateSortValues(list); + } + + /** + * 重新修改 name中对应的sort信息 + * @param list + */ + private static void updateSortValues(List list) { + long sortValue = 1L; + for (NameSort item : list) { + item.setSort(sortValue++); + } + } + } \ No newline at end of file