diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighMerchantStoreController.java b/hai-cweb/src/main/java/com/cweb/controller/HighMerchantStoreController.java index 3210332d..52610f70 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighMerchantStoreController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighMerchantStoreController.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 map = new HashMap<>(); map.put("merchantId", coupon.getMerchantId()); + map.put("longitude", longitude); + map.put("latitude", latitude); - PageHelper.startPage(pageNum, pageSize); - List storeList = highMerchantStoreService.getMerchantStoreList(map); - + List 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); diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java index dfa29ed5..ed4fcb9b 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java @@ -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); diff --git a/hai-cweb/src/test/common/RegionTest.java b/hai-cweb/src/test/common/RegionTest.java index 2627d97a..e13b25ef 100644 --- a/hai-cweb/src/test/common/RegionTest.java +++ b/hai-cweb/src/test/common/RegionTest.java @@ -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 diff --git a/hai-service/pom.xml b/hai-service/pom.xml index 2094d06a..b4de946e 100644 --- a/hai-service/pom.xml +++ b/hai-service/pom.xml @@ -281,6 +281,23 @@ easyexcel 2.2.6 + + org.gavaghan + geodesy + 1.1.3 + + + org.gavaghan + geodesy + 1.1.3 + compile + + + org.gavaghan + geodesy + 1.1.3 + compile + diff --git a/hai-service/src/main/java/com/hai/common/utils/CoordCommonUtil.java b/hai-service/src/main/java/com/hai/common/utils/CoordCommonUtil.java new file mode 100644 index 00000000..4e59b16e --- /dev/null +++ b/hai-service/src/main/java/com/hai/common/utils/CoordCommonUtil.java @@ -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左右 + + /** + * 获得球面(地球)上两个点之间的距离(坐标可以为WGS84、GCJ02等任何一种坐标,但两个点的坐标类型必须相同) + * + * @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; // 最终结果附上原文出处链接及本声明。 + } +} diff --git a/hai-service/src/main/java/com/hai/entity/HighMerchantStore.java b/hai-service/src/main/java/com/hai/entity/HighMerchantStore.java index 658752ff..b4519948 100644 --- a/hai-service/src/main/java/com/hai/entity/HighMerchantStore.java +++ b/hai-service/src/main/java/com/hai/entity/HighMerchantStore.java @@ -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; } @@ -357,4 +367,4 @@ public class HighMerchantStore implements Serializable { sb.append("]"); return sb.toString(); } -} \ No newline at end of file +} diff --git a/hai-service/src/main/java/com/hai/entity/HighUser.java b/hai-service/src/main/java/com/hai/entity/HighUser.java index 652345c4..6a8c6916 100644 --- a/hai-service/src/main/java/com/hai/entity/HighUser.java +++ b/hai-service/src/main/java/com/hai/entity/HighUser.java @@ -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 { /** * 用户编号 */ @@ -271,4 +273,4 @@ public class HighUser implements Serializable { sb.append("]"); return sb.toString(); } -} \ No newline at end of file +} diff --git a/hai-service/src/main/java/com/hai/model/UserModel.java b/hai-service/src/main/java/com/hai/model/UserModel.java new file mode 100644 index 00000000..e24e3aea --- /dev/null +++ b/hai-service/src/main/java/com/hai/model/UserModel.java @@ -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; + } +} diff --git a/hai-service/src/main/java/com/hai/service/HighMerchantStoreService.java b/hai-service/src/main/java/com/hai/service/HighMerchantStoreService.java index 5d542d25..ae0a1afa 100644 --- a/hai-service/src/main/java/com/hai/service/HighMerchantStoreService.java +++ b/hai-service/src/main/java/com/hai/service/HighMerchantStoreService.java @@ -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 getMerchantStoreList(Map map); + /** + * @Author 胡锐 + * @Description 查询门店列表(计算精度) + * @Date 2021/4/1 20:34 + **/ + List getStoreListByLongitude(Map map); + } diff --git a/hai-service/src/main/java/com/hai/service/HighUserCouponService.java b/hai-service/src/main/java/com/hai/service/HighUserCouponService.java index 31605772..875cce2c 100644 --- a/hai-service/src/main/java/com/hai/service/HighUserCouponService.java +++ b/hai-service/src/main/java/com/hai/service/HighUserCouponService.java @@ -55,4 +55,7 @@ public interface HighUserCouponService { **/ HighUserCoupon getUserCoupon(Long userId, Long couponId); + + List getCouponList(Long userId,Integer status); + } diff --git a/hai-service/src/main/java/com/hai/service/impl/HighMerchantStoreServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighMerchantStoreServiceImpl.java index fcfca6ed..e5f66a64 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighMerchantStoreServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighMerchantStoreServiceImpl.java @@ -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 getStoreListByLongitude(Map map) { + List 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 collect = storeList.stream().sorted(Comparator.comparing(HighMerchantStore::getDistance)).collect(Collectors.toList()); + return collect; + } + + } diff --git a/hai-service/src/main/java/com/hai/service/impl/HighUserCouponServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighUserCouponServiceImpl.java index a93781dc..76bc660e 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighUserCouponServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighUserCouponServiceImpl.java @@ -92,4 +92,15 @@ public class HighUserCouponServiceImpl implements HighUserCouponService { } return null; } + + @Override + public List 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); + } }