package com.tuling.tulingmall.ordercurr.service.impl;//package com.tuling.tulingmall.service.impl;
//
//import com.github.pagehelper.PageHelper;
//import com.tuling.tulingmall.common.api.CommonResult;
//import com.tuling.tulingmall.common.api.ResultCode;
//import com.tuling.tulingmall.common.exception.BusinessException;
//import com.tuling.tulingmall.component.CancelOrderSender;
//import com.tuling.tulingmall.dao.PortalOrderDao;
//import com.tuling.tulingmall.dao.PortalOrderItemDao;
//import com.tuling.tulingmall.domain.*;
//import com.tuling.tulingmall.feignapi.pms.PmsProductFeignApi;
//import com.tuling.tulingmall.feignapi.ums.UmsCouponFeignApi;
//import com.tuling.tulingmall.feignapi.ums.UmsMemberFeignApi;
//import com.tuling.tulingmall.mapper.*;
//import com.tuling.tulingmall.model.*;
//import com.tuling.tulingmall.service.OmsCartItemService;
//import com.tuling.tulingmall.service.OmsPortalOrderService;
//import com.tuling.tulingmall.service.RedisService;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.CollectionUtils;
//
//import java.math.BigDecimal;
//import java.math.RoundingMode;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
///**
// * 前台订单管理Service
// * Created by macro on 2018/8/30.
// */
//@Service
//@Slf4j
//public class OmsPortalOrderServiceImplV4 implements OmsPortalOrderService {
//
//    @Autowired
//    private OmsCartItemService cartItemService;
//
//    @Autowired
//    private UmsMemberFeignApi umsMemberFeignApi;
//
//    @Autowired
//    private PmsProductFeignApi pmsProductFeignApi;
//
//    @Autowired
//    private UmsCouponFeignApi umsCouponFeignApi;
//
//
//    @Autowired
//    private UmsIntegrationConsumeSettingMapper integrationConsumeSettingMapper;
//    @Autowired
//    private PmsSkuStockMapper skuStockMapper;
//
//    @Autowired
//    private OmsOrderMapper orderMapper;
//    @Autowired
//    private PortalOrderItemDao orderItemDao;
//    @Autowired
//    private SmsCouponHistoryMapper couponHistoryMapper;
//    @Autowired
//    private RedisService redisService;
//    @Value("${redis.key.prefix.orderId}")
//    private String REDIS_KEY_PREFIX_ORDER_ID;
//    @Autowired
//    private PortalOrderDao portalOrderDao;
//    @Autowired
//    private OmsOrderSettingMapper orderSettingMapper;
//    @Autowired
//    private OmsOrderItemMapper orderItemMapper;
//    @Autowired
//    private CancelOrderSender cancelOrderSender;
//
//    /**
//     * 查询会员订单
//     * @param memberId
//     *          会员ID
//     * @param status
//     *          订单状态
//     * @return
//     *      CommonResult<List>
//     */
//    @Override
//    public CommonResult<List<OmsOrderDetail>> findMemberOrderList(Integer pageSize, Integer pageNum, Long memberId, Integer status) {
//        PageHelper.startPage(pageNum,pageSize);
//        return CommonResult.success(portalOrderDao.findMemberOrderList(memberId,status));
//    }
//
//    /**
//     * 删除订单[逻辑删除],只能status为：3->已完成；4->已关闭；5->无效订单，才可以删除
//     * ，否则只能先取消订单然后删除。
//     * @param orderId
//     * @return
//     *      受影响的行数
//     */
//    @Override
//    public int deleteOrder(Long orderId){
//        return portalOrderDao.deleteOrder(orderId);
//    }
//
//    /**
//     * 确认选择购买的商品
//     * @param itemIds
//     *        选择的购物车商品
//     * @return
//     */
//    @Override
//    public ConfirmOrderResult generateConfirmOrder(List<Long> itemIds,Long memberId) throws BusinessException {
//        ConfirmOrderResult result = new ConfirmOrderResult();
//        //获取购物车信息
//        List<CartPromotionItem> cartPromotionItemList = cartItemService.listSelectedPromotion(memberId,itemIds);
//        result.setCartPromotionItemList(cartPromotionItemList);
//        //获取用户收货地址列表
//        //List<UmsMemberReceiveAddress> memberReceiveAddressList = memberReceiveAddressService.list();
//        //todo 测试通过了
//        List<UmsMemberReceiveAddress> memberReceiveAddressList = umsMemberFeignApi.list().getData();
//
//        result.setMemberReceiveAddressList(memberReceiveAddressList);
//        //获取用户可用优惠券列表
//        //List<SmsCouponHistoryDetail> couponHistoryDetailList = memberCouponService.listCart(cartPromotionItemList, 1);
//
//        //todo 已经改成了Feign远程调用 还没有测试
//        List<SmsCouponHistoryDetail> couponHistoryDetailList = umsCouponFeignApi.listCart2Feign(1,cartPromotionItemList).getData();
//
//        //todo 已经改成了Feign远程调用 还没有测试
//        UmsMember umsMember = umsMemberFeignApi.getMemberById().getData();
//
//        result.setCouponHistoryDetailList(couponHistoryDetailList);
//        //获取用户积分
//        result.setMemberIntegration(umsMember.getIntegration());
//        //获取积分使用规则
//        UmsIntegrationConsumeSetting integrationConsumeSetting = integrationConsumeSettingMapper.selectByPrimaryKey(1L);
//        result.setIntegrationConsumeSetting(integrationConsumeSetting);
//        //计算总金额、活动优惠、应付金额
//        ConfirmOrderResult.CalcAmount calcAmount = calcCartAmount(cartPromotionItemList);
//        result.setCalcAmount(calcAmount);
//        return result;
//    }
//
//    /**
//     * 生成订单
//     * @param orderParam
//     * @return
//     */
//    @Override
//    //@GlobalTransactional(name = "generateOrder",rollbackFor = Exception.class)
//    @Transactional
//    public CommonResult generateOrder(OrderParam orderParam,Long memberId) throws BusinessException {
//        List<OmsOrderItem> orderItemList = new ArrayList<>();
//
//        UmsMember umsMember = umsMemberFeignApi.getMemberById().getData();
//
//        /*
//         * 杨过修改业务,原业务为清空购物车，改为选择购物车商品下单
//         */
//        List<CartPromotionItem> cartPromotionItemList = cartItemService.listSelectedPromotion(memberId
//                ,orderParam.getItemIds());
//        //cartItemService.listPromotion(currentMember.getId());
//        for (CartPromotionItem cartPromotionItem : cartPromotionItemList) {
//            //生成下单商品信息
//            OmsOrderItem orderItem = new OmsOrderItem();
//            orderItem.setProductId(cartPromotionItem.getProductId());
//            orderItem.setProductName(cartPromotionItem.getProductName());
//            orderItem.setProductPic(cartPromotionItem.getProductPic());
//            orderItem.setProductAttr(cartPromotionItem.getProductAttr());
//            orderItem.setProductBrand(cartPromotionItem.getProductBrand());
//            orderItem.setProductSn(cartPromotionItem.getProductSn());
//            orderItem.setProductPrice(cartPromotionItem.getPrice());
//            orderItem.setProductQuantity(cartPromotionItem.getQuantity());
//            orderItem.setProductSkuId(cartPromotionItem.getProductSkuId());
//            orderItem.setProductSkuCode(cartPromotionItem.getProductSkuCode());
//            orderItem.setProductCategoryId(cartPromotionItem.getProductCategoryId());
//            orderItem.setPromotionAmount(cartPromotionItem.getReduceAmount());
//            orderItem.setPromotionName(cartPromotionItem.getPromotionMessage());
//            orderItem.setGiftIntegration(cartPromotionItem.getIntegration());
//            orderItem.setGiftGrowth(cartPromotionItem.getGrowth());
//            orderItemList.add(orderItem);
//        }
//        //判断购物车中商品是否都有库存
//        if (!hasStock(cartPromotionItemList)) {
//            return CommonResult.failed("库存不足，无法下单");
//        }
//        //判断是否使用了优惠券
//        if (orderParam.getCouponId() == null) {
//            //不用优惠券
//            for (OmsOrderItem orderItem : orderItemList) {
//                orderItem.setCouponAmount(new BigDecimal(0));
//            }
//        } else {
//            //使用优惠券
//            SmsCouponHistoryDetail couponHistoryDetail = getUseCoupon(cartPromotionItemList, orderParam.getCouponId());
//            if (couponHistoryDetail == null) {
//                return CommonResult.failed("该优惠券不可用");
//            }
//            //对下单商品的优惠券进行处理
//            handleCouponAmount(orderItemList, couponHistoryDetail);
//        }
//        //判断是否使用积分
//        if (orderParam.getUseIntegration() == null) {
//            //不使用积分
//            for (OmsOrderItem orderItem : orderItemList) {
//                orderItem.setIntegrationAmount(new BigDecimal(0));
//            }
//        } else {
//            //使用积分
//            BigDecimal totalAmount = calcTotalAmount(orderItemList);
//            BigDecimal integrationAmount = getUseIntegrationAmount(orderParam.getUseIntegration(), totalAmount, umsMember.getIntegration(), orderParam.getCouponId() != null);
//            if (integrationAmount.compareTo(new BigDecimal(0)) == 0) {
//                return CommonResult.failed("积分不可用");
//            } else {
//                //可用情况下分摊到可用商品中
//                for (OmsOrderItem orderItem : orderItemList) {
//                    BigDecimal perAmount = orderItem.getProductPrice().divide(totalAmount, 3, RoundingMode.HALF_EVEN).multiply(integrationAmount);
//                    orderItem.setIntegrationAmount(perAmount);
//                }
//            }
//        }
//        //计算order_item的实付金额
//        handleRealAmount(orderItemList);
//        //进行库存锁定 ==>todo 在这里演示分布式事务
//        CommonResult lockResult = pmsProductFeignApi.lockStock(cartPromotionItemList);
//        if(lockResult.getCode() ==ResultCode.FAILED.getCode()) {
//            //标识扣减库存失败
//            log.warn("远程调用锁定库存失败");
//            throw new RuntimeException("远程调用锁定库存失败");
//        }
//        //lockStock(cartPromotionItemList);
//        //根据商品合计、运费、活动优惠、优惠券、积分计算应付金额
//        OmsOrder order = new OmsOrder();
//        order.setDiscountAmount(new BigDecimal(0));
//        order.setTotalAmount(calcTotalAmount(orderItemList));
//        order.setFreightAmount(new BigDecimal(0));
//        order.setPromotionAmount(calcPromotionAmount(orderItemList));
//        order.setPromotionInfo(getOrderPromotionInfo(orderItemList));
//        if (orderParam.getCouponId() == null) {
//            order.setCouponAmount(new BigDecimal(0));
//        } else {
//            order.setCouponId(orderParam.getCouponId());
//            order.setCouponAmount(calcCouponAmount(orderItemList));
//        }
//        if (orderParam.getUseIntegration() == null) {
//            order.setIntegration(0);
//            order.setIntegrationAmount(new BigDecimal(0));
//        } else {
//            order.setIntegration(orderParam.getUseIntegration());
//            order.setIntegrationAmount(calcIntegrationAmount(orderItemList));
//        }
//
//        order.setPayAmount(calcPayAmount(order));
//        //转化为订单信息并插入数据库
//        order.setMemberId(memberId);
//        order.setCreateTime(new Date());
//        order.setMemberUsername(umsMember.getUsername());
//        //支付方式：0->未支付；1->支付宝；2->微信
//        order.setPayType(orderParam.getPayType());
//        //订单来源：0->PC订单；1->app订单
//        order.setSourceType(1);
//        //订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单
//        order.setStatus(0);
//        //订单类型：0->正常订单；1->秒杀订单
//        order.setOrderType(0);
//        //收货人信息：姓名、电话、邮编、地址
//        /**
//         * 通过Feign远程调用 会员服务
//         */
//        UmsMemberReceiveAddress address = umsMemberFeignApi.getItem(orderParam.getMemberReceiveAddressId()).getData();
//
//        order.setReceiverName(address.getName());
//        order.setReceiverPhone(address.getPhoneNumber());
//        order.setReceiverPostCode(address.getPostCode());
//        order.setReceiverProvince(address.getProvince());
//        order.setReceiverCity(address.getCity());
//        order.setReceiverRegion(address.getRegion());
//        order.setReceiverDetailAddress(address.getDetailAddress());
//        //0->未确认；1->已确认
//        order.setConfirmStatus(0);
//        order.setDeleteStatus(0);
//        //计算赠送积分
//        order.setIntegration(calcGifIntegration(orderItemList));
//        //计算赠送成长值
//        order.setGrowth(calcGiftGrowth(orderItemList));
//        //生成订单号
//        order.setOrderSn(generateOrderSn(order));
//        // TODO: 2018/9/3 bill_*,delivery_*
//        //插入order表和order_item表
//        orderMapper.insert(order);
//        for (OmsOrderItem orderItem : orderItemList) {
//            orderItem.setOrderId(order.getId());
//            orderItem.setOrderSn(order.getOrderSn());
//        }
//        orderItemDao.insertList(orderItemList);
//        //如使用优惠券更新优惠券使用状态
//        if (orderParam.getCouponId() != null) {
//            updateCouponStatus(orderParam.getCouponId(), memberId, 1);
//        }
//        //如使用积分需要扣除积分
//        if (orderParam.getUseIntegration() != null) {
//            order.setUseIntegration(orderParam.getUseIntegration());
//            //需要远程调用 用户服务扣除用户积分
//            //todo 这里需要做分布式事务
//            umsMember.setIntegration(umsMember.getIntegration()-orderParam.getUseIntegration());
//            CommonResult<String> result= umsMemberFeignApi.updateUmsMember(umsMember);
//            if(result.getCode() == ResultCode.FAILED.getCode()) {
//                log.warn("远程调用会员服务扣除用户积分异常");
//                throw new RuntimeException("远程调用会员服务扣除用户积分异常");
//            }
//        }
//        //删除购物车中的下单商品 --wly
////        deleteCartItemList(cartPromotionItemList, memberId);
//        Map<String, Object> result = new HashMap<>();
//        result.put("order", order);
//        result.put("orderItemList", orderItemList);
//        return CommonResult.success(result, "下单成功");
//    }
//
//    /**
//     * 订单详情
//     * @param orderId
//     * @return
//     */
//    public CommonResult getDetailOrder(Long orderId){
//        return CommonResult.success(portalOrderDao.getDetail(orderId));
//    }
//
//    @Override
//    public Integer paySuccess(Long orderId,Integer payType) {
//        //修改订单支付状态
//        OmsOrder order = new OmsOrder();
//        order.setId(orderId);
//        order.setStatus(1);
//        order.setPayType(payType);
//        order.setPaymentTime(new Date());
//        orderMapper.updateByPrimaryKeySelective(order);
//        //恢复所有下单商品的锁定库存，扣减真实库存
//        OmsOrderDetail orderDetail = portalOrderDao.getDetail(orderId);
//        return portalOrderDao.updateSkuStock(orderDetail.getOrderItemList());
//    }
//
//    @Override
//    public CommonResult cancelTimeOutOrder() {
//        OmsOrderSetting orderSetting = orderSettingMapper.selectByPrimaryKey(1L);
//        //查询超时、未支付的订单及订单详情
//        List<OmsOrderDetail> timeOutOrders = portalOrderDao.getTimeOutOrders(orderSetting.getNormalOrderOvertime());
//        if (CollectionUtils.isEmpty(timeOutOrders)) {
//            return CommonResult.failed("暂无超时订单");
//        }
//        //修改订单状态为交易取消
//        List<Long> ids = new ArrayList<>();
//        for (OmsOrderDetail timeOutOrder : timeOutOrders) {
//            ids.add(timeOutOrder.getId());
//        }
//        portalOrderDao.updateOrderStatus(ids, 4);
//        for (OmsOrderDetail timeOutOrder : timeOutOrders) {
//            if(CollectionUtils.isEmpty(timeOutOrder.getOrderItemList())){
//                throw new RuntimeException("订单产品不允许为空");
//            }
//            //解除订单商品库存锁定
//            portalOrderDao.releaseSkuStockLock(timeOutOrder.getOrderItemList());
//            //修改优惠券使用状态
//            updateCouponStatus(timeOutOrder.getCouponId(), timeOutOrder.getMemberId(), 0);
//            //返还使用积分
//            if (timeOutOrder.getUseIntegration() != null) {
//                //todo 这里需要做分布式事务
//                UmsMember umsMember = umsMemberFeignApi.getMemberById().getData();
//                umsMember.setIntegration(umsMember.getIntegration()+timeOutOrder.getUseIntegration());
//                CommonResult<String> result= umsMemberFeignApi.updateUmsMember(umsMember);
//                if(result.getCode() == ResultCode.FAILED.getCode()) {
//                    log.warn("远程调用会员服务扣除用户积分异常");
//                    throw new RuntimeException("远程调用会员服务扣除用户积分异常");
//                }
//            }
//        }
//        return CommonResult.success(null);
//    }
//
//    @Override
//    public void cancelOrder(Long orderId,Long memberId) {
//        //查询为付款的取消订单
//        OmsOrderExample example = new OmsOrderExample();
//        example.createCriteria().andIdEqualTo(orderId).andStatusEqualTo(0).andDeleteStatusEqualTo(0);
//        List<OmsOrder> cancelOrderList = orderMapper.selectByExample(example);
//        if (CollectionUtils.isEmpty(cancelOrderList)) {
//            return;
//        }
//        OmsOrder cancelOrder = cancelOrderList.get(0);
//        if (cancelOrder != null) {
//            //修改订单状态为取消
//            cancelOrder.setStatus(4);
//            orderMapper.updateByPrimaryKeySelective(cancelOrder);
//            OmsOrderItemExample orderItemExample = new OmsOrderItemExample();
//            orderItemExample.createCriteria().andOrderIdEqualTo(orderId);
//            List<OmsOrderItem> orderItemList = orderItemMapper.selectByExample(orderItemExample);
//            //解除订单商品库存锁定
//            if (!CollectionUtils.isEmpty(orderItemList)) {
//                portalOrderDao.releaseSkuStockLock(orderItemList);
//            }
//            //修改优惠券使用状态
//            updateCouponStatus(cancelOrder.getCouponId(), cancelOrder.getMemberId(), 0);
//            //返还使用积分
//            if (cancelOrder.getUseIntegration() != null) {
//                //todo 这里需要做分布式事务
//                UmsMember umsMember = umsMemberFeignApi.getMemberById().getData();
//                umsMember.setIntegration(umsMember.getIntegration()+cancelOrder.getUseIntegration());
//                CommonResult<String> result= umsMemberFeignApi.updateUmsMember(umsMember);
//                if(result.getCode() == ResultCode.FAILED.getCode()) {
//                    log.warn("远程调用会员服务扣除用户积分异常");
//                    throw new RuntimeException("远程调用会员服务扣除用户积分异常");
//                }
//            }
//        }
//    }
//
//    @Override
//    public void sendDelayMessageCancelOrder(MqCancelOrder mqCancelOrder) {
//        //获取订单超时时间
//        OmsOrderSetting orderSetting = orderSettingMapper.selectByPrimaryKey(1L);
//        //long delayTimes = orderSetting.getNormalOrderOvertime() * 60 * 1000;
//        long delayTimes = 5000L;
//        //发送延迟消息
//        cancelOrderSender.sendMessage(mqCancelOrder, delayTimes);
//    }
//
//    /**
//     * 生成18位订单编号:8位日期+2位平台号码+2位支付方式+6位以上自增id
//     */
//    private String generateOrderSn(OmsOrder order) {
//        StringBuilder sb = new StringBuilder();
//        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
//        String key = REDIS_KEY_PREFIX_ORDER_ID + date;
//        Long increment = redisService.increment(key, 1);
//        sb.append(date);
//        sb.append(String.format("%02d", order.getSourceType()));
//        sb.append(String.format("%02d", order.getPayType()));
//        String incrementStr = increment.toString();
//        if (incrementStr.length() <= 6) {
//            sb.append(String.format("%06d", increment));
//        } else {
//            sb.append(incrementStr);
//        }
//        return sb.toString();
//    }
//
//    /**
//     * 删除下单商品的购物车信息
//     */
//    private void deleteCartItemList(List<CartPromotionItem> cartPromotionItemList,Long memberId) {
//        List<Long> ids = new ArrayList<>();
//        for (CartPromotionItem cartPromotionItem : cartPromotionItemList) {
//            ids.add(cartPromotionItem.getId());
//        }
//        cartItemService.delete(memberId, ids);
//    }
//
//    /**
//     * 计算该订单赠送的成长值
//     */
//    private Integer calcGiftGrowth(List<OmsOrderItem> orderItemList) {
//        Integer sum = 0;
//        for (OmsOrderItem orderItem : orderItemList) {
//            sum = sum + orderItem.getGiftGrowth() * orderItem.getProductQuantity();
//        }
//        return sum;
//    }
//
//    /**
//     * 计算该订单赠送的积分
//     */
//    private Integer calcGifIntegration(List<OmsOrderItem> orderItemList) {
//        int sum = 0;
//        for (OmsOrderItem orderItem : orderItemList) {
//            sum += orderItem.getGiftIntegration() * orderItem.getProductQuantity();
//        }
//        return sum;
//    }
//
//    /**
//     * 将优惠券信息更改为指定状态
//     *
//     * @param couponId  优惠券id
//     * @param memberId  会员id
//     * @param useStatus 0->未使用；1->已使用
//     */
//    private void updateCouponStatus(Long couponId, Long memberId, Integer useStatus) {
//        if (couponId == null) return;
//        //查询第一张优惠券
//        SmsCouponHistoryExample example = new SmsCouponHistoryExample();
//        example.createCriteria().andMemberIdEqualTo(memberId)
//                .andCouponIdEqualTo(couponId).andUseStatusEqualTo(useStatus == 0 ? 1 : 0);
//        List<SmsCouponHistory> couponHistoryList = couponHistoryMapper.selectByExample(example);
//        if (!CollectionUtils.isEmpty(couponHistoryList)) {
//            SmsCouponHistory couponHistory = couponHistoryList.get(0);
//            couponHistory.setUseTime(new Date());
//            couponHistory.setUseStatus(useStatus);
//            couponHistoryMapper.updateByPrimaryKeySelective(couponHistory);
//        }
//    }
//
//    private void handleRealAmount(List<OmsOrderItem> orderItemList) {
//        for (OmsOrderItem orderItem : orderItemList) {
//            //原价-促销优惠-优惠券抵扣-积分抵扣
//            BigDecimal realAmount = orderItem.getProductPrice();
//            if (null!=orderItem.getPromotionAmount()){
//                realAmount.subtract(orderItem.getPromotionAmount());
//            }
//            if (null!=orderItem.getCouponAmount()){
//                realAmount.subtract(orderItem.getCouponAmount());
//            }
//            if (null!=orderItem.getIntegrationAmount()){
//                realAmount.subtract(orderItem.getIntegrationAmount());
//            }
//           orderItem.setRealAmount(realAmount);
//        }
//    }
//
//    /**
//     * 获取订单促销信息
//     */
//    private String getOrderPromotionInfo(List<OmsOrderItem> orderItemList) {
//        StringBuilder sb = new StringBuilder();
//        for (OmsOrderItem orderItem : orderItemList) {
//            sb.append(orderItem.getPromotionName());
//            sb.append(",");
//        }
//        String result = sb.toString();
//        if (result.endsWith(",")) {
//            result = result.substring(0, result.length() - 1);
//        }
//        return result;
//    }
//
//    /**
//     * 计算订单应付金额
//     */
//    private BigDecimal calcPayAmount(OmsOrder order) {
//        //总金额+运费-促销优惠-优惠券优惠-积分抵扣
//        BigDecimal payAmount = order.getTotalAmount()
//                .add(order.getFreightAmount())
//                .subtract(order.getPromotionAmount())
//                .subtract(order.getCouponAmount())
//                .subtract(order.getIntegrationAmount());
//        return payAmount;
//    }
//
//    /**
//     * 计算订单优惠券金额
//     */
//    private BigDecimal calcIntegrationAmount(List<OmsOrderItem> orderItemList) {
//        BigDecimal integrationAmount = new BigDecimal(0);
//        for (OmsOrderItem orderItem : orderItemList) {
//            if (orderItem.getIntegrationAmount() != null) {
//                integrationAmount = integrationAmount.add(orderItem.getIntegrationAmount().multiply(new BigDecimal(orderItem.getProductQuantity())));
//            }
//        }
//        return integrationAmount;
//    }
//
//    /**
//     * 计算订单优惠券金额
//     */
//    private BigDecimal calcCouponAmount(List<OmsOrderItem> orderItemList) {
//        BigDecimal couponAmount = new BigDecimal(0);
//        for (OmsOrderItem orderItem : orderItemList) {
//            if (orderItem.getCouponAmount() != null) {
//                couponAmount = couponAmount.add(orderItem.getCouponAmount().multiply(new BigDecimal(orderItem.getProductQuantity())));
//            }
//        }
//        return couponAmount;
//    }
//
//    /**
//     * 计算订单活动优惠
//     */
//    private BigDecimal calcPromotionAmount(List<OmsOrderItem> orderItemList) {
//        BigDecimal promotionAmount = new BigDecimal(0);
//        for (OmsOrderItem orderItem : orderItemList) {
//            if (orderItem.getPromotionAmount() != null) {
//                promotionAmount = promotionAmount.add(orderItem.getPromotionAmount().multiply(new BigDecimal(orderItem.getProductQuantity())));
//            }
//        }
//        return promotionAmount;
//    }
//
//    /**
//     * 获取可用积分抵扣金额
//     *
//     * @param useIntegration 使用的积分数量
//     * @param totalAmount    订单总金额
//     * @param memberIntegration  用户的积分
//     * @param hasCoupon      是否已经使用优惠券
//     */
//    private BigDecimal getUseIntegrationAmount(Integer useIntegration, BigDecimal totalAmount, Integer memberIntegration, boolean hasCoupon) {
//        BigDecimal zeroAmount = new BigDecimal(0);
//        //判断用户是否有这么多积分
//        if (useIntegration.compareTo(memberIntegration) > 0) {
//            return zeroAmount;
//        }
//        //根据积分使用规则判断是否可用
//        //是否可与优惠券共用
//        UmsIntegrationConsumeSetting integrationConsumeSetting = integrationConsumeSettingMapper.selectByPrimaryKey(1L);
//        if (hasCoupon && integrationConsumeSetting.getCouponStatus().equals(0)) {
//            //不可与优惠券共用
//            return zeroAmount;
//        }
//        //是否达到最低使用积分门槛
//        if (useIntegration.compareTo(integrationConsumeSetting.getUseUnit()) < 0) {
//            return zeroAmount;
//        }
//        //是否超过订单抵用最高百分比
//        BigDecimal integrationAmount = new BigDecimal(useIntegration).divide(new BigDecimal(integrationConsumeSetting.getUseUnit()), 2, RoundingMode.HALF_EVEN);
//        BigDecimal maxPercent = new BigDecimal(integrationConsumeSetting.getMaxPercentPerOrder()).divide(new BigDecimal(100), 2, RoundingMode.HALF_EVEN);
//        if (integrationAmount.compareTo(totalAmount.multiply(maxPercent)) > 0) {
//            return zeroAmount;
//        }
//        return integrationAmount;
//    }
//
//    /**
//     * 对优惠券优惠进行处理
//     *
//     * @param orderItemList       order_item列表
//     * @param couponHistoryDetail 可用优惠券详情
//     */
//    private void handleCouponAmount(List<OmsOrderItem> orderItemList, SmsCouponHistoryDetail couponHistoryDetail) {
//        SmsCoupon coupon = couponHistoryDetail.getCoupon();
//        if (coupon.getUseType().equals(0)) {
//            //全场通用
//            calcPerCouponAmount(orderItemList, coupon);
//        } else if (coupon.getUseType().equals(1)) {
//            //指定分类
//            List<OmsOrderItem> couponOrderItemList = getCouponOrderItemByRelation(couponHistoryDetail, orderItemList, 0);
//            calcPerCouponAmount(couponOrderItemList, coupon);
//        } else if (coupon.getUseType().equals(2)) {
//            //指定商品
//            List<OmsOrderItem> couponOrderItemList = getCouponOrderItemByRelation(couponHistoryDetail, orderItemList, 1);
//            calcPerCouponAmount(couponOrderItemList, coupon);
//        }
//    }
//
//    /**
//     * 对每个下单商品进行优惠券金额分摊的计算
//     *
//     * @param orderItemList 可用优惠券的下单商品商品
//     */
//    private void calcPerCouponAmount(List<OmsOrderItem> orderItemList, SmsCoupon coupon) {
//        BigDecimal totalAmount = calcTotalAmount(orderItemList);
//        for (OmsOrderItem orderItem : orderItemList) {
//            //(商品价格/可用商品总价)*优惠券面额
//            BigDecimal couponAmount = orderItem.getProductPrice().divide(totalAmount, 3, RoundingMode.HALF_EVEN).multiply(coupon.getAmount());
//            orderItem.setCouponAmount(couponAmount);
//        }
//    }
//
//    /**
//     * 获取与优惠券有关系的下单商品
//     *
//     * @param couponHistoryDetail 优惠券详情
//     * @param orderItemList       下单商品
//     * @param type                使用关系类型：0->相关分类；1->指定商品
//     */
//    private List<OmsOrderItem> getCouponOrderItemByRelation(SmsCouponHistoryDetail couponHistoryDetail, List<OmsOrderItem> orderItemList, int type) {
//        List<OmsOrderItem> result = new ArrayList<>();
//        if (type == 0) {
//            List<Long> categoryIdList = new ArrayList<>();
//            for (SmsCouponProductCategoryRelation productCategoryRelation : couponHistoryDetail.getCategoryRelationList()) {
//                categoryIdList.add(productCategoryRelation.getProductCategoryId());
//            }
//            for (OmsOrderItem orderItem : orderItemList) {
//                if (categoryIdList.contains(orderItem.getProductCategoryId())) {
//                    result.add(orderItem);
//                } else {
//                    orderItem.setCouponAmount(new BigDecimal(0));
//                }
//            }
//        } else if (type == 1) {
//            List<Long> productIdList = new ArrayList<>();
//            for (SmsCouponProductRelation productRelation : couponHistoryDetail.getProductRelationList()) {
//                productIdList.add(productRelation.getProductId());
//            }
//            for (OmsOrderItem orderItem : orderItemList) {
//                if (productIdList.contains(orderItem.getProductId())) {
//                    result.add(orderItem);
//                } else {
//                    orderItem.setCouponAmount(new BigDecimal(0));
//                }
//            }
//        }
//        return result;
//    }
//
//    /**
//     * 获取该用户可以使用的优惠券
//     *
//     * @param cartPromotionItemList 购物车优惠列表
//     * @param couponId              使用优惠券id
//     */
//    private SmsCouponHistoryDetail getUseCoupon(List<CartPromotionItem> cartPromotionItemList, Long couponId) {
//        //todo 远程调用可用优惠卷列表
//        List<SmsCouponHistoryDetail> couponHistoryDetailList = new ArrayList<>();
//        //List<SmsCouponHistoryDetail> couponHistoryDetailList = memberCouponService.listCart(cartPromotionItemList, 1);
//        for (SmsCouponHistoryDetail couponHistoryDetail : couponHistoryDetailList) {
//            if (couponHistoryDetail.getCoupon().getId().equals(couponId)) {
//                return couponHistoryDetail;
//            }
//        }
//        return null;
//    }
//
//    /**
//     * 计算总金额
//     */
//    private BigDecimal calcTotalAmount(List<OmsOrderItem> orderItemList) {
//        BigDecimal totalAmount = new BigDecimal("0");
//        for (OmsOrderItem item : orderItemList) {
//            totalAmount = totalAmount.add(item.getProductPrice().multiply(new BigDecimal(item.getProductQuantity())));
//        }
//        return totalAmount;
//    }
//
//    /**
//     * 锁定下单商品的所有库存
//     */
//    private void lockStock(List<CartPromotionItem> cartPromotionItemList) {
//        for (CartPromotionItem cartPromotionItem : cartPromotionItemList) {
//            PmsSkuStock skuStock = skuStockMapper.selectByPrimaryKey(cartPromotionItem.getProductSkuId());
//            skuStock.setLockStock(skuStock.getLockStock() + cartPromotionItem.getQuantity());
//            skuStockMapper.updateByPrimaryKeySelective(skuStock);
//        }
//    }
//
//    /**
//     * 判断下单商品是否都有库存
//     */
//    private boolean hasStock(List<CartPromotionItem> cartPromotionItemList) {
//        for (CartPromotionItem cartPromotionItem : cartPromotionItemList) {
//            if (cartPromotionItem.getRealStock()==null||cartPromotionItem.getRealStock() <= 0) {
//                return false;
//            }
//        }
//        return true;
//    }
//
//    /**
//     * 计算购物车中商品的价格
//     */
//    private ConfirmOrderResult.CalcAmount calcCartAmount(List<CartPromotionItem> cartPromotionItemList) {
//        ConfirmOrderResult.CalcAmount calcAmount = new ConfirmOrderResult.CalcAmount();
//        calcAmount.setFreightAmount(new BigDecimal(0));
//        BigDecimal totalAmount = new BigDecimal("0");
//        BigDecimal promotionAmount = new BigDecimal("0");
//        for (CartPromotionItem cartPromotionItem : cartPromotionItemList) {
//            totalAmount = totalAmount.add(cartPromotionItem.getPrice().multiply(new BigDecimal(cartPromotionItem.getQuantity())));
//            if (null!=cartPromotionItem.getReduceAmount()) {
//                promotionAmount = promotionAmount.add(cartPromotionItem.getReduceAmount().multiply(new BigDecimal(cartPromotionItem.getQuantity())));
//            }
//        }
//        calcAmount.setTotalAmount(totalAmount);
//        calcAmount.setPromotionAmount(promotionAmount);
//        calcAmount.setPayAmount(totalAmount.subtract(promotionAmount));
//        return calcAmount;
//    }
//
//}
