xiongzhu 4 years ago
parent
commit
9cd718b112

+ 10 - 1
src/main/java/com/izouma/nineth/domain/Order.java

@@ -54,7 +54,7 @@ public class Order extends BaseEntity {
     private Long minterId;
     private Long minterId;
 
 
     @ApiModelProperty("铸造者头像")
     @ApiModelProperty("铸造者头像")
-    private Long minterAvatar;
+    private String minterAvatar;
 
 
     @ApiModelProperty("价格")
     @ApiModelProperty("价格")
     @Column(precision = 10, scale = 2)
     @Column(precision = 10, scale = 2)
@@ -88,4 +88,13 @@ public class Order extends BaseEntity {
 
 
     @ApiModelProperty("消耗gas")
     @ApiModelProperty("消耗gas")
     private int gas;
     private int gas;
+
+    @ApiModelProperty("收货人")
+    private String contactName;
+
+    @ApiModelProperty("收货电话")
+    private String contactPhone;
+
+    @ApiModelProperty("收货地址")
+    private String address;
 }
 }

+ 38 - 0
src/main/java/com/izouma/nineth/domain/UserAddress.java

@@ -0,0 +1,38 @@
+package com.izouma.nineth.domain;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import javax.persistence.Entity;
+
+@Data
+@Entity
+@AllArgsConstructor
+@NoArgsConstructor
+@Builder
+public class UserAddress extends BaseEntity {
+
+    private Long userId;
+
+    private Long provinceId;
+
+    private String provinceName;
+
+    private String cityId;
+
+    private String cityName;
+
+    private Long districtId;
+
+    private String districtName;
+
+    private String address;
+
+    private String name;
+
+    private String phone;
+
+    private boolean def;
+}

+ 3 - 0
src/main/java/com/izouma/nineth/repo/CollectionRepo.java

@@ -7,10 +7,13 @@ import org.springframework.data.jpa.repository.Modifying;
 import org.springframework.data.jpa.repository.Query;
 import org.springframework.data.jpa.repository.Query;
 
 
 import javax.transaction.Transactional;
 import javax.transaction.Transactional;
+import java.util.Optional;
 
 
 public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
 public interface CollectionRepo extends JpaRepository<Collection, Long>, JpaSpecificationExecutor<Collection> {
     @Query("update Collection t set t.del = true where t.id = ?1")
     @Query("update Collection t set t.del = true where t.id = ?1")
     @Modifying
     @Modifying
     @Transactional
     @Transactional
     void softDelete(Long id);
     void softDelete(Long id);
+
+    Optional<Collection> findByIdAndDelFalse(Long id);
 }
 }

+ 24 - 0
src/main/java/com/izouma/nineth/repo/UserAddressRepo.java

@@ -0,0 +1,24 @@
+package com.izouma.nineth.repo;
+
+import com.izouma.nineth.domain.UserAddress;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+
+import javax.transaction.Transactional;
+import java.util.List;
+
+public interface UserAddressRepo extends JpaRepository<UserAddress, Long>, JpaSpecificationExecutor<UserAddress> {
+    @Query("update UserAddress t set t.del = true where t.id = ?1")
+    @Modifying
+    @Transactional
+    void softDelete(Long id);
+
+    List<UserAddress> findAllByUserId(Long userId);
+
+    @Query("update UserAddress t set t.def = false where t.userId = ?1")
+    @Modifying
+    @Transactional
+    void unsetDefault(Long userId);
+}

+ 60 - 1
src/main/java/com/izouma/nineth/service/OrderService.java

@@ -1,20 +1,79 @@
 package com.izouma.nineth.service;
 package com.izouma.nineth.service;
 
 
+import com.izouma.nineth.domain.Collection;
 import com.izouma.nineth.domain.Order;
 import com.izouma.nineth.domain.Order;
+import com.izouma.nineth.domain.User;
+import com.izouma.nineth.domain.UserAddress;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.enums.OrderStatus;
+import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.repo.CollectionRepo;
 import com.izouma.nineth.repo.OrderRepo;
 import com.izouma.nineth.repo.OrderRepo;
+import com.izouma.nineth.repo.UserAddressRepo;
+import com.izouma.nineth.repo.UserRepo;
 import com.izouma.nineth.utils.JpaUtils;
 import com.izouma.nineth.utils.JpaUtils;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Page;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
+import javax.transaction.Transactional;
+import java.math.BigDecimal;
+import java.util.Optional;
+
 @Service
 @Service
 @AllArgsConstructor
 @AllArgsConstructor
 public class OrderService {
 public class OrderService {
 
 
-    private OrderRepo orderRepo;
+    private OrderRepo       orderRepo;
+    private CollectionRepo  collectionRepo;
+    private UserAddressRepo userAddressRepo;
+    private UserRepo        userRepo;
 
 
     public Page<Order> all(PageQuery pageQuery) {
     public Page<Order> all(PageQuery pageQuery) {
         return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
         return orderRepo.findAll(JpaUtils.toSpecification(pageQuery, Order.class), JpaUtils.toPageRequest(pageQuery));
     }
     }
+
+    @Transactional
+    public Order create(Long userId, Long collectionId, int qty, Long addressId) {
+        if (qty <= 0) throw new BusinessException("数量必须大于0");
+        User user = userRepo.findByIdAndDelFalse(userId).orElseThrow(new BusinessException("用户不存在"));
+        Collection collection = collectionRepo.findById(collectionId).orElseThrow(new BusinessException("藏品不存在"));
+
+        if (!collection.isOnShelf()) {
+            throw new BusinessException("藏品已下架");
+        }
+        if (qty > collection.getStock()) {
+            throw new BusinessException("库存不足");
+        }
+        UserAddress userAddress = null;
+        if (addressId != null) {
+            userAddress = userAddressRepo.findById(addressId).orElseThrow(new BusinessException("地址信息不存在"));
+        }
+
+        collection.setStock(collection.getStock() - qty);
+        collection.setSale(collection.getSale() + qty);
+        collectionRepo.save(collection);
+
+        User minter = userRepo.findById(collection.getMinterId()).orElse(null);
+        Order order = Order.builder()
+                .userId(userId)
+                .collectionId(collectionId)
+                .name(collection.getName())
+                .pic(collection.getPics())
+                .minter(Optional.ofNullable(minter).map(User::getNickname).orElse(collection.getMinter()))
+                .minterAvatar(Optional.ofNullable(minter).map(User::getAvatar).orElse(collection.getMinterAvatar()))
+                .qty(qty)
+                .price(collection.getPrice())
+                .gasPrice(BigDecimal.valueOf(1))
+                .totalPrice(collection.getPrice().multiply(BigDecimal.valueOf(qty)).add(BigDecimal.valueOf(1)))
+                .contactName(Optional.ofNullable(userAddress).map(UserAddress::getName).orElse(null))
+                .contactPhone(Optional.ofNullable(userAddress).map(UserAddress::getPhone).orElse(null))
+                .address(Optional.ofNullable(userAddress).map(u ->
+                                u.getProvinceId() + " " + u.getCityId() + " " + u.getDistrictId() + " " + u.getAddress())
+                        .orElse(null))
+                .status(OrderStatus.NOT_PAID)
+                .build();
+        return orderRepo.save(order);
+
+    }
 }
 }

+ 20 - 0
src/main/java/com/izouma/nineth/service/UserAddressService.java

@@ -0,0 +1,20 @@
+package com.izouma.nineth.service;
+
+import com.izouma.nineth.domain.UserAddress;
+import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.repo.UserAddressRepo;
+import com.izouma.nineth.utils.JpaUtils;
+import lombok.AllArgsConstructor;
+import org.springframework.data.domain.Page;
+import org.springframework.stereotype.Service;
+
+@Service
+@AllArgsConstructor
+public class UserAddressService {
+
+    private UserAddressRepo userAddressRepo;
+
+    public Page<UserAddress> all(PageQuery pageQuery) {
+        return userAddressRepo.findAll(JpaUtils.toSpecification(pageQuery, UserAddress.class), JpaUtils.toPageRequest(pageQuery));
+    }
+}

+ 3 - 5
src/main/java/com/izouma/nineth/service/UserService.java

@@ -30,12 +30,10 @@ import org.springframework.data.jpa.domain.Specification;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
 import java.text.SimpleDateFormat;
 import java.text.SimpleDateFormat;
-import java.util.*;
+import java.util.Collections;
+import java.util.Date;
+import java.util.UUID;
 
 
 @Service
 @Service
 @Slf4j
 @Slf4j

+ 8 - 1
src/main/java/com/izouma/nineth/web/OrderController.java

@@ -1,10 +1,12 @@
 package com.izouma.nineth.web;
 package com.izouma.nineth.web;
+
 import com.izouma.nineth.domain.Order;
 import com.izouma.nineth.domain.Order;
 import com.izouma.nineth.service.OrderService;
 import com.izouma.nineth.service.OrderService;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.dto.PageQuery;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.exception.BusinessException;
 import com.izouma.nineth.repo.OrderRepo;
 import com.izouma.nineth.repo.OrderRepo;
 import com.izouma.nineth.utils.ObjUtils;
 import com.izouma.nineth.utils.ObjUtils;
+import com.izouma.nineth.utils.SecurityUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import com.izouma.nineth.utils.excel.ExcelUtils;
 import lombok.AllArgsConstructor;
 import lombok.AllArgsConstructor;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Page;
@@ -20,7 +22,7 @@ import java.util.List;
 @AllArgsConstructor
 @AllArgsConstructor
 public class OrderController extends BaseController {
 public class OrderController extends BaseController {
     private OrderService orderService;
     private OrderService orderService;
-    private OrderRepo orderRepo;
+    private OrderRepo    orderRepo;
 
 
     //@PreAuthorize("hasRole('ADMIN')")
     //@PreAuthorize("hasRole('ADMIN')")
     @PostMapping("/save")
     @PostMapping("/save")
@@ -56,5 +58,10 @@ public class OrderController extends BaseController {
         List<Order> data = all(pageQuery).getContent();
         List<Order> data = all(pageQuery).getContent();
         ExcelUtils.export(response, data);
         ExcelUtils.export(response, data);
     }
     }
+
+    @PostMapping("/create")
+    public Order create(@RequestParam Long collectionId, @RequestParam int qty, @RequestParam(required = false) Long addressId) {
+        return orderService.create(SecurityUtils.getAuthenticatedUser().getId(), collectionId, qty, addressId);
+    }
 }
 }
 
 

+ 68 - 0
src/main/java/com/izouma/nineth/web/UserAddressController.java

@@ -0,0 +1,68 @@
+package com.izouma.nineth.web;
+
+import com.izouma.nineth.domain.UserAddress;
+import com.izouma.nineth.service.UserAddressService;
+import com.izouma.nineth.dto.PageQuery;
+import com.izouma.nineth.exception.BusinessException;
+import com.izouma.nineth.repo.UserAddressRepo;
+import com.izouma.nineth.utils.ObjUtils;
+import com.izouma.nineth.utils.SecurityUtils;
+import com.izouma.nineth.utils.excel.ExcelUtils;
+import lombok.AllArgsConstructor;
+import org.springframework.data.domain.Page;
+import org.springframework.security.access.prepost.PreAuthorize;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.List;
+
+@RestController
+@RequestMapping("/userAddress")
+@AllArgsConstructor
+public class UserAddressController extends BaseController {
+    private UserAddressService userAddressService;
+    private UserAddressRepo    userAddressRepo;
+
+    //@PreAuthorize("hasRole('ADMIN')")
+    @PostMapping("/save")
+    public UserAddress save(@RequestBody UserAddress record) {
+        if (record.getId() != null) {
+            UserAddress orig = userAddressRepo.findById(record.getId()).orElseThrow(new BusinessException("无记录"));
+            ObjUtils.merge(orig, record);
+            if (orig.isDef()) {
+                userAddressRepo.unsetDefault(SecurityUtils.getAuthenticatedUser().getId());
+            }
+            return userAddressRepo.save(orig);
+        }
+        if (record.isDef()) {
+            userAddressRepo.unsetDefault(SecurityUtils.getAuthenticatedUser().getId());
+        }
+        return userAddressRepo.save(record);
+    }
+
+
+    //@PreAuthorize("hasRole('ADMIN')")
+    @PostMapping("/all")
+    public Page<UserAddress> all(@RequestBody PageQuery pageQuery) {
+        return userAddressService.all(pageQuery);
+    }
+
+    @GetMapping("/get/{id}")
+    public UserAddress get(@PathVariable Long id) {
+        return userAddressRepo.findById(id).orElseThrow(new BusinessException("无记录"));
+    }
+
+    @PostMapping("/del/{id}")
+    public void del(@PathVariable Long id) {
+        userAddressRepo.softDelete(id);
+    }
+
+    @GetMapping("/excel")
+    @ResponseBody
+    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
+        List<UserAddress> data = all(pageQuery).getContent();
+        ExcelUtils.export(response, data);
+    }
+}
+

+ 1 - 0
src/main/resources/genjson/UserAddress.json

@@ -0,0 +1 @@
+{"tableName":"UserAddress","className":"UserAddress","remark":"地址","genTable":true,"genClass":true,"genList":false,"genForm":false,"genRouter":false,"javaPath":"/Users/drew/Projects/Java/9th/src/main/java/com/izouma/nineth","viewPath":"/Users/drew/Projects/Java/9th/src/main/vue/src/views","routerPath":"/Users/drew/Projects/Java/9th/src/main/vue/src","resourcesPath":"/Users/drew/Projects/Java/9th/src/main/resources","dataBaseType":"Mysql","fields":[{"name":"userId","modelName":"userId","remark":"userId","showInList":true,"showInForm":true,"formType":"number"},{"name":"provinceId","modelName":"provinceId","remark":"provinceId","showInList":true,"showInForm":true,"formType":"number"},{"name":"provinceName","modelName":"provinceName","remark":"provinceName","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"cityId","modelName":"cityId","remark":"cityId","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"cityName","modelName":"cityName","remark":"cityName","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"districtId","modelName":"districtId","remark":"districtId","showInList":true,"showInForm":true,"formType":"number"},{"name":"districtName","modelName":"districtName","remark":"districtName","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"address","modelName":"address","remark":"address","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"name","modelName":"name","remark":"name","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"phone","modelName":"phone","remark":"phone","showInList":true,"showInForm":true,"formType":"singleLineText"},{"name":"def","modelName":"def","remark":"def","showInList":true,"showInForm":true,"formType":"singleLineText"}],"readTable":false,"dataSourceCode":"dataSource","genJson":"","subtables":[],"update":false,"basePackage":"com.izouma.nineth","tablePackage":"com.izouma.nineth.domain.UserAddress"}