package com.doumee.service.business.impl; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.model.LoginUserInfo; import com.doumee.core.model.PageData; import com.doumee.core.model.PageWrap; import com.doumee.core.utils.Constants; import com.doumee.core.utils.Utils; import com.doumee.dao.business.*; import com.doumee.dao.business.dao.CompanyMapper; import com.doumee.dao.business.model.*; import com.doumee.service.business.CategoryService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.apache.commons.lang3.StringUtils; import org.apache.shiro.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; /** * 分类信息表Service实现 * @author 江蹄蹄 * @date 2023/11/30 15:33 */ @Service public class CategoryServiceImpl implements CategoryService { @Autowired private CategoryMapper categoryMapper; @Autowired private YwCustomerMapper ywCustomerMapper; @Autowired private YwWorkorderMapper ywWorkorderMapper; @Autowired private YwPatrolPointMapper ywPatrolPointMapper; @Autowired private YwDeviceMapper ywDeviceMapper; @Autowired private YwMaterialMapper ywMaterialMapper; @Override public Integer create(Category category) { checkUnique(category); LoginUserInfo loginUserInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal(); if(loginUserInfo ==null){ loginUserInfo = category.getLoginUserInfo(); } Category insert = new Category(); insert.setCreator(loginUserInfo.getId()); insert.setCreateDate(new Date()); insert.setEditor(loginUserInfo.getId()); insert.setEditDate(new Date()); insert.setIsdeleted(Constants.ZERO); insert.setName(category.getName()); insert.setRemark(category.getRemark()); insert.setStatus(Constants.ZERO); insert.setSortnum(category.getSortnum()); insert.setImgurl(category.getImgurl()); insert.setType(category.getType()); insert.setParentId(category.getParentId()); categoryMapper.insert(insert); return insert.getId(); } @Override public void deleteById(Integer id) { Category category = categoryMapper.selectById(id); if(Objects.isNull(category)){ throw new BusinessException(ResponseStatus.DATA_EMPTY); } if(categoryMapper.selectCount(new QueryWrapper().lambda() .eq(Category::getIsdeleted,Constants.ZERO) .eq(Category::getParentId,id) )>Constants.ZERO){ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"存在子集数据,无法进行删除"); } //查询数据是否已使用 3运维-工单分类 4运维-巡检区域 5运维-设备分类 6=客户行业 7=资产分类 if(Constants.equalsInteger(category.getType(),Constants.THREE)){ if(ywWorkorderMapper.selectCount(new QueryWrapper().lambda().eq(YwWorkorder::getIsdeleted,Constants.ZERO).eq(YwWorkorder::getCateId,id))>Constants.ZERO) throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"分类已被使用,无法进行删除"); }else if (Constants.equalsInteger(category.getType(),Constants.FOUR)){ if(ywPatrolPointMapper.selectCount(new QueryWrapper().lambda().eq(YwPatrolPoint::getIsdeleted,Constants.ZERO).eq(YwPatrolPoint::getAreaId,id))>Constants.ZERO) throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"分类已被使用,无法进行删除"); }else if (Constants.equalsInteger(category.getType(),Constants.FIVE)){ if(ywDeviceMapper.selectCount(new QueryWrapper().lambda().eq(YwDevice::getIsdeleted,Constants.ZERO).eq(YwDevice::getCateId,id))>Constants.ZERO) throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"分类已被使用,无法进行删除"); }else if (Constants.equalsInteger(category.getType(),Constants.SIX)){ if(ywCustomerMapper.selectCount(new QueryWrapper().lambda().eq(YwCustomer::getIsdeleted,Constants.ZERO).eq(YwCustomer::getIndustryId,id))>Constants.ZERO) throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"分类已被使用,无法进行删除"); }else if (Constants.equalsInteger(category.getType(),Constants.SEVEN)){ if(ywMaterialMapper.selectCount(new QueryWrapper().lambda().eq(YwMaterial::getIsdeleted,Constants.ZERO).eq(YwMaterial::getCateId,id))>Constants.ZERO) throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"分类已被使用,无法进行删除"); } categoryMapper.update(null,new UpdateWrapper().lambda().set(Category::getIsdeleted,Constants.ONE) .eq(Category::getId,id) ); } @Override public void delete(Category category) { UpdateWrapper deleteWrapper = new UpdateWrapper<>(category); categoryMapper.delete(deleteWrapper); } @Override public void deleteByIdInBatch(List ids) { if (CollectionUtils.isEmpty(ids)) { return; } categoryMapper.update(null,new UpdateWrapper().lambda().set(Category::getIsdeleted,Constants.ONE) .in(Category::getId,ids) ); } @Override public void updateById(Category category) { checkUnique(category); categoryMapper.updateById(category); } @Override public void updateByIdInBatch(List categorys) { if (CollectionUtils.isEmpty(categorys)) { return; } for (Category category: categorys) { this.updateById(category); } } @Override public Category findById(Integer id) { return categoryMapper.selectById(id); } @Override public Category findOne(Category category) { QueryWrapper wrapper = new QueryWrapper<>(category); return categoryMapper.selectOne(wrapper); } @Override public List findList(Category category) { QueryWrapper wrapper = new QueryWrapper<>(category); return categoryMapper.selectList(wrapper); } @Override public List queryList(Category category) { List categoryList = categoryMapper.selectList(new QueryWrapper().lambda() .eq(Objects.nonNull(category)&&Objects.nonNull(category.getType()),Category::getType,category.getType()) .isNull(Objects.nonNull(category)&&Objects.isNull(category.getParentId()),Category::getParentId) .eq(Objects.nonNull(category)&&Objects.nonNull(category.getParentId()),Category::getParentId,category.getParentId()) .eq(Category::getIsdeleted,Constants.ZERO) ); return categoryList; } @Override public PageData findPage(PageWrap pageWrap) { IPage page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity()); QueryWrapper queryWrapper = new QueryWrapper<>(); Utils.MP.blankToNull(pageWrap.getModel()); queryWrapper.lambda().eq(Category::getIsdeleted,Constants.ZERO) .eq(Objects.nonNull(pageWrap.getModel().getType()),Category::getType,pageWrap.getModel().getType()) .isNull(Category::getParentId) .orderByAsc(Category::getSortnum) ; PageData categoryPageData = PageData.from(categoryMapper.selectPage(page, queryWrapper)); //查询所有二级数据 List categoryList = categoryMapper.selectList( new QueryWrapper().lambda() .eq(Objects.nonNull(pageWrap.getModel().getType()),Category::getType,pageWrap.getModel().getType()) .eq(Category::getIsdeleted,Constants.ZERO).isNotNull(Category::getParentId)); for (Category category:categoryPageData.getRecords()) { category.setChildCategoryList( categoryList.stream().filter(i->Constants.equalsInteger(i.getParentId(),category.getId())).collect(Collectors.toList()) ); } return categoryPageData; } @Override public long count(Category category) { QueryWrapper wrapper = new QueryWrapper<>(category); return categoryMapper.selectCount(wrapper); } @Override public List findChileList(Category model) { List list =findList(model); List data = new ArrayList<>(); if(list!=null){ for(Category category : list){ if(category.getParentId()!=null){ Category pcate = getParentById(category.getParentId(),list); if(pcate!=null){ category.setParentName(StringUtils.defaultString(pcate.getName(),"")); category.setName(StringUtils.defaultString(category.getName(),"")); category.setGroupName( category.getParentName()+"/"+category.getName()); data.add(category); } } } } return data; } private Category getParentById(Integer parentId, List list) { if(list!=null){ for(Category category : list){ if(Constants.equalsInteger(parentId,category.getId())){ return category; } } } return null; } private void checkUnique(Category category){ QueryWrapper wrapper = new QueryWrapper<>(); wrapper.lambda() .ne(Objects.nonNull(category.getId()),Category::getId,category.getId()) .eq(Category::getIsdeleted,Constants.ZERO) .eq(Objects.nonNull(category.getParentId()),Category::getParentId,category.getParentId()) .isNull(Objects.isNull(category.getParentId()),Category::getParentId) .eq(Category::getType,category.getType()) .eq(Category::getName,category.getName()); List categories = categoryMapper.selectList(wrapper); if (org.apache.commons.collections.CollectionUtils.isNotEmpty(categories)){ throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"分类信息已存在"); } } }