package com.gk.hotwork.Service.ServiceImpl; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gk.hotwork.Domain.ElementManagement; import com.gk.hotwork.Domain.ElementTree; import com.gk.hotwork.Domain.Exception.BusinessException; import com.gk.hotwork.Domain.UserInfo; import com.gk.hotwork.Domain.Utils.StringUtils; import com.gk.hotwork.Mapper.ElementManagementMapper; import com.gk.hotwork.Service.ElementManagementService; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @Service("elementManagementService") @Transactional public class ElementManagementImpl extends ServiceImpl implements ElementManagementService { @Autowired private ElementManagementMapper elementManagementMapper; /** * @Description: 分页 */ @Override public IPage selectPage(Page page, Map filter, UserInfo user) { return elementManagementMapper.selectPages(page,filter); } /** * @Description: 新增 */ @Override public void addOne(ElementManagement param, UserInfo user) { requiredVerification(param); param.setValidFlag(Boolean.TRUE); param.setUpdateBy(user.getRealname()); param.setCreateBy(user.getRealname()); param.setUpdateTime(new Date()); param.setCreateTime(new Date()); this.save(param); } /** * @Description: 修改 */ @Override public void modOne(ElementManagement param, UserInfo user) { selectVerification(param.getId()); requiredVerification(param); param.setUpdateTime(new Date()); param.setUpdateBy(user.getRealname()); this.updateById(param); } /** * @Description: 删除 */ @Override public void delOne(Long id, UserInfo user) { ElementManagement elementManagement = selectVerification(id); deleteVerification(elementManagement); ElementManagement delOne = new ElementManagement(); delOne.setId(id); delOne.setUpdateTime(new Date()); delOne.setUpdateBy(user.getRealname()); delOne.setValidFlag(Boolean.FALSE); this.updateById(delOne); } @Override public List getParentElement() { return elementManagementMapper.getElementByType(0); } @Override public List getElementTree() { List elementTreeList = new ArrayList<>(); List list1 = elementManagementMapper.getElementByType(0); List list2 = elementManagementMapper.getElementByType(1); if (CollectionUtils.isNotEmpty(list1)){ for (ElementManagement elementManagement : list1){ ElementTree elementTree=new ElementTree(); elementTree.setType(0); elementTree.setLabel(elementManagement.getName()); elementTree.setValue(elementManagement.getId()); elementTreeList.add(elementTree); } } if (CollectionUtils.isNotEmpty(list2)) { for (int i = 0; i < list2.size(); i++) { for (int j = 0; j < elementTreeList.size(); j++) { if (list2.get(i).getParentId().equals(elementTreeList.get(j).getValue())) { if (CollectionUtils.isEmpty(elementTreeList.get(j).getChildren())) { //如果为空 则new一个出来 elementTreeList.get(j).setChildren(new ArrayList<>()); } ElementTree elementTree = new ElementTree(); elementTree.setType(1); elementTree.setLabel(list2.get(i).getName()); elementTree.setValue(list2.get(i).getId()); elementTreeList.get(j).getChildren().add(elementTree); } } } } return elementTreeList; } /** * 查询验证 * 验证对象存在 */ public ElementManagement selectVerification(Long id){ if (id == null) throw new BusinessException("id传参不能为空"); ElementManagement elementManagement = this.getById(id); if (elementManagement == null) throw new BusinessException("找不到对应实体"); return elementManagement; } /** * 操作验证 * 验证必填项 * */ public void requiredVerification(ElementManagement param){ if(StringUtils.isBlank(param.getName())) throw new BusinessException("请填写要素名称"); if (param.getType() == null) throw new BusinessException("请选择要素类型"); if (param.getType() == 1 && param.getParentId()==null){ throw new BusinessException("请选择父要素"); } } /** * 操作验证 * 删除已被B级要素绑定的A级要素 * */ public void deleteVerification(ElementManagement param){ if (param.getType()==0){ int count = this.count(new LambdaQueryWrapper() .eq(ElementManagement::getParentId, param.getId()) .eq(ElementManagement::getType , 1) .eq(ElementManagement::getValidFlag, Boolean.TRUE)); if (count > 0) throw new BusinessException("当前A级要素仍然有已绑定的B级要素,无法删除"); } } }