Search in sources :

Example 11 with XmallException

use of cn.exrick.common.exception.XmallException in project xmall by Exrick.

the class UserServiceImpl method getPermissionList.

@Override
public DataTablesResult getPermissionList() {
    DataTablesResult result = new DataTablesResult();
    TbPermissionExample example = new TbPermissionExample();
    List<TbPermission> list = tbPermissionMapper.selectByExample(example);
    if (list == null) {
        throw new XmallException("获取权限列表失败");
    }
    result.setSuccess(true);
    result.setData(list);
    return result;
}
Also used : DataTablesResult(cn.exrick.common.pojo.DataTablesResult) XmallException(cn.exrick.common.exception.XmallException)

Example 12 with XmallException

use of cn.exrick.common.exception.XmallException in project xmall by Exrick.

the class MyMessageListener method onMessage.

@Override
public void onMessage(Message message) {
    // 取消息内容
    TextMessage textMessage = (TextMessage) message;
    try {
        String text = textMessage.getText();
        log.info(text);
    } catch (JMSException e) {
        e.printStackTrace();
        throw new XmallException("监听消息失败");
    }
}
Also used : JMSException(javax.jms.JMSException) XmallException(cn.exrick.common.exception.XmallException) TextMessage(javax.jms.TextMessage)

Example 13 with XmallException

use of cn.exrick.common.exception.XmallException in project xmall by Exrick.

the class SearchItemServiceImpl method importAllItems.

@Override
public int importAllItems() {
    try {
        Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
        // 批量添加
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        // 查询商品列表
        List<SearchItem> itemList = itemMapper.getItemList();
        // 遍历商品列表
        for (SearchItem searchItem : itemList) {
            String image = searchItem.getProductImageBig();
            if (image != null && !"".equals(image)) {
                String[] strings = image.split(",");
                image = strings[0];
            } else {
                image = "";
            }
            searchItem.setProductImageBig(image);
            bulkRequest.add(client.prepareIndex(ITEM_INDEX, ITEM_TYPE, String.valueOf(searchItem.getProductId())).setSource(jsonBuilder().startObject().field("productId", searchItem.getProductId()).field("salePrice", searchItem.getSalePrice()).field("productName", searchItem.getProductName()).field("subTitle", searchItem.getSubTitle()).field("productImageBig", searchItem.getProductImageBig()).field("categoryName", searchItem.getCategory_name()).endObject()));
        }
        BulkResponse bulkResponse = bulkRequest.get();
        log.info("更新索引成功");
        client.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmallException("导入ES索引库出错");
    }
    return 1;
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) TransportAddress(org.elasticsearch.common.transport.TransportAddress) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) BulkRequestBuilder(org.elasticsearch.action.bulk.BulkRequestBuilder) XmallException(cn.exrick.common.exception.XmallException) Settings(org.elasticsearch.common.settings.Settings) XmallException(cn.exrick.common.exception.XmallException) SearchItem(cn.exrick.manager.dto.front.SearchItem)

Example 14 with XmallException

use of cn.exrick.common.exception.XmallException in project xmall by Exrick.

the class SearchItemServiceImpl method getEsInfo.

@Override
public EsInfo getEsInfo() {
    String healthUrl = "http://" + ES_CONNECT_IP + ":" + ES_NODE_CLIENT_PORT + "/_cluster/health";
    String countUrl = "http://" + ES_CONNECT_IP + ":" + ES_NODE_CLIENT_PORT + "/_count";
    String healthResult = HttpUtil.sendGet(healthUrl);
    String countResult = HttpUtil.sendGet(countUrl);
    if (StringUtils.isBlank(healthResult) || StringUtils.isBlank(countResult)) {
        throw new XmallException("连接集群失败,请检查ES运行状态");
    }
    EsInfo esInfo = new EsInfo();
    EsCount esCount = new EsCount();
    try {
        esInfo = new Gson().fromJson(healthResult, EsInfo.class);
        esCount = new Gson().fromJson(countResult, EsCount.class);
        esInfo.setCount(esCount.getCount());
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmallException("获取ES信息出错");
    }
    return esInfo;
}
Also used : EsInfo(cn.exrick.manager.dto.EsInfo) Gson(com.google.gson.Gson) XmallException(cn.exrick.common.exception.XmallException) EsCount(cn.exrick.manager.dto.EsCount) XmallException(cn.exrick.common.exception.XmallException)

Example 15 with XmallException

use of cn.exrick.common.exception.XmallException in project xmall by Exrick.

the class SearchServiceImpl method search.

@Override
public SearchResult search(String key, int page, int size, String sort, int priceGt, int priceLte) {
    try {
        Settings settings = Settings.builder().put("cluster.name", ES_CLUSTER_NAME).build();
        TransportClient client = new PreBuiltTransportClient(settings).addTransportAddress(new TransportAddress(InetAddress.getByName(ES_CONNECT_IP), 9300));
        SearchResult searchResult = new SearchResult();
        // 设置查询条件
        QueryBuilder qb = matchQuery("productName", key);
        // 设置分页
        if (page <= 0) {
            page = 1;
        }
        int start = (page - 1) * size;
        // 设置高亮显示
        HighlightBuilder hiBuilder = new HighlightBuilder();
        hiBuilder.preTags("<a style=\"color: #e4393c\">");
        hiBuilder.postTags("</a>");
        hiBuilder.field("productName");
        // 执行搜索
        SearchResponse searchResponse = null;
        if (priceGt >= 0 && priceLte >= 0 && sort.isEmpty()) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).get();
        } else if (priceGt >= 0 && priceLte >= 0 && sort.equals("1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.ASC).get();
        } else if (priceGt >= 0 && priceLte >= 0 && sort.equals("-1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).setPostFilter(// 过滤条件
            QueryBuilders.rangeQuery("salePrice").gt(priceGt).lt(priceLte)).addSort("salePrice", SortOrder.DESC).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.isEmpty()) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.equals("1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).addSort("salePrice", SortOrder.ASC).get();
        } else if ((priceGt < 0 || priceLte < 0) && sort.equals("-1")) {
            searchResponse = client.prepareSearch(ITEM_INDEX).setTypes(ITEM_TYPE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(// Query
            qb).setFrom(start).setSize(size).setExplain(// 从第几个开始,显示size个数据
            true).highlighter(// 设置高亮显示
            hiBuilder).addSort("salePrice", SortOrder.DESC).get();
        }
        SearchHits hits = searchResponse.getHits();
        // 返回总结果数
        searchResult.setRecordCount(hits.totalHits);
        List<SearchItem> list = new ArrayList<>();
        if (hits.totalHits > 0) {
            for (SearchHit hit : hits) {
                // 总页数
                int totalPage = (int) (hit.getScore() / size);
                if ((hit.getScore() % size) != 0) {
                    totalPage++;
                }
                // 返回结果总页数
                searchResult.setTotalPages(totalPage);
                // 设置高亮字段
                SearchItem searchItem = new Gson().fromJson(hit.getSourceAsString(), SearchItem.class);
                String productName = hit.getHighlightFields().get("productName").getFragments()[0].toString();
                searchItem.setProductName(productName);
                // 返回结果
                list.add(searchItem);
            }
        }
        searchResult.setItemList(list);
        return searchResult;
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmallException("查询ES索引库出错");
    }
}
Also used : TransportClient(org.elasticsearch.client.transport.TransportClient) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) SearchHit(org.elasticsearch.search.SearchHit) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) SearchResult(cn.exrick.manager.dto.front.SearchResult) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) XmallException(cn.exrick.common.exception.XmallException) XmallException(cn.exrick.common.exception.XmallException) SearchResponse(org.elasticsearch.action.search.SearchResponse) SearchItem(cn.exrick.manager.dto.front.SearchItem) PreBuiltTransportClient(org.elasticsearch.transport.client.PreBuiltTransportClient) SearchHits(org.elasticsearch.search.SearchHits) Settings(org.elasticsearch.common.settings.Settings) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Aggregations

XmallException (cn.exrick.common.exception.XmallException)47 Date (java.util.Date)22 TbMember (cn.exrick.manager.pojo.TbMember)12 DataTablesResult (cn.exrick.common.pojo.DataTablesResult)11 TbMemberExample (cn.exrick.manager.pojo.TbMemberExample)5 SimpleDateFormat (java.text.SimpleDateFormat)5 Gson (com.google.gson.Gson)4 TbItem (cn.exrick.manager.pojo.TbItem)3 TbThanksExample (cn.exrick.manager.pojo.TbThanksExample)3 PageInfo (com.github.pagehelper.PageInfo)3 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 CartProduct (cn.exrick.manager.dto.front.CartProduct)2 SearchItem (cn.exrick.manager.dto.front.SearchItem)2 TbContentCategory (cn.exrick.manager.pojo.TbContentCategory)2 TbItemCat (cn.exrick.manager.pojo.TbItemCat)2 TbItemDesc (cn.exrick.manager.pojo.TbItemDesc)2 TbThanks (cn.exrick.manager.pojo.TbThanks)2 TransportClient (org.elasticsearch.client.transport.TransportClient)2 Settings (org.elasticsearch.common.settings.Settings)2