| | |
| | | |
| | | import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.basic.PageDTO; |
| | |
| | | import com.ruoyi.common.utils.BeanUtils; |
| | | import com.ruoyi.common.utils.CollUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.domain.TbField; |
| | | import com.ruoyi.system.domain.TbFieldCategory; |
| | | import com.ruoyi.system.dto.FieldCategoryDTO; |
| | | import com.ruoyi.system.dto.ShowHideDTO; |
| | |
| | | import com.ruoyi.system.mapper.TbFieldCategoryMapper; |
| | | import com.ruoyi.system.query.FieldCategoryQuery; |
| | | import com.ruoyi.system.service.TbFieldCategoryService; |
| | | import com.ruoyi.system.service.TbFieldService; |
| | | import com.ruoyi.system.vo.FieldCategoryDetailVO; |
| | | import com.ruoyi.system.vo.FieldCategoryVO; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.CompletableFuture; |
| | | import java.util.stream.Collectors; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @author mitao |
| | | * @since 2024-03-13 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class TbFieldCategoryServiceImpl extends ServiceImpl<TbFieldCategoryMapper, TbFieldCategory> implements TbFieldCategoryService { |
| | | @RequiredArgsConstructor |
| | | public class TbFieldCategoryServiceImpl extends |
| | | ServiceImpl<TbFieldCategoryMapper, TbFieldCategory> implements TbFieldCategoryService { |
| | | |
| | | private final TbFieldService tbFieldService; |
| | | |
| | | @Override |
| | | public void add(FieldCategoryDTO dto) { |
| | |
| | | @Override |
| | | public PageDTO<FieldCategoryVO> queryPage(FieldCategoryQuery query) { |
| | | Page<TbFieldCategory> page = this.lambdaQuery() |
| | | .like(StringUtils.isNotBlank(query.getFieldCategoryName()), TbFieldCategory::getFieldCategoryName, query.getFieldCategoryName()) |
| | | .eq(ObjectUtils.isNotEmpty(query.getStatus()), TbFieldCategory::getStatus, query.getStatus()) |
| | | .like(StringUtils.isNotBlank(query.getFieldCategoryName()), |
| | | TbFieldCategory::getFieldCategoryName, query.getFieldCategoryName()) |
| | | .eq(ObjectUtils.isNotEmpty(query.getStatus()), TbFieldCategory::getStatus, |
| | | query.getStatus()) |
| | | .eq(TbFieldCategory::getParentId, 0) |
| | | .orderByDesc(TbFieldCategory::getCreateTime) |
| | | .page(new Page<>(query.getPageNum(), query.getPageSize())); |
| | |
| | | .eq(TbFieldCategory::getId, id) |
| | | .set(TbFieldCategory::getStatus, status) |
| | | .update(); |
| | | List<TbFieldCategory> children = this.lambdaQuery().eq(TbFieldCategory::getParentId, category.getId()).list(); |
| | | List<TbFieldCategory> children = this.lambdaQuery() |
| | | .eq(TbFieldCategory::getParentId, category.getId()).list(); |
| | | if (CollUtils.isNotEmpty(children)) { |
| | | List<Long> childIds = children.stream().map(TbFieldCategory::getId).collect(Collectors.toList()); |
| | | List<Long> childIds = children.stream().map(TbFieldCategory::getId) |
| | | .collect(Collectors.toList()); |
| | | childIds.forEach(childId -> updateCategoryAndChildren(childId, status)); |
| | | } |
| | | } |
| | |
| | | TbFieldCategory category = this.getById(id); |
| | | if (Objects.nonNull(category)) { |
| | | //查询是否有三级分类 |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list(); |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery() |
| | | .eq(TbFieldCategory::getParentId, id).list(); |
| | | if (CollectionUtils.isNotEmpty(threeCategoryList)) { |
| | | List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList()); |
| | | List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId) |
| | | .collect(Collectors.toList()); |
| | | //删除该二级分类下面的三级分类 |
| | | this.removeByIds(ids); |
| | | } |
| | |
| | | //一级分类 |
| | | validateParam(id); |
| | | //查询是否有二级分类 |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list(); |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery() |
| | | .eq(TbFieldCategory::getParentId, id).list(); |
| | | if (CollectionUtils.isNotEmpty(threeCategoryList)) { |
| | | List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId).collect(Collectors.toList()); |
| | | List<Long> ids = threeCategoryList.stream().map(TbFieldCategory::getId) |
| | | .collect(Collectors.toList()); |
| | | //查询三级分类 |
| | | List<TbFieldCategory> list = this.lambdaQuery().in(TbFieldCategory::getParentId, ids).list(); |
| | | List<TbFieldCategory> list = this.lambdaQuery().in(TbFieldCategory::getParentId, ids) |
| | | .list(); |
| | | if (CollectionUtils.isNotEmpty(list)) { |
| | | List<Long> ids1 = list.stream().map(TbFieldCategory::getId).collect(Collectors.toList()); |
| | | List<Long> ids1 = list.stream().map(TbFieldCategory::getId) |
| | | .collect(Collectors.toList()); |
| | | //删除三级分类 |
| | | this.removeByIds(ids1); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void edit(FieldCategoryUpdateDTO dto) { |
| | | log.info("主线程名{}", Thread.currentThread().getName()); |
| | | validateParam(dto.getId()); |
| | | //更新一级分类 |
| | | updateCategory(dto); |
| | | TbFieldCategory category1 = updateCategory(dto, null); |
| | | CompletableFuture.runAsync(() -> handleFieldCategory(category1)); |
| | | List<FieldCategoryUpdateDTO> children = dto.getChildren(); |
| | | //更新二级分类 |
| | | if (!CollectionUtils.isEmpty(children)) { |
| | | if (CollUtils.isNotEmpty(children)) { |
| | | children.forEach(item->{ |
| | | updateCategory(item); |
| | | //二级分类 |
| | | TbFieldCategory category2 = updateCategory(item, category1); |
| | | CompletableFuture.runAsync(() -> handleFieldCategory(category2)); |
| | | //三级分类 |
| | | List<FieldCategoryUpdateDTO> next = item.getChildren(); |
| | | if (!CollectionUtils.isEmpty(next)) { |
| | | next.forEach(this::updateCategory); |
| | | for (FieldCategoryUpdateDTO fieldCategoryUpdateDTO : next) { |
| | | TbFieldCategory category3 = updateCategory(fieldCategoryUpdateDTO, |
| | | category2); |
| | | CompletableFuture.runAsync(() -> handleFieldCategory(category3)); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | |
| | | private void updateCategory(FieldCategoryUpdateDTO dto) { |
| | | if (dto == null) return; |
| | | this.lambdaUpdate() |
| | | .set(TbFieldCategory::getFieldCategoryName, dto.getFieldCategoryName()) |
| | | .set(Objects.nonNull(dto.getSortOrder()),TbFieldCategory::getSortOrder, dto.getSortOrder()) |
| | | .eq(TbFieldCategory::getId, dto.getId()) |
| | | .update(); |
| | | protected void handleFieldCategory(TbFieldCategory category) { |
| | | log.info("开始处理字段分类名称同步,线程名{}", Thread.currentThread().getName()); |
| | | //查询使用到该分类的字段 |
| | | List<TbField> list1 = tbFieldService.list(Wrappers.<TbField>lambdaQuery() |
| | | .eq(TbField::getLevelOneCategoryId, category.getId())); |
| | | if (CollUtils.isNotEmpty(list1)) { |
| | | List<TbField> fieldList = list1.stream().peek(item -> { |
| | | item.setLevelOneCategoryId(category.getId()); |
| | | item.setLevelOneCategory(category.getFieldCategoryName()); |
| | | }).collect(Collectors.toList()); |
| | | tbFieldService.updateBatchById(fieldList); |
| | | } |
| | | List<TbField> list2 = tbFieldService.list(Wrappers.<TbField>lambdaQuery() |
| | | .eq(TbField::getLevelTwoCategoryId, category.getId())); |
| | | if (CollUtils.isNotEmpty(list2)) { |
| | | List<TbField> fieldList = list2.stream().peek(item -> { |
| | | item.setLevelTwoCategoryId(category.getId()); |
| | | item.setLevelTwoCategory(category.getFieldCategoryName()); |
| | | }).collect(Collectors.toList()); |
| | | tbFieldService.updateBatchById(fieldList); |
| | | } |
| | | List<TbField> list3 = tbFieldService.list(Wrappers.<TbField>lambdaQuery() |
| | | .eq(TbField::getLevelThreeCategoryId, category.getId())); |
| | | if (CollUtils.isNotEmpty(list2)) { |
| | | List<TbField> fieldList = list3.stream().peek(item -> { |
| | | item.setLevelThreeCategoryId(category.getParentId()); |
| | | item.setLevelThreeCategory(category.getFieldCategoryName()); |
| | | }).collect(Collectors.toList()); |
| | | tbFieldService.updateBatchById(fieldList); |
| | | } |
| | | } |
| | | |
| | | private TbFieldCategory updateCategory(FieldCategoryUpdateDTO dto, |
| | | TbFieldCategory parent) { |
| | | TbFieldCategory category = BeanUtils.copyBean(dto, TbFieldCategory.class); |
| | | if (Objects.isNull(dto.getId()) && Objects.nonNull(parent)) { |
| | | assert category != null; |
| | | category.setParentId(parent.getId()); |
| | | this.save(category); |
| | | } else { |
| | | this.updateById(category); |
| | | } |
| | | return category; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | FieldCategoryDetailVO vo = BeanUtils.copyBean(oneCategory, FieldCategoryDetailVO.class); |
| | | //根据一级分类id,查询二级分类 |
| | | List<TbFieldCategory> twoCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, oneCategory.getId()).list(); |
| | | List<TbFieldCategory> twoCategoryList = this.lambdaQuery() |
| | | .eq(TbFieldCategory::getParentId, oneCategory.getId()).list(); |
| | | twoCategoryList.forEach(item->{ |
| | | FieldCategoryDetailVO twoCategoryVO = BeanUtils.copyBean(item, FieldCategoryDetailVO.class); |
| | | FieldCategoryDetailVO twoCategoryVO = BeanUtils.copyBean(item, |
| | | FieldCategoryDetailVO.class); |
| | | vo.getChildren().add(twoCategoryVO); |
| | | //根据二级分类id,查询三级分类 |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list(); |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery() |
| | | .eq(TbFieldCategory::getParentId, item.getId()).list(); |
| | | threeCategoryList.forEach(threeCategory->{ |
| | | FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, FieldCategoryDetailVO.class); |
| | | FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, |
| | | FieldCategoryDetailVO.class); |
| | | twoCategoryVO.getChildren().add(threeCategoryVO); |
| | | }); |
| | | }); |