lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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; }
    }
}