|
@@ -1,6 +1,8 @@
|
|
|
package com.izouma.dingdong.service.merchant;
|
|
package com.izouma.dingdong.service.merchant;
|
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil;
|
|
import cn.hutool.core.collection.CollUtil;
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.izouma.dingdong.domain.MoneyRecord;
|
|
import com.izouma.dingdong.domain.MoneyRecord;
|
|
|
import com.izouma.dingdong.domain.OrderInfo;
|
|
import com.izouma.dingdong.domain.OrderInfo;
|
|
|
import com.izouma.dingdong.domain.User;
|
|
import com.izouma.dingdong.domain.User;
|
|
@@ -11,24 +13,25 @@ import com.izouma.dingdong.domain.backstage.TimeTag;
|
|
|
import com.izouma.dingdong.domain.merchant.Goods;
|
|
import com.izouma.dingdong.domain.merchant.Goods;
|
|
|
import com.izouma.dingdong.domain.merchant.Merchant;
|
|
import com.izouma.dingdong.domain.merchant.Merchant;
|
|
|
import com.izouma.dingdong.domain.merchant.MerchantSettings;
|
|
import com.izouma.dingdong.domain.merchant.MerchantSettings;
|
|
|
|
|
+import com.izouma.dingdong.domain.user.UserCoupon;
|
|
|
|
|
+import com.izouma.dingdong.dto.FilterDTO;
|
|
|
import com.izouma.dingdong.dto.MerchantDTO;
|
|
import com.izouma.dingdong.dto.MerchantDTO;
|
|
|
|
|
+import com.izouma.dingdong.dto.PageQuery;
|
|
|
|
|
+import com.izouma.dingdong.enums.ApplyStatus;
|
|
|
import com.izouma.dingdong.enums.FinancialType;
|
|
import com.izouma.dingdong.enums.FinancialType;
|
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
import com.izouma.dingdong.exception.BusinessException;
|
|
|
-import com.izouma.dingdong.repo.AppraisalRepo;
|
|
|
|
|
-import com.izouma.dingdong.repo.MoneyRecordRepo;
|
|
|
|
|
-import com.izouma.dingdong.repo.OrderInfoRepo;
|
|
|
|
|
-import com.izouma.dingdong.repo.UserRepo;
|
|
|
|
|
|
|
+import com.izouma.dingdong.repo.*;
|
|
|
import com.izouma.dingdong.repo.backstage.BannerRepo;
|
|
import com.izouma.dingdong.repo.backstage.BannerRepo;
|
|
|
import com.izouma.dingdong.repo.backstage.CategoryRepo;
|
|
import com.izouma.dingdong.repo.backstage.CategoryRepo;
|
|
|
import com.izouma.dingdong.repo.backstage.PromoteRepo;
|
|
import com.izouma.dingdong.repo.backstage.PromoteRepo;
|
|
|
import com.izouma.dingdong.repo.backstage.TimeTagRepo;
|
|
import com.izouma.dingdong.repo.backstage.TimeTagRepo;
|
|
|
import com.izouma.dingdong.repo.merchant.*;
|
|
import com.izouma.dingdong.repo.merchant.*;
|
|
|
|
|
+import com.izouma.dingdong.repo.user.UserCouponRepo;
|
|
|
import com.izouma.dingdong.service.backstage.CategoryService;
|
|
import com.izouma.dingdong.service.backstage.CategoryService;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
-import org.springframework.data.domain.Sort;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
|
|
|
|
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
@@ -55,6 +58,9 @@ public class MerchantSettingsService {
|
|
|
private GoodsSpecificationRepo goodsSpecificationRepo;
|
|
private GoodsSpecificationRepo goodsSpecificationRepo;
|
|
|
private MerchantClassificationRepo merchantClassificationRepo;
|
|
private MerchantClassificationRepo merchantClassificationRepo;
|
|
|
private FullReductionRepo fullReductionRepo;
|
|
private FullReductionRepo fullReductionRepo;
|
|
|
|
|
+ private MerchantService merchantService;
|
|
|
|
|
+ private CouponRepo couponRepo;
|
|
|
|
|
+ private UserCouponRepo userCouponRepo;
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
显示所有订单
|
|
显示所有订单
|
|
@@ -184,8 +190,8 @@ public class MerchantSettingsService {
|
|
|
/*
|
|
/*
|
|
|
人均消费 月订单
|
|
人均消费 月订单
|
|
|
*/
|
|
*/
|
|
|
- public BigDecimal consumption(Merchant merchant) {
|
|
|
|
|
- List<OrderInfo> orders = orderInfoRepo.findAllByMerchantId(merchant.getId());
|
|
|
|
|
|
|
+ public BigDecimal consumption(Long merchantId) {
|
|
|
|
|
+ List<OrderInfo> orders = orderInfoRepo.findAllByMerchantId(merchantId);
|
|
|
List<BigDecimal> collect = orders.stream().map(o ->
|
|
List<BigDecimal> collect = orders.stream().map(o ->
|
|
|
o.getRealAmount().subtract(o.getDeliveryAmount())
|
|
o.getRealAmount().subtract(o.getDeliveryAmount())
|
|
|
).collect(Collectors.toList());
|
|
).collect(Collectors.toList());
|
|
@@ -226,12 +232,12 @@ public class MerchantSettingsService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//商家推广语
|
|
//商家推广语
|
|
|
|
|
+ //每月一更
|
|
|
public void merPromo() {
|
|
public void merPromo() {
|
|
|
List<Category> byParent = categoryRepo.findAllByParent(1L);
|
|
List<Category> byParent = categoryRepo.findAllByParent(1L);
|
|
|
byParent.forEach(this::accept);
|
|
byParent.forEach(this::accept);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
private void accept(Category c) {
|
|
private void accept(Category c) {
|
|
|
List<MerchantDTO> list = categoryService.categoryMer(c.getId());
|
|
List<MerchantDTO> list = categoryService.categoryMer(c.getId());
|
|
|
List<Merchant> merchants = new ArrayList<>();
|
|
List<Merchant> merchants = new ArrayList<>();
|
|
@@ -269,4 +275,231 @@ public class MerchantSettingsService {
|
|
|
dto.setFullReductions(fullReductionRepo.findAllByMerchantId(id));
|
|
dto.setFullReductions(fullReductionRepo.findAllByMerchantId(id));
|
|
|
return dto;
|
|
return dto;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ //好评优先 1
|
|
|
|
|
+ //起送费最低 2
|
|
|
|
|
+ //配送最快 3
|
|
|
|
|
+ //配送费最低 4
|
|
|
|
|
+ //人均最低 5
|
|
|
|
|
+ //人均最高 6
|
|
|
|
|
+ //距离 7
|
|
|
|
|
+ //销量 8
|
|
|
|
|
+ */
|
|
|
|
|
+ //排序
|
|
|
|
|
+ public List<MerchantDTO> listSort(Double longitude, Double latitude, Integer sort) {
|
|
|
|
|
+ //处于营业内
|
|
|
|
|
+ LocalTime now = LocalTime.now();
|
|
|
|
|
+ List<MerchantSettings> all = merchantSettingsRepo.findAll();
|
|
|
|
|
+
|
|
|
|
|
+ List<MerchantSettings> collect = all.stream()
|
|
|
|
|
+ .filter(a -> a.getStartTime().isBefore(now) && a.getEndTime().isAfter(now) && a.getPriority() >= 2)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ Set<Merchant> merchantList = new HashSet<>();
|
|
|
|
|
+
|
|
|
|
|
+ collect.forEach(c ->
|
|
|
|
|
+ merchantRepo.findById(c.getMerchantId()).ifPresent(merchantList::add)
|
|
|
|
|
+ );
|
|
|
|
|
+ //带距离 且按距离排好序
|
|
|
|
|
+ List<MerchantDTO> dtos = merchantService.withDistance(merchantList, longitude, latitude, null);
|
|
|
|
|
+
|
|
|
|
|
+ if (ObjectUtil.isNotNull(sort)) {
|
|
|
|
|
+ switch (sort) {
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ //好评
|
|
|
|
|
+ dtos.sort((a, b) -> {
|
|
|
|
|
+ Integer sumA = a.getGoodNum() - a.getBadNum() * 10;
|
|
|
|
|
+ Integer sumB = b.getGoodNum() - b.getBadNum() * 10;
|
|
|
|
|
+ /*if (i == 0) {
|
|
|
|
|
+ return a.getDistance().compareTo(b.getDistance());
|
|
|
|
|
+ }*/
|
|
|
|
|
+ return sumA.compareTo(sumB);
|
|
|
|
|
+ });
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ //起送价
|
|
|
|
|
+ dtos.sort(Comparator.comparing(MerchantDTO::getStartingAmount));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ //平均配送时间 暂时 准备时间
|
|
|
|
|
+ dtos.sort(Comparator.comparing(MerchantDTO::getPreparationTime));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 4:
|
|
|
|
|
+ //配送费最低??
|
|
|
|
|
+ case 5:
|
|
|
|
|
+ //人均最低
|
|
|
|
|
+ dtos.sort((a, b) -> {
|
|
|
|
|
+ BigDecimal conA = this.consumption(a.getMid());
|
|
|
|
|
+ BigDecimal conB = this.consumption(b.getMid());
|
|
|
|
|
+ return conA.compareTo(conB);
|
|
|
|
|
+ });
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 6:
|
|
|
|
|
+ //人均最高
|
|
|
|
|
+ dtos.sort((a, b) -> {
|
|
|
|
|
+ BigDecimal conA = this.consumption(a.getMid());
|
|
|
|
|
+ BigDecimal conB = this.consumption(b.getMid());
|
|
|
|
|
+
|
|
|
|
|
+ return conB.compareTo(conA);
|
|
|
|
|
+ });
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 7:
|
|
|
|
|
+ //距离
|
|
|
|
|
+ //dtos.sort(Comparator.comparing(MerchantDTO::getDistance));
|
|
|
|
|
+ case 8:
|
|
|
|
|
+ //月销
|
|
|
|
|
+ dtos.sort((a, b) -> b.getMonthSales().compareTo(a.getMonthSales()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return dtos;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<MerchantDTO> listFilter(List<MerchantDTO> dtos, Integer filter, Long userId) {
|
|
|
|
|
+ List<MerchantDTO> collect = new ArrayList<>();
|
|
|
|
|
+// if (filter != null) {
|
|
|
|
|
+ switch (filter) {
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ //首单立减
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> d.getFirstOrder() != null && d.getFirstOrder().compareTo(BigDecimal.ZERO) > 0)
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ //满减优惠
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> CollUtil.isNotEmpty(fullReductionRepo.findAllByMerchantId(d.getMid())))
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ //折扣商家
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> StrUtil.isNotEmpty(merchantClassificationRepo.findByMerchantIdAndType(d.getMid(), 2).getGoodsIds()))
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 4:
|
|
|
|
|
+ //下单返红包
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> CollUtil.isNotEmpty(couponRepo.findAllByMerchantIdAndEnabledTrue(d.getMid())))
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case 5:
|
|
|
|
|
+ //可用红包
|
|
|
|
|
+ List<UserCoupon> coupons = userCouponRepo.findAllByUserIdAndIsUsedFalse(userId);
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d ->
|
|
|
|
|
+ ObjectUtil.isNotEmpty(coupons.stream().filter(c ->
|
|
|
|
|
+ !c.getCoupon().getEndDate().isBefore(LocalDate.now())
|
|
|
|
|
+ && (c.getCoupon().getMerchantId() == null || c.getCoupon().getMerchantId().equals(d.getMid()))
|
|
|
|
|
+ ).collect(Collectors.toList()))
|
|
|
|
|
+ )
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+// }
|
|
|
|
|
+ return collect;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //其他筛选条件
|
|
|
|
|
+ public List<MerchantDTO> otherFilter(List<MerchantDTO> dtos, FilterDTO filterDto) {
|
|
|
|
|
+ List<MerchantDTO> collect = new ArrayList<>();
|
|
|
|
|
+ if (filterDto.getNatureId() != null) {
|
|
|
|
|
+ //商家性质筛选
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> d.getMerchantNatureId().equals(filterDto.getNatureId()))
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (filterDto.getStartAmount() != null && filterDto.getEndAmount() != null) {
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> this.consumption(d.getMid()).compareTo(filterDto.getStartAmount()) >= 0
|
|
|
|
|
+ && this.consumption(d.getMid()).compareTo(filterDto.getEndAmount()) <= 0
|
|
|
|
|
+ ).collect(Collectors.toList()));
|
|
|
|
|
+
|
|
|
|
|
+ } else if (filterDto.getStartAmount() != null) {
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> this.consumption(d.getMid()).compareTo(filterDto.getStartAmount()) >= 0
|
|
|
|
|
+ ).collect(Collectors.toList()));
|
|
|
|
|
+
|
|
|
|
|
+ } else if (filterDto.getEndAmount() != null) {
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> this.consumption(d.getMid()).compareTo(filterDto.getEndAmount()) <= 0
|
|
|
|
|
+ ).collect(Collectors.toList()));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ collect.addAll(dtos);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (filterDto.getTag() != null) {
|
|
|
|
|
+ switch (filterDto.getTag()) {
|
|
|
|
|
+ case 1:
|
|
|
|
|
+ //新商家
|
|
|
|
|
+ LocalDate date = LocalDate.now().minusMonths(1);
|
|
|
|
|
+ LocalTime time = LocalTime.parse("00:00:00");
|
|
|
|
|
+ LocalDateTime dateTime = LocalDateTime.of(date, time);
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(d -> d.getEstablishTime().isAfter(dateTime))
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ break;
|
|
|
|
|
+
|
|
|
|
|
+ case 2:
|
|
|
|
|
+ //明星商家
|
|
|
|
|
+
|
|
|
|
|
+ case 3:
|
|
|
|
|
+ //口碑联盟
|
|
|
|
|
+ collect.addAll(dtos.stream()
|
|
|
|
|
+ .filter(MerchantDTO::getBuyAlliance)
|
|
|
|
|
+ .collect(Collectors.toList()));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return collect;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<MerchantDTO> page(Double longitude, Double latitude, Integer sort, FilterDTO filterDto, Long userId) {
|
|
|
|
|
+ List<MerchantDTO> dtoList;
|
|
|
|
|
+ dtoList = this.listSort(longitude, latitude, sort);
|
|
|
|
|
+ if (filterDto.getFilter() != null) {
|
|
|
|
|
+ dtoList = this.listFilter(dtoList, filterDto.getFilter(), userId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return this.otherFilter(dtoList, filterDto);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public List<MerchantDTO> showAll(PageQuery pageQuery, Double longitude, Double latitude, Integer popularTag, Long userId) {
|
|
|
|
|
+
|
|
|
|
|
+ List<Merchant> merchantList = merchantRepo.findAll((root, criteriaQuery, criteriaBuilder) -> {
|
|
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
|
|
+ predicates.add(criteriaBuilder.equal(root.get("status"), ApplyStatus.PASS));
|
|
|
|
|
+ if (StrUtil.isNotBlank(pageQuery.getSearch())) {
|
|
|
|
|
+ predicates.add(criteriaBuilder.like(root.get("name"), "%" + pageQuery.getSearch() + "%"));
|
|
|
|
|
+ }
|
|
|
|
|
+ return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ Set<Merchant> merchants = new HashSet<>(merchantList);
|
|
|
|
|
+
|
|
|
|
|
+ if (StrUtil.isNotBlank(pageQuery.getSearch())) {
|
|
|
|
|
+ List<Goods> goods = goodsRepo.findAll((root, criteriaQuery, criteriaBuilder) ->
|
|
|
|
|
+ criteriaQuery.where(
|
|
|
|
|
+ criteriaBuilder.and(
|
|
|
|
|
+ criteriaBuilder.like(root.get("name"), "%" + pageQuery.getSearch() + "%")),
|
|
|
|
|
+ criteriaBuilder.equal(root.get("status"), ApplyStatus.PASS)).getRestriction()
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ goods.forEach(g -> {
|
|
|
|
|
+ Merchant merchant = merchantRepo.findById(g.getMerchantId()).orElse(null);
|
|
|
|
|
+ if (ObjectUtil.isNotNull(merchant) && ApplyStatus.PASS.equals(merchant.getStatus())) {
|
|
|
|
|
+ merchants.add(merchant);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //带距离 且按距离排好序
|
|
|
|
|
+ List<MerchantDTO> dtos = merchantService.withDistance(merchants, longitude, latitude, null);
|
|
|
|
|
+ if (popularTag != null) {
|
|
|
|
|
+ return this.listFilter(dtos, popularTag, userId);
|
|
|
|
|
+ }
|
|
|
|
|
+ return dtos;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|