|
@@ -5,19 +5,16 @@ import com.izouma.ticketExchange.domain.CouponInfo;
|
|
|
import com.izouma.ticketExchange.exception.BusinessException;
|
|
import com.izouma.ticketExchange.exception.BusinessException;
|
|
|
import com.izouma.ticketExchange.repo.CinemaRepo;
|
|
import com.izouma.ticketExchange.repo.CinemaRepo;
|
|
|
import com.izouma.ticketExchange.repo.CouponInfoRepo;
|
|
import com.izouma.ticketExchange.repo.CouponInfoRepo;
|
|
|
|
|
+import com.izouma.ticketExchange.utils.MapUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.hibernate.query.criteria.internal.OrderImpl;
|
|
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Page;
|
|
|
import org.springframework.data.domain.PageImpl;
|
|
import org.springframework.data.domain.PageImpl;
|
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.data.domain.Pageable;
|
|
|
-import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import javax.persistence.criteria.*;
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
@Service
|
|
@Service
|
|
|
@AllArgsConstructor
|
|
@AllArgsConstructor
|
|
@@ -25,24 +22,35 @@ public class CinemaService {
|
|
|
private CinemaRepo cinemaRepo;
|
|
private CinemaRepo cinemaRepo;
|
|
|
private CouponInfoRepo couponInfoRepo;
|
|
private CouponInfoRepo couponInfoRepo;
|
|
|
|
|
|
|
|
- public Page<Cinema> getByCity(Long cityId, Long couponId, String region, Double lat, Double lng, Pageable pageable) {
|
|
|
|
|
|
|
+ public Page<Cinema> getByCity(Long cityId, Long couponId, String regionName, Double lat, Double lng, Pageable pageable) {
|
|
|
CouponInfo couponInfo = couponInfoRepo.findById(couponId).orElseThrow(new BusinessException("无记录"));
|
|
CouponInfo couponInfo = couponInfoRepo.findById(couponId).orElseThrow(new BusinessException("无记录"));
|
|
|
|
|
+ List<Cinema> list;
|
|
|
|
|
+ long total;
|
|
|
if (couponInfo.getCinemas() != null && !couponInfo.getCinemas().isEmpty()) {
|
|
if (couponInfo.getCinemas() != null && !couponInfo.getCinemas().isEmpty()) {
|
|
|
- return new PageImpl<>(couponInfo.getCinemas(), pageable, couponInfo.getCinemas().size());
|
|
|
|
|
|
|
+ list = couponInfo.getCinemas();
|
|
|
|
|
+ total = couponInfo.getCinemas().size();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ list = cinemaRepo.findByCityIdOrderByDistance(cityId, lat, lng, regionName,
|
|
|
|
|
+ pageable.getPageNumber() * pageable.getPageSize(), pageable.getPageSize());
|
|
|
|
|
+ total = cinemaRepo.count(cityId, regionName);
|
|
|
}
|
|
}
|
|
|
- cinemaRepo.findAll(new Specification<Cinema>() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public Predicate toPredicate(Root<Cinema> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
|
|
|
|
|
- List<Predicate> and = new ArrayList<>();
|
|
|
|
|
- if (StringUtils.isNotBlank(region)) {
|
|
|
|
|
- and.add(criteriaBuilder.equal(root.get("regionName"), region));
|
|
|
|
|
|
|
+ if (lat != null && lng != null) {
|
|
|
|
|
+ for (Cinema cinema : list) {
|
|
|
|
|
+ double d = MapUtils.GetDistance(cinema.getLat(), cinema.getLng(), lat, lng);
|
|
|
|
|
+ String distance;
|
|
|
|
|
+ if (d < 1000) {
|
|
|
|
|
+ distance = String.format("%.1f米", d);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ distance = String.format("%.1f千米", d / 1000);
|
|
|
}
|
|
}
|
|
|
- if (lat != null && lng != null) {
|
|
|
|
|
- criteriaQuery.orderBy(new OrderImpl(root.get("lat")));
|
|
|
|
|
- }
|
|
|
|
|
- return criteriaBuilder.and(and.toArray(new Predicate[0]));
|
|
|
|
|
|
|
+ distance = distance.replaceAll("\\.0(米|千米)$", "$1");
|
|
|
|
|
+ cinema.setDistance(distance);
|
|
|
}
|
|
}
|
|
|
- }, pageable);
|
|
|
|
|
- return cinemaRepo.findByCityId(cityId, pageable);
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ return new PageImpl<>(list, pageable, total);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public String[] getRegions(Long cityId) {
|
|
|
|
|
+ return cinemaRepo.findRegionsByCityId(cityId);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|