嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-cweb/src/main/java/com/cweb/controller/HighGasController.java

166 lines
7.3 KiB

package com.cweb.controller;
import com.cweb.config.TuanYouConfig;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.security.SessionObject;
import com.hai.common.utils.CoordCommonUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighMerchantStore;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.HighGasOilPriceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Controller
@RequestMapping(value = "/highGas")
@Api(value = "团油业务接口")
public class HighGasController {
private static Logger log = LoggerFactory.getLogger(HighGasController.class);
@Resource
private HighGasOilPriceService highGasOilPriceService;
@RequestMapping(value="/getGasStoreList",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "查询加油站列表")
public ResponseData getGasStoreList(@RequestParam(name = "storeName", required = false) String storeName,
@RequestParam(name = "regionId", required = true) Long regionId,
@RequestParam(name = "oilNoName", required = true) String oilNoName,
@RequestParam(name = "longitude", required = true) String longitude,
@RequestParam(name = "latitude", required = true) String latitude,
@RequestParam(name = "pageNum", required = true) Integer pageNum,
@RequestParam(name = "pageSize", required = true) Integer pageSize
) {
try {
if (StringUtils.isBlank(storeName)) {
storeName = null;
}
List<Map<String, Object>> storeList = highGasOilPriceService.getStoreListByOilNo(storeName,regionId, oilNoName);
for (Map<String, Object> store : storeList) {
double distance = CoordCommonUtil.getDistance(Double.valueOf(store.get("latitude").toString()), Double.valueOf(store.get("longitude").toString()), Double.valueOf(latitude), Double.valueOf(longitude));
store.put("distance", Math.round(distance/100d)/10d);
}
List<Map<String, Object>> distance = storeList.stream().sorted(Comparator.comparingDouble(entry -> Double.valueOf(entry.get("distance").toString()))).collect(Collectors.toList());
return ResponseMsgUtil.success(initPageInfoObj(pageNum,distance.size(),pageSize,new PageInfo<>(distance)));
} catch (Exception e) {
log.error("HighGasController -> getGasDetailByStoreKey() error!",e);
return ResponseMsgUtil.exception(e);
}
}
public static <T> PageInfo<T> initPageInfoObj(int currentPage, int total, int pageSize, PageInfo<T> pageInfo) {
List<T> list = pageInfo.getList();
int fromIndex = 0;
int toIndex = 0;
if (total / pageSize == 0 && total % pageSize > 0) {
fromIndex = 0;
toIndex = total;
} else {
if (total / pageSize >= 1 && total % pageSize >= 0) {
fromIndex = pageSize * (currentPage - 1);
if (pageSize * currentPage >= total) {
toIndex = total;
} else {
toIndex = pageSize * currentPage;
}
}
}
try {
list = list.subList(fromIndex, toIndex);
} catch (IndexOutOfBoundsException e) {
fromIndex = 0;
toIndex= pageSize;
list = list.subList(fromIndex, toIndex);
}catch(IllegalArgumentException e) {
fromIndex = total-pageSize;
toIndex =total;
list = list.subList(fromIndex, toIndex);
}
pageInfo.setList(list);
pageInfo.setNextPage(currentPage < ((total + pageSize - 1) / pageSize) ? currentPage + 1 : currentPage);
pageInfo.setTotal(total);
pageInfo.setPageNum(currentPage);
pageInfo.setPages((total + pageSize - 1) / pageSize);
pageInfo.setNavigateLastPage((total + pageSize - 1) / pageSize);
pageInfo.setPrePage(currentPage > 1 ? currentPage - 1 : currentPage);
pageInfo.setIsFirstPage(currentPage == 1 ? true : false);
pageInfo.setIsLastPage(currentPage == (total + pageSize - 1) / pageSize ? true : false);
pageInfo.setHasPreviousPage(currentPage == 1 ? false : true);
pageInfo.setHasNextPage(currentPage == (total + pageSize - 1) / pageSize ? false : true);
return calcNavigatepageNums(pageInfo);
}
private static <T> PageInfo<T> calcNavigatepageNums(PageInfo<T> pageInfo) {
// 当总页数小于或等于导航页码数时
if (pageInfo.getPages() <= pageInfo.getNavigatePages()) {
pageInfo.setNavigatepageNums(new int[pageInfo.getPages()]);
for (int i = 0; i < pageInfo.getPages(); i++) {
pageInfo.getNavigatepageNums()[i] = i + 1;
}
} else { // 当总页数大于导航页码数时
pageInfo.setNavigatepageNums(new int[pageInfo.getNavigatePages()]);
int startNum = pageInfo.getPageNum() - pageInfo.getNavigatePages() / 2;
int endNum = pageInfo.getPageNum() + pageInfo.getNavigatePages() / 2;
if (startNum < 1) {
startNum = 1;
// (最前navigatePages页
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
pageInfo.getNavigatepageNums()[i] = startNum++;
}
} else if (endNum > pageInfo.getPages()) {
endNum = pageInfo.getPages();
// 最后navigatePages页
for (int i = pageInfo.getNavigatePages() - 1; i >= 0; i--) {
pageInfo.getNavigatepageNums()[i] = endNum--;
}
} else {
// 所有中间页
for (int i = 0; i < pageInfo.getNavigatePages(); i++) {
pageInfo.getNavigatepageNums()[i] = startNum++;
}
}
}
return pageInfo;
}
@RequestMapping(value="/getGasDetailByStoreKey",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "根据门店key 查询")
public ResponseData getGasDetailByStoreKey(@RequestParam(name = "storeKey", required = true) String storeKey) {
try {
return ResponseMsgUtil.success(TuanYouConfig.queryGasInfoByGasId(storeKey));
} catch (Exception e) {
log.error("HighGasController -> getGasDetailByStoreKey() error!",e);
return ResponseMsgUtil.exception(e);
}
}
}