|
|
@@ -3,35 +3,39 @@ package com.izouma.dingdong.service.merchant;
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.izouma.dingdong.config.Constants;
|
|
|
import com.izouma.dingdong.domain.User;
|
|
|
import com.izouma.dingdong.domain.Verified;
|
|
|
import com.izouma.dingdong.domain.merchant.Merchant;
|
|
|
import com.izouma.dingdong.domain.merchant.MerchantClassification;
|
|
|
import com.izouma.dingdong.domain.merchant.MerchantSettings;
|
|
|
+import com.izouma.dingdong.domain.user.UserCoupon;
|
|
|
import com.izouma.dingdong.dto.MerchantDTO;
|
|
|
import com.izouma.dingdong.dto.PageQuery;
|
|
|
import com.izouma.dingdong.enums.ApplyStatus;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
+import com.izouma.dingdong.repo.CouponRepo;
|
|
|
import com.izouma.dingdong.repo.UserRepo;
|
|
|
import com.izouma.dingdong.repo.VerifiedRepo;
|
|
|
+import com.izouma.dingdong.repo.merchant.FullReductionRepo;
|
|
|
import com.izouma.dingdong.repo.merchant.MerchantClassificationRepo;
|
|
|
import com.izouma.dingdong.repo.merchant.MerchantRepo;
|
|
|
import com.izouma.dingdong.repo.merchant.MerchantSettingsRepo;
|
|
|
+import com.izouma.dingdong.repo.user.UserCouponRepo;
|
|
|
+import com.izouma.dingdong.utils.MapUtils;
|
|
|
import com.izouma.dingdong.utils.ObjUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
-import org.springframework.data.domain.Pageable;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import sun.security.krb5.internal.PAData;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Set;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static com.izouma.dingdong.web.BaseController.*;
|
|
|
|
|
|
@@ -45,6 +49,10 @@ public class MerchantService {
|
|
|
private MerchantClassificationRepo merchantClassificationRepo;
|
|
|
private VerifiedRepo verifiedRepo;
|
|
|
|
|
|
+ private FullReductionRepo fullReductionRepo;
|
|
|
+ private CouponRepo couponRepo;
|
|
|
+ private UserCouponRepo userCouponRepo;
|
|
|
+
|
|
|
/*
|
|
|
商户注册申请
|
|
|
*/
|
|
|
@@ -201,55 +209,107 @@ public class MerchantService {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 显示所有商家信息
|
|
|
+ * 管理后台显示所有商家信息
|
|
|
+ * 按时间排序
|
|
|
*
|
|
|
- * @param pageable 分页
|
|
|
+ * @param pageQuery 分页
|
|
|
* @return 分页列表
|
|
|
*/
|
|
|
- public Page<MerchantDTO> showAll(Pageable pageable) {
|
|
|
-
|
|
|
- //所有商家要按距离排序
|
|
|
+ public Page<MerchantDTO> backAll(PageQuery pageQuery) {
|
|
|
|
|
|
List<MerchantDTO> merchantDTOS = CollUtil.newArrayList();
|
|
|
- List<Merchant> merchants = merchantRepo.findAll();
|
|
|
+ Page<Merchant> merchants = merchantRepo.findAll(toSpecification(pageQuery, Merchant.class), toPageRequest(pageQuery));
|
|
|
for (Merchant merchant : merchants) {
|
|
|
MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(merchant.getId()).orElseThrow(new BusinessException("商户不存在"));
|
|
|
|
|
|
merchantDTOS.add(new MerchantDTO(merchant, merchantSettings));
|
|
|
}
|
|
|
- return new PageImpl<>(merchantDTOS, pageable, merchantDTOS.size());
|
|
|
+ return new PageImpl<>(merchantDTOS, toPageRequest(pageQuery), merchantDTOS.size());
|
|
|
}
|
|
|
|
|
|
- public Page<MerchantDTO> showAll(PageQuery pageQuery, Double longitude, Double latitude) {
|
|
|
+ /**
|
|
|
+ * 用户端显示的所有商家
|
|
|
+ *
|
|
|
+ * @param pageQuery 分页
|
|
|
+ * @param longitude 经度
|
|
|
+ * @param latitude 纬度
|
|
|
+ * @param popularTag 热门标签
|
|
|
+ * @param userId 用户Id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public List<MerchantDTO> showAll(PageQuery pageQuery, Double longitude, Double latitude, String popularTag, Long userId) {
|
|
|
+
|
|
|
+ //Specification<Merchant> specification = toSpecification(pageQuery, Merchant.class);
|
|
|
|
|
|
- //所有商家要按距离排序
|
|
|
|
|
|
- Set<MerchantDTO> merchantDTOS = CollUtil.newHashSet();
|
|
|
List<Merchant> merchants = merchantRepo.findAll(toSpecification(pageQuery, Merchant.class));
|
|
|
|
|
|
-
|
|
|
+ //所有商家要按距离排序规则
|
|
|
+ Map<Merchant, Double> map = new HashMap<>();
|
|
|
+ //算距离
|
|
|
for (Merchant m : merchants) {
|
|
|
- MerchantSettings merchantSettings = merchantSettingsRepo.findByMerchantId(m.getId()).orElseThrow(new BusinessException("商户不存在"));
|
|
|
-
|
|
|
- double lat1 = m.getLatitude() * Math.PI / 180.0;
|
|
|
- double lat2 = latitude * Math.PI / 180.0;
|
|
|
-
|
|
|
- double a = lat1 - lat2;
|
|
|
- double b = m.getLongitude() * Math.PI / 180.0 - longitude * Math.PI / 180.0;
|
|
|
- double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
|
|
|
- + Math.cos(lat1)
|
|
|
- * Math.cos(lat2)
|
|
|
- * Math.pow(Math.sin(b / 2), 2)));
|
|
|
- s = s * 6378.137 * 1000;
|
|
|
- s = Math.round(s);
|
|
|
+ if (m.getLatitude() != null && m.getLongitude() != null) {
|
|
|
+ Double distance = MapUtils.distance(m.getLongitude(), m.getLatitude(), longitude, latitude);
|
|
|
+ map.put(m, distance);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ //排序
|
|
|
+ List<Map.Entry<Merchant, Double>> list = new ArrayList<>(map.entrySet());
|
|
|
+ list.sort(Comparator.comparing(Map.Entry<Merchant, Double>::getValue));
|
|
|
+ List<Merchant> mers = new ArrayList<>();
|
|
|
+ for (Map.Entry<Merchant, Double> m : list) {
|
|
|
+ mers.add(m.getKey());
|
|
|
+ }
|
|
|
|
|
|
- merchantDTOS.add(new MerchantDTO(m, merchantSettings));
|
|
|
+ if (popularTag == null) {
|
|
|
+ popularTag = pageQuery.getSearch();
|
|
|
}
|
|
|
|
|
|
- List<MerchantDTO> dtos = new ArrayList<>(merchantDTOS);
|
|
|
+ //转DTO
|
|
|
+ List<MerchantDTO> merchantDTOS = CollUtil.newArrayList();
|
|
|
+ for (Merchant m : mers) {
|
|
|
+ MerchantSettings settings = merchantSettingsRepo.findByMerchantId(m.getId()).orElseThrow(new BusinessException("商户不存在"));
|
|
|
+ if (StrUtil.isNotBlank(popularTag)) {
|
|
|
+ switch (popularTag) {
|
|
|
+ case "首单立减":
|
|
|
+ if ((settings.getFirstOrder() != null ? settings.getFirstOrder().compareTo(BigDecimal.ZERO) : 0) > 0) {
|
|
|
+ merchantDTOS.add(new MerchantDTO(m, settings));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "满减优惠":
|
|
|
+ if (CollUtil.isNotEmpty(fullReductionRepo.findAllByMerchantId(m.getId()))) {
|
|
|
+ merchantDTOS.add(new MerchantDTO(m, settings));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "折扣商家":
|
|
|
+ if (StrUtil.isNotEmpty(merchantClassificationRepo.findByMerchantIdAndType(m.getId(), 2).getGoodsIds())) {
|
|
|
+ merchantDTOS.add(new MerchantDTO(m, settings));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "下单返红包":
|
|
|
+ if (CollUtil.isNotEmpty(couponRepo.findAllByMerchantIdAndEnabledTrue(m.getId()))) {
|
|
|
+ merchantDTOS.add(new MerchantDTO(m, settings));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "可用红包":
|
|
|
+ List<UserCoupon> coupons = userCouponRepo.findAllByUserIdAndIsUsedFalse(userId);
|
|
|
+ List<UserCoupon> collect = coupons.stream().filter(c ->
|
|
|
+ !c.getCoupon().getEndDate().isBefore(LocalDate.now()) && (c.getCoupon().getMerchantId() == null || c.getCoupon().getMerchantId().equals(m.getId()))
|
|
|
+ ).collect(Collectors.toList());
|
|
|
+ if (CollUtil.isNotEmpty(collect)) {
|
|
|
+ merchantDTOS.add(new MerchantDTO(m, settings));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ merchantDTOS.add(new MerchantDTO(m, settings));
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return new PageImpl<>(dtos, toPageRequest(pageQuery), merchantDTOS.size());
|
|
|
+// new PageImpl<>(merchantDTOS, toPageRequest(pageQuery), merchantDTOS.size())
|
|
|
+ return merchantDTOS;
|
|
|
}
|
|
|
|
|
|
|