package com.hai.order.service.impl;

import com.hai.entity.*;
import com.hai.order.service.OrderPaySuccessService;
import com.hai.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @className: OrderPaySuccessServiceImpl
 * @author: HuRui
 * @date: 2022/8/29
 **/
@Service("orderPaySuccessService")
public class OrderPaySuccessServiceImpl implements OrderPaySuccessService {

    @Resource
    private HighCouponService couponService;

    @Resource
    private HighCouponCodeService couponCodeService;

    @Resource
    private HighOrderPreService orderPreService;

    @Resource
    private HighUserService userService;


    @Override
    public void couponHandle(HighOrder order,HighChildOrder childOrder) throws Exception {
        HighCoupon coupon = couponService.getCouponById(childOrder.getGoodsId());
        // 是否预约
        if (coupon.getReserveStatus()) {
            HighOrderPre orderPre = new HighOrderPre();
            orderPre.setCompanyId(coupon.getCompanyId());
            orderPre.setMerchantId(coupon.getMerchantId());
            orderPre.setMerchantStoreId(childOrder.getStoreId());
            orderPre.setPreOrderNo(System.currentTimeMillis()+"");
            orderPre.setOrderId(order.getId());
            orderPre.setOrderNo(order.getOrderNo());
            orderPre.setChildOrderId(childOrder.getId());
            orderPre.setPayRealPrice(order.getPayRealPrice());
            orderPre.setGoodsType(1);
            orderPre.setGoodsName(coupon.getCouponName());
            orderPre.setGoodsId(coupon.getId());
            orderPre.setPreUserId(order.getMemId());
            orderPre.setPreUserName(order.getMemName());
            orderPre.setPreUserPhone(order.getMemPhone());
            orderPre.setPreUserRemark(order.getRemarks());
            orderPre.setStatus(1);
            orderPreService.insertOrderPre(orderPre);
        }

        // 支付购买卡券
        couponCodeService.payCoupon(order,childOrder,coupon);

    }

    @Override
    public void integralRecharge(HighOrder order,HighChildOrder childOrder) {
        // 金币 1：100
        Integer goldNum = new BigDecimal(childOrder.getTotalPrice().toString()).multiply(new BigDecimal("100")).intValue();
        String remark = "订单" + order.getOrderNo() + "积分充值积分：" +  goldNum;
        userService.goldHandle(childOrder.getGoodsId(), goldNum, 1, 1, childOrder.getId() , remark);
    }
}
