package cn.iocoder.yudao.module.pay.api.order;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.pay.core.client.PayClient;
import cn.iocoder.yudao.framework.pay.core.client.dto.divide.PayDivideRespDto;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayCodeUnifiedDto;
import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderUnifiedReqDTO;
import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderStatusRespEnum;
import cn.iocoder.yudao.module.pay.api.order.dto.PayCodesUnifiedDto;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderRespDTO;
import cn.iocoder.yudao.module.pay.convert.order.PayOrderConvert;
import cn.iocoder.yudao.module.pay.dal.dataobject.order.PayOrderDO;
import cn.iocoder.yudao.module.pay.service.channel.PayChannelService;
import cn.iocoder.yudao.module.pay.service.order.PayOrderService;
import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest;
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付单 API 实现类
 *
 * @author 芋道源码
 */
@Service
public class PayOrderApiImpl implements PayOrderApi {

    @Resource
    private PayOrderService payOrderService;

    @Resource
    private PayChannelService channelService;

    @Override
    public Long createOrder(PayOrderCreateReqDTO reqDTO) {
        return payOrderService.createOrder(reqDTO);
    }

    @Override
    public PayOrderRespDTO getOrder(Long id) {
        PayOrderDO order = payOrderService.getOrder(id);
        return PayOrderConvert.INSTANCE.convert2(order);
    }

    @Override
    public void updatePayOrderPrice(Long id, Integer payPrice) {
        payOrderService.updatePayOrderPrice(id, payPrice);
    }

    @Override
    public boolean codePay(PayCodesUnifiedDto dto) {

        PayCodeUnifiedDto payCodeUnifiedDto = new PayCodeUnifiedDto();
        BeanUtil.copyProperties(dto, payCodeUnifiedDto);
        PayClient payClient = channelService.getPayClient(42L);
        payClient.unifiedCode(payCodeUnifiedDto);

        PayOrderUnifiedReqDTO payOrderUnifiedReqDTO = new PayOrderUnifiedReqDTO();
        payOrderUnifiedReqDTO.setOutTradeNo(dto.getOutTradeNo());
        payOrderUnifiedReqDTO.setSubject(dto.getSubject());
        payOrderUnifiedReqDTO.setBody(dto.getBody());
        payOrderUnifiedReqDTO.setPrice(dto.getPrice());
        payOrderUnifiedReqDTO.setUserIp(dto.getUserIp());
        payOrderUnifiedReqDTO.setNotifyUrl("https://yclhit.com:8896/admin-api/pay/notify/order/xx");
        payOrderUnifiedReqDTO.setExpireTime(LocalDateTime.now().plusMinutes(1));
        Map<String, String> channelExtras = new HashMap<>();
        channelExtras.put("authCode", dto.getAuthCode());
        payOrderUnifiedReqDTO.setChannelExtras(channelExtras);
        try {
            cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO payOrderRespDTO = payClient.unifiedOrder(payOrderUnifiedReqDTO);
            return PayOrderStatusRespEnum.isSuccess(payOrderRespDTO.getStatus());
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
}
