Commit 5da4e654 authored by Quxl's avatar Quxl

x

parent 92fc93cf
...@@ -12,12 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping; ...@@ -12,12 +12,9 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.egolm.common.GsonUtil;
import com.egolm.common.StringUtil; import com.egolm.common.StringUtil;
import com.egolm.common.bean.Rjx; import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.Page; import com.egolm.common.jdbc.Page;
import com.egolm.common.web.ServletUtil;
import com.egolm.shop.config.XException; import com.egolm.shop.config.XException;
import com.egolm.shop.service.GoodsService; import com.egolm.shop.service.GoodsService;
import com.egolm.shop.util.I18NUtils; import com.egolm.shop.util.I18NUtils;
...@@ -162,37 +159,6 @@ public class GoodsController { ...@@ -162,37 +159,6 @@ public class GoodsController {
return Rjx.jsonOk().set("list", listMap).set("page",page).toJson(); return Rjx.jsonOk().set("list", listMap).set("page",page).toJson();
} }
@ApiOperation("统一搜索(支持品牌,分类,关键字,仓库等)")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "data", dataType = "String", required = true, value = "JSON参数", defaultValue = "{\"topCategoryNO\":\"01\",\"upCategoryNO\":\"\",\"categoryID\":[\"\"],\"brandID\":[\"\"],\"agentID\":\"\",\"warehouseNO\":\"\",\"scopeTypeID\":\"1\",\"districtID\":\"\",\"keyword\":\"*\",\"index\":1,\"limit\":10,\"userNO\":\"00034\",\"orgNO\":\"XIAN\",\"shopNO\":\"167728271\"}"),
@ApiImplicitParam(paramType = "query", name = "terminal", dataType = "String", required = true, value = "终端标识", defaultValue = "wechat"),
@ApiImplicitParam(paramType = "query", name = "langID", dataType = "String", required = true, value = "语言", defaultValue = "936"),
@ApiImplicitParam(paramType = "query", name = "sign", dataType = "String", required = true, value = "签名", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "timestamp", dataType = "String", required = true, value = "时间戳", defaultValue = "")
})
@RequestMapping(value = "/searchGoods",method=RequestMethod.POST)
public String searchGoods(HttpServletRequest request, HttpServletResponse response) {
String reqJson = request.getParameter("data");
System.out.println("searchGoods-------"+reqJson);
Map<String, Object> argsMap = new HashMap<String,Object>();
argsMap = GsonUtil.toMap(reqJson);
String langID = request.getParameter("langID");
argsMap.put("langID", langID);
try {
if(!argsMap.containsKey("index") || !argsMap.containsKey("limit") || !argsMap.containsKey("userNO") || !argsMap.containsKey("shopNO") || !argsMap.containsKey("orgNO") ) {
return Rjx.jsonErr().setCode(-100).setMessage(I18NUtils.getMessage(langID, "Msg_Parameter_empty")).set("errorMsg", "index,limit,userNO,shopNO,orgNO " +I18NUtils.getMessage(langID, "Msg_Must_enter")).toJson();
}
} catch (Exception e) {
e.printStackTrace();
return Rjx.jsonErr().setCode(-100).setMessage(I18NUtils.getMessage(langID, "Msg_QueryParam_error")).toJson();
}
return goodsService.searchGoods(argsMap);
}
@ApiOperation("商品详情") @ApiOperation("商品详情")
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(paramType = "query", name = "orgNo", dataType = "String", required = true), @ApiImplicitParam(paramType = "query", name = "orgNo", dataType = "String", required = true),
......
package com.egolm.shop.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.Page;
import com.egolm.shop.service.SolrGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(tags={"搜索引擎商品相关接口"})
@Controller
@RequestMapping("solr/goods")
public class SolrGoodsController {
@Autowired
private SolrGoodsService service;
@Autowired
@Qualifier("redisTemplate")
private RedisTemplate<String, ?> redisTemplate;
@ResponseBody
@PostMapping("query")
@ApiOperation("搜索引擎商品搜索")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", dataType="long", allowMultiple=false, required=true, name="index", value="分页编号"),
@ApiImplicitParam(paramType = "query", dataType="long", allowMultiple=false, required=true, name="limit", value="分页大小"),
@ApiImplicitParam(paramType = "query", dataType="string", allowMultiple=false, required=true, name="queryKey", value="搜索关键字"),
@ApiImplicitParam(paramType = "query", dataType="string", allowMultiple=true, required=true, name="orders", value="排序关键字"),
@ApiImplicitParam(paramType = "query", dataType="string", allowMultiple=true, required=true, name="keywords", value="精确搜索条件"),
@ApiImplicitParam(paramType = "query", dataType="string", allowMultiple=true, required=true, name="facetFields", value="单字段分组字段"),
@ApiImplicitParam(paramType = "query", dataType="string", allowMultiple=true, required=true, name="facetPivotFields", value="多字段分组字段"),
@ApiImplicitParam(paramType = "query", name = "sign", dataType = "String", required = true, value = "签名", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "timestamp", dataType = "String", required = true, value = "时间戳", defaultValue = "")
})
public Object query(Long index, Long limit, String queryKey, String[] orders, String[] keywords, String[] facetFields, String[] facetPivotFields) {
Page page = new Page(index, limit, orders);
return service.query(page, queryKey, keywords, facetFields, facetPivotFields);
}
@ResponseBody
@PostMapping("updateByDistrictID")
@ApiOperation("搜索引擎按区域编号更新商品索引")
@ApiImplicitParams({
@ApiImplicitParam(paramType = "query", dataType="string", allowMultiple=false, required=true, name="sDistrictID", value="区域编号"),
@ApiImplicitParam(paramType = "query", name = "sign", dataType = "String", required = true, value = "签名", defaultValue = ""),
@ApiImplicitParam(paramType = "query", name = "timestamp", dataType = "String", required = true, value = "时间戳", defaultValue = "")
})
public Object updateByDistrictID(String sDistrictID) {
service.update(sDistrictID);
return Rjx.jsonOk();
}
@ResponseBody
@PostMapping("update")
@Scheduled(cron="0 0 4 * * ?")
@ApiOperation("搜索引擎更新全部商品索引")
public Object update() {
if(this.getLock().getCode() == 200) {
try {
service.update(null);
return Rjx.jsonOk();
} finally {
this.releaseLock();
}
} else {
return Rjx.jsonErr().setMessage("该任务正在运行,请不要重复执行");
}
}
public static final String SCHEDULE_LOCK_KEY = "SCHEDULE_LOCK_KEY";
public static Long SCHEDULE_LOCK_EXPIRE = 1000L*60*60;
@ResponseBody
@PostMapping("getLock")
@ApiOperation("设置搜索引擎更新索引定时任务分布式互斥锁")
public Rjx getLock() {
/*String result = redisTemplate.execute(new RedisCallback<String>() {
@Override
public String doInRedis(RedisConnection connection) throws DataAccessException {
JedisCommands commands = (JedisCommands) connection.getNativeConnection();
String uuid = UUID.randomUUID().toString();
return commands.set(SCHEDULE_LOCK_KEY, uuid, "NX", "PX", SCHEDULE_LOCK_EXPIRE);
}
});
if(result != null && result.equals("OK")) {
return Rjx.jsonOk();
} else {
return Rjx.jsonErr();
}*/
return Rjx.jsonOk().setMessage("这个注释掉了");
}
@ResponseBody
@PostMapping("releaseLock")
@ApiOperation("删除搜索引擎更新索引定时任务分布式互斥锁")
public Rjx releaseLock() {
redisTemplate.delete(SCHEDULE_LOCK_KEY);
return Rjx.jsonOk();
}
}
...@@ -14,8 +14,6 @@ public interface GoodsService { ...@@ -14,8 +14,6 @@ public interface GoodsService {
public List<Map<String,Object>> GetCategoryGoods (Map<String,Object> map, Page page); public List<Map<String,Object>> GetCategoryGoods (Map<String,Object> map, Page page);
String searchGoods(Map<String, Object> map);
List<Map<String, Object>> hotSearchKey(Map<String, Object> map, Page page); List<Map<String, Object>> hotSearchKey(Map<String, Object> map, Page page);
String goodsDetail(Map<String, Object> map); String goodsDetail(Map<String, Object> map);
......
package com.egolm.shop.service;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.Page;
public interface SolrGoodsService {
public Rjx query(Page page, String queryKey, String[] keywords, String[] facetFields, String[] facetPivotFields);
public void update(String sDistrictID);
public void deleteByQuery(String queryKey);
public void deleteById(String[] documentIds);
}
...@@ -3,7 +3,6 @@ package com.egolm.shop.service.impl; ...@@ -3,7 +3,6 @@ package com.egolm.shop.service.impl;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -27,7 +26,6 @@ import com.egolm.shop.pojo.TCustomer; ...@@ -27,7 +26,6 @@ import com.egolm.shop.pojo.TCustomer;
import com.egolm.shop.pojo.TShop; import com.egolm.shop.pojo.TShop;
import com.egolm.shop.service.CommonService; import com.egolm.shop.service.CommonService;
import com.egolm.shop.service.GoodsService; import com.egolm.shop.service.GoodsService;
import com.egolm.shop.service.SolrGoodsService;
import com.egolm.shop.service.UserService; import com.egolm.shop.service.UserService;
import com.egolm.shop.util.I18NUtils; import com.egolm.shop.util.I18NUtils;
@Service @Service
...@@ -41,9 +39,6 @@ public class GoodsServiceImpl implements GoodsService { ...@@ -41,9 +39,6 @@ public class GoodsServiceImpl implements GoodsService {
@Autowired @Autowired
private CommonService commonService; private CommonService commonService;
@Autowired
private SolrGoodsService solrGoodsService;
/** /**
* 获取当前用户历史购买的商品同分类(三级)下的三个月内购买数量最多的商品Top 20, * 获取当前用户历史购买的商品同分类(三级)下的三个月内购买数量最多的商品Top 20,
* 未登录时获取当前区域三个月内购买数量最多的商品Top 20 * 未登录时获取当前区域三个月内购买数量最多的商品Top 20
...@@ -159,205 +154,6 @@ public class GoodsServiceImpl implements GoodsService { ...@@ -159,205 +154,6 @@ public class GoodsServiceImpl implements GoodsService {
return jdbcTemplate.limit(sql,page); return jdbcTemplate.limit(sql,page);
} }
/**
* 统一搜索
* <p>Title: searchGoods</p>
* <p>Description: </p>
* @param map
* @param page
* @return
* {"topCategoryNO":"01","upCategoryNO":"","categoryID":[""],"brandID":[""],"agentID":"","warehouseNO":"","scopeTypeID":"1","districtID":"","keyword":"*","index":1,"limit":10,"userNO":"00034","orgNO":"XIAN","shopNO":"167728271"}
*/
@Override
public String searchGoods(Map<String, Object> map) {
String langID= (String) map.get("langID");
try {
String userNO=(String) map.get("userNO");
String orgNO=(String) map.get("orgNO");
String shopNO=(String) map.get("shopNO");
List<String> queryParams=new ArrayList<String>(); //搜索引擎查询参数
String districtID = ""; //店铺区域
if(StringUtil.isNotBlank(map.get("districtID"))) {
districtID = map.get("districtID")+"";
}
if(StringUtil.isEmpty(districtID)){
TCustomer customer = userService.getCustomer(userNO, shopNO);
if(customer != null && customer.gettShops() != null && customer.gettShops().size()>0) {
TShop tshop = customer.gettShops().get(0);
districtID = tshop.getsDistrictID();
}
}
queryParams.add("sDistrictID:"+districtID);
Page page = new Page();
page.setIndex(Util.objTo(map.get("index"), Long .class));
page.setLimit(Util.objTo(map.get("limit"), Long.class));
page.setLimitKey("nGoodsID desc");
//分类
if(StringUtil.isNotBlank(map.get("categoryID"))){
//支持多分类筛选
List <String> cList=(List<String>) map.get("categoryID");
if(cList.size()>0){
String distractCategory="";
for(String category :cList){
if(StringUtil.isNotBlank(category)){
distractCategory +="sCategoryNO:"+category+" OR ";
}
}
if(distractCategory.length()>3){
queryParams.add(distractCategory.substring(0, distractCategory.length()-3));
}
}
}
//品牌
if(StringUtil.isNotBlank(map.get("brandID"))){
//支持多品牌筛选
List <String> bList=(List<String>) map.get("brandID");
if(bList.size()>0){
String distractBrand="";
for(String brand :bList){
if(StringUtil.isNotBlank(brand)){
distractBrand +="sBrandID:"+brand+" OR ";
}
}
if(distractBrand.length()>3){
queryParams.add(distractBrand.substring(0, distractBrand.length()-3));
}
}
}
if(StringUtil.isNotBlank(map.get("agentID"))){
queryParams.add("nAgentID:"+map.get("agentID"));
}
if(StringUtil.isNotEmpty(map.get("warehouseNO"))){
queryParams.add("sWarehouseNO:"+map.get("warehouseNO"));
}
if(StringUtil.isNotEmpty(map.get("scopeTypeID"))){
queryParams.add("sScopeTypeID:"+map.get("scopeTypeID"));
}
if(StringUtil.isNotEmpty(map.get("upCategoryDesc"))){
queryParams.add("sUpCategoryDesc:"+map.get("upCategoryDesc"));
}
if(StringUtil.isNotEmpty(map.get("topCategoryDesc"))){
queryParams.add("sTopCategoryDesc:"+map.get("topCategoryDesc"));
}
if(StringUtil.isNotEmpty(map.get("upCategoryNO"))){
queryParams.add("sUpCategoryNO:"+map.get("upCategoryNO"));
}
if(StringUtil.isNotEmpty(map.get("topCategoryNO"))){
queryParams.add("sTopCategoryNO:"+map.get("topCategoryNO"));
}
//查询礼品
if(StringUtil.isNotEmpty(map.get("tag"))&&512==Util.objTo(map.get("tag"),Integer.class)){
queryParams.add("nGoodsTypeID:1");
queryParams.add("sOrgNO:china");
}else{
queryParams.add("sOrgNO:"+orgNO+" OR sOrgNO:china");
}
//搜索,全场商品,分类商品,品牌商品,只显示已上架的
queryParams.add("nOnShelves:1");
//精选商品
if(StringUtil.isNotEmpty(map.get("choiceness"))){
queryParams.add("nChoiceness:"+map.get("choiceness"));
}
String key = (String)map.get("keyword");
key = StringUtil.isBlank(key) ? "*" : key;
if(!"*".equals(key)){
try{
insertSearchLog(key, "",""+map.get("shopNO"));
}catch(Exception e){
e.printStackTrace();
}
}
String orderKey = (String)map.get("orderBy");
String orderSc = (String)map.get("orderbyparms");
orderSc = orderSc == null ? "DESC" : orderSc;
List<String> queryOrders = new ArrayList<String>();
if(StringUtil.isBlank(orderKey)) {
if(key.equals("*")) {
queryOrders.add("nStockQty:DESC");
queryOrders.add("sBrandID:ASC");
queryOrders.add("sGoodsDesc:ASC");
}
} else {
if(orderKey.equals("choiceness")) {
queryOrders.add("nChoiceness:DESC");
}
if(orderKey.equals("time")) {
queryOrders.add("dCreateDate:" + orderSc);
} else if(orderKey.equals("price")) {
queryOrders.add("nRealSalePrice:" + orderSc);
} else if(orderKey.equals("sale")) {
queryOrders.add("nTotalSaleQty:" + orderSc);
}
}
String[] paramsArray = queryParams.toArray(new String[queryParams.size()]);
String[] groupByArray = new String[]{"sCategoryDesc", "sTopCategoryDesc", "sUpCategoryDesc","sBrand","sAgentName","sWarehouseName"};
String[] facetPivotFields = new String[]{"sCategoryNO,sCategoryDesc", "sUpCategoryNO,sUpCategoryDesc", "sTopCategoryNO,sTopCategoryDesc", "sBrandID,sBrand", "nAgentID,sAgentName", "sWarehouseNO,sWarehouseName"};
logger.info("商品检索:" + Rjx.json().set("page", page).set("queryKey", key).set("keywords", paramsArray).set("groupsNames", groupByArray));
Map<String,Object> goodsReturnMap=(Map<String, Object>) solrGoodsService.query(page, key, paramsArray, groupByArray, facetPivotFields);
List<Map<String,Object>> goodsList=(List<Map<String, Object>>) goodsReturnMap.get("datas");
Map<String,Object> group=(Map<String, Object>) goodsReturnMap.get("groups");
List<String> goodsBrandList=(List<String>)group.get("sBrand");
List<String>goodsCategoryList=(List<String>) group.get("sCategoryDesc");
List<String>upCategoryList=(List<String>) group.get("sUpCategoryDesc");
List<String>topCategoryList=(List<String>) group.get("sTopCategoryDesc");
List<String> goodsAgentList=(List<String>) group.get("sAgentName");
List<String> goodsHouseList=(List<String>) group.get("sWarehouseName");
if(goodsBrandList == null) {
goodsBrandList = new ArrayList<String>();
}
if(goodsCategoryList == null) {
goodsCategoryList = new ArrayList<String>();
}
if(upCategoryList == null) {
upCategoryList = new ArrayList<String>();
}
if(topCategoryList == null) {
topCategoryList = new ArrayList<String>();
}
if(goodsAgentList == null) {
goodsAgentList = new ArrayList<String>();
}
if(goodsHouseList == null) {
goodsHouseList = new ArrayList<String>();
}
Collections.sort(goodsBrandList);
Util.replaceKey(goodsList, ":", true,"nGoodsID:goodsID","sGoodsName:goodsName","sGoodsDesc:goodsDesc","sAgentName:agentName","sAgentContractNO:contractNO","sCategoryDesc:categoryName","sCategoryNO:categoryID","sBrand:brandName","sBrandID:brandID","sMainBarcode:barcode","sSpec:spec","nMinSaleQty:minSaleQty","nSaleUnits:saleUnits","sUnit:agentUnit","sURL:imgPath","nAgentID:agentID","nRetailPrice:normalSalePrice","sRetailPriceUnit:goodsUnit","sWarehouseNO:warehouseNO","sWarehouseName:warehouseName","nCaseUnits:caseUnits");
Rjx rjx = Rjx.jsonOk();
rjx.set("goodsList",goodsList);
rjx.set("page", goodsReturnMap.get("page"));
rjx.set("brandList",goodsBrandList);
rjx.set("categoryList", goodsCategoryList );
rjx.set("upCategoryList", upCategoryList );
rjx.set("topCategoryList", topCategoryList );
rjx.set("agentList",goodsAgentList);
rjx.set("warehouseList", goodsHouseList);
rjx.set("g", goodsReturnMap.get("pivots"));
return rjx.toJson();
} catch (Exception e) {
e.printStackTrace();
}
return Rjx.jsonErr().setCode(-100).setMessage(I18NUtils.getMessage(langID, "Msg_Search_failure")).toJson();
}
/** /**
* 商品详情 * 商品详情
* <p>Title: goodsDetail</p> * <p>Title: goodsDetail</p>
......
package com.egolm.shop.service.impl;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.FacetField;
import org.apache.solr.client.solrj.response.FacetField.Count;
import org.apache.solr.client.solrj.response.PivotField;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.NamedList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.egolm.common.DateUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.Util;
import com.egolm.common.bean.Rjx;
import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.common.jdbc.Page;
import com.egolm.shop.config.XException;
import com.egolm.shop.controller.SolrGoodsController;
import com.egolm.shop.service.SolrGoodsService;
@Service
public class SolrGoodsServiceImpl implements SolrGoodsService {
private static final Logger logger = Logger.getLogger(SolrGoodsService.class);
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private HttpSolrClient solrClient;
@Override
public Rjx query(Page page, String queryKey, String[] keywords, String[] facetFields, String[] facetPivotFields) {
logger.info("收到商品检索请求:" + Rjx.json().set("page", page).set("queryKey", queryKey).set("keywords", keywords).set("facetFields", facetFields).set("facetPivotFields", facetPivotFields));
if(page == null) {
page = new Page(1L, 20L);
}
queryKey = queryKey.toUpperCase();
String query = queryKey;
query = query.replaceAll("[-~!@#\\$%\\^&\\(\\)=_\\+\\[\\]\\{\\}|\\\\;':\",\\./<>\\?]", " ").replaceAll("[\\*]+", "*").replaceAll("\\s+", " ").trim();
logger.info("处理后搜索关键字:" + query);
SolrQuery solrQuery = new SolrQuery(query);
if(keywords != null) {
for(String keyword : keywords) {
solrQuery.addFilterQuery(keyword);
}
}
String[] limitKeys = page.getLimitKey();
if(limitKeys != null) {
for(String key : limitKeys) {
System.out.println("key---"+key);
String[] kv = key.split(" ", 2);
ORDER sc = kv[1] == null || kv[1].trim().length() == 0 || kv[1].equalsIgnoreCase("asc") ? ORDER.asc : ORDER.desc;
solrQuery.addSort(kv[0], sc);
}
}
solrQuery.setFacet(true);
solrQuery.addFacetField(facetFields);
solrQuery.addFacetPivotField(facetPivotFields);
solrQuery.setRows((int)(long)page.getLimit());
solrQuery.setStart(Util.objTo(page.getFirstRowNumber(), Integer.class) - 1);
try {
QueryResponse response = solrClient.query(solrQuery);
Map<String, List<String>> groups = new HashMap<String, List<String>>();
List<FacetField> facetFieldList = response.getFacetFields();
for (FacetField facet : facetFieldList) {
String fieldName = facet.getName();
if(StringUtil.isNotBlank(fieldName)) {
List<Count> values = facet.getValues();
for(int i = 0; i < values.size(); i++) {
Count count = values.get(i);
List<String> list = groups.get(fieldName);
if(list == null) {
list = new ArrayList<String>();
}
if(count.getCount() > 0) {
list.add(count.getName());
} else {
break;
}
groups.put(fieldName, list);
}
}
}
NamedList<List<PivotField>> namedList = response.getFacetPivot();
Map<String, List<Map<String, Object>>> pivots = new HashMap<String, List<Map<String, Object>>>();
if(namedList != null) {
for(int i = 0; i < namedList.size(); i++) {
List<Map<String, Object>> _l = new ArrayList<Map<String, Object>>();
String name = namedList.getName(i);
List<PivotField> list1 = namedList.getVal(i);
for(PivotField pivotField1 : list1) {
Map<String, Object> _m = new HashMap<String, Object>();
Object _v1 = pivotField1.getValue();
String _f1 = pivotField1.getField();
_m.put(_f1, _v1);
List<PivotField> list2 = pivotField1.getPivot();
for(PivotField pivotField2 : list2) {
Object _v2 = pivotField2.getValue();
String _f2 = pivotField2.getField();
_m.put(_f2, _v2);
}
_l.add(_m);
}
pivots.put(name, _l);
}
}
Rjx result = Rjx.jsonOk();
SolrDocumentList docList = response.getResults();
if(docList != null) {
page.setTotal(docList.getNumFound());
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for(SolrDocument doc : docList) {
Map<String, Object> map = new HashMap<String, Object>();
for(String key : doc.keySet()) {
Object objValue = doc.get(key);
map.put(key, objValue);
}
list.add(map);
}
result = Rjx.jsonOk().set("datas", list).set("page", page).set("groups", groups).set("pivots", pivots);
}
return result;
} catch (Exception e) {
throw new XException("SolrQuery:" + queryKey + "," + StringUtil.toJson(keywords), e);
}
}
@Override
public void deleteByQuery(String queryKey) {
try {
solrClient.deleteByQuery(queryKey);
solrClient.commit();
} catch (SolrServerException | IOException e) {
throw new XException("SolrDelete:" + queryKey, e);
}
}
@Override
public void deleteById(String[] documentIds) {
try {
solrClient.deleteById(Arrays.asList(documentIds));
solrClient.commit();
} catch (SolrServerException | IOException e) {
throw new XException("SolrDelete:" + StringUtil.toJson(documentIds), e);
}
}
@Async
public void update(String sDistrictID) {
Long start = System.currentTimeMillis();
List<String> strs = new ArrayList<String>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if(StringUtil.isBlank(sDistrictID)) {
String sql = "SELECT DISTINCT sDistrictID FROM tShop WHERE sDistrictID IS NOT NULL AND sDistrictID != ''";
Page pageShop = new Page(1L, 5000L, "sDistrictID");
list = jdbcTemplate.limitAll(sql, pageShop);
} else {
list.add(Rjx.json().set("sDistrictID", sDistrictID).toMap());
}
String sIndexID = null;
for(Map<String, Object> map : list) {
try {
sIndexID = (String)map.get("sDistrictID");
if(StringUtil.isBlank(sIndexID)) {
continue;
}
logger.info(DateUtil.format(new Date()) + "-重建区域[" + sIndexID + "]索引... " + SolrGoodsServiceImpl.class.getResource("/").getPath());
String queryKey = "sDistrictID:" + sIndexID;
solrClient.deleteByQuery(queryKey);
String sqlGoods = "SELECT ttt.*, ssoc.sCategoryDesc sTopOrgCategoryDesc, ssocc.sScopeTypeID sDefScopeTypeID, ssocc.sScopeType sDefScopeType FROM ( SELECT tt.*, ss.sCategoryDesc sTopCategoryDesc, soc.sUpCategoryNO sTopOrgCategoryNO, soc.sScopeTypeID sTopOrgScopeTypeID FROM ( SELECT t.nTag, t.nCaseUnits, t.sDistrictID, t.sWarehouseNO, t.sAgentContractNO, t.nGoodsID, t.sOrgNO, t.sGoodsDesc, t.sBrand, t.sBrandID, t.nAgentID, t.sMainBarcode, t.sSpec, t.sUnit, t.nMinSaleQty, t.nRealSalePrice, t.nSaleUnits, t.sOrgCategoryNO, t.sCategoryNO, t.sCategoryDesc, t.sUpCategoryNO, cc.sCategoryDesc sUpCategoryDesc, t.sOrgScopeType, t.sOrgScopeTypeID, t.sGoodsName, t.sAgentName, t.sURL, t.sWarehouseName, t.nRetailPrice, t.sRetailPriceUnit, t.sOrgCategoryDesc, t.sUpOrgCaregoryNO, t.nTotalSaleQty, cccc.sCategoryDesc sUpOrgCategoryDesc, cc.sUpCategoryNO sTopCategoryNO, oc.sScopeTypeID, oc.sScopeType, aso.nStockQty - aso.nLockedQty nStockQty, t.dCreateDate FROM ( SELECT awda.*, ogc.sCategoryNO sOrgCategoryNO, ogc.sCategoryDesc sOrgCategoryDesc, ogc.sUpCategoryNO sUpOrgCaregoryNO, ogc.sScopeType sOrgScopeType, ogc.sScopeTypeID sOrgScopeTypeID, g.sGoodsName, a.sAgentName, ( CASE WHEN agp.sURL IS NULL THEN p.sURL ELSE agp.sURL END ) sURL, w.sWarehouseName, gmp.nPrice nRetailPrice, gmp.sUnit sRetailPriceUnit, ccc.sCategoryDesc, ccc.sUpCategoryNO FROM ( SELECT GG.*, acg.nTag, acg.sOrgNO, acg.sGoodsDesc, acg.sBrand, acg.sBrandID, acg.nAgentID, acg.sMainBarcode, acg.sSpec, acg.sUnit, acg.nMinSaleQty, acg.nRealSalePrice, acg.nSaleUnits, acg.sCategoryNO, acg.nTotalSaleQty, acg.dCreateDate, acg.nCaseUnits FROM ( SELECT f1.sDistrictID, f1.sWarehouseNO, f1.sAgentContractNO, f1.nGoodsID, f2.nMaxDCPriority FROM ( SELECT wd.sDistrictID, wd.sWarehouseNO, acw.sAgentContractNO, acg.nGoodsID, wd.nDCPriority FROM tWarehouseDistrict wd LEFT JOIN tAgentContractWarehouse acw ON acw.sWarehouseNO = wd.sWarehouseNO AND acw.nTag & 1 = 0 LEFT JOIN tAgentContractGoods acg ON acg.sAgentContractNO = acw.sAgentContractNO AND acg.nTag & 1 = 0 WHERE wd.sDistrictID = '" + sIndexID + "' AND wd.nTag & 1 = 0 GROUP BY wd.sDistrictID, wd.sWarehouseNO, acw.sAgentContractNO, acg.nGoodsID, wd.nDCPriority ) f1, ( SELECT wd.sDistrictID, acw.sAgentContractNO, acg.nGoodsID, MIN (wd.nDCPriority) nMaxDCPriority FROM tWarehouseDistrict wd LEFT JOIN tAgentContractWarehouse acw ON acw.sWarehouseNO = wd.sWarehouseNO AND acw.nTag & 1 = 0 LEFT JOIN tAgentContractGoods acg ON acg.sAgentContractNO = acw.sAgentContractNO AND acg.nTag & 1 = 0 WHERE wd.sDistrictID = '" + sIndexID + "' AND wd.nTag & 1 = 0 GROUP BY wd.sDistrictID, acw.sAgentContractNO, acg.nGoodsID ) f2 WHERE f1.sDistrictID = f2.sDistrictID AND f1.sAgentContractNO = f2.sAgentContractNO AND f1.nGoodsID = f2.nGoodsID AND f1.nDCPriority = f2.nMaxDCPriority ) GG LEFT JOIN tAgentContractGoods acg ON acg.sAgentContractNO = GG.sAgentContractNO AND acg.nTag & 1 = 0 AND GG.nGoodsID = acg.nGoodsID ) awda LEFT JOIN ( SELECT ogcc.*, c.sCategoryDesc, c.sUpCategoryNO FROM tOrgGoodsCategory ogcc LEFT JOIN tCategory c ON c.sCategoryNO = ogcc.sCategoryNO AND c.nTag & 1 = 0 ) ogc ON ogc.sOrgNO = awda.sOrgNO AND ogc.nGoodsID = awda.nGoodsID LEFT JOIN tCategory ccc ON ccc.sCategoryNO = awda.sCategoryNO AND ccc.nTag & 1 = 0 LEFT JOIN tGoods g ON g.nGoodsID = awda.nGoodsID AND g.nTag & 1 = 0 LEFT JOIN tAgent a ON a.nAgentID = awda.nAgentID AND a.nTag & 1 = 0 LEFT JOIN tGoodsPic p ON p.nGoodsID = awda.nGoodsID AND p.nTag & 1 = 0 AND p.sPicTypeID = '1' AND p.nIdx = 1 LEFT JOIN tAgentGoodsPic agp ON agp.nAgentID = awda.nAgentID AND agp.nGoodsID = awda.nGoodsID AND agp.nTag & 1 = 0 AND agp.sPicTypeID = '1' AND agp.nIdx = 1 LEFT JOIN tWarehouse w ON w.sWarehouseNO = awda.sWarehouseNO AND w.nTag & 1 = 0 LEFT JOIN tGoodsMarketPrice gmp ON gmp.nGoodsID = awda.nGoodsID AND gmp.nTag & 1 = 0 AND gmp.sOrgNO = awda.sOrgNO WHERE awda.sAgentContractNO IS NOT NULL AND awda.nGoodsID IS NOT NULL AND awda.nTag & 1 = 0 AND awda.nTag & 256 = 0 ) t LEFT JOIN tCategory cc ON cc.sCategoryNO = t.sUpCategoryNO AND cc.nTag & 1 = 0 LEFT JOIN tCategory cccc ON cccc.sCategoryNO = t.sUpOrgCaregoryNO AND cccc.nTag & 1 = 0 LEFT JOIN tOrgCategory oc ON oc.sCategoryNO = t.sOrgCategoryNO AND oc.nTag & 1 = 0 AND oc.sOrgNO = t.sOrgNO LEFT JOIN tAgentStockOnline aso ON aso.nGoodsID = t.nGoodsID AND aso.sAgentContractNO = t.sAgentContractNO AND aso.sWarehouseNO = t.sWarehouseNO ) tt LEFT JOIN tCategory ss ON ss.sCategoryNO = tt.sTopCategoryNO AND ss.nTag & 1 = 0 LEFT JOIN tOrgCategory soc ON soc.sCategoryNO = tt.sUpOrgCaregoryNO AND soc.nTag & 1 = 0 AND soc.sScopeTypeID = tt.sOrgScopeTypeID AND soc.sOrgNO = tt.sOrgNO ) ttt LEFT JOIN tOrgCategory ssoc ON ssoc.sCategoryNO = ttt.sTopOrgCategoryNO AND ssoc.nTag & 1 = 0 AND ssoc.sScopeTypeID = ttt.sTopOrgScopeTypeID AND ssoc.sOrgNO = ttt.sOrgNO LEFT JOIN tOrgCategory ssocc ON ssocc.sCategoryNO = ttt.sCategoryNO AND ssocc.nTag & 1 = 0 AND ssocc.sOrgNO = ttt.sOrgNO WHERE ttt.sWarehouseName IS NOT NULL AND ttt.sWarehouseNO IS NOT NULL " + StringUtil.join(" AND ", " AND ", strs);
Long goodsPageIndex = 1L;
Page pageGoods = new Page(goodsPageIndex, 5000L, "sDistrictID", "sScopeTypeID", "sAgentContractNO", "nGoodsID");
List<Map<String, Object>> goodsList = null;
do {
pageGoods.setIndex(goodsPageIndex++);
goodsList = jdbcTemplate.limit(sqlGoods, pageGoods);
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
for(Map<String, Object> goods : goodsList) {
SolrInputDocument solrInputDocument = new SolrInputDocument();
String sDistrictID_DOC = Util.objTo(goods.get("sDistrictID"), String.class, "");
String sAgentContractNO_DOC = Util.objTo(goods.get("sAgentContractNO"), String.class, "");
String nGoodsID_DOC = Util.objTo(goods.get("nGoodsID"), String.class, "");
String sGoodsDesc = Util.objTo(goods.get("sGoodsDesc"), String.class, "");
String sGoodsName = Util.objTo(goods.get("sGoodsName"), String.class, "");
String sURL = Util.objTo(goods.get("sURL"), String.class, "");
String sBrand = Util.objTo(goods.get("sBrand"), String.class, "");
String sBrandID = Util.objTo(goods.get("sBrandID"), String.class, "");
String sCategoryNO = Util.objTo(goods.get("sCategoryNO"), String.class, "");
String sCategoryDesc = Util.objTo(goods.get("sCategoryDesc"), String.class, "");
String sUpCategoryNO = Util.objTo(goods.get("sUpCategoryNO"), String.class, "");
String sUpCategoryDesc = Util.objTo(goods.get("sUpCategoryDesc"), String.class, "");
String sTopCategoryNO = Util.objTo(goods.get("sTopCategoryNO"), String.class, "");
String sTopCategoryDesc = Util.objTo(goods.get("sTopCategoryDesc"), String.class, "");
String sOrgCategoryNO = Util.objTo(goods.get("sOrgCategoryNO"), String.class, "");
String sOrgCategoryDesc = Util.objTo(goods.get("sOrgCategoryDesc"), String.class, "");
String sUpOrgCategoryNO = Util.objTo(goods.get("sUpOrgCategoryNO"), String.class, "");
String sUpOrgCategoryDesc = Util.objTo(goods.get("sUpOrgCategoryDesc"), String.class, "");
String sTopOrgCategoryNO = Util.objTo(goods.get("sTopOrgCategoryNO"), String.class, "");
String sTopOrgCategoryDesc = Util.objTo(goods.get("sTopOrgCategoryDesc"), String.class, "");
sCategoryNO = StringUtil.isBlank(sOrgCategoryNO) ? sCategoryNO : sOrgCategoryNO;
sCategoryDesc = StringUtil.isBlank(sOrgCategoryDesc) ? sCategoryDesc : sOrgCategoryDesc;
sUpCategoryNO = StringUtil.isBlank(sUpOrgCategoryNO) ? sUpCategoryNO : sUpOrgCategoryNO;
sUpCategoryDesc = StringUtil.isBlank(sUpOrgCategoryDesc) ? sUpCategoryDesc : sUpOrgCategoryDesc;
sTopCategoryNO = StringUtil.isBlank(sTopOrgCategoryNO) ? sTopCategoryNO : sTopOrgCategoryNO;
sTopCategoryDesc = StringUtil.isBlank(sTopOrgCategoryDesc) ? sTopCategoryDesc : sTopOrgCategoryDesc;
String sScopeTypeID_DOC = Util.objTo(goods.get("sScopeTypeID"), String.class, "");
String sScopeType = Util.objTo(goods.get("sScopeType"), String.class, "");
String sDefScopeTypeID = Util.objTo(goods.get("sDefScopeTypeID"), String.class, "");
String sDefScopeType = Util.objTo(goods.get("sDefScopeType"), String.class, "");
sScopeTypeID_DOC = StringUtil.isBlank(sScopeTypeID_DOC) ? sDefScopeTypeID : sScopeTypeID_DOC;
sScopeType = StringUtil.isBlank(sScopeType) ? sDefScopeType : sScopeType;
String nAgentID = Util.objTo(goods.get("nAgentID"), String.class, "");
String sAgentName = Util.objTo(goods.get("sAgentName"), String.class, "");
String sMainBarcode = Util.objTo(goods.get("sMainBarcode"), String.class, "");
String sSpec = Util.objTo(goods.get("sSpec"), String.class, "");
String sUnit = Util.objTo(goods.get("sUnit"), String.class, "");
String nMinSaleQty = Util.objTo(goods.get("nMinSaleQty"), String.class, "");
String nRealSalePrice = Util.objTo(goods.get("nRealSalePrice"), String.class, "");
String nSaleUnits = Util.objTo(goods.get("nSaleUnits"), String.class, "");
String nRetailPrice = Util.objTo(goods.get("nRetailPrice"), String.class, "");
String sRetailPriceUnit = Util.objTo(goods.get("sRetailPriceUnit"), String.class, "");
String sWarehouseNO = Util.objTo(goods.get("sWarehouseNO"), String.class, "");
String sWarehouseName = Util.objTo(goods.get("sWarehouseName"), String.class, "");
String sOrgNO = Util.objTo(goods.get("sOrgNO"), String.class, "");
Date dCreateDate = Util.objTo(goods.get("dCreateDate"), Date.class, null);
Integer nStockQty = Util.objTo(goods.get("nStockQty"), Integer.class, 0);
Integer nTotalSaleQty = Util.objTo(goods.get("nTotalSaleQty"), Integer.class, 0);
Integer nTag = Util.objTo(goods.get("nTag"), Integer.class, null);
Integer nCaseUnits = Util.objTo(goods.get("nCaseUnits"), Integer.class, 0);
solrInputDocument.addField("id", StringUtil.join("$", Arrays.asList(sDistrictID_DOC, sScopeTypeID_DOC, sAgentContractNO_DOC, nGoodsID_DOC)));
solrInputDocument.addField("sDistrictID", sDistrictID_DOC);
solrInputDocument.addField("sAgentContractNO", sAgentContractNO_DOC);
solrInputDocument.addField("nGoodsID", nGoodsID_DOC);
solrInputDocument.addField("sGoodsDesc", sGoodsDesc);
solrInputDocument.addField("sGoodsName", sGoodsName);
solrInputDocument.addField("sURL", sURL);
solrInputDocument.addField("sBrand", sBrand);
solrInputDocument.addField("sBrandID", sBrandID);
solrInputDocument.addField("sCategoryNO", sCategoryNO);
solrInputDocument.addField("sCategoryDesc", sCategoryDesc);
solrInputDocument.addField("sUpCategoryNO", sUpCategoryNO);
solrInputDocument.addField("sUpCategoryDesc", sUpCategoryDesc);
solrInputDocument.addField("sTopCategoryNO", sTopCategoryNO);
solrInputDocument.addField("sTopCategoryDesc", sTopCategoryDesc);
solrInputDocument.addField("nAgentID", nAgentID);
solrInputDocument.addField("sAgentName", sAgentName);
solrInputDocument.addField("sMainBarcode", sMainBarcode);
solrInputDocument.addField("sSpec", sSpec);
solrInputDocument.addField("sUnit", sUnit);
solrInputDocument.addField("nMinSaleQty", nMinSaleQty);
solrInputDocument.addField("nRealSalePrice", nRealSalePrice);
solrInputDocument.addField("nSaleUnits", nSaleUnits);
solrInputDocument.addField("nRetailPrice", nRetailPrice);
solrInputDocument.addField("sRetailPriceUnit", sRetailPriceUnit);
solrInputDocument.addField("sWarehouseNO", sWarehouseNO);
solrInputDocument.addField("sWarehouseName", sWarehouseName);
solrInputDocument.addField("sScopeTypeID", sScopeTypeID_DOC);
solrInputDocument.addField("sScopeType", sScopeType);
solrInputDocument.addField("sOrgNO", sOrgNO);
solrInputDocument.addField("dCreateDate", dCreateDate == null ? 0 : dCreateDate.getTime());
solrInputDocument.addField("nStockQty", nStockQty == 0 ? 0 : 1);
solrInputDocument.addField("nTotalSaleQty", nTotalSaleQty == null ? 0 : nTotalSaleQty);
solrInputDocument.addField("nOnShelves", (((nTag.intValue()&16) == 16) ? "1" : "0"));
solrInputDocument.addField("nGoodsTypeID", (((nTag.intValue()&512) == 512) ? 1 : 0));
solrInputDocument.addField("nChoiceness", (((nTag.intValue()&64) == 64) ? 1 : 0));
solrInputDocument.addField("nTag", nTag);
solrInputDocument.addField("nCaseUnits", nCaseUnits);
Set<String> set = new HashSet<String>();
String sSimplePinyin = StringUtil.SimplePinyin(sGoodsDesc);
set.addAll(Arrays.asList(StringUtil.PinyinKAnalyzer(sSimplePinyin, 2, 4)));
String sPinyin = StringUtil.join(" ", set);
String code4 = sMainBarcode.length() >= 4 ? sMainBarcode.substring(sMainBarcode.length() - 4, sMainBarcode.length()) : "";
String code5 = sMainBarcode.length() >= 5 ? sMainBarcode.substring(sMainBarcode.length() - 5, sMainBarcode.length()) : "";
String code6 = sMainBarcode.length() >= 6 ? sMainBarcode.substring(sMainBarcode.length() - 6, sMainBarcode.length()) : "";
String manu = StringUtil.join(" ", Arrays.asList(code4, sGoodsDesc, code6, sBrand, sCategoryDesc, sUpCategoryDesc, code5, sAgentName, sMainBarcode, sScopeType, sPinyin));
manu = manu.replaceAll("[-~!@#\\$%\\^&\\*\\(\\)=_+\\[\\]\\{\\}|\\\\;':\",\\./<>\\?]", " ").replaceAll("\\s+", " ").trim();
docs.add(solrInputDocument);
}
List<List<SolrInputDocument>> cdocs = Util.split(docs, 500);
for(List<SolrInputDocument> cdoc : cdocs) {
solrClient.add(cdoc);
}
} while(goodsList.size() > 0);
solrClient.commit();
} catch (IOException | SolrServerException e) {
logger.error("重建区域[" + sIndexID + "]索引失败", e);
try {
solrClient.rollback();
} catch (IOException | SolrServerException ex) {
logger.error("重建区域[" + sIndexID + "]索引异常回滚失败", ex);
}
}
}
SolrGoodsController.SCHEDULE_LOCK_EXPIRE = System.currentTimeMillis() - start;;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment