/**
* Copyright (c) https://github.com/gushizone
*/
package org.mmall.service.impl;
import com.alipay.api.AlipayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.demo.trade.config.Configs;
import com.alipay.demo.trade.model.ExtendParams;
import com.alipay.demo.trade.model.GoodsDetail;
import com.alipay.demo.trade.model.builder.AlipayTradePrecreateRequestBuilder;
import com.alipay.demo.trade.model.result.AlipayF2FPrecreateResult;
import com.alipay.demo.trade.service.AlipayTradeService;
import com.alipay.demo.trade.service.impl.AlipayTradeServiceImpl;
import com.alipay.demo.trade.utils.ZxingUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.mmall.common.Const;
import org.mmall.common.ServerResponse;
import org.mmall.dao.*;
import org.mmall.pojo.*;
import org.mmall.service.IOrderService;
import org.mmall.util.BigDecimalUtil;
import org.mmall.util.DateTimeUtil;
import org.mmall.util.FTPUtil;
import org.mmall.util.PropertiesUtil;
import org.mmall.vo.OrderItemVo;
import org.mmall.vo.OrderProductVo;
import org.mmall.vo.OrderVo;
import org.mmall.vo.ShippingVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
/**
* @author gushizone@gmail.com
* @createTime 2018/4/29 18:42
* @desc 订单Service实现
*/
@Service("iOrderService")
@Slf4j
public class OrderServiceImpl implements IOrderService {
private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Autowired
private PayInfoMapper payInfoMapper;
@Autowired
private CartMapper cartMapper;
@Autowired
private ProductMapper productMapper;
@Autowired
private ShippingMapper shippingMapper;
/** 创建订单 */
@Override
public ServerResponse createOrder(Integer userId, Integer shippingId){
// 从购物车中获取数据
List<Cart> cartList = cartMapper.selectCheckedCartByUserId(userId);
// 获取购物车订单子项,计算总价
ServerResponse serverResponse = getCartOrderItem(userId, cartList);
if(!serverResponse.isSuccess()){
return serverResponse;
}
List<OrderItem> orderItemList = (List<OrderItem>)serverResponse.getData();
BigDecimal payment = getOrderTotalPrice(orderItemList);
// 生成订单
Order order = assembleOrder(userId, shippingId, payment);
if(order == null){
return ServerResponse.createByErrorMessage("生成订单错误");
}
if(CollectionUtils.isEmpty(orderItemList)){
return ServerResponse.createByErrorMessage("购物车为空");
}
for (OrderItem orderItem : orderItemList) {
orderItem.setOrderNo(order.getOrderNo());
}
// TODO mybatis 批量插入
orderItemMapper.batchInsert(orderItemList);
// 生成成功,减少库存
reduceProductStock(orderItemList);
// 清空购物车
cleanCart(cartList);
// 组装订单VO
OrderVo orderVo = assembleOrderVo(order, orderItemList);
return ServerResponse.createBySuccess(orderVo);
}
/** 组装订单VO */
private OrderVo assembleOrderVo(Order order, List<OrderItem> orderItemList){
OrderVo orderVo = new OrderVo();
orderVo.setOrderNo(order.getOrderNo());
orderVo.setPayment(order.getPayment());
orderVo.setPaymentType(order.getPaymentType());
orderVo.setPaymentTypeDesc(Const.PaymentTypeEnum.codeOf(order.getPaymentType()).getValue());
orderVo.setPostage(order.getPostage());
orderVo.setStatus(order.getStatus());
orderVo.setStatusDesc(Const.OrderStatusEnum.codeOf(order.getStatus()).getValue());
orderVo.setShippingId(order.getShippingId());
Shipping shipping = shippingMapper.selectByPrimaryKey(order.getShippingId());
if(shipping != null){
orderVo.setReceiverName(shipping.getReceiverName());
orderVo.setShippingVo(assembleShippingVo(shipping));
}
orderVo.setPaymentTime(DateTimeUtil.dateToStr(order.getPaymentTime()));
orderVo.setSendTime(DateTimeUtil.dateToStr(order.getSendTime()));
orderVo.setEndTime(DateTimeUtil.dateToStr(order.getEndTime()));
orderVo.setCreateTime(DateTimeUtil.dateToStr(order.getCreateTime()));
orderVo.setCloseTime(DateTimeUtil.dateToStr(order.getCloseTime()));
orderVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
List<OrderItemVo> orderItemVoList = Lists.newArrayList();
for (OrderItem orderItem : orderItemList) {
OrderItemVo orderItemVo = assembleOrderItemVo(orderItem);
orderItemVoList.add(orderItemVo);
}
orderVo.setOrderItemVoList(orderItemVoList);
return orderVo;
}
/** 组装订单子项Vo */
private OrderItemVo assembleOrderItemVo(OrderItem orderItem){
OrderItemVo orderItemVo = new OrderItemVo();
orderItemVo.setOrderNo(orderItem.getOrderNo());
orderItemVo.setProductId(orderItem.getProductId());
orderItemVo.setProductName(orderItem.getProductName());
orderItemVo.setProductImage(orderItem.getProductImage());
orderItemVo.setCurrentUnitPrice(orderItem.getCurrentUnitPrice());
orderItemVo.setQuantity(orderItem.getQuantity());
orderItemVo.setTotalPrice(orderItem.getTotalPrice());
orderItemVo.setCreateTime(DateTimeUtil.dateToStr(orderItem.getCreateTime()));
return orderItemVo;
}
/** 组装收货地址VO */
private ShippingVo assembleShippingVo(Shipping shipping){
ShippingVo shippingVo = new ShippingVo();
shippingVo.setReceiverName(shipping.getReceiverName());
shippingVo.setReceiverAddress(shipping.getReceiverAddress());
shippingVo.setReceiverProvince(shipping.getReceiverProvince());
shippingVo.setReceiverCity(shipping.getReceiverCity());
shippingVo.setReceiverDistrict(shipping.getReceiverDistrict());
shippingVo.setReceiverMobile(shipping.getReceiverMobile());
shippingVo.setReceiverZip(shipping.getReceiverZip());
shippingVo.setReceiverPhone(shipping.getReceiverPhone());
return shippingVo;
}
/** 清空购物车 */
private void cleanCart(List<Cart> cartList){
for (Cart cart : cartList) {
cartMapper.deleteByPrimaryKey(cart.getId());
}
}
/** 减少库存 */
private void reduceProductStock(List<OrderItem> orderItemList){
for (OrderItem orderItem : orderItemList) {
Product product = productMapper.selectByPrimaryKey(orderItem.getProductId());
product.setStock(product.getStock() - orderItem.getQuantity());
productMapper.updateByPrimaryKeySelective(product);
}
}
/** 组装并保存订单 */
private Order assembleOrder(Integer userId, Integer shippingId, BigDecimal payment){
Order order = new Order();
long orderNo = generateOrderNo();
order.setOrderNo(orderNo);
order.setStatus(Const.OrderStatusEnum.NO_PAY.getCode()); // 订单状态:未支付
order.setPostage(0); // 邮费
order.setPaymentType(Const.PaymentTypeEnum.ONLINE_PAY.getCode()); // 支付方式:在线支付
order.setPayment(payment);
order.setUserId(userId);
order.setShippingId