package com.hai.service.pay.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.pay.util.XmlUtil;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.WxUtils;
import com.hai.config.CommonSysConst;
import com.hai.dao.HighGasOrderPushMapper;
import com.hai.entity.*;
import com.hai.enum_type.OrderPushType;
import com.hai.model.ResultProfitSharing;
import com.hai.service.*;
import com.hai.service.pay.PayService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.security.KeyStore;
import java.util.*;

/**
 * @Auther: 胡锐
 * @Description:
 * @Date: 2021/3/27 00:35
 */
@Service("rechargeOrderService")
public class RechargeOrderServiceImpl implements PayService {

    private static Logger log = LoggerFactory.getLogger(RechargeOrderServiceImpl.class);

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Resource
    private HighGasOrderPushMapper highGasOrderPushMapper;



    @Override
    public void paySuccess(Map<String, String> map, String payType) throws Exception {

        if (payType.equals("Alipay")) {
            // 支付宝支付 todo 暂未开发
            return;
        }
        if (payType.equals("WechatPay")) {
            // 查询订单信息
            OutRechargeOrder order = outRechargeOrderService.findByOrderNo(map.get("out_trade_no"));
            // 推送订单记录
            HighGasOrderPush highGasOrderPush = new HighGasOrderPush();
            highGasOrderPush.setType(OrderPushType.type2.getType());
            highGasOrderPush.setOrderNo(order.getOrderNo());
            highGasOrderPush.setCreateTime(new Date());
            highGasOrderPush.setRequestContent(order.getOrderNo());
            highGasOrderPush.setReturnContent(String.valueOf(map));
            highGasOrderPushMapper.insert(highGasOrderPush);
            if (order.getPayStatus() == 101) {
                order.setPaySerialNo(map.get("transaction_id")); // 支付流水号
                order.setPayRealPrice(new BigDecimal(map.get("total_fee")).divide(new BigDecimal("100"))); // 实付金额
                order.setPayTime(new Date()); // 支付时间
                // 订单支付状态 ： 101.待支付 102.已支付 100.已完成 104.已取消 105.已退款
                order.setPayStatus(102);
                order.setRechargeStatus(204);
                order.setPayType(2);
                outRechargeOrderService.updateOrder(order);
//                if (order.getRechargeType() == 1) {
//                    JSONObject object =  outRechargeOrderService.getMobile(order.getRechargeContent() , order.getOrderPrice().intValue() , order.getOrderNo() , order.getRechargeType());
//                    if (object.getInteger("code") != 200) {
//                        order.setRechargeStatus(1);
//                        order.setAbnormalMsg(object.getString("message"));
//                    }
//                }
                outRechargeOrderService.pollRequest(order);

            }
        }
    }

}
