Drew 6 gadi atpakaļ
vecāks
revīzija
c9dd7a04ff

+ 4 - 2
src/main/java/com/izouma/ticketExchange/domain/Cinema.java

@@ -7,6 +7,7 @@ import lombok.Builder;
 import lombok.Data;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
 
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Id;
 import javax.persistence.Transient;
 import javax.persistence.Transient;
@@ -28,10 +29,10 @@ public class Cinema extends AuditedEntity {
     private Integer scheduleCloseTime;
     private Integer scheduleCloseTime;
 
 
     @ApiModelProperty(value = "纬度", name = "lat")
     @ApiModelProperty(value = "纬度", name = "lat")
-    private String lat;
+    private Double lat;
 
 
     @ApiModelProperty(value = "经度", name = "lng")
     @ApiModelProperty(value = "经度", name = "lng")
-    private String lng;
+    private Double lng;
 
 
     @ApiModelProperty(value = "区,县", name = "regionName")
     @ApiModelProperty(value = "区,县", name = "regionName")
     private String regionName;
     private String regionName;
@@ -45,6 +46,7 @@ public class Cinema extends AuditedEntity {
     @ApiModelProperty(value = "影院电话", name = "tel")
     @ApiModelProperty(value = "影院电话", name = "tel")
     private String tel;
     private String tel;
 
 
+    @Column(length = 20)
     @ApiModelProperty(value = "专资编码", name = "standardId")
     @ApiModelProperty(value = "专资编码", name = "standardId")
     private String standardId;
     private String standardId;
 
 

+ 11 - 14
src/main/java/com/izouma/ticketExchange/repo/CinemaRepo.java

@@ -12,20 +12,17 @@ import java.util.List;
 
 
 public interface CinemaRepo extends JpaRepository<Cinema, Long>, JpaSpecificationExecutor<Cinema> {
 public interface CinemaRepo extends JpaRepository<Cinema, Long>, JpaSpecificationExecutor<Cinema> {
 
 
-    Page<Cinema> findByCityId(Long cityId, Pageable pageable);
+    @Query(value = "select region_name from cinema where city_id = :cityId group by region_name", nativeQuery = true)
+    String[] findRegionsByCityId(Long cityId);
 
 
-    @Query(value = "select *, " +
-            "       (IF((ifnull(:lat, false) and ifnull(:lng, false)), ((lat - :lat) * (lat - :lat) + (lng - :lng) * (lng - :lng)), 0)) as d " +
-            "from cinema " +
-            "where city_id = :cityId " +
-            "  and IF(ifnull(:region, true), true, region_name = :region) " +
-            "order by d, id", nativeQuery = true)
-    Page<Cinema> findNearCinema(@Param("cityId") Long cityId, @Param("region") String region, @Param("lat") Double lat, @Param("lng") Double lng, Pageable pageable);
-
-    @Query(value = "select c, case when (:lat is null or :lng is null) then 0 " +
-            "                      else ((c.lat - :lat) * (c.lat - :lat) + (c.lng - :lng) * (c.lng - :lng)) end as d " +
-            "from Cinema c where c.cityId = :cityId and (case when (:region is null) then (c.regionName is not null) else (c.regionName = :region) end) " +
-            "order by d, c.id")
-    Page<Cinema> findNearCinema1(@Param("cityId") Long cityId, @Param("region") String region, @Param("lat") Double lat, @Param("lng") Double lng, Pageable pageable);
+    @Query(value = "select *, IF(?2 is null or ?3 is null, 0, (lat - ?2) * (lat - ?2) + (lng - ?3) * (lng - ?3)) as d " +
+            "from cinema where city_id = ?1 " +
+            "and IF(?4 is null, true, region_name = ?4) " +
+            "order by d, id limit ?5, ?6",
+            nativeQuery = true)
+    List<Cinema> findByCityIdOrderByDistance(Long cityId, Double lat, Double lng, String region, int offset, int page);
 
 
+    @Query(value = "select count(id) from cinema where city_id = ?1 and IF(?2 is null, true, region_name = ?2)",
+            nativeQuery = true)
+    long count(Long cityId, String regionName);
 }
 }

+ 27 - 19
src/main/java/com/izouma/ticketExchange/service/CinemaService.java

@@ -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);
     }
     }
 }
 }

+ 12 - 2
src/main/java/com/izouma/ticketExchange/service/TppService.java

@@ -131,8 +131,8 @@ public class TppService {
                                      .cinemaName(c.getCinemaName())
                                      .cinemaName(c.getCinemaName())
                                      .addr(c.getAddress())
                                      .addr(c.getAddress())
                                      .cityId(c.getCityId())
                                      .cityId(c.getCityId())
-                                     .lat(c.getLatitude())
-                                     .lng(c.getLongitude())
+                                     .lat(Double.parseDouble(c.getLatitude()))
+                                     .lng(Double.parseDouble(c.getLongitude()))
                                      .tel(c.getPhone())
                                      .tel(c.getPhone())
                                      .regionName(c.getRegionName())
                                      .regionName(c.getRegionName())
                                      .scheduleCloseTime(Integer.valueOf(c.getScheduleCloseTime().toString()))
                                      .scheduleCloseTime(Integer.valueOf(c.getScheduleCloseTime().toString()))
@@ -183,4 +183,14 @@ public class TppService {
         log.error("获取排片失败\n\t{}", rsp.getBody());
         log.error("获取排片失败\n\t{}", rsp.getBody());
         throw new BusinessException("获取排片失败");
         throw new BusinessException("获取排片失败");
     }
     }
+
+    public void getSeats(Long scheduleId) throws ApiException {
+        FilmDataThirdPartySeatMapRequest req = new FilmDataThirdPartySeatMapRequest();
+        req.setPlatform(platform);
+        req.setParamsString("{}");
+        req.setUserId(userId);
+        req.setScheduleId(scheduleId);
+        FilmDataThirdPartySeatMapResponse rsp = client.execute(req);
+        System.out.println(rsp.getBody());
+    }
 }
 }

+ 36 - 0
src/main/java/com/izouma/ticketExchange/utils/MapUtils.java

@@ -0,0 +1,36 @@
+package com.izouma.ticketExchange.utils;
+
+public class MapUtils {
+    //private static double EARTH_RADIUS = 6378.137;
+    private static double EARTH_RADIUS = 6371.393;
+
+    private static double rad(double d) {
+        return d * Math.PI / 180.0;
+    }
+
+    /**
+     * 计算两个经纬度之间的距离
+     *
+     * @param lat1
+     * @param lng1
+     * @param lat2
+     * @param lng2
+     * @return
+     */
+    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
+        double radLat1 = rad(lat1);
+        double radLat2 = rad(lat2);
+        double a = radLat1 - radLat2;
+        double b = rad(lng1) - rad(lng2);
+        double s = 2 * Math.asin(Math.sqrt(Math.abs(Math.pow(Math.sin(a / 2), 2) +
+                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))));
+        s = s * EARTH_RADIUS;
+        s = Math.round(s * 1000);
+        return s;
+    }
+
+
+    public static void main(String[] args) {
+        System.out.println(MapUtils.GetDistance(29.490295, 106.486654, 29.615467, 106.581515));
+    }
+}

+ 8 - 3
src/main/java/com/izouma/ticketExchange/web/CinemaController.java

@@ -7,7 +7,9 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.domain.Pageable;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.bind.annotation.*;
 
 
+import java.util.HashMap;
 import java.util.List;
 import java.util.List;
+import java.util.Map;
 
 
 @RestController
 @RestController
 @RequestMapping("/cinema")
 @RequestMapping("/cinema")
@@ -16,8 +18,11 @@ public class CinemaController {
     private CinemaService cinemaService;
     private CinemaService cinemaService;
 
 
     @GetMapping("city/{cityId}")
     @GetMapping("city/{cityId}")
-    public Page<Cinema> getByCity(@PathVariable Long cityId, Long couponId, String region,
-                                  Double lat, Double lng, Pageable pageable) {
-        return cinemaService.getByCity(cityId, couponId, region, lat, lng, pageable);
+    public Map getByCity(@PathVariable Long cityId, Long couponId, String regionName,
+                         Double lat, Double lng, Pageable pageable) {
+        Map<String, Object> map = new HashMap<>();
+        map.put("page", cinemaService.getByCity(cityId, couponId, regionName, lat, lng, pageable));
+        map.put("regions", cinemaService.getRegions(cityId));
+        return map;
     }
     }
 }
 }

+ 8 - 0
src/main/java/com/izouma/ticketExchange/web/ShowController.java

@@ -1,6 +1,8 @@
 package com.izouma.ticketExchange.web;
 package com.izouma.ticketExchange.web;
 
 
 import com.izouma.ticketExchange.domain.Show;
 import com.izouma.ticketExchange.domain.Show;
+import com.izouma.ticketExchange.exception.BusinessException;
+import com.izouma.ticketExchange.repo.ShowRepo;
 import com.izouma.ticketExchange.service.ShowService;
 import com.izouma.ticketExchange.service.ShowService;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -13,10 +15,16 @@ import java.util.List;
 @RequestMapping("/show")
 @RequestMapping("/show")
 @AllArgsConstructor
 @AllArgsConstructor
 public class ShowController {
 public class ShowController {
+    private ShowRepo    showRepo;
     private ShowService showService;
     private ShowService showService;
 
 
     @GetMapping("/city/{cityId}")
     @GetMapping("/city/{cityId}")
     public List<Show> getByCity(@PathVariable Long cityId, @RequestParam(required = false) Long couponId) {
     public List<Show> getByCity(@PathVariable Long cityId, @RequestParam(required = false) Long couponId) {
         return showService.getByCityIdAndCouponId(cityId, couponId);
         return showService.getByCityIdAndCouponId(cityId, couponId);
     }
     }
+
+    @GetMapping("/detail/{id}")
+    public Show detail(@PathVariable Long id) {
+        return showRepo.findById(id).orElseThrow(new BusinessException("无记录"));
+    }
 }
 }

+ 1 - 1
src/test/java/com/izouma/ticketExchange/CommonTest.java

@@ -128,6 +128,6 @@ public class CommonTest {
 
 
     @Test
     @Test
     public void testEnum() {
     public void testEnum() {
-
+        System.out.println("100.0米".replaceAll("\\.0(米|千米)$", "$1"));
     }
     }
 }
 }

+ 6 - 1
src/test/java/com/izouma/ticketExchange/repo/CinemaRepoTest.java

@@ -6,6 +6,7 @@ import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
 import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Sort;
 
 
 import java.util.List;
 import java.util.List;
 
 
@@ -24,7 +25,11 @@ public class CinemaRepoTest extends ApplicationTests {
 
 
     @Test
     @Test
     public void findByCityId() {
     public void findByCityId() {
-        Page<Cinema> page = cinemaRepo.findNearCinema1(63L, null, null, null, PageRequest.of(0, 10));
+    }
+
+    @Test
+    public void findRegionByCityId() {
+        List<Cinema> page = cinemaRepo.findByCityIdOrderByDistance(63L, 31.949584, 118.792339, "江宁区", 0, 10);
         System.out.println(page);
         System.out.println(page);
     }
     }
 }
 }

+ 6 - 1
src/test/java/com/izouma/ticketExchange/service/TppServiceTest.java

@@ -31,6 +31,11 @@ public class TppServiceTest extends ApplicationTests {
 
 
     @Test
     @Test
     public void getSchedules() throws ApiException {
     public void getSchedules() throws ApiException {
-        tppService.getSchedules(45100L);
+        tppService.getSchedules(18957L);
+    }
+
+    @Test
+    public void getSeats() throws ApiException {
+        tppService.getSeats(729164896L);
     }
     }
 }
 }