Skip to content

Commit

Permalink
订单确认页面功能完善
Browse files Browse the repository at this point in the history
  • Loading branch information
yl940127 committed Mar 17, 2018
1 parent e9cc28c commit 5f9bc76
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@
@Setter
public class OrderDto {
private Long productId;

private Integer productNum;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package com.rhinoceros.mall.core.dto;

import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
public class OrderListDto {
private List<OrderDto> orders;
}
17 changes: 17 additions & 0 deletions mall-dao-impl/src/main/resources/mapper/OrderMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,21 @@
WHERE id = #{id}
</select>

<insert id="addAll" useGeneratedKeys="true" keyProperty="id">
INSERT INTO `order`
(status,identifier,create_at,total_price,address_id,user_id,product_id,product_num)
VALUES
<foreach collection="orders" item="order" separator=",">
(
#{order.status},
#{order.identifier},
#{order.createAt},
#{order.totalPrice},
#{order.addressId},
#{order.userId},
#{order.productId},
#{order.productNum}
)
</foreach>
</insert>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ public interface CartProductDao {

/**
* 更新购物车中不为null的字段
*
* @param cartProduct
* @return
*/
int updateSelectionById(CartProduct cartProduct);

/**
* 向购物车中添加商品
*
* @param cartProduct
* @return 修改条目数目
*/
Expand All @@ -48,7 +46,6 @@ public interface CartProductDao {

/**
* 根据id查询购物车,不存在返回null
*
* @param id
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ public interface OrderDao {
*/
Order findById(Long id);

/**
* 形成订单
* @param orders
* @return
*/
int addAll(@Param("orders") List<Order> orders);

}
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
package com.rhinoceros.mall.service.impl.service;
/* created at 8:11 PM 3/6/2018 */

import com.rhinoceros.mall.core.dto.OrderDto;
import com.rhinoceros.mall.core.dto.OrderListDto;
import com.rhinoceros.mall.core.enumeration.OrderStatus;
import com.rhinoceros.mall.core.po.Address;
import com.rhinoceros.mall.core.po.Order;
import com.rhinoceros.mall.core.po.Product;
import com.rhinoceros.mall.core.po.User;
import com.rhinoceros.mall.core.query.PageQuery;
import com.rhinoceros.mall.dao.dao.AddressDao;
import com.rhinoceros.mall.dao.dao.OrderDao;
import com.rhinoceros.mall.dao.dao.ProductDao;
import com.rhinoceros.mall.dao.dao.UserDao;
import com.rhinoceros.mall.service.impl.exception.common.EntityNotExistException;
import com.rhinoceros.mall.service.impl.exception.common.ParameterIsNullException;
import com.rhinoceros.mall.service.service.OrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.DocValueFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import com.rhinoceros.mall.core.enumeration.OrderStatus;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

@Autowired
private OrderDao orderDao;

@Autowired
private UserDao userDao;
@Autowired
private ProductDao productDao;
@Autowired
private AddressDao addressDao;
/**
* 根据userId和订单状态找出所有符合条件的分页订单
*
Expand Down Expand Up @@ -72,7 +92,95 @@ public Order findById(Long id) {
return orderDao.findById(id);
}

/**
* 添加订单
* @param dtos
* @param userId
* @param addressId
* @return
*/
@Transactional
@Override
public List<Order> add(OrderListDto dtos, Long userId,Long addressId) {
if(userId == null){
log.info("用户id不能为空");
throw new ParameterIsNullException("用户id不能为空");
}
User user = userDao.findById(userId);
if(user == null){
log.info("用户不存在");
throw new EntityNotExistException("用户不存在");
}
if(addressId==null){
log.info("地址Id不能为空");
throw new ParameterIsNullException("地址Id不能为空");
}
Address address = addressDao.findById(addressId);
if(address==null){
log.info("地址不存在");
throw new EntityNotExistException("地址不存在");
}
List<Order> orders = new LinkedList<>();
//设置订单号
List<OrderDto> orderDtos = dtos.getOrders();

for(OrderDto orderDto:orderDtos){
Order order = new Order();
Integer productNum = orderDto.getProductNum();
if(productNum==null||productNum<=0){
log.info("商品数量需要大于0");

}
Long productId = orderDto.getProductId();
if(productId==null){
log.info("商品ID不能为空");
throw new ParameterIsNullException("商品ID不能为空");
}
Product product = productDao.findById(productId);
if(product==null){
log.info("商品不存在");
throw new EntityNotExistException("商品不存在");
}
if(product.getStoreNum()-productNum<0){
log.info("库存不足");
throw new EntityNotExistException("库存不足");
}
BigDecimal price = product.getPrice();
BigDecimal discount = product.getDiscount();
BigDecimal totalPrice = calculate(price,discount,productNum);
product.setSaleNum(product.getStoreNum()-productNum);
productDao.updateSelectionById(product);
String identifier = randNumber();
order.setIdentifier(identifier);
order.setStatus(OrderStatus.WAIT_PAY);
order.setCreateAt(new Date());
order.setAddressId(addressId);
order.setProductId(productId);
order.setProductNum(productNum);
order.setTotalPrice(totalPrice);
order.setUserId(userId);
orders.add(order);
}
orderDao.addAll(orders);
return orders;
}
private BigDecimal calculate(BigDecimal price, BigDecimal discount, Integer num) {
BigDecimal totalPrice = BigDecimal.ZERO;
if (discount == null) {
totalPrice = price.multiply(BigDecimal.valueOf(num));
} else {
totalPrice = discount.multiply(BigDecimal.valueOf(num));
}
return totalPrice;
}

private String randNumber(){
Random rand = new Random();
int max=9999,min=1000;
int shu = rand.nextInt(max)%(max-min+1) + min;
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
String randomNumber = sdf.format(date)+shu;
return randomNumber;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.rhinoceros.mall.service.service;
/* created at 8:10 PM 3/6/2018 */

import com.rhinoceros.mall.core.dto.OrderListDto;
import com.rhinoceros.mall.core.enumeration.OrderStatus;
import com.rhinoceros.mall.core.po.Order;
import com.rhinoceros.mall.core.query.PageQuery;

import java.math.BigDecimal;
import java.util.List;


Expand Down Expand Up @@ -41,6 +43,12 @@ public interface OrderService {
*/
Order findById(Long id);



/**
* 添加订单
*
* @param dtos
* @param userId
* @return
*/
List<Order> add(OrderListDto dtos, Long userId,Long addressId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class CategoryController {
public String list(@RequestParam("cid") Long cid,
@PageDefault(required = false) PageQuery pageQuery,
Model model) {


List<Product> products = productService.findDeepByCategoryId(cid, pageQuery);

List<ProductVo> productVos = new LinkedList<>();
Expand Down
Loading

0 comments on commit 5f9bc76

Please sign in to comment.