package com.wzj.soopin.transaction.kit.params.impl;

import com.wzj.soopin.order.domain.entity.Order;
import com.wzj.soopin.order.domain.entity.OrderItem;
import com.wzj.soopin.order.emum.OrderStatusEnum;
import com.wzj.soopin.order.mapper.OrderMapper;
import com.wzj.soopin.order.service.OrderItemService;
import com.wzj.soopin.order.service.OrderService;
import com.wzj.soopin.transaction.enums.CashierEnum;
import com.wzj.soopin.transaction.kit.dto.PayParam;
import com.wzj.soopin.transaction.kit.dto.PaymentSuccessParams;
import com.wzj.soopin.transaction.kit.params.CashierExecute;
import com.wzj.soopin.transaction.kit.params.dto.CashierParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.constant.ResultCode;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单支付信息获取
 *
 * @author Chopper
 * @since 2021-01-25 20:00
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderCashier implements CashierExecute {
    /**
     * 订单
     */
    private final OrderService orderService;

    private final OrderItemService orderItemService;
    /**
     * 设置
     */
    private final ISysConfigService settingService;

    @Override
    public CashierEnum cashierEnum() {
        return CashierEnum.ORDER;
    }

    @Override
    public CashierParam getPaymentParams(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
            //准备返回的数据
            CashierParam cashierParam = new CashierParam();
            //订单信息获取
            Order order = orderService.getByNo(payParam.getSn());

//            //如果订单已支付，则不能发器支付
            if (order.getStatus().equals(OrderStatusEnum.PAID.name())) {
                throw new ServiceException(ResultCode.PAY_DOUBLE_ERROR);
            }
            //如果订单状态不是待付款，则抛出异常
            if (!order.getStatus().equals(OrderStatusEnum.UNPAID.getValue())) {
                throw new ServiceException(ResultCode.PAY_BAN);
            }
            cashierParam.setPrice(order.getPayAmount());
//
//            try {
//                BaseSetting baseSetting = JSONUtil.toBean(settingService.get(SettingEnum.BASE_SETTING.name()).getSettingValue(), BaseSetting.class);
//                cashierParam.setTitle(baseSetting.getSiteName());
//            } catch (Exception e) {
//                cashierParam.setTitle("多用户商城，在线支付");
//            }
//
//
            List<OrderItem> orderItemList = orderItemService.findByOrderId(order.getId());
            StringBuilder subject = new StringBuilder();
            for (OrderItem orderItem : orderItemList) {
                subject.append(orderItem.getProductName()).append(";");
            }

            cashierParam.setDetail(subject.toString());

            cashierParam.setOrderSns(payParam.getSn());
            cashierParam.setCreateTime(order.getCreateTime());
            return cashierParam;
        }

        return null;
    }

    @Override
    public void paymentSuccess(PaymentSuccessParams paymentSuccessParams) {

        PayParam payParam = paymentSuccessParams.getPayParam();
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
//            orderService.payOrder(payParam.getSn(),
//                    paymentSuccessParams.getPaymentMethod(),
//                    paymentSuccessParams.getReceivableNo());
            log.info("订单{}支付成功,金额{},方式{}", payParam.getSn(),
                    paymentSuccessParams.getPaymentMethod(),
                    paymentSuccessParams.getReceivableNo());
        }
    }

    @Override
    public Boolean paymentResult(PayParam payParam) {
        if (payParam.getOrderType().equals(CashierEnum.ORDER.name())) {
//            Order order = orderService.getBySn(payParam.getSn());
//            if (order != null) {
//                return PayStatusEnum.PAID.name().equals(order.getPayStatus());
//            } else {
//                throw new ServiceException(ResultCode.PAY_NOT_EXIST_ORDER);
//            }
        }
        return false;
    }
}
