package com.fs.company.service.impl;

import com.alibaba.fastjson.JSON;
import com.fs.common.utils.IpUtil;
import com.fs.company.constant.PaymentStatus;
import com.fs.company.domain.CompanyRechargeOrder;
import com.fs.company.mapper.CompanyRechargeOrderMapper;
import com.fs.company.util.OrderUtils;
import com.fs.company.service.CompanyRechargeOrderService;
import com.fs.core.config.WxPayProperties;
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
//public class CompanyRechargeOrderServiceImpl implements ICompanyRechargeOrderService {
public class CompanyRechargeOrderServiceImpl implements CompanyRechargeOrderService {
    @Autowired
    private CompanyRechargeOrderMapper companyRechargeOrderMapper;

    @Autowired
    private WxPayProperties wxPayProperties;

    @Autowired
    private WxPayService wxPayService;
    @Override
    public List<CompanyRechargeOrder> queryOverdueOrder() {
        return companyRechargeOrderMapper.queryOverdueOrder();
    }

    /**
     * 自动关闭超时订单号
     */
    @Override
    public void autoClosedOrder(){
        List<CompanyRechargeOrder> companyRechargeOrders = companyRechargeOrderMapper.queryOverdueOrder();
        for (CompanyRechargeOrder order : companyRechargeOrders) {
            order.setPayStatus(PaymentStatus.FAILED.getCode());
            companyRechargeOrderMapper.updateByOrderNo(order);
        }
    }

    @Override
    @Transactional(rollbackFor = Throwable.class,propagation = Propagation.REQUIRED)
    public CompanyRechargeOrder createOrder(CompanyRechargeOrder order) throws Exception{

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(wxPayProperties.getAppId());
        payConfig.setMchId(wxPayProperties.getMchId());
        payConfig.setMchKey(wxPayProperties.getMchKey());
        payConfig.setPrivateKeyPath(wxPayProperties.getPrivateKeyPath());
        payConfig.setPrivateCertPath(wxPayProperties.getPrivateCertPath());
        payConfig.setCertSerialNo(wxPayProperties.getCertSerialNo());
        payConfig.setUseSandboxEnv(false);
        payConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);

        wxPayService.setConfig(payConfig);

        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
        String orderNo = OrderUtils.getOrderNo();
        orderRequest.setBody("公司余额充值");
        orderRequest.setOutTradeNo("recharge-"+orderNo);
        orderRequest.setTotalFee(order.getPayAmount().multiply(BigDecimal.valueOf(100)).intValue());
        orderRequest.setSpbillCreateIp(IpUtil.getLocalhostIp());
        orderRequest.setNotifyUrl(wxPayProperties.getNativeNotifyUrl());
        orderRequest.setTradeType(WxPayConstants.TradeType.NATIVE);
        orderRequest.setProductId(String.valueOf(order.getId()));


        WxPayNativeOrderResult wxOrder = wxPayService.createOrder(orderRequest);


        order.setOrderNo(orderNo);
        order.setPayType(1);
        order.setPayStatus(1);
        order.setCreateTime(LocalDateTime.now());
        order.setCallbackContent(JSON.toJSONString(wxOrder));
        order.setQrLink(wxOrder.getCodeUrl());

        companyRechargeOrderMapper.insert(order);

        return order;
    }

    @Override
    public CompanyRechargeOrder queryOrder(String orderNo) {
        return companyRechargeOrderMapper.selectByOrderNo(orderNo);
    }


}
