/**
* @program: mycs-java
* @description: 拖拽编辑试题分类标签参数数据类
* @author: wupeiguo
* @create: 2020-03-17 11:30
**/
@Data
@Accessors(chain = true)
@ApiModel("拖拽编辑试题分类标签参数数据类")
public class UpdateDragQuestionCategoryRequestDto {
@ApiModelProperty(value = "被拖拽分类id", dataType = "Long")
@NotNull(message = "被拖拽分类id不为空!")
@Min(value = 1, message = "被拖拽分类id不为空!")
private Long questionCategoryId;
@ApiModelProperty(value = "父级id(没有父级为0)",dataType = "Long")
@NotNull(message = "父级不能为空!")
@Min(value = 0, message = "父级不能为空!")
private Long parentId;
@ApiModelProperty(value = "排序在被拖拽分类上方的分类id(排序上方没有分类为0)",dataType = "Long")
@NotNull(message = "排序在被拖拽分类上方的分类id不能为空!")
@Min(value = 0, message = "排序在被拖拽分类上方的分类id不能为空!")
private Long upperCategoryId;
@ApiModelProperty(value = "单位id",dataType = "Long")
@NotNull(message = "单位不能为空!")
@Min(value = 0, message = "单位不能为空!")
private Long orgId;
@ApiModelProperty(value = "创建用户id",dataType = "Long")
@NotNull(message = "创建用户不能为空!")
@Min(value = 1, message = "创建用户不能为空!")
private Long updateUid;
/**
* 添加操作完成后清除缓存注解解析“key”是必须要用到
* @return
*/
public void setOrgId(Long orgId) {
this.orgId = orgId;
}
}
/**
* 拖拽更新分类:父级、层级、排序
*
* @param updateDragDto
*/
@Cache(method = MycsCacheMethod.CLEAR, prefix = Caches.CACHE_EXAM_CATEGORY, key = "updateDragDto.orgId")
@Transactional(rollbackFor = Exception.class)
public synchronized void dragUpdate(UpdateDragQuestionCategoryRequestDto updateDragDto) throws Exception {
//校验:被拖拽分类
ExamQuestionCategory categoryInfo = categoryService.getById(updateDragDto.getQuestionCategoryId());
if (categoryInfo == null) {
throw new ServiceException(PARAMS_ERROR, "该分类信息已过期,请刷新列表!");
}
//是否是改单位信息
if (!updateDragDto.getOrgId().equals(categoryInfo.getOrgId())) {
throw new ServiceException(PARAMS_ERROR, "你没有权限修改该分类信息!");
}
//校验:排序在被拖拽分类上方的分类
ExamQuestionCategory upperCategoryInfo = null;
if (NumberKit.effective(updateDragDto.getUpperCategoryId())) {
upperCategoryInfo = categoryService.getById(updateDragDto.getUpperCategoryId());
if (upperCategoryInfo == null) {
throw new ServiceException(PARAMS_ERROR, "部分分类信息已过期,无法重新排序,请刷新列表!");
}
}
//校验:被拖拽分类的父级分类
if (NumberKit.effective(updateDragDto.getParentId()) && !categoryInfo.getParentId().equals(updateDragDto.getParentId())) {
ExamQuestionCategory parentCategory = categoryService.getById(updateDragDto.getParentId());
if (parentCategory == null) {
throw new ServiceException(PARAMS_ERROR, "父分类不存在");
}
//校验名称是否已存在
int number = categoryService.count(updateDragDto.getOrgId(), updateDragDto.getQuestionCategoryId(), updateDragDto.getParentId(), categoryInfo.getName());
if (number > 0) {
throw new ServiceException(PARAMS_ERROR, "同级分类该名称已存在!");
}
}
//拼装数据
UpdateQuestionHierarchyTreeRequestDto hierarchyTree = new UpdateQuestionHierarchyTreeRequestDto();
hierarchyTree.setQuestionCategoryId(updateDragDto.getQuestionCategoryId())
.setParentId(updateDragDto.getParentId())
.setSymbol(CommonConstant.EXAM_QUESTION_CATEGORY_IDS_SYMBOL)
.setUpdateUid(updateDragDto.getUpdateUid())
.setUpdateTime(LocalDateTime.now());
//处理同级排序
List<ExamQuestionCategory> infoList = categoryService.findList(updateDragDto.getOrgId(), updateDragDto.getParentId());
List<ExamQuestionCategory> dtoList = new ArrayList<>(infoList.size() + 1);
int i = 1;
int listOrder = 0;
if (upperCategoryInfo != null) {
i = 0;
listOrder = upperCategoryInfo.getListOrder() + 1;
}
hierarchyTree.setListOrder(listOrder);
if (CollectionUtils.isNotEmpty(infoList)) {
for (ExamQuestionCategory c : infoList) {
if (c.getQuestionCategoryId().equals(hierarchyTree.getQuestionCategoryId())) {
continue;
}
if (i == 1) {
listOrder++;
ExamQuestionCategory cate = new ExamQuestionCategory();
cate.setQuestionCategoryId(c.getQuestionCategoryId());
cate.setListOrder(listOrder);
cate.setUpdateUid(hierarchyTree.getUpdateUid());
cate.setUpdateTime(hierarchyTree.getUpdateTime());
dtoList.add(cate);
} else if (i == 0 && c.getQuestionCategoryId().equals(upperCategoryInfo.getQuestionCategoryId())) {
i = 1;
continue;
}
}
}
if (!categoryInfo.getParentId().equals(updateDragDto.getParentId())) {
//检查是否有下级树
Integer existSuboTree = 0;
int number = categoryService.count(updateDragDto.getOrgId(), null, updateDragDto.getQuestionCategoryId(), null);
if (number > 0) {
//更新下级树 ids
existSuboTree = 1;
}
hierarchyTree.setExistSuboTree(existSuboTree);
boolean bool = categoryService.updateHierarchyTree(hierarchyTree);
if (!bool) {
throw new Exception("更新层级失败!");
}
} else {
//不编辑层级就通过同级集合一起更新排序
ExamQuestionCategory cate = new ExamQuestionCategory();
cate.setQuestionCategoryId(hierarchyTree.getQuestionCategoryId());
cate.setListOrder(hierarchyTree.getListOrder());
cate.setUpdateUid(hierarchyTree.getUpdateUid());
cate.setUpdateTime(hierarchyTree.getUpdateTime());
dtoList.add(cate);
}
//保存排序
if (CollectionUtils.isNotEmpty(dtoList)) {
boolean bool = categoryService.updateBatchById(dtoList);
if (!bool) {
throw new Exception("更新排序失败!");
}
}
}
/**
* @program: mycs-java
* @description: 分类更新上级参数对象类
* @author: wupeiguo
* @create: 2021-05-12
**/
@Data
@Accessors(chain = true)
public class UpdateQuestionHierarchyTreeRequestDto implements Serializable {
/**
* 被拖拽分类id
*/
private Long questionCategoryId;
/**
* 父级id(没有父级为0)
*/
private Long parentId;
/**
* 排序号
*/
private Integer listOrder;
/**
* pIds 分隔符
*/
private String symbol;
/**
* 是否存在子级
*/
private Integer existSuboTree;
/**
* 创建用户id
*/
private Long updateUid;
/**
* 修改时间
*/
LocalDateTime updateTime;
}
/**
* <p>
* 考试试题分类(标签) 服务类
* </p>
*
* @author wupeiguo
* @since 2021-05-11
*/
public interface IExamQuestionCategoryService extends IService<ExamQuestionCategory> {
/**
* 更新上级
*
* @param param 更新参数
* @return boolean
*/
boolean updateHierarchyTree(UpdateQuestionHierarchyTreeRequestDto param);
}
/**
* <p>
* 考试试题分类(标签) 服务实现类
* </p>
*
* @author wupeiguo
* @since 2021-05-11
*/
@Service
public class ExamQuestionCategoryServiceImpl extends ServiceImpl<ExamQuestionCategoryMapper, ExamQuestionCategory> implements IExamQuestionCategoryService {
/**
* 更新上级
*
* @param param 更新参数
* @return boolean
*/
@Override
public boolean updateHierarchyTree(UpdateQuestionHierarchyTreeRequestDto param) {
int n = baseMapper.updateHierarchyTree(param);
if (n > 0) {
return true;
}
return false;
}
}
/**
* <p>
* 考试试题分类(标签) Mapper 接口
* </p>
*
* @author wupeiguo
* @since 2021-05-11
*/
public interface ExamQuestionCategoryMapper extends BaseMapper<ExamQuestionCategory> {
/**
* 更新上级
*
* @param param 参数
* @return int
*/
int updateHierarchyTree(@Param("param") UpdateQuestionHierarchyTreeRequestDto param);
}
<!--更新上级:上级不能是子级、包含被修改分类 ec 的子级树 sc-->
<update id="updateHierarchyTree">
<if test="param.parentId != null and param.parentId > 0">
UPDATE exam_question_category ec, exam_question_category p
<if test="param.existSuboTree != null and param.existSuboTree == 1">
, exam_question_category sc
</if>
SET ec.parent_id = #{param.parentId}, ec.level = (p.level+1), ec.list_order = #{param.listOrder},
ec.parent_ids = CONCAT(p.parent_ids,p.question_category_id,#{param.symbol}),
ec.update_uid = #{param.updateUid}, ec.update_time = #{param.updateTime}
<if test="param.existSuboTree != null and param.existSuboTree == 1">
,sc.parent_ids = REPLACE(CONCAT(#{param.symbol},sc.parent_ids), CONCAT(#{param.symbol},ec.parent_ids) ,CONCAT(p.parent_ids,p.question_category_id,#{param.symbol})),
sc.update_uid = #{param.updateUid}, sc.update_time = #{param.updateTime}
</if>
WHERE ec.question_category_id = #{param.questionCategoryId} AND p.question_category_id = #{param.parentId}
AND CONCAT(#{param.symbol},p.parent_ids,p.question_category_id,#{param.symbol}) NOT LIKE CONCAT("%",#{param.symbol},#{param.questionCategoryId},#{param.symbol},"%")
<if test="param.existSuboTree != null and param.existSuboTree == 1">
AND sc.parent_ids LIKE CONCAT(ec.parent_ids,ec.question_category_id,#{param.symbol},"%")
</if>
</if>
<if test="param.parentId != null and param.parentId == 0">
UPDATE exam_question_category ec
<if test="param.existSuboTree != null and param.existSuboTree == 1">
,exam_question_category sc
</if>
SET ec.parent_id = #{param.parentId}, ec.parent_ids = '', ec.level = 1, ec.list_order = #{param.listOrder},
ec.update_uid = #{param.updateUid}, ec.update_time = #{param.updateTime}
<if test="param.existSuboTree != null and param.existSuboTree == 1">
,sc.parent_ids = REPLACE(CONCAT(#{param.symbol},sc.parent_ids), CONCAT(#{param.symbol},ec.parent_ids) ,''),
sc.update_uid = #{param.updateUid}, sc.update_time = #{param.updateTime}
</if>
WHERE ec.question_category_id = #{param.questionCategoryId}
<if test="param.existSuboTree != null and param.existSuboTree == 1">
AND sc.parent_ids LIKE CONCAT(ec.parent_ids,ec.question_category_id,#{param.symbol},"%")
</if>
</if>
</update>