10a5c64e8e6028d8db051953014fe81e8c724830
[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.api.vo.request.ConfigCreateRequest;
25 import com.supwisdom.institute.backend.system.api.vo.request.ConfigQueryRequest;
26 import com.supwisdom.institute.backend.system.api.vo.request.ConfigUpdateRequest;
27 import com.supwisdom.institute.backend.system.api.vo.response.ConfigCreateResponseData;
28 import com.supwisdom.institute.backend.system.api.vo.response.ConfigLoadResponseData;
29 import com.supwisdom.institute.backend.system.api.vo.response.ConfigQueryResponseData;
30 import com.supwisdom.institute.backend.system.api.vo.response.ConfigUpdateResponseData;
31 import com.supwisdom.institute.backend.system.domain.entity.Config;
32 import com.supwisdom.institute.backend.system.domain.exception.ConfigException;
33 import com.supwisdom.institute.backend.system.domain.service.ConfigService;
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     if (id == null || id.length() == 0) {
135       throw new ConfigException().newInstance("exception.update.id.must.not.empty");
136     }
137
138     Config tmp = configService.selectById(id);
139     if (tmp == null) {
140       throw new ConfigException().newInstance("exception.update.domain.not.exist");
141     }
142
143     if (!tmp.getEditable().booleanValue()) {
144       throw new ConfigException().newInstance("exception.editable.can.not.update");
145     }
146
147     Config entity = configUpdateRequest.getEntity();
148     entity.setId(id);
149
150     entity = EntityUtils.merge(tmp, entity);
151     
152 //    if (tmp.getEditable().booleanValue() != entity.getEditable().booleanValue()) {
153 //      throw new ConfigException().newInstance("exception.editable.can.not.update");
154 //    }
155
156     entity.setEditable(true);  // 防止 可修改记录的 editable 被置为false
157
158     Config ret = configService.update(entity);
159
160     ConfigUpdateResponseData resp = ConfigUpdateResponseData.build(ret);
161     
162     return new DefaultApiResponse<ConfigUpdateResponseData>(resp);
163   }
164
165   
166   /**
167    * @param categoryCode
168    * @param configKey
169    * @return
170    */
171   @ApiOperation(value = "根据 categoryCode、configKey 获取配置项", notes = "根据 categoryCode、configKey 获取配置项", nickname = "systemAdminConfigLoadByCategoryKey")
172   @RequestMapping(method = RequestMethod.GET, path = "/loadByCategoryKey", produces = MimeTypeUtils.APPLICATION_JSON_VALUE)
173   @ResponseStatus(value = HttpStatus.OK)
174   @ResponseBody
175   public DefaultApiResponse<ConfigLoadResponseData> loadByCategoryKey(
176       @RequestParam("categoryCode") String categoryCode, 
177       @RequestParam("configKey") String configKey
178       ) {
179     
180     if (categoryCode == null || categoryCode.length() == 0) {
181       throw new ConfigException().newInstance("exception.load.params.must.not.empty");
182     }
183     if (configKey == null || configKey.length() == 0) {
184       throw new ConfigException().newInstance("exception.load.params.must.not.empty");
185     }
186     
187     Config config = configService.selectByCategoryKey(categoryCode, configKey);
188     
189     if (config == null) {
190       throw new ConfigException().newInstance("exception.load.domain.not.exist");
191     }
192
193     ConfigLoadResponseData resp = ConfigLoadResponseData.build(config);
194     
195     return new DefaultApiResponse<ConfigLoadResponseData>(resp);
196   }
197
198 }