目录拖动

This commit is contained in:
cjw 2024-07-12 14:31:10 +08:00
parent 34b479fe1a
commit 38320d8acb
7 changed files with 132 additions and 1 deletions

View File

@ -12,6 +12,7 @@ import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.CatalogDragBo;
import org.dromara.system.domain.bo.SysCatalogTextbookBo;
import org.dromara.system.domain.vo.SysCatalogTextbookVo;
import org.dromara.system.service.ISysCatalogTextbookService;
@ -116,4 +117,16 @@ public class SysCatalogTextbookController extends BaseController {
}
return toAjax(textbookService.deleteById(catalogId));
}
/**
* 拖动目录-同步教材
*
* @param bo 拖动类
*/
@SaCheckPermission("catalog:textbook:edit")
@Log(title = "目录-同步教材", businessType = BusinessType.DELETE)
@DeleteMapping("/{catalogId}")
public R<Void> drag(@Validated @RequestBody CatalogDragBo bo) {
return toAjax(textbookService.dragCatalog(bo));
}
}

View File

@ -52,4 +52,10 @@ public class SysCatalogTextbook extends TenantEntity {
*/
private Integer type;
/**
* 删除标志0代表存在 2代表删除
*/
@TableLogic
private String delFlag;
}

View File

@ -0,0 +1,22 @@
package org.dromara.system.domain.bo;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
/**
* <p>TODO<p>
*
* @author cjw
* @version V1.0.0
* @date 2024/7/12 10:41
*/
@Data
public class CatalogDragBo {
@NotNull(message = "旧目录id不能为空")
private Long oldCatalog;
@NotNull(message = "新目录id不能为空")
private Long newCatalog;
@NotBlank(message = "位置信息不能为空")
private String position;
}

View File

@ -19,5 +19,11 @@ public interface SysCatalogTextbookMapper extends BaseMapperPlus<SysCatalogTextb
Page<SysCatalogTextbookVo> selectPageUserList(@Param("page") Page<SysCatalogTextbook> page, @Param("catalogId") Long catalogId);
int selectMaxOrderNumByParentId(Long parentId);
int addOrderNum(@Param("mix") Long mix, @Param("max") Long max);
int subOrderNum(@Param("mix") Long mix, @Param("max") Long max);
}

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.Wrapper;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.SysCatalogTextbook;
import org.dromara.system.domain.bo.CatalogDragBo;
import org.dromara.system.domain.bo.SysCatalogTextbookBo;
import org.dromara.system.domain.vo.SysCatalogTextbookVo;
@ -118,4 +119,6 @@ public interface ISysCatalogTextbookService {
* @return 下拉树结构列表
*/
List<Tree<Long>> buildTreeSelect(SysCatalogTextbookBo bo);
boolean dragCatalog(CatalogDragBo bo);
}

View File

@ -4,12 +4,14 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.constant.CacheNames;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
@ -20,6 +22,7 @@ import org.dromara.common.mybatis.helper.DataBaseHelper;
import org.dromara.common.redis.utils.CacheUtils;
import org.dromara.system.domain.SysCatalogTextbook;
import org.dromara.system.domain.SysOssTextbook;
import org.dromara.system.domain.bo.CatalogDragBo;
import org.dromara.system.domain.bo.SysCatalogTextbookBo;
import org.dromara.system.domain.vo.SysCatalogTextbookVo;
import org.dromara.system.mapper.SysCatalogTextbookMapper;
@ -91,11 +94,15 @@ public class SysCatalogTextbookServiceImpl implements ISysCatalogTextbookService
* @param bo 目录-同步教材
* @return 是否新增成功
*/
@Lock4j
@Override
public Boolean insertByBo(SysCatalogTextbookBo bo) {
SysCatalogTextbook info = baseMapper.selectById(bo.getParentId());
Long parentId = bo.getParentId();
SysCatalogTextbook info = baseMapper.selectById(parentId);
int orderNum = baseMapper.selectMaxOrderNumByParentId(parentId) + 1;
SysCatalogTextbook add = MapstructUtils.convert(bo, SysCatalogTextbook.class);
add.setAncestors(info.getAncestors() + StringUtils.SEPARATOR + add.getParentId());
add.setOrderNum((long) orderNum);
String[] split = StringUtils.split(add.getAncestors(), StringUtils.SEPARATOR);
if (split.length < 7) {
add.setType(split.length);
@ -204,6 +211,48 @@ public class SysCatalogTextbookServiceImpl implements ISysCatalogTextbookService
return buildCatalogTreeSelect(voList);
}
@Override
public boolean dragCatalog(CatalogDragBo bo) {
Long oldCatalogId = bo.getOldCatalog();
Long newCatalogId = bo.getNewCatalog();
SysCatalogTextbook oldCatalog = baseMapper.selectById(oldCatalogId);
SysCatalogTextbook newCatalog = baseMapper.selectById(newCatalogId);
if (!oldCatalog.getParentId().equals(newCatalog.getParentId())) {
throw new ServiceException("只支持同级目录的拖动");
}
if (oldCatalogId.equals(newCatalogId)) {
throw new ServiceException("不支持单个目录的拖动");
}
Long oldOrder = oldCatalog.getOrderNum();
Long newOrder = newCatalog.getOrderNum();
boolean upFlag = oldOrder > newOrder;
String position = bo.getPosition();
//移动到new的前面
if ("before".equals(position)) {
if (upFlag) {
//上移
int i = baseMapper.addOrderNum(newOrder - 1, oldOrder);
} else {
//下移
baseMapper.subOrderNum(oldOrder, newOrder);
}
} else if ("after".equals(position)) {
if (upFlag) {
//上移
int i = baseMapper.addOrderNum(newOrder, oldOrder);
} else {
//下移
baseMapper.subOrderNum(oldOrder, newOrder + 1);
}
} else {
throw new ServiceException("不支持的拖动方式");
}
SysCatalogTextbook update = new SysCatalogTextbook();
update.setCatalogId(oldCatalogId);
update.setOrderNum(newOrder);
return baseMapper.updateById(update) > 0;
}
//@Override
private List<Tree<Long>> buildCatalogTreeSelect(List<SysCatalogTextbookVo> catalogs) {
if (CollUtil.isEmpty(catalogs)) {

View File

@ -12,4 +12,36 @@
having ct.parent_id = #{catalogId}
order by ct.order_num
</select>
<select id="selectMaxOrderNumByParentId" resultType="int">
select IFNULL(Max(order_num),0)
from sys_catalog_textbook
where parent_id = #{parentId}
</select>
<update id="addOrderNum">
update sys_catalog_textbook
set order_num = order_num + 1
<where>
<if test="mix != null">
order_num &gt; #{mix}
</if>
<if test="max != null">
and order_num &lt; #{mix}
</if>
</where>
</update>
<update id="subOrderNum">
update sys_catalog_textbook
set order_num = order_num - 1
<where>
<if test="mix != null">
order_num &gt; #{mix}
</if>
<if test="max != null">
and order_num &lt; #{mix}
</if>
</where>
</update>
</mapper>