'增加距离排序'

dev-discount
199901012 4 years ago
parent ada630b7cb
commit a28cbf77be
  1. 12
      hai-cweb/src/main/java/com/cweb/controller/HighMerchantStoreController.java
  2. 12
      hai-cweb/src/main/java/com/cweb/controller/HighUserController.java
  3. 7
      hai-cweb/src/test/common/RegionTest.java
  4. 17
      hai-service/pom.xml
  5. 38
      hai-service/src/main/java/com/hai/common/utils/CoordCommonUtil.java
  6. 10
      hai-service/src/main/java/com/hai/entity/HighMerchantStore.java
  7. 4
      hai-service/src/main/java/com/hai/entity/HighUser.java
  8. 20
      hai-service/src/main/java/com/hai/model/UserModel.java
  9. 8
      hai-service/src/main/java/com/hai/service/HighMerchantStoreService.java
  10. 3
      hai-service/src/main/java/com/hai/service/HighUserCouponService.java
  11. 21
      hai-service/src/main/java/com/hai/service/impl/HighMerchantStoreServiceImpl.java
  12. 11
      hai-service/src/main/java/com/hai/service/impl/HighUserCouponServiceImpl.java

@ -78,8 +78,8 @@ public class HighMerchantStoreController {
@ResponseBody
@ApiOperation(value = "根据卡卷查询门店列表")
public ResponseData getStoreListByCoupon(@RequestParam(name = "couponId", required = true) Long couponId,
@RequestParam(name = "pageNum", required = true) Integer pageNum,
@RequestParam(name = "pageSize", required = true) Integer pageSize) {
@RequestParam(name = "longitude", required = true) String longitude,
@RequestParam(name = "latitude", required = true) String latitude) {
try {
HighCoupon coupon = highCouponService.getCouponById(couponId);
@ -88,17 +88,17 @@ public class HighMerchantStoreController {
}
Map<String,Object> map = new HashMap<>();
map.put("merchantId", coupon.getMerchantId());
map.put("longitude", longitude);
map.put("latitude", latitude);
PageHelper.startPage(pageNum, pageSize);
List<HighMerchantStore> storeList = highMerchantStoreService.getMerchantStoreList(map);
List<HighMerchantStore> storeList = highMerchantStoreService.getStoreListByLongitude(map);
if (storeList != null && storeList.size() > 0) {
HighMerchant merchant = highMerchantService.getMerchantById(coupon.getMerchantId());
for (HighMerchantStore store : storeList) {
store.setExt1(merchant.getMerchantLogo());
}
}
return ResponseMsgUtil.success(new PageInfo<>(storeList));
return ResponseMsgUtil.success(storeList);
} catch (Exception e) {
log.error("HighMerchantStoreController -> getStoreListByCoupon() error!",e);

@ -3,8 +3,11 @@ package com.cweb.controller;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighUser;
import com.hai.entity.HighUserCoupon;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.service.HighUserCouponService;
import com.hai.service.HighUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -37,6 +40,9 @@ public class HighUserController {
@Resource
private HighUserService highUserService;
@Resource
private HighUserCouponService highUserCouponService;
@RequestMapping(value = "/findUser", method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "根据id查询详情")
@ -47,7 +53,11 @@ public class HighUserController {
SessionObject sessionObject = userCenter.getSessionObject(request);
HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();
return ResponseMsgUtil.success(highUserService.findByUserId(userInfoModel.getHighUser().getId()));
HighUser highUser = highUserService.findByUserId(userInfoModel.getHighUser().getId());
if (highUser != null) {
highUser.setUnusedCouponNum( highUserCouponService.getCouponList(highUser.getId(), 1).size()); //未使用卡卷数量
}
return ResponseMsgUtil.success(highUser);
} catch (Exception e) {
log.error("HighUserController --> findByUserId() error!", e);

@ -3,10 +3,13 @@ package common;
import com.CWebApplication;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hai.common.utils.HttpsUtils;
import com.hai.entity.HighMerchantStore;
import com.hai.entity.SecRegion;
import com.hai.service.CommonService;
import com.hai.service.HighMerchantStoreService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
@ -35,6 +38,10 @@ public class RegionTest {
@Resource
private CommonService commonService;
@Resource
private HighMerchantStoreService highMerchantStoreService;
private static final String[] HEX_DIGITS = {"0" ,"1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
@Test

@ -281,6 +281,23 @@
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.gavaghan</groupId>
<artifactId>geodesy</artifactId>
<version>1.1.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

@ -0,0 +1,38 @@
package com.hai.common.utils;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/4/1 20:32
*/
public class CoordCommonUtil {
private static final double EARTH_RADIUS = 6371000; // 平均半径,单位:m;不是赤道半径。赤道为6378左右
/**
* 获得球面地球上两个点之间的距离坐标可以为WGS84GCJ02等任何一种坐标但两个点的坐标类型必须相同
*
* @param lon1 起点的经度
* @param lat1 起点的纬度
* @param lon2 终点的经度
* @param lat2 终点的纬度
* @return 两点之间的距离单位是米
*/
public static double getDistance(Double lat1,Double lng1,Double lat2,Double lng2) {
// 经纬度(角度)转弧度。弧度用作参数,以调用Math.cos和Math.sin
double radiansAX = Math.toRadians(lng1); // A经弧度
double radiansAY = Math.toRadians(lat1); // A纬弧度
double radiansBX = Math.toRadians(lng2); // B经弧度
double radiansBY = Math.toRadians(lat2); // B纬弧度
// 公式中“cosβ1cosβ2cos(α1-α2)+sinβ1sinβ2”的部分,得到∠AOB的cos值
double cos = Math.cos(radiansAY) * Math.cos(radiansBY) * Math.cos(radiansAX - radiansBX)
+ Math.sin(radiansAY) * Math.sin(radiansBY);
// System.out.println("cos = " + cos); // 值域[-1,1]
double acos = Math.acos(cos); // 反余弦值
// System.out.println("acos = " + acos); // 值域[0,π]
// System.out.println("∠AOB = " + Math.toDegrees(acos)); // 球心角 值域[0,180]
return EARTH_RADIUS * acos; // 最终结果附上原文出处链接及本声明。
}
}

@ -104,8 +104,18 @@ public class HighMerchantStore implements Serializable {
private String ext3;
private String distance;
private static final long serialVersionUID = 1L;
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public Long getId() {
return id;
}

@ -1,5 +1,7 @@
package com.hai.entity;
import com.hai.model.UserModel;
import java.io.Serializable;
import java.util.Date;
@ -12,7 +14,7 @@ import java.util.Date;
* 代码由工具生成
*
**/
public class HighUser implements Serializable {
public class HighUser extends UserModel implements Serializable {
/**
* 用户编号
*/

@ -0,0 +1,20 @@
package com.hai.model;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/4/1 23:37
*/
public class UserModel {
// 未使用卡卷数量
private Integer unusedCouponNum;
public Integer getUnusedCouponNum() {
return unusedCouponNum;
}
public void setUnusedCouponNum(Integer unusedCouponNum) {
this.unusedCouponNum = unusedCouponNum;
}
}

@ -1,5 +1,6 @@
package com.hai.service;
import com.github.pagehelper.PageInfo;
import com.hai.entity.HighMerchantStore;
import com.hai.model.HighMerchantStoreModel;
@ -55,4 +56,11 @@ public interface HighMerchantStoreService {
**/
List<HighMerchantStore> getMerchantStoreList(Map<String, Object> map);
/**
* @Author 胡锐
* @Description 查询门店列表(计算精度)
* @Date 2021/4/1 20:34
**/
List<HighMerchantStore> getStoreListByLongitude(Map<String, Object> map);
}

@ -55,4 +55,7 @@ public interface HighUserCouponService {
**/
HighUserCoupon getUserCoupon(Long userId, Long couponId);
List<HighUserCoupon> getCouponList(Long userId,Integer status);
}

@ -1,9 +1,12 @@
package com.hai.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.utils.CoordCommonUtil;
import com.hai.common.utils.MD5Util;
import com.hai.dao.HighMerchantStoreMapper;
import com.hai.entity.HighMerchantStore;
@ -14,15 +17,20 @@ import com.hai.service.HighMerchantStoreService;
import com.hai.service.SecUserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.hai.common.utils.CoordCommonUtil.getDistance;
@Service("highMerchantStoreService")
public class HighMerchantStoreServiceImpl implements HighMerchantStoreService {
@ -151,4 +159,17 @@ public class HighMerchantStoreServiceImpl implements HighMerchantStoreService {
example.setOrderByClause("create_time desc");
return highMerchantStoreMapper.selectByExample(example);
}
@Override
public List<HighMerchantStore> getStoreListByLongitude(Map<String, Object> map) {
List<HighMerchantStore> storeList = getMerchantStoreList(map);
for (HighMerchantStore store : storeList) {
double distance = CoordCommonUtil.getDistance(Double.valueOf(store.getLatitude()), Double.valueOf(store.getLongitude()), Double.valueOf(map.get("latitude").toString()), Double.valueOf(map.get("longitude").toString()));
store.setDistance(String.valueOf( Math.round(distance/100d)/10d));
}
List<HighMerchantStore> collect = storeList.stream().sorted(Comparator.comparing(HighMerchantStore::getDistance)).collect(Collectors.toList());
return collect;
}
}

@ -92,4 +92,15 @@ public class HighUserCouponServiceImpl implements HighUserCouponService {
}
return null;
}
@Override
public List<HighUserCoupon> getCouponList(Long userId, Integer status) {
HighUserCouponExample example = new HighUserCouponExample();
HighUserCouponExample.Criteria criteria = example.createCriteria().andUserIdEqualTo(userId);
if (status != null) {
criteria.andStatusEqualTo(status);
}
example.setOrderByClause("create_time desc");
return highUserCouponMapper.selectByExample(example);
}
}

Loading…
Cancel
Save