package com.yonge.cooleshow.portal.service.impl;

import com.yonge.cooleshow.mbg.mapper.UserOrderPaymentMapper;
import com.yonge.cooleshow.mbg.model.UserOrderPayment;
import com.yonge.cooleshow.portal.service.UserOrderPaymentService;
import com.yonge.toolset.base.result.BaseResult;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.payment.base.enums.OpenEnum;
import com.yonge.toolset.payment.base.enums.PayChannelEnum;
import com.yonge.toolset.payment.base.enums.PaymentClientEnum;
import com.yonge.toolset.payment.base.enums.TradeStatusEnum;
import com.yonge.toolset.payment.base.model.ClosePayment;
import com.yonge.toolset.payment.base.model.callback.PaymentCallBack;
import com.yonge.toolset.payment.core.service.PaymentClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description
 *
 * @author liujunchi
 * @date 2022-04-24
 */
@Service
public class UserOrderPaymentServiceImpl implements UserOrderPaymentService {

    @Autowired
    private UserOrderPaymentMapper userOrderPaymentMapper;

    @Autowired(required = true)
    private PaymentClient paymentClient;

    @Override
    public UserOrderPayment getByTranNo(String transNo) {
        return userOrderPaymentMapper.selectByTranNo(transNo);
    }

    @Override
    public void save(UserOrderPayment userOrderPayment) {
        userOrderPaymentMapper.insert(userOrderPayment);
    }

    @Override
    public void saveOrUpdate(UserOrderPayment userOrderPayment) {
        if (userOrderPayment.getId() == null) {
            userOrderPaymentMapper.insert(userOrderPayment);
        } else {
            userOrderPaymentMapper.updateByPrimaryKeySelective(userOrderPayment);
        }
    }

    @Override
    public UserOrderPayment getByOrderNo(String orderSn, PayChannelEnum payChannel, TradeStatusEnum payStatus) {

        return userOrderPaymentMapper.selectByOrderNoAndStatus(orderSn, payChannel.getCode(), payStatus.getCode());
    }

    @Override
    public List<UserOrderPayment> getByOrderNo(String orderSn) {
        return userOrderPaymentMapper.selectByOrderNo(orderSn);
    }

    @Override
    public void closePayment(String orderSn, String message, PaymentCallBack data) {
        UserOrderPayment orderPayment = userOrderPaymentMapper.selectByOrderNoAndStatusPaying(orderSn);

        if (orderPayment != null) {
            //更新付款单
            orderPayment.setStatus(TradeStatusEnum.failed.getCode());
            orderPayment.setPayFailMsg(StringUtil.isEmpty(message) ? "交易取消" : message);
            orderPayment.setUpdateTime(new Date());
            if(data != null) {
                orderPayment.setTransNo(data.getId());
            }

            //发送支付关单请求
            if (!StringUtil.isEmpty(orderPayment.getTransNo()) || !StringUtil.isEmpty(orderPayment.getAdapayNo())) {
                ClosePayment closePayment = new ClosePayment(OpenEnum.valueOf(orderPayment.getOpenType()),
                        PaymentClientEnum.valueOf(orderPayment.getPaymentClient()),
                        PayChannelEnum.valueOf(orderPayment.getPayChannel()));
                closePayment.setId(orderPayment.getTransNo());
                closePayment.setReason(message);
                closePayment.setPaymentNo(orderPayment.getAdapayNo());
                BaseResult<ClosePayment> closePaymentBaseResult = paymentClient.closePayment(closePayment);
                if (!closePaymentBaseResult.getStatus()) {
                    orderPayment.setCloseStatus(TradeStatusEnum.failed.getCode());
                    orderPayment.setCloseFailMsg(closePaymentBaseResult.getMsg());
                } else {
                    orderPayment.setCloseStatus(TradeStatusEnum.pending.getCode());
                }
            }
            userOrderPaymentMapper.updateByPrimaryKeySelective(orderPayment);
        }
    }

    @Override
    public UserOrderPayment getByOrderNoAndStatusPaying(String orderSn) {
        return userOrderPaymentMapper.selectByOrderNoAndStatusPaying(orderSn);
    }

    @Override
    public UserOrderPayment getByOrderNoAndStatusSucceeded(String orderSn) {
        return userOrderPaymentMapper.selectByOrderNoAndStatusSucceeded(orderSn);
    }

    @Override
    public UserOrderPayment getByAdapayNo(String paymentNo) {
        return userOrderPaymentMapper.selectByAdapayNo(paymentNo);
    }
}
