package com.rongyichuang.region.resolver;
|
|
import com.rongyichuang.region.entity.Region;
|
import com.rongyichuang.region.service.RegionService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.domain.Page;
|
import org.springframework.graphql.data.method.annotation.Argument;
|
import org.springframework.graphql.data.method.annotation.MutationMapping;
|
import org.springframework.graphql.data.method.annotation.QueryMapping;
|
import org.springframework.stereotype.Controller;
|
|
import java.util.List;
|
import java.util.Optional;
|
|
@Controller
|
public class RegionResolver {
|
|
@Autowired
|
private RegionService regionService;
|
|
/**
|
* 分页查询区域
|
*/
|
@QueryMapping(name = "regions")
|
public RegionPageResult regions(@Argument String name,
|
@Argument Integer state,
|
@Argument int page,
|
@Argument int size) {
|
Page<Region> regionPage = regionService.findRegions(name, state, page, size);
|
return new RegionPageResult(
|
regionPage.getContent(),
|
regionPage.getTotalElements(),
|
regionPage.getTotalPages(),
|
regionPage.getNumber(),
|
regionPage.getSize()
|
);
|
}
|
|
/**
|
* 查询所有区域
|
*/
|
@QueryMapping(name = "allRegions")
|
public List<Region> allRegions() {
|
return regionService.findAllRegions();
|
}
|
|
/**
|
* 根据ID查询区域
|
*/
|
@QueryMapping(name = "region")
|
public Region region(@Argument Long id) {
|
Optional<Region> region = regionService.findById(id);
|
return region.orElse(null);
|
}
|
|
/**
|
* 获取省级区域列表
|
*/
|
@QueryMapping(name = "provinces")
|
public List<Region> provinces() {
|
return regionService.getProvinces();
|
}
|
|
/**
|
* 获取指定省份下的城市列表
|
*/
|
@QueryMapping(name = "cities")
|
public List<Region> cities(@Argument Long provinceId) {
|
return regionService.getCitiesByProvinceId(provinceId);
|
}
|
|
/**
|
* 获取指定城市下的区县列表
|
*/
|
@QueryMapping(name = "districts")
|
public List<Region> districts(@Argument Long cityId) {
|
return regionService.getDistrictsByCityId(cityId);
|
}
|
|
/**
|
* 获取子区域列表
|
*/
|
@QueryMapping(name = "regionChildren")
|
public List<Region> regionChildren(@Argument Long parentId) {
|
return regionService.getChildren(parentId);
|
}
|
|
/**
|
* 保存区域(新增或更新)
|
*/
|
@MutationMapping(name = "saveRegion")
|
public Region saveRegion(@Argument RegionInput input) {
|
Region region = new Region();
|
if (input.getId() != null) {
|
Optional<Region> existingRegion = regionService.findById(input.getId());
|
if (existingRegion.isPresent()) {
|
region = existingRegion.get();
|
}
|
}
|
|
region.setName(input.getName());
|
region.setPid(input.getPid());
|
region.setCode(input.getCode());
|
region.setLevel(input.getLevel());
|
region.setLeafFlag(input.getLeafFlag());
|
region.setState(input.getState());
|
region.setFullPath(input.getFullPath());
|
|
return regionService.saveRegion(region);
|
}
|
|
/**
|
* 删除区域
|
*/
|
@MutationMapping(name = "deleteRegion")
|
public Boolean deleteRegion(@Argument Long id) {
|
try {
|
regionService.deleteRegion(id);
|
return true;
|
} catch (Exception e) {
|
return false;
|
}
|
}
|
|
/**
|
* 切换区域状态
|
*/
|
@MutationMapping(name = "toggleRegionState")
|
public Region toggleRegionState(@Argument Long id) {
|
return regionService.toggleRegionState(id);
|
}
|
|
// 内部类:分页结果
|
public static class RegionPageResult {
|
private List<Region> content;
|
private long totalElements;
|
private int totalPages;
|
private int currentPage;
|
private int pageSize;
|
|
public RegionPageResult(List<Region> content, long totalElements, int totalPages, int currentPage, int pageSize) {
|
this.content = content;
|
this.totalElements = totalElements;
|
this.totalPages = totalPages;
|
this.currentPage = currentPage;
|
this.pageSize = pageSize;
|
}
|
|
// Getters
|
public List<Region> getContent() { return content; }
|
public long getTotalElements() { return totalElements; }
|
public int getTotalPages() { return totalPages; }
|
public int getCurrentPage() { return currentPage; }
|
public int getPageSize() { return pageSize; }
|
}
|
|
// 内部类:输入参数
|
public static class RegionInput {
|
private Long id;
|
private String name;
|
private Long pid;
|
private String code;
|
private Integer level;
|
private Boolean leafFlag;
|
private String fullPath;
|
private Integer state;
|
|
// Getters and Setters
|
public Long getId() { return id; }
|
public void setId(Long id) { this.id = id; }
|
|
public String getName() { return name; }
|
public void setName(String name) { this.name = name; }
|
|
public Long getPid() { return pid; }
|
public void setPid(Long pid) { this.pid = pid; }
|
|
public String getCode() { return code; }
|
public void setCode(String code) { this.code = code; }
|
|
public Integer getLevel() { return level; }
|
public void setLevel(Integer level) { this.level = level; }
|
|
public Boolean getLeafFlag() { return leafFlag; }
|
public void setLeafFlag(Boolean leafFlag) { this.leafFlag = leafFlag; }
|
|
public String getFullPath() { return fullPath; }
|
public void setFullPath(String fullPath) { this.fullPath = fullPath; }
|
|
public Integer getState() { return state; }
|
public void setState(Integer state) { this.state = state; }
|
}
|
}
|