| | |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.basic.PageVO; |
| | | import com.ruoyi.common.basic.PageDTO; |
| | | import com.ruoyi.common.enums.ShowStatusEnum; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.BeanUtils; |
| | | import com.ruoyi.common.utils.CollUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.bean.BeanUtils; |
| | | 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.vo.FieldCategoryDetailVO; |
| | | import com.ruoyi.system.vo.FieldCategoryVO; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | |
| | | } |
| | | |
| | | @Override |
| | | public PageVO<FieldCategoryVO> queryPage(FieldCategoryQuery query) { |
| | | 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()) |
| | | .eq(TbFieldCategory::getParentId, 0) |
| | | .orderByDesc(TbFieldCategory::getCreateTime) |
| | | .page(new Page<>(query.getPageNum(), query.getPageSize())); |
| | | PageVO<FieldCategoryVO> pageVO = new PageVO<>(); |
| | | pageVO.setPageNo(page.getPages()); |
| | | pageVO.setPageSize(page.getSize()); |
| | | pageVO.setTotalPages(page.getTotal()); |
| | | pageVO.setTotalCount(page.getTotal()); |
| | | pageVO.setRecords(page.getRecords().stream().map(item -> { |
| | | FieldCategoryVO fieldCategoryVO = new FieldCategoryVO(); |
| | | BeanUtils.copyBeanProp(fieldCategoryVO, item); |
| | | return fieldCategoryVO; |
| | | }).collect(Collectors.toList())); |
| | | return pageVO; |
| | | if (CollUtils.isEmpty(page.getRecords())) { |
| | | return PageDTO.empty(page); |
| | | } |
| | | return PageDTO.of(page, FieldCategoryVO.class); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void showHide(ShowHideDTO dto) { |
| | | TbFieldCategory category = this.getById(dto.getId()); |
| | | if (Objects.isNull(category)) { |
| | | throw new RuntimeException("非法id"); |
| | | updateCategoryAndChildren(dto.getId(), dto.getStatus()); |
| | | } |
| | | |
| | | private void updateCategoryAndChildren(Integer id, ShowStatusEnum status) { |
| | | TbFieldCategory category = this.validateParam(id); |
| | | this.lambdaUpdate() |
| | | .eq(TbFieldCategory::getId, id) |
| | | .set(TbFieldCategory::getStatus, status) |
| | | .update(); |
| | | List<TbFieldCategory> children = this.lambdaQuery().eq(TbFieldCategory::getParentId, category.getId()).list(); |
| | | if (CollUtils.isNotEmpty(children)) { |
| | | List<Integer> childIds = children.stream().map(TbFieldCategory::getId).collect(Collectors.toList()); |
| | | childIds.forEach(childId -> updateCategoryAndChildren(childId, status)); |
| | | } |
| | | this.lambdaUpdate().eq(TbFieldCategory::getId, dto.getId()).set(TbFieldCategory::getStatus, dto.getStatus()); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void delete(Integer id) { |
| | | //一级分类 |
| | | TbFieldCategory category = this.getById(id); |
| | | validateParam(id); |
| | | //查询是否有二级分类 |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, id).list(); |
| | | if (CollectionUtils.isNotEmpty(threeCategoryList)) { |
| | |
| | | this.removeById(id); |
| | | } |
| | | |
| | | private TbFieldCategory validateParam(Integer id) { |
| | | TbFieldCategory category = this.getById(id); |
| | | if (Objects.isNull(category)) { |
| | | throw new ServiceException("非法参数"); |
| | | } |
| | | return category; |
| | | } |
| | | |
| | | @Override |
| | | public void edit(FieldCategoryUpdateDTO dto) { |
| | | validateParam(dto.getId()); |
| | | //更新一级分类 |
| | | updateCategory(dto); |
| | | List<FieldCategoryUpdateDTO> children = dto.getChildren(); |
| | |
| | | }); |
| | | } |
| | | } |
| | | |
| | | private void updateCategory(FieldCategoryUpdateDTO dto) { |
| | | if (dto == null) return; |
| | | this.lambdaUpdate() |
| | |
| | | .eq(TbFieldCategory::getId, dto.getId()) |
| | | .update(); |
| | | } |
| | | |
| | | @Override |
| | | public List<FieldCategoryVO> queryFieldCategories(Integer id) { |
| | | List<TbFieldCategory> list = this.lambdaQuery() |
| | | .select(TbFieldCategory::getId,TbFieldCategory::getFieldCategoryName) |
| | | .eq(TbFieldCategory::getParentId, id) |
| | | .eq(TbFieldCategory::getStatus, ShowStatusEnum.SHOW) |
| | | .list(); |
| | | return BeanUtils.copyList(list, FieldCategoryVO.class); |
| | | } |
| | | |
| | | @Override |
| | | public FieldCategoryDetailVO getDetailsById(Integer id) { |
| | | TbFieldCategory oneCategory = this.getById(id); |
| | | if (Objects.isNull(oneCategory)) { |
| | | return new FieldCategoryDetailVO(); |
| | | } |
| | | FieldCategoryDetailVO vo = BeanUtils.copyBean(oneCategory, FieldCategoryDetailVO.class); |
| | | //根据一级分类id,查询二级分类 |
| | | List<TbFieldCategory> twoCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, oneCategory.getId()).list(); |
| | | twoCategoryList.forEach(item->{ |
| | | FieldCategoryDetailVO twoCategoryVO = BeanUtils.copyBean(item, FieldCategoryDetailVO.class); |
| | | vo.getChildren().add(twoCategoryVO); |
| | | //根据二级分类id,查询三级分类 |
| | | List<TbFieldCategory> threeCategoryList = this.lambdaQuery().eq(TbFieldCategory::getParentId, item.getId()).list(); |
| | | threeCategoryList.forEach(threeCategory->{ |
| | | FieldCategoryDetailVO threeCategoryVO = BeanUtils.copyBean(threeCategory, FieldCategoryDetailVO.class); |
| | | twoCategoryVO.getChildren().add(threeCategoryVO); |
| | | }); |
| | | }); |
| | | return vo; |
| | | } |
| | | } |