gasWellWatch/src/main/java/com/isu/gaswellwatch/service/impl/DepartmentServiceImpl.java

76 lines
3.0 KiB
Java

package com.isu.gaswellwatch.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isu.gaswellwatch.config.SnowflakeConfig;
import com.isu.gaswellwatch.entity.BlockDepartment;
import com.isu.gaswellwatch.service.BlockDepartmentService;
import com.isu.gaswellwatch.service.DepartmentService;
import com.isu.gaswellwatch.dao.DepartmentDao;
import com.isu.gaswellwatch.dto.*;
import com.isu.gaswellwatch.entity.Department;
import com.isu.gaswellwatch.exception.BusinessException;
import com.isu.gaswellwatch.utils.ConverterUtil;
import com.isu.gaswellwatch.vo.DepartmentVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service("departmentService")
@Transactional(rollbackFor = Exception.class)
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, Department> implements DepartmentService {
@Resource
private SnowflakeConfig snowflakeConfig;
@Resource
private DepartmentDao companyDao;
@Resource
private BlockDepartmentService blockDepartmentService;
@Override
public Page<DepartmentVO> page(Integer currentPage, Integer pageSize, String name, String code){
Page<DepartmentVO> page = companyDao.page(new Page<>(currentPage, pageSize),name,code);
return ConverterUtil.convertPage(page, DepartmentVO.class);
}
@Override
public void add(DepartmentDTO companyDTO){
//查重
List<Department> list = list(new LambdaQueryWrapper<Department>().eq(Department::getName, companyDTO.getName()));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("已有相同名称,请重新输入");
}
Long companyId = snowflakeConfig.snowflakeId();
save(Department.builder().id(companyId).name(companyDTO.getName()).description(companyDTO.getDescription()).code(companyDTO.getCode()).build());
}
@Override
public void edit(DepartmentEditDTO departmentEditDTO){
List<Department> list = list(new LambdaQueryWrapper<Department>().eq(Department::getName, departmentEditDTO.getName()).ne(Department::getId, departmentEditDTO.getId()));
if(CollectionUtil.isNotEmpty(list)) {
throw new BusinessException("部门名称已存在,请重新输入");
}
updateById(ConverterUtil.convert(departmentEditDTO, Department.class));
}
@Override
public void delete(Long id){
//如果部门绑定了区块 不能删除
BlockDepartment blockDepartment = blockDepartmentService.getOne(new LambdaQueryWrapper<BlockDepartment>().eq(BlockDepartment::getDepartmentId, id));
if(blockDepartment != null){
throw new BusinessException("该部门已被区块绑定,不能删除");
}else {
removeById(id);
}
}
}