/** * 删除接口(逻辑删除) */ @GetMapping("/deleteCategory") public Result updateCategory(@RequestParam("id") Long id) { broadScopeService.updateCategory(id); return Result.success(); }
service
Result updateCategory(Long id);
/**
* 删除分类(修改)
*
* @param id
* @return
*/
@Transactional
@Override
public Result updateCategory(Long id) {
if (ObjectUtil.isNull(id)){
throw new BusinessException(MessageConstant.ID_NULL);
}
//当用户点选的指令分类下没有任何指令时,才可删除
long l = broadScopePromptMapper.countByCategoryId(id);
if (l ==0){
boolean b = broadScopeMapper.deleteCategory(id);
if (ObjectUtil.isNull(b)){
//删除失败
throw new BusinessException(MessageConstant.DELETE_FAILED);
}
}else {
//该分类下有指令,不能删除
throw new BusinessException(MessageConstant.CATEGORY_DELETE);
}
return Result.success();
}
broadScopePromptMapper
mapper:
/** * 根据id查询分类下有多少个指令 * @param id * @return */ long countByCategoryId(Long id);
xml:
<!--查询两个表中有没有相同的名字数量--> <select id="countByCategoryId" resultType="java.lang.Long"> SELECT COUNT(*) AS related_category_count FROM broadscope_prompt WHERE category = (SELECT category_name FROM broadscope_prompt_category WHERE id = #{id}); </select>
BroadScopeMapper
mapper:
boolean deleteCategory(Long id);
xml:
<!--删除分类--> <update id="deleteCategory"> update broadscope_prompt_category <set> <if test="status!=null"> status=0 </if> </set> where id=#{id} </update>
测试: