9eac867996d094c140ba0d388c626354517768e8
[institute/sw-backend.git] /
1 package com.supwisdom.institute.backend.system.api.v1.admin;
2
3 import io.swagger.annotations.Api;
4 import io.swagger.annotations.ApiImplicitParam;
5 import io.swagger.annotations.ApiImplicitParams;
6 import io.swagger.annotations.ApiOperation;
7 import lombok.extern.slf4j.Slf4j;
8
9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.data.domain.Page;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.util.MimeTypeUtils;
13 import org.springframework.web.bind.annotation.PathVariable;
14 import org.springframework.web.bind.annotation.RequestBody;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.RequestMethod;
17 import org.springframework.web.bind.annotation.RequestParam;
18 import org.springframework.web.bind.annotation.ResponseBody;
19 import org.springframework.web.bind.annotation.ResponseStatus;
20 import org.springframework.web.bind.annotation.RestController;
21
22 import com.supwisdom.institute.backend.common.framework.entity.EntityUtils;
23 import com.supwisdom.institute.backend.common.framework.vo.response.DefaultApiResponse;
24 import com.supwisdom.institute.backend.system.domain.entity.Config;
25 import com.supwisdom.institute.backend.system.domain.exception.ConfigException;
26 import com.supwisdom.institute.backend.system.domain.service.ConfigService;
27 import com.supwisdom.institute.backend.system.domain.vo.request.ConfigCreateRequest;
28 import com.supwisdom.institute.backend.system.domain.vo.request.ConfigQueryRequest;
29 import com.supwisdom.institute.backend.system.domain.vo.request.ConfigUpdateRequest;
30 import com.supwisdom.institute.backend.system.domain.vo.response.ConfigCreateResponseData;
31 import com.supwisdom.institute.backend.system.domain.vo.response.ConfigLoadResponseData;
32 import com.supwisdom.institute.backend.system.domain.vo.response.ConfigQueryResponseData;
33 import com.supwisdom.institute.backend.system.domain.vo.response.ConfigUpdateResponseData;
34
35 @Api(value = "SystemAdminConfig", tags = { "SystemAdminConfig" }, description = "配置项的操作接口")
36 @Slf4j
37 @RestController
38 @RequestMapping("/v1/admin/configs")
39 public class AdminConfigController {
40   
41   @Autowired
42   private ConfigService configService;
43
44
45   /**
46    * @param configQueryRequest
47    * @return
48    */
49   @ApiOperation(value = "查询配置列表", notes = "查询配置列表", nickname = "systemAdminConfigQuery")
50   @ApiImplicitParams({
51     @ApiImplicitParam(name = "loadAll", value = "是否加载全部", required = true, dataType = "boolean", paramType = "query", defaultValue = "false"),
52     @ApiImplicitParam(name = "pageIndex", value = "分页 - 页码", required = true, dataType = "int", paramType = "query", defaultValue = "0", example = "0"),
53     @ApiImplicitParam(name = "pageSize", value = "分页 - 每页记录数", required = true, dataType = "int", paramType = "query", defaultValue = "20", example = "20"),
54     @ApiImplicitParam(name = "mapBean[deleted]", value = "查询条件 - 删除状态 (精确)", required = false, dataType = "boolean", paramType = "query"),
55     @ApiImplicitParam(name = "mapBean[categoryCode]", value = "查询条件 - 分类代码 (精确)", required = false, dataType = "string", paramType = "query"),
56     @ApiImplicitParam(name = "mapBean[categoryName]", value = "查询条件 - 分类名称 (模糊)", required = false, dataType = "string", paramType = "query"),
57     @ApiImplicitParam(name = "mapBean[name]", value = "查询条件 - 名称 (模糊)", required = false, dataType = "string", paramType = "query"),
58     @ApiImplicitParam(name = "mapBean[description]", value = "查询条件 - 描述 (模糊)", required = false, dataType = "string", paramType = "query"),
59     @ApiImplicitParam(name = "mapBean[configKey]", value = "查询条件 - 配置Key (精确)", required = false, dataType = "string", paramType = "query"),
60   })
61   @RequestMapping(method = RequestMethod.GET, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
62   @ResponseStatus(value = HttpStatus.OK)
63   @ResponseBody
64   public DefaultApiResponse<ConfigQueryResponseData> query(ConfigQueryRequest configQueryRequest) {
65     
66     Page<Config> page = configService.selectPageList(
67         configQueryRequest.isLoadAll(),
68         configQueryRequest.getPageIndex(),
69         configQueryRequest.getPageSize(),
70         configQueryRequest.getMapBean(),
71         configQueryRequest.getOrderBy());
72
73     ConfigQueryResponseData resp = ConfigQueryResponseData.of(configQueryRequest).build(page);
74     
75     return new DefaultApiResponse<ConfigQueryResponseData>(resp);
76   }
77
78   /**
79    * @param id
80    * @return
81    */
82   @ApiOperation(value = "根据ID获取配置项", notes = "根据ID获取配置项", nickname="systemAdminConfigLoad")
83   @RequestMapping(method = RequestMethod.GET, path = "/{id}", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
84   @ResponseStatus(value = HttpStatus.OK)
85   @ResponseBody
86   public DefaultApiResponse<ConfigLoadResponseData> load(
87       @PathVariable("id") String id) {
88     
89     if (id == null || id.length() == 0) {
90       throw new ConfigException().newInstance("exception.get.id.must.not.empty");
91     }
92     
93     Config config = configService.selectById(id);
94     
95     if (config == null) {
96       throw new ConfigException().newInstance("exception.get.domain.not.exist");
97     }
98
99     ConfigLoadResponseData resp = ConfigLoadResponseData.build(config);
100     
101     return new DefaultApiResponse<ConfigLoadResponseData>(resp);
102   }
103   
104   /**
105    * @param configCreateRequest
106    * @return
107    */
108   @ApiOperation(value = "创建配置项", notes = "创建配置项", nickname = "systemAdminConfigCreate")
109   @RequestMapping(method = RequestMethod.POST, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
110   @ResponseStatus(value = HttpStatus.CREATED)
111   @ResponseBody
112   public DefaultApiResponse<ConfigCreateResponseData> create(
113       @RequestBody ConfigCreateRequest configCreateRequest) {
114
115     // FIXME: 验证数据有效性
116
117     Config entity = configCreateRequest.getEntity();
118
119     Config ret = configService.insert(entity);
120
121     ConfigCreateResponseData resp = ConfigCreateResponseData.build(ret);
122
123     return new DefaultApiResponse<ConfigCreateResponseData>(resp);
124   }
125
126   @ApiOperation(value = "更新配置项", notes = "更新配置项", nickname = "systemAdminConfigUpdate")
127   @RequestMapping(method = RequestMethod.PUT, consumes = MimeTypeUtils.APPLICATION_JSON_VALUE, produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
128   @ResponseStatus(value = HttpStatus.OK)
129   @ResponseBody
130   public DefaultApiResponse<ConfigUpdateResponseData> update(
131       @PathVariable("id") String id,
132       @RequestBody ConfigUpdateRequest configUpdateRequest) {
133
134     Config entity = configUpdateRequest.getEntity();
135
136     if (entity.getId() == null || entity.getId().length() == 0) {
137       throw new ConfigException().newInstance("exception.update.id.must.not.empty");
138     }
139
140     Config tmp = configService.selectById(entity.getId());
141     if (tmp == null) {
142       throw new ConfigException().newInstance("exception.update.domain.not.exist");
143     }
144
145     if (!tmp.getEditable().booleanValue()) {
146       throw new ConfigException().newInstance("exception.editable.can.not.update");
147     }
148
149     entity = EntityUtils.merge(tmp, entity);
150     
151 //    if (tmp.getEditable().booleanValue() != entity.getEditable().booleanValue()) {
152 //      throw new ConfigException().newInstance("exception.editable.can.not.update");
153 //    }
154
155     entity.setEditable(true);  // 防止 可修改记录的 editable 被置为false
156
157     Config ret = configService.update(entity);
158
159     ConfigUpdateResponseData resp = ConfigUpdateResponseData.build(ret);
160     
161     return new DefaultApiResponse<ConfigUpdateResponseData>(resp);
162   }
163
164   
165   /**
166    * @param categoryCode
167    * @param configKey
168    * @return
169    */
170   @ApiOperation(value = "根据 categoryCode、configKey 获取配置项", notes = "根据 categoryCode、configKey 获取配置项", nickname = "systemAdminConfigLoadByCategoryKey")
171   @RequestMapping(method = RequestMethod.GET, path = "/loadByCategoryKey", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
172   @ResponseStatus(value = HttpStatus.OK)
173   @ResponseBody
174   public DefaultApiResponse<ConfigLoadResponseData> loadByCategoryKey(
175       @RequestParam("categoryCode") String categoryCode, 
176       @RequestParam("configKey") String configKey
177       ) {
178     
179     if (categoryCode == null || categoryCode.length() == 0) {
180       throw new ConfigException().newInstance("exception.load.params.must.not.empty");
181     }
182     if (configKey == null || configKey.length() == 0) {
183       throw new ConfigException().newInstance("exception.load.params.must.not.empty");
184     }
185     
186     Config config = configService.selectByCategoryKey(categoryCode, configKey);
187     
188     if (config == null) {
189       throw new ConfigException().newInstance("exception.load.domain.not.exist");
190     }
191
192     ConfigLoadResponseData resp = ConfigLoadResponseData.build(config);
193     
194     return new DefaultApiResponse<ConfigLoadResponseData>(resp);
195   }
196
197 }