diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbMerchantCouponMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbMerchantCouponMapper.java index 96c03c3..76d881b 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbMerchantCouponMapper.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/dao/TbMerchantCouponMapper.java @@ -1,6 +1,8 @@ package com.chaozhanggui.system.cashierservice.dao; import com.chaozhanggui.system.cashierservice.entity.TbMerchantCoupon; +import com.chaozhanggui.system.cashierservice.entity.dto.HomeDto; +import com.chaozhanggui.system.cashierservice.entity.vo.CouAndShopVo; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.springframework.data.domain.Pageable; @@ -36,7 +38,9 @@ public interface TbMerchantCouponMapper { List queryAllByLimit(TbMerchantCoupon tbMerchantCoupon, @Param("pageable") Pageable pageable); - List queryAllByPage(@Param("page")Integer page, @Param("size")Integer size); + List queryAllByPage(@Param("pageable")Integer page, @Param("sizeable")Integer size, @Param("rightTopLng") Double rightTopLng, + @Param("rightTopLat")Double rightTopLat, @Param("leftBottomLng")Double leftBottomLng, @Param("leftBottomLat")Double leftBottomLat, + @Param("cities")String cities, @Param("order")String order,@Param("lng")String lng,@Param("lat")String lat); /** * 统计总行数 * diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMerchantCoupon.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMerchantCoupon.java index 8e922e5..c71db04 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMerchantCoupon.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbMerchantCoupon.java @@ -135,6 +135,7 @@ public class TbMerchantCoupon implements Serializable { private String merchantId; + public Integer getId() { return id; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/HomeDto.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/HomeDto.java index 74f70e2..a0a4a63 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/HomeDto.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/dto/HomeDto.java @@ -12,12 +12,20 @@ public class HomeDto { */ private String address; /** - * 品类 + * 经度 + */ + private String lat; + /** + * 纬度 + */ + private String lng; + /** + * 品类 1.附近1Km */ private String type; /** - * 1.理我最近 2.销量优先 3.价格优先 + * 1.离我最近 2.销量优先 3.价格优先 */ private Integer orderBy; @@ -26,52 +34,4 @@ public class HomeDto { private Integer page; private Integer size; - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Integer getOrderBy() { - return orderBy; - } - - public void setOrderBy(Integer orderBy) { - this.orderBy = orderBy; - } - - public Integer getOther() { - return other; - } - - public void setOther(Integer other) { - this.other = other; - } - - public Integer getPage() { - return page; - } - - public void setPage(Integer page) { - this.page = page; - } - - public Integer getSize() { - return size; - } - - public void setSize(Integer size) { - this.size = size; - } } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/CouAndShopVo.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/CouAndShopVo.java new file mode 100644 index 0000000..1610d4f --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/CouAndShopVo.java @@ -0,0 +1,41 @@ +package com.chaozhanggui.system.cashierservice.entity.vo; + +import lombok.Data; + +/** + * @author lyf + */ +@Data +public class CouAndShopVo { + /** + * 自增 + */ + private Integer id; + /** + * 优惠券名称 + */ + private String title; + + private String shopId; + /** + * 优惠金额 + */ + private Double amount; + /** + * 发放数量 + */ + private Integer number; + /** + * 折扣 ,一位小数 + */ + private Float ratio; + + /** + * 关联商品Id + */ + private String relationIds; + + private String shopName; + + private String logo; +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/HomeVO.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/HomeVO.java index aa73d32..fb05d93 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/HomeVO.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/vo/HomeVO.java @@ -36,7 +36,7 @@ public class HomeVO { /** * 折扣 */ - private Float discount; + private BigDecimal discount; /** * 共省金额 */ @@ -164,11 +164,11 @@ public class HomeVO { this.salePrice = salePrice; } - public Float getDiscount() { + public BigDecimal getDiscount() { return discount; } - public void setDiscount(Float discount) { + public void setDiscount(BigDecimal discount) { this.discount = discount; } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/HomePageService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/HomePageService.java index 9f6c2c4..b4267bc 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/HomePageService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/HomePageService.java @@ -19,11 +19,9 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; +import java.math.BigInteger; import java.math.RoundingMode; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.stream.Collectors; @@ -59,13 +57,23 @@ public class HomePageService { } else { beginNo = (homeDto.getPage() - 1) * homeDto.getSize(); } - //优惠卷 - List tbMerchantCoupons = merchantCouponMapper.queryAllByPage(beginNo, homeDto.getSize()); + //经纬度(附近一km) + Map topAndBottomMap = new HashMap<>(); + List tbMerchantCoupons = new ArrayList<>(); + if (homeDto.getOther() != null && homeDto.getOther() == 1){ + topAndBottomMap = LocationUtils.returnLLSquarePoint(Double.parseDouble(homeDto.getLng()), Double.parseDouble(homeDto.getLat()), 1); + tbMerchantCoupons = merchantCouponMapper.queryAllByPage(beginNo, homeDto.getSize(), topAndBottomMap.get("rightTopPoint")[0],topAndBottomMap.get("rightTopPoint")[1], + topAndBottomMap.get("leftBottomPoint")[0],topAndBottomMap.get("leftBottomPoint")[1],homeDto.getAddress(),homeDto.getOrderBy().toString(),homeDto.getLng(),homeDto.getLat()); + }else { + tbMerchantCoupons = merchantCouponMapper.queryAllByPage(beginNo, homeDto.getSize(), null,null,null,null, + homeDto.getAddress(),homeDto.getOrderBy().toString(),homeDto.getLng(),homeDto.getLat()); + } + //统计shopId,以及productId List shopIds = new ArrayList<>(); List productIds = new ArrayList<>(); List productIdsInt = new ArrayList<>(); - for (TbMerchantCoupon coupon : tbMerchantCoupons) { + for (CouAndShopVo coupon : tbMerchantCoupons) { shopIds.add(coupon.getShopId()); productIds.add(coupon.getRelationIds()); productIdsInt.add(Integer.valueOf(coupon.getRelationIds())); @@ -82,10 +90,9 @@ public class HomePageService { //组装 List homeVOList = new ArrayList<>(); - for (TbMerchantCoupon o : tbMerchantCoupons) { + for (CouAndShopVo o : tbMerchantCoupons) { HomeVO homeVO = new HomeVO(); homeVO.setId(o.getId()); - homeVO.setDiscount(o.getRatio()); for (TbShopInfo tbShopInfo : shopInfo.get()) { if (o.getShopId().equals(tbShopInfo.getId().toString())) { homeVO.setShopName(tbShopInfo.getShopName()); @@ -104,18 +111,26 @@ public class HomePageService { } for (TbProduct tbProduct : product.get()) { if (o.getRelationIds().equals(tbProduct.getId().toString())) { - homeVO.setProductName(tbProduct.getName()); + homeVO.setProductName(o.getTitle()); homeVO.setImage(tbProduct.getCoverImg()); homeVO.setProductId(tbProduct.getId()); } } for (TbProductSku tbProductSku : productSku.get()) { if (o.getRelationIds().equals(tbProductSku.getProductId())) { - homeVO.setOriginPrice(tbProductSku.getSalePrice()); - homeVO.setRealSalesNumber(tbProductSku.getRealSalesNumber() == null ? BigDecimal.ZERO : new BigDecimal(tbProductSku.getRealSalesNumber())); - Float discount = homeVO.getDiscount(); - BigDecimal discountDecimal = new BigDecimal(discount); - homeVO.setSalePrice(tbProductSku.getSalePrice().multiply((discountDecimal.multiply(new BigDecimal("0.1"))))); + //原价 + if (tbProductSku.getSalePrice().compareTo(BigDecimal.ZERO)==0){ + homeVO.setOriginPrice(BigDecimal.ZERO); + homeVO.setDiscount(BigDecimal.ZERO); + }else { + homeVO.setOriginPrice(tbProductSku.getSalePrice()); + homeVO.setDiscount(new BigDecimal(o.getAmount()).divide(tbProductSku.getSalePrice(),2,RoundingMode.DOWN).multiply(BigDecimal.TEN)); + } + //销量 + homeVO.setRealSalesNumber(new BigDecimal(o.getNumber())); + //现价 + homeVO.setSalePrice(new BigDecimal(o.getAmount().toString())); + // 共省金额 homeVO.setSave(homeVO.getOriginPrice().subtract(homeVO.getSalePrice())); } } @@ -161,7 +176,7 @@ public class HomePageService { BigDecimal originPrice = o.getOriginPrice(); if (originPrice.compareTo(BigDecimal.ZERO) != 0) { BigDecimal multiply = o.getSalePrice().divide(o.getOriginPrice(), 2, RoundingMode.DOWN).multiply(BigDecimal.TEN); - o.setDiscount(multiply.floatValue()); + o.setDiscount(multiply); } else { o.setDiscount(null); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/util/LocationUtils.java b/src/main/java/com/chaozhanggui/system/cashierservice/util/LocationUtils.java index 791eb32..8066463 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/util/LocationUtils.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/util/LocationUtils.java @@ -43,12 +43,14 @@ public class LocationUtils { } /** - * @param longitude 经度 - * @param latitude 纬度 + * @param longitude 经度 108 + * @param latitude 纬度 34 * @param distanceInKm 范围(千米) + * 大于左下的经度lat,小于右上的经度lat + * 大于左下的纬度lng,小于右上的纬度lng * return Map stringMap - * 右顶点 double[] leftTopPoints = stringMap.get("rightTopPoint"); - * 左顶点 double[] leftBottomPoints = stringMap.get("leftBottomPoint"); + * 右上顶点 double[] leftTopPoints = stringMap.get("rightTopPoint"); + * 左下点 double[] leftBottomPoints = stringMap.get("leftBottomPoint"); */ public static Map returnLLSquarePoint(double longitude, double latitude, double distanceInKm) { Map squareMap = new HashMap<>(); @@ -67,31 +69,38 @@ public class LocationUtils { double[] leftBottomPoint = {latitude - dLatitude / 2, longitude - dLongitude / 2}; squareMap.put("rightTopPoint", rightTopPoint); squareMap.put("leftBottomPoint", leftBottomPoint); - // 打印结果 -// System.out.println("rightTop:" + rightTopPoint[0] + "======" + rightTopPoint[1]); -// System.out.println("leftBottom:" + leftBottomPoint[0] + "======" + leftBottomPoint[1]); + // 打印结果rightTopPoint[0]纬度lng,rightTopPoint[1]经度lat + System.out.println("rightTop:" + rightTopPoint[0] + "======" + rightTopPoint[1]); + System.out.println("leftBottom:" + leftBottomPoint[0] + "======" + leftBottomPoint[1]); return squareMap; } + /** * 将角度转化为弧度 */ public static double radians(double d) { return d * Math.PI / 180.0; } + //1KM + public static void main(String[] args) { + Map stringMap = returnLLSquarePoint(108.954398, 34.308687, 1); + double[] leftTopPoints = stringMap.get("rightTopPoint"); + double[] leftBottomPoints = stringMap.get("leftBottomPoint"); + double lat2 = 108.975418; + double lon2 = 34.280890; + double distance3 = getDistanceFrom2LngLat(109.019993, 34.342849, lat2, lon2); + System.out.println(distance3); + + // 计算距离 + double distance1 = getDistanceFrom2LngLat(leftTopPoints[1], leftTopPoints[0], lat2, lon2); + double distance2 = getDistanceFrom2LngLat(leftBottomPoints[1], leftBottomPoints[0], lat2, lon2); + System.out.println("Distance between the two points is: " + distance1 + " km"); + System.out.println("Distance between the two points is: " + distance2 + " km"); + } // public static void main(String[] args) { -// Map stringMap = returnLLSquarePoint(108.954398, 34.308687, 1); -// double[] leftTopPoints = stringMap.get("rightTopPoint"); -// double[] leftBottomPoints = stringMap.get("leftBottomPoint"); +// Map stringMap = returnLLSquarePoint(34.342849, 109.019993, 1); // -// double lat2 = 108.954398; -// double lon2 = 34.308687; -// -// // 计算距离 -// double distance1 = getDistanceFrom2LngLat(leftTopPoints[1], leftTopPoints[0], lat2, lon2); -// double distance2 = getDistanceFrom2LngLat(leftBottomPoints[1], leftBottomPoints[0], lat2, lon2); -// System.out.println("Distance between the two points is: " + distance1 + " km"); -// System.out.println("Distance between the two points is: " + distance2 + " km"); // } } diff --git a/src/main/resources/mapper/TbMerchantCouponMapper.xml b/src/main/resources/mapper/TbMerchantCouponMapper.xml index 5dbcdc5..85c3d5d 100644 --- a/src/main/resources/mapper/TbMerchantCouponMapper.xml +++ b/src/main/resources/mapper/TbMerchantCouponMapper.xml @@ -268,13 +268,45 @@ - + SELECT + cou.id as id, + cou.title as title, + cou.shop_id as shopId, + cou.amount as amount, + cou.number as number, + cou.ratio as ratio, + cou.relation_ids as relationIds, + info.shop_name as shopName, + info.logo + FROM + tb_merchant_coupon as cou + LEFT JOIN tb_shop_info as info ON cou.shop_id = info.id + + cou.class_type = 'product' + AND cou.`status` = 1 + AND info.cities =#{cities} + + AND info.lng BETWEEN #{leftBottomLng} AND #{rightTopLng} + AND info.lat BETWEEN #{leftBottomLat} AND #{rightTopLat} + + + + + + ORDER BY (ABS(info.lat - #{lat}) + ABS(info.lng - #{lng})) ASC + + + ORDER BY cou.number ASC + + + ORDER BY cou.amount ASC + + + ORDER BY cou.id ASC + + + limit #{pageable}, #{sizeable}