import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.modules.homestead.service.*;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.util.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Override
public PageData<Map<String, Object>> loadHomesteadHousehold(Map<String, Object> params) throws Exception {
Integer curPage = Integer.parseInt(StringUtils.checkObj2Str(params.get(Constant.PAGE), "1"));
Integer limit = Integer.parseInt(StringUtils.checkObj2Str(params.get(Constant.LIMIT), "10"));
String regionId = StringUtils.checkObj2Str(params.get("regionId"));
if (StringUtils.isEmpty(regionId)) {
regionId = SecurityUser.getRegionId().toString();
}
List<Map<String, Object>> list = null;
if (StringUtils.isEmpty(regionId)) {
list = new ArrayList<Map<String, Object>>();
return new PageData<Map<String, Object>>(list, 0);
}
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("homestead_household");
SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
sourceBulider.from(limit * (curPage - 1));
sourceBulider.size(limit);
sourceBulider.sort("village_code.keyword", SortOrder.ASC).sort("household_code.keyword", SortOrder.ASC);
BoolQueryBuilder query = QueryBuilders.boolQuery();
PrefixQueryBuilder termQuery1 = QueryBuilders.prefixQuery("village_code", regionId.toString());
query.must(termQuery1);
QueryBuilder termQuery2 = QueryBuilders.termQuery("datasource", "1");
query.must(termQuery2);
sourceBulider.query(query);
searchRequest.source(sourceBulider);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
list = Stream.of(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(Collectors.toList());
if (StringUtils.isNotEmpty(list) && list.size() > 0) {
Map<String, Object> map = null;
String villageCode = "";
String householdCode = "";
String xuHao = "";
String villageName = "";
String parentRegionName = "";
for (int i = 0; i < list.size(); i++) {
map = list.get(i);
xuHao = "";
householdCode = StringUtils.checkObj2Str(map.get("household_code"));
if (StringUtils.isNotEmpty(householdCode) && householdCode.length() >= 4) {
xuHao = householdCode.substring(householdCode.length() - 4);
} else {
xuHao = householdCode;
}
map.put("xu_hao", xuHao);
villageCode = StringUtils.checkObj2Str(map.get("village_code"));
villageName = regionService.getRegionName(Long.parseLong(villageCode));
map.put("village_name", villageName);
parentRegionName = regionService.getParentRegionName(Long.parseLong(villageCode));
map.put("parent_region_name", parentRegionName);
}
}
SearchHits searchHits = searchResponse.getHits();
long total = searchHits.getTotalHits().value;
PageData<Map<String, Object>> pageData = new PageData(list, total);
return pageData;
}
@Override
public PageData<Map<String, Object>> loadHomesteadRegionCun(Map<String, Object> params) throws Exception {
String regionLevel = "5";
Integer curPage = Integer.parseInt(StringUtils.checkObj2Str(params.get(Constant.PAGE), "1"));
Integer limit = Integer.parseInt(StringUtils.checkObj2Str(params.get(Constant.LIMIT), "10"));
String regionId = StringUtils.checkObj2Str(params.get("regionId"));
if (StringUtils.isEmpty(regionId)) {
regionId = SecurityUser.getRegionId().toString();
}
List<Map<String, Object>> list = null;
if (StringUtils.isEmpty(regionId)) {
list = new ArrayList<Map<String, Object>>();
return new PageData<Map<String, Object>>(list, 0);
}
String name = "";
String value = "";
if (regionId.length() == 2) {
name = "province_code";
value = regionId;
} else if (regionId.length() == 4) {
name = "city_code";
value = regionId;
} else if (regionId.length() == 6) {
name = "county_code";
value = regionId;
} else if (regionId.length() == 9) {
name = "town_code";
value = regionId;
} else if (regionId.length() == 12) {
name = "village_code";
value = regionId;
}
if (StringUtils.isEmpty(name)) {
list = new ArrayList<Map<String, Object>>();
return new PageData<Map<String, Object>>(list, 0);
}
SearchRequest searchRequest = new SearchRequest("homestead_region_index");
SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
sourceBulider.from(limit * (curPage - 1));
sourceBulider.size(limit);
BoolQueryBuilder query = QueryBuilders.boolQuery();
QueryBuilder termQuery1 = QueryBuilders.termQuery(name, value);
query.must(termQuery1);
QueryBuilder termQuery2 = QueryBuilders.termQuery("region_level", regionLevel);
query.must(termQuery2);
QueryBuilder termQuery3 = QueryBuilders.termQuery("datasource", "1");
query.must(termQuery3);
sourceBulider.query(query);
searchRequest.source(sourceBulider);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
list = Stream.of(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(Collectors.toList());
if (StringUtils.isNotEmpty(list) && list.size() > 0) {
Map<String, Object> map = null;
String villageCode = "";
String villageName = "";
String parentRegionName = "";
for (int i = 0; i < list.size(); i++) {
map = list.get(i);
villageCode = StringUtils.checkObj2Str(map.get("village_code"));
villageName = regionService.getRegionName(Long.parseLong(villageCode));
map.put("village_name", villageName);
parentRegionName = regionService.getParentRegionName(Long.parseLong(villageCode));
map.put("parent_region_name", parentRegionName);
}
}
SearchHits searchHits = searchResponse.getHits();
long total = searchHits.getTotalHits().value;
PageData<Map<String, Object>> pageData = new PageData(list, total);
return pageData;
}
@Override
public PageData<Map<String, Object>> loadHomesteadDaily(Map<String, Object> params) throws Exception {
Integer curPage = Integer.parseInt(StringUtils.checkObj2Str(params.get(Constant.PAGE), "1"));
Integer limit = Integer.parseInt(StringUtils.checkObj2Str(params.get(Constant.LIMIT), "10"));
String stat_date=StringUtils.checkObj2Str(params.get("stat_date"));
String regionId = StringUtils.checkObj2Str(params.get("regionId"));
if (StringUtils.isEmpty(regionId)) {
regionId = SecurityUser.getRegionId().toString();
}
List<Map<String, Object>> list = null;
if (StringUtils.isEmpty(regionId)) {
list = new ArrayList<Map<String, Object>>();
return new PageData<Map<String, Object>>(list, 0);
}
String name = "";
String value = "";
String regionLevel = "";
String key = "";
if (regionId.length() == 2) {
name = "province_code";
value = regionId;
regionLevel = "2";
key = "city_code";
} else if (regionId.length() == 4) {
name = "city_code";
value = regionId;
regionLevel = "3";
key = "county_code";
} else if (regionId.length() == 6) {
name = "county_code";
value = regionId;
regionLevel = "4";
key = "town_code";
} else if (regionId.length() == 9) {
name = "town_code";
value = regionId;
regionLevel = "5";
key = "village_code";
} else if (regionId.length() == 12) {
name = "village_code";
value = regionId;
regionLevel = "5";
key = "village_code";
}
if (StringUtils.isEmpty(name)) {
list = new ArrayList<Map<String, Object>>();
return new PageData<Map<String, Object>>(list, 0);
}
SearchRequest searchRequest;
String currDate = DateUtils.formatShortDate(new Date());
String searchDate = StringUtils.checkObj2Str(params.get("searchDate"));
if (StringUtils.isEmpty(searchDate) || searchDate.equals(currDate)) {
searchRequest = new SearchRequest("homestead_daily");
}else{
searchRequest = new SearchRequest("homestead_daily_index_history");
}
SearchSourceBuilder sourceBulider = new SearchSourceBuilder();
sourceBulider.from(limit * (curPage - 1));
sourceBulider.size(limit);
BoolQueryBuilder query = QueryBuilders.boolQuery();
QueryBuilder termQuery1 = QueryBuilders.termQuery(name, value);
query.must(termQuery1);
QueryBuilder termQuery2 = QueryBuilders.termQuery("region_level", regionLevel);
query.must(termQuery2);
if(StringUtils.isNotEmpty(searchDate) && !searchDate.equals(currDate)){
stat_date=stat_date.replace("-","");
QueryBuilder termQuery3 = QueryBuilders.termQuery("stat_date", stat_date);
query.must(termQuery3);
}
sourceBulider.query(query);
sourceBulider.sort(key+".keyword",SortOrder.ASC);
searchRequest.source(sourceBulider);
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
list = Stream.of(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(Collectors.toList());
if (StringUtils.isNotEmpty(list) && list.size() > 0) {
Map<String, Object> map = null;
String regionCode = "";
String regionName = "";
String parentRegionName = "";
for (int i = 0; i < list.size(); i++) {
map = list.get(i);
regionCode = StringUtils.checkObj2Str(map.get(key));
regionName = regionService.getRegionName(Long.parseLong(regionCode));
map.put("region_code", regionCode);
map.put("region_name", regionName);
}
}
SearchHits searchHits = searchResponse.getHits();
long total = searchHits.getTotalHits().value;
PageData<Map<String, Object>> pageData = new PageData(list, total);
return pageData;
}