package com.hai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.utils.HttpsUtils;
import com.hai.dao.HighTelOrderMapper;
import com.hai.entity.HighTelOrder;
import com.hai.model.OutTelModel;
import com.hai.service.TelApiService;
import com.hai.service.pay.PayService;
import com.hai.service.pay.impl.NotifyServiceImpl;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("telApiService")
public class TelApiServiceImpl implements TelApiService {

    @Resource
    private HighTelOrderMapper highTelOrderMapper;

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

    @Override
    public List<HighTelOrder> getTelOrderList(Map<String, Object> map) {
        return null;
    }

    @Override
    public void insertOrder(HighTelOrder highTelOrder) {
        highTelOrderMapper.insert(highTelOrder);
    }

    @Override
    public void updateOrder(HighTelOrder highTelOrder) {
        highTelOrderMapper.updateByPrimaryKey(highTelOrder);
    }

    @Override
    public HighTelOrder findOrderById(Integer id) {
        return highTelOrderMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String telApiNotify(Map<String, String> paramsMap) throws Exception {
        String resXml = null;

        if ("1".equals(paramsMap.get("status"))) {
            log.info("话费充值 -> 异步通知：支付成功，进入订单处理");

            String orderId = paramsMap.get("order_id");

//            if (payService != null) {
//                payService.paySuccess(paramsMap, "WechatPay");    // 商户内部实际的交易业务处理
//                log.info("微信支付 -> 异步通知：订单处理完成");
//            } else {
//                log.error("微信支付 -> 异步通知：业务处理，payService获取失败");
//            }

            // 通知微信.异步确认成功.必写.不然会一直通知后台.八次之后就认为交易失败了
            resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
                    + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> ";
        } else {
            log.error("微信支付 -> 异步通知：支付失败,错误信息：" + paramsMap.get("err_code_des"));
            resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA["
                    + paramsMap.get("err_code_des") + "]]></return_msg>" + "</xml> ";
        }


        return resXml;
    }

    @Override
    public JSONObject outApiTel(String tel) {

        String appcode = "c4c79f70e40840959a1515b7cef7a70e";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("shouji", tel);
        return HttpsUtils.doGet("https://jshmgsdmfb.market.alicloudapi.com/shouji/query", querys , headers);
    }

    @Override
    public JSONObject getMemberGoods() {

        Map<String , String> map = new HashMap<>();


        return null;
    }
}
