package com.mall.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 com.mall.common.Const;
import com.mall.common.ServerResponse;
import com.mall.dao.*;
import com.mall.pojo.*;
import com.mall.service.IOrderService;
import com.mall.util.BigDecimalUtil;
import com.mall.util.DateTimeUtil;
import com.mall.util.FTPUtil;
import com.mall.util.PropertiesUtil;
import com.mall.vo.OrderItemVo;
import com.mall.vo.OrderProductVo;
import com.mall.vo.OrderVo;
import com.mall.vo.ShippingVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
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.*;
/**
* Created by 王乾 on 2017/12/25.
*/
@Service("iOrderService")
public class OrderServiceImpl implements IOrderService {
private static AlipayTradeService tradeService;
static {
/** 一定要在创建AlipayTradeService之前调用Configs.init()设置默认参数
* Configs会读取classpath下的zfbinfo.properties文件配置信息,如果找不到该文件则确认该文件是否在classpath目录
*/
Configs.init("zfbinfo.properties");
/** 使用Configs提供的默认参数
* AlipayTradeService可以使用单例或者为静态成员对象,不需要反复new
*/
tradeService = new AlipayTradeServiceImpl.ClientBuilder().build();
}
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;
/**
* 创建订单
* @param userId
* @param shippingId
* @return
*/
public ServerResponse createOrder(Integer userId,Integer shippingId){
//从购物车中获取数据
List<Cart> cartList = cartMapper.selectCheckedCartByUserId(userId);
//把购物车里面的组装成一个订单明细 的集合
ServerResponse serverResponse = this.getCartOrderItem(userId,cartList);
if(!serverResponse.isSuccess()){
return serverResponse;
}
List<OrderItem> orderItemList = (List<OrderItem>)serverResponse.getData();
//计算这个订单的总价
BigDecimal payment = this.getOrderTotalPrice(orderItemList);
//生成订单
Order order = this.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());
}
//mybatis 批量插入
orderItemMapper.batchInsert(orderItemList);
//生成订单成功,减少产品库存
this.reduceProductStock(orderItemList);
//清空购物车
this.cleanCart(cartList);
//返回给前端数据
OrderVo orderVo = assembleOrderVo(order,orderItemList);
return ServerResponse.createBySuccess(orderVo);
}
/**
* 取消订单
* @param userId
* @param orderNo
* @return
*/
public ServerResponse<String> cancel(Integer userId,Long orderNo){
Order order = orderMapper.selectByUserIdAndOrderNo(userId,orderNo);
if(order == null){
return ServerResponse.createByErrorMessage("该用户此订单不存在");
}
if(order.getStatus() != Const.OrderStatusEnum.NO_PAY.getCode()){
return ServerResponse.createByErrorMessage("已付款,无法取消订单");
}
Order updateOrder = new Order();
updateOrder.setId(order.getId());
updateOrder.setStatus(Const.OrderStatusEnum.CANCELED.getCode());
int row = orderMapper.updateByPrimaryKeySelective(updateOrder);
if(row > 0){
return ServerResponse.createBySuccess();
}
return ServerResponse.createByError();
}
/**
* 获取勾选上的商品,包括商品明细的信息
* @param userId
* @return
*/
public ServerResponse getOrderCartProduct(Integer userId){
OrderProductVo orderProductVo = new OrderProductVo();
//从购物车中获取数据
List<Cart> cartList = cartMapper.selectCheckedCartByUserId(userId);
ServerResponse serverResponse = this.getCartOrderItem(userId,cartList);
if(!serverResponse.isSuccess()){
return serverResponse;
}
List<OrderItem> orderItemList = (List<OrderItem>)serverResponse.getData();
List<OrderItemVo> orderItemVoList = Lists.newArrayList();
BigDecimal payment = new BigDecimal("0");
for(OrderItem orderItem : orderItemList){
payment = BigDecimalUtil.add(payment.doubleValue(),orderItem.getTotalPrice().doubleValue());
orderItemVoList.add(assembleOrderItemVo(orderItem));
}
orderProductVo.setProductTotalPrice(payment);
orderProductVo.setOrderItemVoList(orderItemVoList);
orderProductVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
return ServerResponse.createBySuccess(orderProductVo);
}
/**
* 获取订单的详情接口
* @param userId
* @param orderNo
* @return
*/
public ServerResponse<OrderVo> getOrderDetail(Integer userId,Long orderNo){
Order order = orderMapper.selectByUserIdAndOrderNo(userId,orderNo);
if(order != null){
List<OrderItem> orderItemList = orderItemMapper.getByOrderNoUserId(orderNo,userId);
OrderVo orderVo = assembleOrderVo(order,orderItemList);
return ServerResponse.createBySuccess(orderVo);
}
return ServerResponse.createByErrorMessage("没有找到该订单");
}
/**
* 订单信息
* @param userId
* @param pageNum
* @param pageSize
* @return
*/
public ServerResponse<PageInfo> getOrderList(Integer userId,int pageNum,int pageSize){
PageHelper.startPage(pageNum,pageSize);
List<Order> orderList = orderMapper.selectByUserId(userId);
List<OrderVo> orderVoList = assembleOrderVoList(orderList,userId);
PageInfo pageResult = new PageInfo(orderList);
pageResult.setList(orderVoList);
return ServerResponse.createBySuccess(pageResult);
}
/**
* 某用户的
* @param orderList
* @param userId
* @return
*/
private List<OrderVo> assembleOrderVoList(List<Order> orderList,Integer userId){
List<OrderV