部门curd

This commit is contained in:
2023-07-29 23:31:17 +08:00
parent 15e3d67cf5
commit 1621b00476
11 changed files with 211 additions and 15 deletions

View File

@@ -0,0 +1,47 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.dto.*;
import cn.lihongjie.coal.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/department")
@RestController
public class DepartmentController {
@Autowired
DepartmentService service;
@PostMapping("/create")
public DepartmentDto create(@RequestBody CreateDepartmentDto dto) {
return this.service.create(dto);
}
@PostMapping("/update")
public DepartmentDto update(@RequestBody UpdateDepartmentDto dto) {
return this.service.update(dto);
}
@PostMapping("/delete")
public Object delete(@RequestBody IdRequest dto) {
this.service.delete(dto);
return null;
}
@PostMapping("/list")
public Page<DepartmentDto> list(@RequestBody CommonQuery dto) {
return this.service.list(dto);
}
@PostMapping("/getById")
public DepartmentDto getById(@RequestBody IdRequest dto) {
return this.service.getById(dto.getId());
}
}

View File

@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/Organization")
@RequestMapping("/organization")
@RestController
public class OrganizationController {

View File

@@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/Role")
@RequestMapping("/role")
@RestController
public class RoleController {

View File

@@ -0,0 +1,8 @@
package cn.lihongjie.coal.dao;
import cn.lihongjie.coal.entity.DepartmentEntity;
import org.springframework.stereotype.Repository;
@Repository
public interface DepartmentRepository extends BaseRepository<DepartmentEntity> {
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.dto;
import cn.lihongjie.coal.dto.base.OrgCommonDto;
import lombok.Data;
@Data
public class CreateDepartmentDto extends OrgCommonDto
{
}

View File

@@ -0,0 +1,13 @@
package cn.lihongjie.coal.dto;
import cn.lihongjie.coal.dto.base.OrgCommonDto;
import lombok.Data;
import java.util.List;
@Data
public class DepartmentDto extends OrgCommonDto
{
}

View File

@@ -1,13 +0,0 @@
package cn.lihongjie.coal.dto;
import java.util.List;
public class PagedData <T>{
private List<T> data;
private Integer pageNo;
private Integer pageSize;
private Integer totalCount;
}

View File

@@ -0,0 +1,11 @@
package cn.lihongjie.coal.dto;
import cn.lihongjie.coal.dto.base.OrgCommonDto;
import lombok.Data;
@Data
public class UpdateDepartmentDto extends OrgCommonDto
{
}

View File

@@ -0,0 +1,14 @@
package cn.lihongjie.coal.entity;
import cn.lihongjie.coal.entity.base.OrgCommonEntity;
import jakarta.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Comment;
@Entity
@Comment("部门信息")
@Getter
@Setter
public class DepartmentEntity extends OrgCommonEntity {
}

View File

@@ -0,0 +1,24 @@
package cn.lihongjie.coal.mapper;
import cn.lihongjie.coal.dto.CreateDepartmentDto;
import cn.lihongjie.coal.dto.UpdateDepartmentDto;
import cn.lihongjie.coal.dto.DepartmentDto;
import cn.lihongjie.coal.entity.DepartmentEntity;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.MappingTarget;
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING,
uses = {CommonMapper.class}
)
public interface DepartmentMapper {
DepartmentDto toDto(DepartmentEntity user);
DepartmentEntity toEntity(CreateDepartmentDto request);
void updateEntity(@MappingTarget DepartmentEntity entity, UpdateDepartmentDto dto);
}

View File

@@ -0,0 +1,81 @@
package cn.lihongjie.coal.service;
import cn.lihongjie.coal.dao.DepartmentRepository;
import cn.lihongjie.coal.dto.*;
import cn.lihongjie.coal.entity.DepartmentEntity;
import cn.lihongjie.coal.mapper.DepartmentMapper;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class DepartmentService {
@Autowired
DepartmentRepository repository;
@Autowired
DepartmentMapper mapper;
@PostConstruct
public void init() {
}
public DepartmentDto create(CreateDepartmentDto request) {
DepartmentEntity entity = mapper.toEntity(request);
this.repository.save(entity);
return getById(entity.getId());
}
public DepartmentDto update(UpdateDepartmentDto request) {
DepartmentEntity entity = this.repository.get(request.getId());
this.mapper.updateEntity(entity, request);
return null;
}
public void delete(IdRequest request) {
this.repository.deleteAllById(request.getIds());
}
public DepartmentDto getById(String id) {
DepartmentEntity entity = repository.get(id);
return mapper.toDto(entity);
}
@Autowired
ConversionService conversionService;
public Page<DepartmentDto> list(CommonQuery query) {
Page<DepartmentEntity> page = repository.findAll(query.specification(conversionService), PageRequest.of(query.getPageNo(), query.getPageSize(), Sort.by(query.getOrders())));
return page.map(this.mapper::toDto);
}
}