Pārlūkot izejas kodu

带距离的用户列表

licailing 5 gadi atpakaļ
vecāks
revīzija
425cacef47

+ 50 - 0
src/main/java/com/izouma/dingdong/dto/AddressDTO.java

@@ -0,0 +1,50 @@
+package com.izouma.dingdong.dto;
+
+import com.izouma.dingdong.enums.AddressTag;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+@ApiModel(value = "Address", description = "收获地址记录")
+public class AddressDTO {
+    @ApiModelProperty(value = "用户Id", name = "userId")
+    private Long userId;
+
+    @ApiModelProperty(value = "联系人", name = "name")
+    private String name;
+
+    @ApiModelProperty(value = "女士/先生", name = "sex")
+    private String sex;
+
+    @ApiModelProperty(value = "电话", name = "phone")
+    private String phone;
+
+    @ApiModelProperty(value = "详细地址", name = "addressName")
+    private String addressName;
+
+    @ApiModelProperty(value = "门牌号", name = "number")
+    private String number;
+
+    @ApiModelProperty(value = "标签", name = "addressTag")
+    private AddressTag addressTag;
+
+    @ApiModelProperty("是否选为默认地址")
+    private Boolean isDefault;
+
+    @ApiModelProperty(value = "经度", name = "longitude")
+    private Double longitude;
+
+    @ApiModelProperty(value = "纬度", name = "latitude")
+    private Double latitude;
+
+    @ApiModelProperty(value = "距离", name = "distance")
+    private Double distance;
+
+}

+ 5 - 1
src/main/java/com/izouma/dingdong/service/OrderInfoService.java

@@ -54,6 +54,7 @@ public class OrderInfoService {
     private OrderRefundApplyService orderRefundApplyService;
     private RiderRepo               riderRepo;
     private DeliveryFeeService      deliveryFeeService;
+    private SysConfigRepo           sysConfigRepo;
 
     /*
     用户下单
@@ -110,7 +111,10 @@ public class OrderInfoService {
         //判断与商家的距离
         Double distance = MapUtils.distance(address.getLongitude(), address.getLatitude(), merchant.getLongitude(), merchant
                 .getLatitude());
-        if (distance > 3000) {
+        //获取范围
+        String range = sysConfigRepo.findByName("range").orElseThrow(new BusinessException("无配置")).getValue();
+
+        if (distance > new Double(range)) {
             throw new BusinessException("超出配送距离");
         }
 

+ 3 - 0
src/main/java/com/izouma/dingdong/service/UserService.java

@@ -50,6 +50,9 @@ public class UserService {
     public User register(String phone, String password, Identity identity) {
         User user = userRepo.findByPhoneAndIdentity(phone, identity);
         if (ObjectUtil.isNull(user)) {
+            if (RIDER.equals(identity)){
+                throw new BusinessException("未注册");
+            }
             user = User.builder()
                     .username(phone)
                     .nickname(UUID.randomUUID().toString())

+ 13 - 10
src/main/java/com/izouma/dingdong/service/rider/RiderService.java

@@ -3,10 +3,7 @@ package com.izouma.dingdong.service.rider;
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.ObjectUtil;
-import com.izouma.dingdong.domain.MoneyRecord;
-import com.izouma.dingdong.domain.OrderInfo;
-import com.izouma.dingdong.domain.OrderRefundApply;
-import com.izouma.dingdong.domain.User;
+import com.izouma.dingdong.domain.*;
 import com.izouma.dingdong.domain.merchant.Merchant;
 import com.izouma.dingdong.domain.rider.Rider;
 import com.izouma.dingdong.domain.rider.RiderSign;
@@ -16,6 +13,7 @@ import com.izouma.dingdong.enums.*;
 import com.izouma.dingdong.exception.BusinessException;
 import com.izouma.dingdong.repo.MoneyRecordRepo;
 import com.izouma.dingdong.repo.OrderInfoRepo;
+import com.izouma.dingdong.repo.SysConfigRepo;
 import com.izouma.dingdong.repo.UserRepo;
 import com.izouma.dingdong.repo.rider.RiderRepo;
 import com.izouma.dingdong.repo.rider.RiderSignRepo;
@@ -40,6 +38,7 @@ public class RiderService {
     private OrderInfoRepo           orderInfoRepo;
     private OrderRefundApplyService orderRefundApplyService;
     private RiderSignRepo           riderSignRepo;
+    private SysConfigRepo           sysConfigRepo;
 
 
     /*
@@ -59,12 +58,12 @@ public class RiderService {
     /*
     骑手审核
      */
-    public void riderAudit(Long riderId, Boolean pass) {
+    public String riderAudit(Long riderId, Boolean pass, String reason) {
         Rider rider = riderRepo.findById(riderId).orElseThrow(new BusinessException("无骑手"));
         if (pass) {
             rider.setStatus(ApplyStatus.PASS);
-            boolean i = true;
-            while (i) {
+            //boolean i = true;
+            while (true) {
                 //随机生成骑手工号 一个首字母+四个数字
                 String jobNumber1 = RandomStringUtils.randomAlphabetic(1);
                 String jobNumber2 = RandomStringUtils.randomNumeric(4);
@@ -72,13 +71,16 @@ public class RiderService {
                 Rider byJobNumber = riderRepo.findByJobNumber(jobNumber);
                 if (ObjectUtil.isNull(byJobNumber)) {
                     rider.setJobNumber(jobNumber);
-                    i = false;
+                    //i = false;
+                    riderRepo.save(rider);
+                    return jobNumber;
                 }
             }
         } else {
             rider.setStatus(ApplyStatus.DENY);
+            riderRepo.save(rider);
+            return reason;
         }
-        riderRepo.save(rider);
     }
 
 
@@ -88,7 +90,8 @@ public class RiderService {
     public void sign(Long riderId, String password, Double longitude, Double latitude) {
         Rider rider = riderRepo.findById(riderId).orElseThrow(new BusinessException("无骑手"));
         Double distance = MapUtils.distance(longitude, latitude, rider.getLongitude(), rider.getLatitude());
-        if (distance > 3000) {
+        String range = sysConfigRepo.findByName("range").orElseThrow(new BusinessException("无配置")).getValue();
+        if (distance > new Double(range)) {
             throw new BusinessException("不在工作区域");
         }
         User user = userRepo.findById(rider.getUserId()).orElseThrow(new BusinessException("无用户"));

+ 70 - 0
src/main/java/com/izouma/dingdong/service/user/AddressService.java

@@ -1,13 +1,83 @@
 package com.izouma.dingdong.service.user;
 
+import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.util.ObjectUtil;
+import com.izouma.dingdong.domain.merchant.Merchant;
+import com.izouma.dingdong.domain.user.Address;
+import com.izouma.dingdong.dto.AddressDTO;
+import com.izouma.dingdong.dto.MerchantDTO;
 import com.izouma.dingdong.repo.user.AddressRepo;
+import com.izouma.dingdong.utils.MapUtils;
 import lombok.AllArgsConstructor;
+import org.apache.poi.ss.formula.functions.T;
 import org.springframework.stereotype.Service;
 
+import java.util.*;
+
 @Service
 @AllArgsConstructor
 public class AddressService {
 
     private AddressRepo addressRepo;
 
+    /*
+    带距离的地址
+     */
+    public List<AddressDTO> sortAddress(Long userId, Double longitude, Double latitude) {
+        List<Address> addressList = addressRepo.findAllByUserIdAndEnabledTrue(userId);
+        Map<Address, Double> map = distanceSorting(new HashSet<>(addressList), latitude, longitude);
+        List<AddressDTO> dtos = new ArrayList<>();
+        for (Map.Entry<Address, Double> m : map.entrySet()) {
+            dtos.add(this.toDTO(m.getKey(), m.getValue()));
+        }
+        return dtos;
+    }
+
+    /*
+    地址排序
+     */
+    public Map<Address, Double> distanceSorting(Set<Address> addresses, Double longitude, Double latitude) {
+        //所有地址要按距离排序规则
+        Map<Address, Double> map = new HashMap<>();
+        //算距离
+        for (Address m : addresses) {
+            if (ObjectUtil.isNotNull(m.getLatitude()) && ObjectUtil.isNotNull(m.getLongitude())) {
+                Double distance = MapUtils.distance(m.getLongitude(), m.getLatitude(), longitude, latitude);
+                map.put(m, distance);
+            }
+        }
+
+        //排序
+        List<Map.Entry<Address, Double>> list = new ArrayList<>(map.entrySet());
+        list.sort(Comparator.comparing(Map.Entry<Address, Double>::getValue));
+
+        Map<Address, Double> result = new HashMap<>();
+        for (Map.Entry<Address, Double> m : list) {
+            result.put(m.getKey(), m.getValue());
+        }
+
+        return result;
+    }
+
+    public Map<T, Double> sortMap(Map<T, Double> map) {
+        //排序
+        List<Map.Entry<T, Double>> list = new ArrayList<>(map.entrySet());
+        list.sort(Comparator.comparing(Map.Entry<T, Double>::getValue));
+
+        Map<T, Double> result = new HashMap<>();
+        for (Map.Entry<T, Double> m : list) {
+            result.put(m.getKey(), m.getValue());
+        }
+        return result;
+    }
+
+    /*
+    转dto
+     */
+    public AddressDTO toDTO(Address address, Double distance) {
+        AddressDTO dto = new AddressDTO();
+        BeanUtil.copyProperties(address, dto);
+        dto.setDistance(distance);
+        return dto;
+    }
 }

+ 8 - 1
src/main/java/com/izouma/dingdong/web/OrderInfoController.java

@@ -9,6 +9,7 @@ import com.izouma.dingdong.enums.MerchantStatus;
 import com.izouma.dingdong.enums.OrderStatus;
 import com.izouma.dingdong.enums.RefundReason;
 import com.izouma.dingdong.repo.UserRepo;
+import com.izouma.dingdong.repo.rider.RiderRepo;
 import com.izouma.dingdong.service.OrderInfoService;
 import com.izouma.dingdong.dto.PageQuery;
 import com.izouma.dingdong.exception.BusinessException;
@@ -38,6 +39,7 @@ public class OrderInfoController extends BaseController {
     private OrderInfoRepo    orderInfoRepo;
     private UserRepo         userRepo;
     private MerchantService  merchantService;
+    private RiderRepo        riderRepo;
 
 
     //@PreAuthorize("hasRole('ADMIN')")
@@ -85,7 +87,7 @@ public class OrderInfoController extends BaseController {
     @GetMapping("/my")
     @ApiOperation("个人订单")
     public Page<OrderInfo> my(PageQuery pageQuery) {
-
+        //query.status 状态传入
         Map<String, Object> query = pageQuery.getQuery();
         Long id = SecurityUtils.getAuthenticatedUser().getId();
         User user = userRepo.findById(id).orElseThrow(new BusinessException("无用户"));
@@ -100,6 +102,10 @@ public class OrderInfoController extends BaseController {
                 Long merchantId = merchantService.findMerchantId(id);
                 query.put("merchantId", merchantId);
                 break;
+            case RIDER:
+                Long riderId = riderRepo.findByUserId(id).orElseThrow(new BusinessException("无骑手")).getId();
+                query.put("riderId", riderId);
+                break;
         }
         return orderInfoRepo.findAll(toSpecification(pageQuery, OrderInfo.class), toPageRequest(pageQuery));
 
@@ -162,5 +168,6 @@ public class OrderInfoController extends BaseController {
     public List<OrderInfo> riderReceived() {
         return orderInfoRepo.findAllByStatusAndMerchantStatus(OrderStatus.PAID, MerchantStatus.RECEIVED);
     }
+
 }
 

+ 8 - 2
src/main/java/com/izouma/dingdong/web/rider/RiderController.java

@@ -1,5 +1,6 @@
 package com.izouma.dingdong.web.rider;
 
+import com.izouma.dingdong.utils.SecurityUtils;
 import com.izouma.dingdong.web.BaseController;
 import com.izouma.dingdong.domain.rider.Rider;
 import com.izouma.dingdong.service.rider.RiderService;
@@ -23,7 +24,7 @@ import java.util.List;
 @AllArgsConstructor
 public class RiderController extends BaseController {
     private RiderService riderService;
-    private RiderRepo riderRepo;
+    private RiderRepo    riderRepo;
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
@@ -40,7 +41,7 @@ public class RiderController extends BaseController {
     //@PreAuthorize("hasRole('ADMIN')")
     @GetMapping("/all")
     public Page<Rider> all(PageQuery pageQuery) {
-        return riderRepo.findAll(toSpecification(pageQuery,Rider.class), toPageRequest(pageQuery));
+        return riderRepo.findAll(toSpecification(pageQuery, Rider.class), toPageRequest(pageQuery));
     }
 
     @GetMapping("/get/{id}")
@@ -59,5 +60,10 @@ public class RiderController extends BaseController {
         List<Rider> data = all(pageQuery).getContent();
         ExcelUtils.export(response, data);
     }
+
+    @GetMapping("/receiver")
+    public void riderReceiveOrder(Long orderId, Boolean pass) {
+        riderService.riderReceiveOrder(SecurityUtils.getAuthenticatedUser().getId(), orderId, pass);
+    }
 }
 

+ 11 - 3
src/main/java/com/izouma/dingdong/web/user/AddressController.java

@@ -1,5 +1,6 @@
 package com.izouma.dingdong.web.user;
 
+import com.izouma.dingdong.dto.AddressDTO;
 import com.izouma.dingdong.utils.SecurityUtils;
 import com.izouma.dingdong.web.BaseController;
 import com.izouma.dingdong.domain.user.Address;
@@ -25,7 +26,7 @@ import java.util.List;
 @AllArgsConstructor
 public class AddressController extends BaseController {
     private AddressService addressService;
-    private AddressRepo addressRepo;
+    private AddressRepo    addressRepo;
 
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
@@ -69,8 +70,15 @@ public class AddressController extends BaseController {
 
     @GetMapping("/my")
     @ApiOperation("用户地址列表")
-    public List<Address> my(){
-        return  addressRepo.findAllByUserIdAndEnabledTrue(SecurityUtils.getAuthenticatedUser().getId());
+    public List<Address> my() {
+        return addressRepo.findAllByUserIdAndEnabledTrue(SecurityUtils.getAuthenticatedUser().getId());
+    }
+
+    @GetMapping("/myDTO")
+    @ApiOperation("带地址的用户地址列表")
+    public List<AddressDTO> myDTO(Double longitude, Double latitude) {
+        Long id = SecurityUtils.getAuthenticatedUser().getId();
+        return addressService.sortAddress(id, longitude, latitude);
     }
 }