From adf6a5d4f6ed38963d299208c528ffac5bd511df Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Sat, 8 Sep 2018 21:30:01 +0800 Subject: [PATCH] =?UTF-8?q?Closed=20#65=20=E6=94=AF=E6=8C=81=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=A0=87=E7=AD=BE=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/repository/TagRepository.java | 8 +- .../handler/api/rest/ApiTagsController.java | 185 ++++++++++++------ .../api/rest/UkefuApiTagsController.java | 60 ++++++ 3 files changed, 191 insertions(+), 62 deletions(-) create mode 100644 contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/UkefuApiTagsController.java diff --git a/contact-center/app/src/main/java/com/chatopera/cc/webim/service/repository/TagRepository.java b/contact-center/app/src/main/java/com/chatopera/cc/webim/service/repository/TagRepository.java index 0533ae08..b279019a 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/webim/service/repository/TagRepository.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/webim/service/repository/TagRepository.java @@ -23,6 +23,8 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import com.chatopera.cc.webim.web.model.Tag; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; public abstract interface TagRepository extends JpaRepository { @@ -30,8 +32,10 @@ public abstract interface TagRepository extends JpaRepository public abstract Tag findByOrgiAndTag(String orgi , String tag); public abstract Tag findByOrgiAndId(String orgi , String id); - - public abstract Page findByOrgiAndTagtype(String orgi , String tagtype ,Pageable paramPageable); + + @Query(value = "select t from Tag t where (:tagtype is null or t.tagtype = :tagtype) " + + "and t.orgi = :orgi") + public Page findByOrgiAndTagtype(@Param("orgi") String orgi , @Param("tagtype") String tagtype , Pageable paramPageable); public abstract List findByOrgi(String orgi); diff --git a/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiTagsController.java b/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiTagsController.java index ae72898b..930349fb 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiTagsController.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiTagsController.java @@ -1,60 +1,125 @@ -/* - * Copyright (C) 2017 优客服-多渠道客服系统 - * Modifications copyright (C) 2018 Chatopera Inc, - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.chatopera.cc.webim.web.handler.api.rest; - -import com.chatopera.cc.core.UKDataContext; -import com.chatopera.cc.util.Menu; -import com.chatopera.cc.webim.service.repository.TagRepository; -import com.chatopera.cc.webim.util.RestResult; -import com.chatopera.cc.webim.web.handler.Handler; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; - -import javax.servlet.http.HttpServletRequest; -import javax.validation.Valid; - -import org.apache.commons.lang.StringUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import com.chatopera.cc.webim.util.RestResultType; - -@RestController -@RequestMapping("/api/tags") -@Api(value = "标签功能" , description = "获取分类标签") -public class ApiTagsController extends Handler { - - @Autowired - private TagRepository tagRes; - - /** - * 按照分类获取标签列表 - * @param request - * @param type 类型 - * @return - */ - @RequestMapping( method = RequestMethod.GET) - @Menu(type = "apps" , subtype = "tags" , access = true) - @ApiOperation("按照分类获取标签列表,Type 参数类型来自于 枚举,可选值目前有三个 : user workorders summary") - public ResponseEntity list(HttpServletRequest request , @Valid String type) { - return new ResponseEntity<>(new RestResult(RestResultType.OK, tagRes.findByOrgiAndTagtype(super.getOrgi(request) , !StringUtils.isBlank(type) ? type : UKDataContext.ModelType.USER.toString())), HttpStatus.OK); - } -} \ No newline at end of file +/* + * Copyright (C) 2018 Chatopera Inc, + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.chatopera.cc.webim.web.handler.api.rest; + +import com.chatopera.cc.util.Menu; +import com.chatopera.cc.util.exception.CSKefuRestException; +import com.chatopera.cc.util.json.GsonTools; +import com.chatopera.cc.webim.service.repository.TagRepository; +import com.chatopera.cc.webim.web.handler.Handler; +import com.chatopera.cc.webim.web.handler.api.request.RestUtils; +import com.chatopera.cc.webim.web.model.Tag; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; + +@RestController +@RequestMapping("/api/repo/tags") +@Api(value = "标签", description = "管理标签") +public class ApiTagsController extends Handler { + private static final Logger logger = LoggerFactory.getLogger(ApiTagsController.class); + + @Autowired + private TagRepository tagRes; + + /** + * 获取标签 + * + * @param j + * @param request + * @return + */ + private JsonObject fetch(final JsonObject j, final HttpServletRequest request) { + JsonObject resp = new JsonObject(); + String tagType = null; + + if (j.has("tagtype")) + tagType = j.get("tagtype").getAsString(); + + Page records = tagRes.findByOrgiAndTagtype(j.get("orgi").getAsString(), tagType, + new PageRequest(super.getP(request), super.getPs(request), Sort.Direction.DESC, new String[]{"createtime"})); + + JsonArray ja = new JsonArray(); + + for(Tag t: records){ + JsonObject o = new JsonObject(); + o.addProperty("id", t.getId()); + o.addProperty("name", t.getTag()); + o.addProperty("type", t.getTagtype()); + o.addProperty("icon", t.getIcon()); + o.addProperty("color", t.getColor()); + ja.add(o); + } + + resp.add("data", ja); + resp.addProperty("size", records.getSize()); // 每页条数 + resp.addProperty("number", records.getNumber()); // 当前页 + resp.addProperty("totalPage", records.getTotalPages()); // 所有页 + resp.addProperty("totalElements", records.getTotalElements()); // 所有检索结果数量 + resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_SUCC); + + return resp; + } + + + @RequestMapping(method = RequestMethod.POST) + @Menu(type = "apps", subtype = "tags", access = true) + @ApiOperation("联系人标签") + public ResponseEntity operations(HttpServletRequest request, @RequestBody final String body) throws CSKefuRestException, GsonTools.JsonObjectExtensionConflictException { + final JsonObject j = (new JsonParser()).parse(body).getAsJsonObject(); + logger.info("[contact tags] operations payload {}", j.toString()); + JsonObject json = new JsonObject(); + HttpHeaders headers = RestUtils.header(); + j.addProperty("creater", super.getUser(request).getId()); + j.addProperty("orgi", super.getUser(request).getOrgi()); + + if (!j.has("ops")) { + json.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_1); + json.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的请求参数。"); + } else { + switch (StringUtils.lowerCase(j.get("ops").getAsString())) { + case "fetch": + json = fetch(j, request); + break; + default: + json.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3); + json.addProperty(RestUtils.RESP_KEY_ERROR, "不支持的操作。"); + break; + } + } + return new ResponseEntity(json.toString(), headers, HttpStatus.OK); + } + +} diff --git a/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/UkefuApiTagsController.java b/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/UkefuApiTagsController.java new file mode 100644 index 00000000..9215bf40 --- /dev/null +++ b/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/UkefuApiTagsController.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2017 优客服-多渠道客服系统 + * Modifications copyright (C) 2018 Chatopera Inc, + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.chatopera.cc.webim.web.handler.api.rest; + +import com.chatopera.cc.core.UKDataContext; +import com.chatopera.cc.util.Menu; +import com.chatopera.cc.webim.service.repository.TagRepository; +import com.chatopera.cc.webim.util.RestResult; +import com.chatopera.cc.webim.web.handler.Handler; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; + +import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; + +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.chatopera.cc.webim.util.RestResultType; + +@RestController +@RequestMapping("/api/tags") +@Api(value = "标签功能" , description = "获取分类标签") +public class UkefuApiTagsController extends Handler { + + @Autowired + private TagRepository tagRes; + + /** + * 按照分类获取标签列表 + * @param request + * @param type 类型 + * @return + */ + @RequestMapping( method = RequestMethod.GET) + @Menu(type = "apps" , subtype = "tags" , access = true) + @ApiOperation("按照分类获取标签列表,Type 参数类型来自于 枚举,可选值目前有三个 : user workorders summary") + public ResponseEntity list(HttpServletRequest request , @Valid String type) { + return new ResponseEntity<>(new RestResult(RestResultType.OK, tagRes.findByOrgiAndTagtype(super.getOrgi(request) , !StringUtils.isBlank(type) ? type : UKDataContext.ModelType.USER.toString())), HttpStatus.OK); + } +} \ No newline at end of file